Advertisement
ES99

Updated snake game

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