Advertisement
ES99

4 coding senpai

Apr 24th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 18.40 KB | None | 0 0
  1. import pygame
  2. from pygame.locals import *
  3. import sys
  4. import time
  5. import pyganim
  6. import random
  7.  
  8.  
  9. pygame.init()
  10.  
  11.  
  12. white = (255,250,250)
  13. black = (0,0,0)
  14. red = (255,0,0)
  15. lime_green = (0,200,0)
  16. snakething = (164,120,80)
  17. gray = (124,124,124)
  18. red = (200,0,0)
  19. light_red = (255,0,0)
  20.  
  21. yellow = (200,200,0)
  22. light_yellow = (255,255,0)
  23.  
  24. green = (34,177,76)
  25. light_green = (0,255,0)
  26.  
  27. display_width = 800
  28. display_height = 600
  29.  
  30.  
  31. gameDisplay = pygame.display.set_mode((display_width,display_height))
  32. pygame.display.set_caption('MLG Snake')
  33.  
  34.  
  35.  
  36. charL = pygame.image.load('goomba left.png')
  37. charR = pygame.image.load('goomba right.png')
  38. charU = pygame.image.load('goomba up.png')
  39. charD = pygame.image.load('goomba down.png')
  40. loser = pygame.image.load('game over.png')
  41. imggg = pygame.image.load('introo.png')
  42. yum = pygame.image.load('doritos.png')
  43. cont = pygame.image.load('controls.png')
  44. button_1 = pygame.image.load('button 1.png')
  45. button_2 = pygame.image.load('button 2.png')
  46. button_3 = pygame.image.load('button 3.png')
  47. button_4 = pygame.image.load('button 4.png')
  48.  
  49.  
  50.  
  51. icon = pygame.image.load('doritos.png')
  52. pygame.display.set_icon(icon)
  53.  
  54.  
  55. ##currentchar = "goomba"
  56. ##if currentchar == "goomba":
  57. ##    charL = pygame.image.load('goomba left.png')
  58. ##    charR = pygame.image.load('goomba right.png')
  59. ##    charU = pygame.image.load('goomba backward.png')
  60. ##    charD = pygame.image.load('goompa forward.png')
  61.    
  62.  
  63. clock = pygame.time.Clock()
  64.  
  65.  
  66.  
  67. block_size = 20
  68.  
  69. AppleThickness = 30
  70.  
  71.  
  72. direction = "left"
  73.  
  74. ##difficulty = "easy"
  75. ##
  76. ##if difficulty == "easy":
  77. ##    FPS = 15
  78. ##if difficulty == "normal":
  79. ##    FPS = 30
  80. ##if difficulty == "hard":
  81. ##    FPS = 45
  82.    
  83.  
  84. smallfont = pygame.font.Font("Pixeled.ttf", 25)
  85.  
  86. smallmedfont = pygame.font.Font("Pixeled.ttf", 38)
  87.  
  88. medfont = pygame.font.Font("Pixeled.ttf", 50)
  89.  
  90. largefont = pygame.font.Font("Pixeled.ttf", 80)
  91.  
  92. tinyfont = pygame.font.Font("Pixeled.ttf", 18)
  93.  
  94. midgetfont = pygame.font.Font("Pixeled.ttf", 14)
  95.  
  96.  
  97.  
  98. def pause():
  99.     paused = True
  100.     message_to_screen("Paused", black, -100, "large")
  101.     message_to_screen("Press U to continue or q to quit.", black, 25)
  102.     pygame.display.update()
  103.  
  104.     while paused:
  105.         for event in pygame.event.get():
  106.             if event.type == pygame.QUIT:
  107.                 pygame.quit()
  108.                 quit()
  109.             if event.type == pygame.KEYDOWN:
  110.                 if event.key == pygame.K_u:
  111.                     paused = False
  112.  
  113.                 elif event.key == pygame.K_q:
  114.                     pygame.quit()
  115.  
  116.                     quit()
  117.  
  118.         #gameDisplay.fill(gray)
  119.        
  120.         clock.tick(15)
  121.  
  122. def score(score):
  123.     text = smallfont.render("Score: "+str(score), True, black)
  124.     gameDisplay.blit(text, [0,-10])
  125.  
  126. def randAppleGen():
  127.     randAppleX = random.randrange(0, display_width - AppleThickness, 10)
  128.     randAppleY = random.randrange(0, display_height - AppleThickness, 10)
  129.  
  130.     return randAppleX, randAppleY
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137. difficulties = ["Easy", "Normal", "Hard"]
  138. difficultyNum = 0
  139. difficulty = difficulties[difficultyNum]
  140.  
  141. def changeDif():
  142.     global difficultyNum
  143.     global difficulties
  144.     global difficulty
  145.     if difficultyNum is len(difficulties) - 1:
  146.         difficultyNum = 0
  147.         difficulty = difficulties[difficultyNum]
  148.     else:
  149.         difficultyNum += 1
  150.         difficulty = difficulties[difficultyNum]
  151. print(difficulty)
  152. changeDif()
  153. print(difficulty)
  154. changeDif()
  155. print(difficulty)
  156. changeDif()
  157. print(difficulty)
  158. changeDif()
  159. print(difficulty)
  160. changeDif()
  161. print(difficulty)
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.        
  171.  
  172.  
  173.  
  174. def snake(block_size, snakeList):
  175.     charU.play()
  176.     charD.play()
  177.     charL.play()
  178.     charR.play()
  179.  
  180.     if direction == "left":
  181.         charL.blit(gameDisplay, (snakeList[-1][0], snakeList[-1][1]))
  182.         proper = charL
  183.     if direction == "right":
  184.         charR.blit(gameDisplay, (snakeList[-1][0], snakeList[-1][1]))
  185.         proper = charR
  186.     if direction == "up":
  187.         charU.blit(gameDisplay, (snakeList[-1][0], snakeList[-1][1]))
  188.         proper = charU
  189.     if direction == "down":
  190.         charD.blit(gameDisplay, (snakeList[-1][0], snakeList[-1][1]))
  191.         proper = charD
  192.  
  193.        
  194.  
  195.     for XnY in snakeList[:-1]:
  196.         proper.blit(gameDisplay, [XnY[0],XnY[1],block_size,block_size])
  197. ##        gameDisplay.blit(proper, [XnY[0],XnY[1],block_size,block_size])
  198. clock.tick(30)
  199. def text_objects(text,color,size):
  200.     if size == "small":
  201.         textSurface = smallfont.render(text,True,color)
  202.     elif size == "medium":
  203.         textSurface = medfont.render(text,True,color)
  204.     elif size == "tiny":
  205.         textSurface = tinyfont.render(text,True,color)
  206.     elif size == "smallmedium":
  207.         textSurface = smallmedfont.render(text,True,color)
  208.     elif size == "large":
  209.         textSurface = largefont.render(text,True,color)
  210.     elif size == "midget":
  211.         textSurface = midgetfont.render(text,True,color)
  212.  
  213.     return textSurface, textSurface.get_rect()
  214.  
  215. def text_to_button(msg,color,buttonx,buttony,buttonwidth,buttonheight,size = "small"):
  216.     textSurf, textRect = text_objects(msg,color,size)
  217.     textRect.center = ((buttonx + buttonwidth/2)), buttony+(buttonheight/2)
  218.     gameDisplay.blit(textSurf, textRect)
  219.    
  220.    
  221.  
  222. def message_to_screen(msg,color, y_displace=0, size = "small"):
  223.     textSurf, textRect = text_objects(msg,color,size)
  224.     textRect.center = (display_width/2), (display_height/2)+ y_displace
  225.     gameDisplay.blit(textSurf, textRect)
  226.  
  227.  
  228. def button(text,x,y,width,height,inactive_color,active_color,size = "small",action = None):
  229.     global charU
  230.     global charL
  231.     global charR
  232.     global charD
  233.     global currentchar
  234.     cur = pygame.mouse.get_pos()
  235.     click = pygame.mouse.get_pressed()
  236.     #print(click)
  237.     if x + width > cur[0] > x and y + height > cur[1] > y:
  238.         pygame.draw.rect(gameDisplay, active_color,(x,y,width,height))
  239.         if click[0] == 1 and action != None:
  240.             if action == "quit":
  241.                 pygame.quit()
  242.                 quit()
  243.             if action == "controls":
  244.                 game_controls()
  245.        
  246.             elif action == "play":
  247.                 gameLoop()
  248.             elif action == "main":
  249.                 game_intro()
  250.             elif action == "char":
  251.                 game_char()
  252.             elif action == "diff":
  253.                 changeDif()
  254.  
  255.             elif action == "difficulty":
  256.                 game_char()
  257.             elif action == "mario":
  258.                 currentchar = ("mario")
  259.                
  260.                 if currentchar == ("mario"):
  261.                     character = "Mario"  
  262.                     charU = pyganim.PygAnimation([('mario up.png', 1),
  263.                                                   ('mario up 2.png', 1)])                                                  
  264.                     charD = pyganim.PygAnimation([('mario down.png', 1),
  265.                                                   ('mario down 2.png', 1)])
  266.                     charL = pyganim.PygAnimation([('mario left.png', 1),
  267.                                                   ('mario left 2.png', 1)])
  268.                     charR = pyganim.PygAnimation([('mario right.png', 1),
  269.                                                   ('mario right 2.png', 1)])
  270.                     gameLoop()
  271.                    
  272.  
  273.             elif action == "goomba":
  274.                     currentchar = ("goomba")
  275.                    
  276.                     if currentchar == ("goomba"):
  277.                             character = "Goomba"
  278.                             charU = pyganim.PygAnimation([('goomba up.png', 1),
  279.                                                   ('goomba up 2.png', 1)])                                                  
  280.                     charD = pyganim.PygAnimation([('goomba down.png', 1),
  281.                                                   ('goomba down 2.png', 1)])
  282.                     charL = pyganim.PygAnimation([('goomba left.png', 1),
  283.                                                   ('goomba left 2.png', 1)])
  284.                     charR = pyganim.PygAnimation([('goomba right.png', 1),
  285.                                                   ('goomba right 2.png', 1)])
  286.                     gameLoop()
  287.                    
  288.  
  289.             elif action == "bowser":
  290.                 currentchar = ("bowser")
  291.                
  292.                 if currentchar == ("bowser"):
  293.                     character = "Bowser"  
  294.                     charU = pyganim.PygAnimation([('bowser up.png', 1),
  295.                                                   ('bowser up 2.png', 1)])                                                  
  296.                     charD = pyganim.PygAnimation([('bowser down.png', 1),
  297.                                                   ('bowser down 2.png', 1)])
  298.                     charL = pyganim.PygAnimation([('bowser left.png', 1),
  299.                                                   ('bowser left 2.png', 1)])
  300.                     charR = pyganim.PygAnimation([('bowser right.png', 1),
  301.                                                   ('bowser right 2.png', 1)])
  302.                     gameLoop()
  303.                    
  304.  
  305.             elif action == "bullet":
  306.                 currentchar = ("bullet")
  307.                
  308.                 if currentchar == ("bullet"):
  309.                     character = "Bullet Bill"  
  310.                     charU = pyganim.PygAnimation([('bullet up.png', 1),
  311.                                                   ('bullet up 2.png', 1)])                                                  
  312.                     charD = pyganim.PygAnimation([('bullet down.png', 1),
  313.                                                   ('bullet down 2.png', 1)])
  314.                     charL = pyganim.PygAnimation([('bullet left.png', 1),
  315.                                                   ('bullet left 2.png', 1)])
  316.                     charR = pyganim.PygAnimation([('bullet right.png', 1),
  317.                                                   ('bullet right 2.png', 1)])
  318.                     gameLoop()
  319.  
  320.             elif action == "donkey":
  321.                 currentchar = ("donkey")
  322.                
  323.                 if currentchar == ("donkey"):
  324.                     character = "Donkey Kong"  
  325.                     charU = pyganim.PygAnimation([('donkey kong up.png', 1),
  326.                                                   ('donkey kong up 2.png', 1)])                                                  
  327.                     charD = pyganim.PygAnimation([('donkey kong down.png', 1),
  328.                                                   ('donkey kong down 2.png', 1)])
  329.                     charL = pyganim.PygAnimation([('donkey kong left.png', 1),
  330.                                                   ('donkey kong left 2.png', 1)])
  331.                     charR = pyganim.PygAnimation([('donkey kong right.png', 1),
  332.                                                   ('donkey kong right 2.png', 1)])
  333.                     gameLoop()
  334.                    
  335.  
  336.  
  337.        
  338.  
  339.                
  340.                
  341.                
  342.            
  343.     else:
  344.         pygame.draw.rect(gameDisplay, inactive_color,(x,y,width,height))
  345.     text_to_button(text,black,x,y,width,height,size)
  346.    
  347.  
  348.  
  349.  
  350.  
  351. def game_char():
  352.     char = True
  353.     while char:
  354.         for event in pygame.event.get():
  355.             if event.type == pygame.QUIT:
  356.                 pygame.quit()
  357.                 quit()
  358.        
  359.         gameDisplay.fill(black)
  360.         message_to_screen("Characters",white,y_displace=-250,size="smallmedium")
  361.        
  362.         button("Main Menu",320,500,150,50,white,gray,"tiny", action = "main")
  363.         button("Quit",550,500,100,50,white,gray,"small", action = "quit")
  364.         button(("Difficulty: "+ difficulty),400,300,250,50,white,gray,"tiny",action = "diff")
  365.        
  366.         button("Goomba",235,100,150,50,white,gray,"tiny",action = "goomba")
  367.         button("Mario",50,100,150,50,white,gray,"tiny",action = "mario")
  368.         button("Bullet Bill",415,100,150,50,white,gray,"tiny",action = "bullet")
  369.         button("Bowser",600,100,150,50,white,gray,"tiny",action = "bowser")
  370.         button("Donkey Kong",50,200,150,50,white,gray,"midget",action = "donkey")
  371.        
  372.        
  373.        
  374.  
  375.        
  376.        
  377.         pygame.display.update()
  378.         clock.tick(60)  
  379.  
  380.        
  381.  
  382.  
  383.  
  384. def game_controls():
  385.     gcont = True
  386.     while gcont:
  387.        
  388.         for event in pygame.event.get():
  389.             if event.type == pygame.QUIT:
  390.                 pygame.quit()
  391.                 quit()
  392.        
  393.         gameDisplay.fill(black)      
  394.         gameDisplay.blit(cont,(0,0))
  395.  
  396.         button("Play",150,500,100,50,white,gray,"small",action = "char")
  397.         button("Main Menu",320,500,150,50,white,gray,"tiny", action = "main")
  398.         button("Quit",550,500,100,50,white,gray,"small", action = "quit")
  399.  
  400.        
  401.         pygame.display.update()
  402.         clock.tick(60)
  403.  
  404.  
  405.  
  406. def game_intro():
  407.     intro = True
  408.     while intro:
  409.         for event in pygame.event.get():
  410.             if event.type == pygame.QUIT:
  411.                 pygame.quit()
  412.                 quit()
  413.             elif event.type == pygame.KEYDOWN:
  414.                     if event.key == pygame.K_p:
  415.                         intro = False
  416.         gameDisplay.fill(black)
  417.         gameDisplay.blit(imggg,(0,0))
  418.        
  419.    
  420.         button("Play",150,500,100,50,white,gray,"small",action = "char")
  421.         button("ControLs",320,500,150,50,white,gray,"tiny", action = "controls")
  422. ##        button("Characters",295,425,200,50,white,gray,"tiny", action = "char")
  423.         button(" ",0,0,0,0,white,gray,"tiny", action = "controls")        
  424.         button("Quit",550,500,100,50,white,gray,"small", action = "quit")
  425.  
  426.        
  427.  
  428.         pygame.display.update()
  429.         clock.tick(60)
  430.        
  431.        
  432.        
  433.  
  434.        
  435.  
  436.  
  437.  
  438. def gameLoop():
  439.     global direction
  440.     direction = "left"
  441.     gameExit = False
  442.     gameOver= False        
  443.     lead_y = display_height/2
  444.     lead_x = display_width/2
  445.  
  446.     lead_x_change = -10
  447.     lead_y_change = 0
  448.  
  449.     snakeList = []
  450.     snakeLength = 1
  451.  
  452.     randAppleX, randAppleY = randAppleGen()
  453.    
  454.     while not gameExit:
  455.  
  456.         while gameOver == True:
  457. ##            gameDisplay.blit(loser,(0,0))
  458.             message_to_screen("Game over",
  459.                               red,y_displace=-50,size="large")
  460.            
  461.             message_to_screen("Press Q to quit or P to play again",
  462.                               black,50,size="small")
  463.             pygame.display.update()
  464.            
  465.  
  466.             for event in pygame.event.get():
  467.                 if event.type == pygame.QUIT:
  468.                    
  469.                     gameExit = True
  470.                     gameOver = False
  471.                    
  472.                 if event.type == pygame.KEYDOWN:
  473.                     if event.key == pygame.K_q:
  474.                         gameExit = True
  475.                         gameOver = False
  476.                     if event.key == pygame.K_p:
  477.                         game_char()
  478.  
  479.        
  480.         for event in pygame.event.get():
  481.             if event.type == pygame.QUIT:
  482.                         gameExit = True
  483.             if event.type == pygame.KEYDOWN:
  484.                     if event.key == pygame.K_a:
  485.  
  486.                             direction = "left"
  487.                             lead_x_change = -block_size
  488.                             lead_y_change = 0
  489.                     elif event.key == pygame.K_d:
  490.                             direction = "right"
  491.                             lead_x_change = block_size
  492.                             lead_y_change = 0
  493.                     elif event.key == pygame.K_w:
  494.                             direction = "up"
  495.                             lead_y_change = -block_size
  496.                             lead_x_change = 0
  497.                     elif event.key == pygame.K_s:
  498.                             direction = "down"
  499.                             lead_y_change = block_size
  500.                             lead_x_change = 0
  501.                     elif event.key == pygame.K_UP:
  502.                             direction = "up"
  503.                             lead_x_change = 0
  504.                             lead_y_change = -block_size
  505.                     elif event.key == pygame.K_DOWN:
  506.                             direction = "down"
  507.                             lead_y_change = block_size
  508.                             lead_x_change = 0
  509.                     elif event.key == pygame.K_LEFT:
  510.                             direction = "left"
  511.                             lead_x_change = -block_size
  512.                             lead_y_change = 0
  513.                     elif event.key == pygame.K_RIGHT:
  514.                             direction = "right"
  515.                             lead_x_change = block_size
  516.                             lead_y_change = 0
  517.                     elif event.key == pygame.K_ESCAPE:
  518.                         pause()
  519.                        
  520.         if lead_x >= display_width or lead_x < 0 or lead_y >= display_height or lead_y < 0:
  521.             gameOver = True
  522.  
  523.        
  524.         '''lead_x = (lead_x + lead_x_change) % 800 lead_y = (lead_y + lead_y_change) % 600'''
  525.  
  526.         lead_x += lead_x_change
  527.         lead_y += lead_y_change
  528.        
  529.        
  530.    
  531.         gameDisplay.fill(gray)
  532.  
  533.         gameDisplay.blit(yum,(randAppleX,randAppleY))
  534.        
  535.        
  536.         snakeHead = []
  537.         snakeHead.append(lead_x)
  538.         snakeHead.append(lead_y)
  539.         snakeList.append(snakeHead)
  540.  
  541.         if len(snakeList)> snakeLength:
  542.             del snakeList [0]
  543.        
  544.         snake(block_size, snakeList)
  545.        
  546.         for eachSegment in snakeList [:-1]:
  547.             if eachSegment == snakeHead:
  548.                 gameOver = True
  549.        
  550.         snake(block_size, snakeList)
  551.  
  552.         score(snakeLength-1)
  553.        
  554.         pygame.display.update()
  555.  
  556.  
  557.  
  558.         if lead_x > randAppleX and lead_x < randAppleX + AppleThickness or lead_x + block_size > randAppleX and lead_x + block_size < randAppleX + AppleThickness:
  559.             if lead_y > randAppleY and lead_y < randAppleY + AppleThickness or lead_y + block_size > randAppleY and lead_y + block_size < randAppleY + AppleThickness:
  560.                 randAppleX, randAppleY = randAppleGen()
  561.                 snakeLength += 1
  562.  
  563.             elif lead_y + block_size > randAppleY and lead_y + block_size < randAppleY + AppleThickness:
  564.                 randAppleX, randAppleY = randAppleGen()
  565.                 snakeLength += 1
  566.  
  567.        
  568.  
  569.         clock.tick(FPS)
  570.  
  571.  
  572.  
  573.     pygame.quit()
  574.     quit()
  575. game_intro()
  576. gameLoop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement