Advertisement
ES99

4 michael snake game

Apr 24th, 2017
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 16.61 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("D:/Desktop/snake/Pixeled.ttf", 25)
  85.  
  86. smallmedfont = pygame.font.Font("D:/Desktop/snake/Pixeled.ttf", 38)
  87.  
  88. medfont = pygame.font.Font("D:/Desktop/snake/Pixeled.ttf", 50)
  89.  
  90. largefont = pygame.font.Font("D:/Desktop/snake/Pixeled.ttf", 80)
  91.  
  92. tinyfont = pygame.font.Font("D:/Desktop/snake/Pixeled.ttf", 18)
  93.  
  94.  
  95.  
  96. def pause():
  97.     paused = True
  98.     message_to_screen("Paused", black, -100, "large")
  99.     message_to_screen("Press U to continue or q to quit.", black, 25)
  100.     pygame.display.update()
  101.  
  102.     while paused:
  103.         for event in pygame.event.get():
  104.             if event.type == pygame.QUIT:
  105.                 pygame.quit()
  106.                 quit()
  107.             if event.type == pygame.KEYDOWN:
  108.                 if event.key == pygame.K_u:
  109.                     paused = False
  110.  
  111.                 elif event.key == pygame.K_q:
  112.                     pygame.quit()
  113.  
  114.                     quit()
  115.  
  116.         #gameDisplay.fill(gray)
  117.        
  118.         clock.tick(15)
  119.  
  120. def score(score):
  121.     text = smallfont.render("Score: "+str(score), True, black)
  122.     gameDisplay.blit(text, [0,-10])
  123.  
  124. def randAppleGen():
  125.     randAppleX = random.randrange(0, display_width - AppleThickness, 10)
  126.     randAppleY = random.randrange(0, display_height - AppleThickness, 10)
  127.  
  128.     return randAppleX, randAppleY
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.        
  137.  
  138.  
  139.  
  140. def snake(block_size, snakeList):
  141.     charU.play()
  142.     charD.play()
  143.     charL.play()
  144.     charR.play()
  145.  
  146.     if direction == "left":
  147.         charL.blit(gameDisplay, (snakeList[-1][0], snakeList[-1][1]))
  148.         proper = charL
  149.     if direction == "right":
  150.         charR.blit(gameDisplay, (snakeList[-1][0], snakeList[-1][1]))
  151.         proper = charR
  152.     if direction == "up":
  153.         charU.blit(gameDisplay, (snakeList[-1][0], snakeList[-1][1]))
  154.         proper = charU
  155.     if direction == "down":
  156.         charD.blit(gameDisplay, (snakeList[-1][0], snakeList[-1][1]))
  157.         proper = charD
  158.  
  159.        
  160.  
  161.     for XnY in snakeList[:-1]:
  162.         proper.blit(gameDisplay, [XnY[0],XnY[1],block_size,block_size])
  163. ##        gameDisplay.blit(proper, [XnY[0],XnY[1],block_size,block_size])
  164. clock.tick(100)
  165. def text_objects(text,color,size):
  166.     if size == "small":
  167.         textSurface = smallfont.render(text,True,color)
  168.     elif size == "medium":
  169.         textSurface = medfont.render(text,True,color)
  170.     elif size == "tiny":
  171.         textSurface = tinyfont.render(text,True,color)
  172.     elif size == "smallmedium":
  173.         textSurface = smallmedfont.render(text,True,color)
  174.     elif size == "large":
  175.         textSurface = largefont.render(text,True,color)
  176.  
  177.     return textSurface, textSurface.get_rect()
  178.  
  179. def text_to_button(msg,color,buttonx,buttony,buttonwidth,buttonheight,size = "small"):
  180.     textSurf, textRect = text_objects(msg,color,size)
  181.     textRect.center = ((buttonx + buttonwidth/2)), buttony+(buttonheight/2)
  182.     gameDisplay.blit(textSurf, textRect)
  183.    
  184.    
  185.  
  186. def message_to_screen(msg,color, y_displace=0, size = "small"):
  187.     textSurf, textRect = text_objects(msg,color,size)
  188.     textRect.center = (display_width/2), (display_height/2)+ y_displace
  189.     gameDisplay.blit(textSurf, textRect)
  190.  
  191.  
  192. def button(text,x,y,width,height,inactive_color,active_color,size = "small",action = None):
  193.     global charU
  194.     global charL
  195.     global charR
  196.     global charD
  197.     global currentchar
  198.     cur = pygame.mouse.get_pos()
  199.     click = pygame.mouse.get_pressed()
  200.     #print(click)
  201.     if x + width > cur[0] > x and y + height > cur[1] > y:
  202.         pygame.draw.rect(gameDisplay, active_color,(x,y,width,height))
  203.         if click[0] == 1 and action != None:
  204.             if action == "quit":
  205.                 pygame.quit()
  206.                 quit()
  207.             if action == "controls":
  208.                 game_controls()
  209.        
  210.             elif action == "play":
  211.                 gameLoop()
  212.             elif action == "main":
  213.                 game_intro()
  214.             elif action == "char":
  215.                 game_char()
  216.  
  217.             elif action == "difficulty":
  218.                 game_char()
  219.             elif action == "mario":
  220.                 currentchar = ("mario")
  221.                
  222.                 if currentchar == ("mario"):
  223.                     character = "Mario"  
  224.                     charU = pyganim.PygAnimation([('mario up.png', 1),
  225.                                                   ('mario up 2.png', 1)])                                                  
  226.                     charD = pyganim.PygAnimation([('mario down.png', 1),
  227.                                                   ('mario down 2.png', 1)])
  228.                     charL = pyganim.PygAnimation([('mario left.png', 1),
  229.                                                   ('mario left 2.png', 1)])
  230.                     charR = pyganim.PygAnimation([('mario right.png', 1),
  231.                                                   ('mario right 2.png', 1)])
  232.                    
  233.  
  234.             elif action == "goomba":
  235.                     currentchar = ("goomba")
  236.                    
  237.                     if currentchar == ("goomba"):
  238.                             character = "Goomba"
  239.                             charU = pyganim.PygAnimation([('goomba up.png', 1),
  240.                                                   ('goomba up 2.png', 1)])                                                  
  241.                     charD = pyganim.PygAnimation([('goomba down.png', 1),
  242.                                                   ('goomba down 2.png', 1)])
  243.                     charL = pyganim.PygAnimation([('goomba left.png', 1),
  244.                                                   ('goomba left 2.png', 1)])
  245.                     charR = pyganim.PygAnimation([('goomba right.png', 1),
  246.                                                   ('goomba right 2.png', 1)])
  247.                    
  248.  
  249.             elif action == "bowser":
  250.                 currentchar = ("bowser")
  251.                
  252.                 if currentchar == ("bowser"):
  253.                     character = "Bowser"  
  254.                     charU = pyganim.PygAnimation([('bowser up.png', 1),
  255.                                                   ('bowser up 2.png', 1)])                                                  
  256.                     charD = pyganim.PygAnimation([('bowser down.png', 1),
  257.                                                   ('bowser down 2.png', 1)])
  258.                     charL = pyganim.PygAnimation([('bowser left.png', 1),
  259.                                                   ('bowser left 2.png', 1)])
  260.                     charR = pyganim.PygAnimation([('bowser right.png', 1),
  261.                                                   ('bowser right 2.png', 1)])
  262.                    
  263.  
  264.             elif action == "bullet":
  265.                 currentchar = ("bullet")
  266.                
  267.                 if currentchar == ("bullet"):
  268.                     character = "Bullet Bill"  
  269.                     charU = pyganim.PygAnimation([('bullet up.png', 1),
  270.                                                   ('bullet up 2.png', 1)])                                                  
  271.                     charD = pyganim.PygAnimation([('bullet down.png', 1),
  272.                                                   ('bullet down 2.png', 1)])
  273.                     charL = pyganim.PygAnimation([('bullet left.png', 1),
  274.                                                   ('bullet left 2.png', 1)])
  275.                     charR = pyganim.PygAnimation([('bullet right.png', 1),
  276.                                                   ('bullet right 2.png', 1)])
  277.                     gameLoop()
  278.                    
  279.  
  280.  
  281.        
  282.  
  283.                
  284.                
  285.                
  286.            
  287.     else:
  288.         pygame.draw.rect(gameDisplay, inactive_color,(x,y,width,height))
  289.     text_to_button(text,black,x,y,width,height,size)
  290.    
  291.  
  292.  
  293.  
  294.  
  295. def game_char():
  296.     char = True
  297.     while char:
  298.         for event in pygame.event.get():
  299.             if event.type == pygame.QUIT:
  300.                 pygame.quit()
  301.                 quit()
  302.        
  303.         gameDisplay.fill(black)
  304.         message_to_screen("Characters",white,y_displace=-250,size="smallmedium")
  305.        
  306.         button("Main Menu",320,500,150,50,white,gray,"tiny", action = "main")
  307.         button("Quit",550,500,100,50,white,gray,"small", action = "quit")
  308. ##        button(("Difficulty: "+ difficulty),400,300,250,50,white,gray,"tiny",action = "difficulty")
  309.        
  310.         button("Goomba",235,100,150,50,white,gray,"tiny",action = "goomba")
  311.         button("Mario",50,100,150,50,white,gray,"tiny",action = "mario")
  312.         button("Bullet Bill",415,100,150,50,white,gray,"tiny",action = "bullet")
  313.         button("Bowser",600,100,150,50,white,gray,"tiny",action = "bowser")        
  314.        
  315.        
  316.        
  317.  
  318.        
  319.        
  320.         pygame.display.update()
  321.         clock.tick(60)  
  322.  
  323.        
  324.  
  325.  
  326.  
  327. def game_controls():
  328.     gcont = True
  329.     while gcont:
  330.        
  331.         for event in pygame.event.get():
  332.             if event.type == pygame.QUIT:
  333.                 pygame.quit()
  334.                 quit()
  335.        
  336.         gameDisplay.fill(black)      
  337.         gameDisplay.blit(cont,(0,0))
  338.  
  339.         button("Play",150,500,100,50,white,gray,"small",action = "char")
  340.         button("Main Menu",320,500,150,50,white,gray,"tiny", action = "main")
  341.         button("Quit",550,500,100,50,white,gray,"small", action = "quit")
  342.  
  343.        
  344.         pygame.display.update()
  345.         clock.tick(60)
  346.  
  347.  
  348.  
  349. def game_intro():
  350.     intro = True
  351.     while intro:
  352.         for event in pygame.event.get():
  353.             if event.type == pygame.QUIT:
  354.                 pygame.quit()
  355.                 quit()
  356.             elif event.type == pygame.KEYDOWN:
  357.                     if event.key == pygame.K_p:
  358.                         intro = False
  359.         gameDisplay.fill(black)
  360.         gameDisplay.blit(imggg,(0,0))
  361.        
  362.    
  363.         button("Play",150,500,100,50,white,gray,"small",action = "char")
  364.         button("ControLs",320,500,150,50,white,gray,"tiny", action = "controls")
  365. ##        button("Characters",295,425,200,50,white,gray,"tiny", action = "char")
  366.         button(" ",0,0,0,0,white,gray,"tiny", action = "controls")        
  367.         button("Quit",550,500,100,50,white,gray,"small", action = "quit")
  368.  
  369.        
  370.  
  371.         pygame.display.update()
  372.         clock.tick(60)
  373.        
  374.        
  375.        
  376.  
  377.        
  378.  
  379.  
  380.  
  381. def gameLoop():
  382.     global direction
  383.     direction = "left"
  384.     gameExit = False
  385.     gameOver= False        
  386.     lead_y = display_height/2
  387.     lead_x = display_width/2
  388.  
  389.     lead_x_change = -10
  390.     lead_y_change = 0
  391.  
  392.     snakeList = []
  393.     snakeLength = 1
  394.  
  395.     randAppleX, randAppleY = randAppleGen()
  396.    
  397.     while not gameExit:
  398.  
  399.         while gameOver == True:
  400. ##            gameDisplay.blit(loser,(0,0))
  401.             message_to_screen("Game over",
  402.                               red,y_displace=-50,size="large")
  403.            
  404.             message_to_screen("Press Q to quit or P to play again",
  405.                               black,50,size="small")
  406.             pygame.display.update()
  407.            
  408.  
  409.             for event in pygame.event.get():
  410.                 if event.type == pygame.QUIT:
  411.                    
  412.                     gameExit = True
  413.                     gameOver = False
  414.                    
  415.                 if event.type == pygame.KEYDOWN:
  416.                     if event.key == pygame.K_q:
  417.                         gameExit = True
  418.                         gameOver = False
  419.                     if event.key == pygame.K_p:
  420.                         game_char()
  421.  
  422.        
  423.         for event in pygame.event.get():
  424.             if event.type == pygame.QUIT:
  425.                         gameExit = True
  426.             if event.type == pygame.KEYDOWN:
  427.                     if event.key == pygame.K_a:
  428.  
  429.                             direction = "left"
  430.                             lead_x_change = -block_size
  431.                             lead_y_change = 0
  432.                     elif event.key == pygame.K_d:
  433.                             direction = "right"
  434.                             lead_x_change = block_size
  435.                             lead_y_change = 0
  436.                     elif event.key == pygame.K_w:
  437.                             direction = "up"
  438.                             lead_y_change = -block_size
  439.                             lead_x_change = 0
  440.                     elif event.key == pygame.K_s:
  441.                             direction = "down"
  442.                             lead_y_change = block_size
  443.                             lead_x_change = 0
  444.                     elif event.key == pygame.K_UP:
  445.                             direction = "up"
  446.                             lead_x_change = 0
  447.                             lead_y_change = -block_size
  448.                     elif event.key == pygame.K_DOWN:
  449.                             direction = "down"
  450.                             lead_y_change = block_size
  451.                             lead_x_change = 0
  452.                     elif event.key == pygame.K_LEFT:
  453.                             direction = "left"
  454.                             lead_x_change = -block_size
  455.                             lead_y_change = 0
  456.                     elif event.key == pygame.K_RIGHT:
  457.                             direction = "right"
  458.                             lead_x_change = block_size
  459.                             lead_y_change = 0
  460.                     elif event.key == pygame.K_ESCAPE:
  461.                         pause()
  462.                        
  463.         if lead_x >= display_width or lead_x < 0 or lead_y >= display_height or lead_y < 0:
  464.             gameOver = True
  465.  
  466.        
  467.         '''lead_x = (lead_x + lead_x_change) % 800 lead_y = (lead_y + lead_y_change) % 600'''
  468.  
  469.         lead_x += lead_x_change
  470.         lead_y += lead_y_change
  471.        
  472.        
  473.    
  474.         gameDisplay.fill(gray)
  475.  
  476.         gameDisplay.blit(yum,(randAppleX,randAppleY))
  477.        
  478.        
  479.         snakeHead = []
  480.         snakeHead.append(lead_x)
  481.         snakeHead.append(lead_y)
  482.         snakeList.append(snakeHead)
  483.  
  484.         if len(snakeList)> snakeLength:
  485.             del snakeList [0]
  486.        
  487.         snake(block_size, snakeList)
  488.        
  489.         for eachSegment in snakeList [:-1]:
  490.             if eachSegment == snakeHead:
  491.                 gameOver = True
  492.        
  493.         snake(block_size, snakeList)
  494.  
  495.         score(snakeLength-1)
  496.        
  497.         pygame.display.update()
  498.  
  499.  
  500.  
  501.         if lead_x > randAppleX and lead_x < randAppleX + AppleThickness or lead_x + block_size > randAppleX and lead_x + block_size < randAppleX + AppleThickness:
  502.             if lead_y > randAppleY and lead_y < randAppleY + AppleThickness or lead_y + block_size > randAppleY and lead_y + block_size < randAppleY + AppleThickness:
  503.                 randAppleX, randAppleY = randAppleGen()
  504.                 snakeLength += 1
  505.  
  506.             elif lead_y + block_size > randAppleY and lead_y + block_size < randAppleY + AppleThickness:
  507.                 randAppleX, randAppleY = randAppleGen()
  508.                 snakeLength += 1
  509.  
  510.        
  511.  
  512.         clock.tick(FPS)
  513.  
  514.  
  515.  
  516.     pygame.quit()
  517.     quit()
  518. game_intro()
  519. gameLoop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement