Advertisement
Guest User

Snake V 3.0

a guest
Jul 11th, 2016
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 11.03 KB | None | 0 0
  1. #Sets up framework, and imports the PyGame module, time module, and random module
  2. import pygame
  3. import time
  4. import random
  5.  
  6. pygame.init()
  7.  
  8. point_sound = pygame.mixer.Sound("point.wav")
  9. game_over_sound = pygame.mixer.Sound("gameovr.wav")
  10. pygame.mixer.music.load("music.mp3")
  11.  
  12.  
  13.  
  14. white = (255,255,255)
  15. black = (0,0,0)
  16. red = (255,0,0)
  17. green = (0,155,0)
  18. blue = (0,0,255)
  19.  
  20. display_width = 800
  21. display_height = 600
  22.  
  23. gameDisplay = pygame.display.set_mode((display_width,display_height))
  24. pygame.display.set_caption("Snake v 3.0")
  25.  
  26.  
  27.  
  28. clock = pygame.time.Clock()
  29.  
  30. block_size = 10
  31. FPS = 20
  32.  
  33. smallfont = pygame.font.SysFont(None, 25)
  34. medfont = pygame.font.SysFont(None, 50)
  35. largefont = pygame.font.SysFont(None, 80)
  36.  
  37. def instruction_screen():
  38.  
  39.     instruction = True
  40.  
  41.     while instruction:
  42.        
  43.         gameDisplay.fill(white)
  44.         message_to_screen("Instructions.",
  45.                           green,
  46.                           -200,
  47.                           "large")
  48.         message_to_screen("Use the arrow keys to move.",
  49.                           black,
  50.                           -130)
  51.         message_to_screen("'M' mutes the music.",
  52.                           black,
  53.                           -80)
  54.         message_to_screen("'K' unmutes the music.",
  55.                           black,
  56.                           -30)
  57.         message_to_screen("Press 'R' to return to the main menu.",
  58.                           black,
  59.                           20)
  60.         for event in pygame.event.get():
  61.             if event.type == pygame.QUIT:
  62.                 pygame.quit()
  63.                 quit()
  64.             if event.type == pygame.KEYDOWN:
  65.                 if event.key == pygame.K_r:
  66.                     game_intro()
  67.  
  68.         pygame.display.update()
  69.  
  70.  
  71. def pause():
  72.  
  73.     paused = True
  74.  
  75.     while paused:
  76.         pygame.mixer.music.pause()
  77.         for event in pygame.event.get():
  78.             if event.type == pygame.QUIT:
  79.                 pygame.quit()
  80.                 quit()
  81.  
  82.             if event.type == pygame.KEYDOWN:
  83.                 if event.key == pygame.K_c:
  84.                     paused = False
  85.                     pygame.mixer.music.unpause()
  86.  
  87.                 elif event.key == pygame.K_q:
  88.                     pygame.quit()
  89.                     quit()
  90.         gameDisplay.fill(white)
  91.         message_to_screen("Paused",
  92.                           black,
  93.                           -100,
  94.                           size="large")
  95.  
  96.         message_to_screen("Press C to continue, or Q to quit.",
  97.                           black,
  98.                           25)
  99.         pygame.display.update()
  100.         clock.tick(5)
  101.      
  102.  
  103. def score(score):
  104.     text = smallfont.render("Score: "+str(score), True, black)
  105.     gameDisplay.blit(text, [0,0])
  106.  
  107. def game_over():
  108.  
  109.     game_over = True
  110.     cursor2 = 2
  111.     pygame.mixer.music.stop()
  112.     pygame.mixer.Sound.play(game_over_sound)
  113.     while game_over:
  114.          for event in pygame.event.get():
  115.             if event.type == pygame.QUIT:
  116.                 pygame.quit()
  117.                 quit()
  118.             gameDisplay.fill(white)
  119.             message_to_screen("You died, game over.",
  120.                                             red,
  121.                                             -100,
  122.                                             "large")
  123.             message_to_screen("Play again.",
  124.                                     black,
  125.                                     10)
  126.             message_to_screen("Quit.",
  127.                                 black,
  128.                                 50)
  129.  
  130.             pygame.display.update()
  131.             clock.tick(15)
  132.             if event.type == pygame.KEYDOWN:
  133.                 if event.key == pygame.K_UP:
  134.                     cursor2 = 0
  135.                 if event.key == pygame.K_DOWN:
  136.                     cursor2 = 1
  137.                 if event.key == pygame.K_RETURN and cursor2 == 0:
  138.                     gameLoop()
  139.                 if event.key == pygame.K_RETURN and cursor2 == 1:
  140.                     pygame.quit()
  141.                     quit()
  142.             if cursor2 == 0:
  143.                 message_to_screen("Quit. <", white, 50)
  144.                 message_to_screen("Quit.", black, 50)
  145.                 message_to_screen("Play again.", white, 10)
  146.                 message_to_screen("Play again. <", blue, 10)
  147.                 pygame.display.update()
  148.             if cursor2 == 1:
  149.                 message_to_screen("Play again. <", white, 10)
  150.                 message_to_screen("Play again.", black, 10)
  151.                 message_to_screen("Quit.", white, 50)
  152.                 message_to_screen("Quit. <", blue, 50)
  153.                 pygame.display.update()
  154.  
  155.                
  156.  
  157.  
  158.  
  159.  
  160. def game_intro():
  161.  
  162.     intro = True
  163.     cursor = 2
  164.  
  165.     while intro:
  166.          for event in pygame.event.get():
  167.             if event.type == pygame.QUIT:
  168.                 pygame.quit()
  169.                 quit()
  170.  
  171.            
  172.             if event.type == pygame.KEYDOWN:
  173.                 if event.key == pygame.K_UP:
  174.                     cursor = 0
  175.                 if event.key == pygame.K_DOWN:
  176.                     cursor = 1
  177.                 if event.key == pygame.K_RETURN and cursor == 0:
  178.                     gameLoop()
  179.                 if event.key == pygame.K_RETURN and cursor == 1:
  180.                     pygame.quit()
  181.                     quit()
  182.                 if event.key == pygame.K_i:
  183.                     instruction_screen()
  184.             if cursor == 0:
  185.                 message_to_screen("Quit. <", white, 180)
  186.                 message_to_screen("Quit.", black, 180)
  187.                 message_to_screen("Play.", white, 140)
  188.                 message_to_screen("Play. <", blue, 140)
  189.                 pygame.display.update()
  190.             if cursor == 1:
  191.                 message_to_screen("Play. <", white, 140)
  192.                 message_to_screen("Play.", black, 140)
  193.                 message_to_screen("Quit.", white, 180)
  194.                 message_to_screen("Quit. <", blue, 180)
  195.                 pygame.display.update()
  196.             if cursor == 2:
  197.                 gameDisplay.fill(white)
  198.                 message_to_screen("Welcome to Snake.",
  199.                                   green,
  200.                                   -100,
  201.                                   "large")
  202.                 message_to_screen("The objective of the game is to eat red apples.",
  203.                                   black,
  204.                                   -30)
  205.  
  206.                 message_to_screen("The more apples you eat, the longer you get.",
  207.                                   black,
  208.                                   10)
  209.  
  210.                 message_to_screen("If you run into yourself, or run off the screen, you die.",
  211.                                   black,
  212.                                   50)
  213.                 #message_to_screen("Controls: Arrow Keys to move, M to mute, K to unmute, P to pause.",
  214.                                   #black,
  215.                                   #90)
  216.  
  217.                 message_to_screen("Press I to view instructions.",
  218.                                   black,
  219.                                   90)
  220.                 message_to_screen("Play.",
  221.                                   black,
  222.                                   140)
  223.                 message_to_screen("Quit.",
  224.                                   black,
  225.                                   180)
  226.  
  227.                 pygame.display.update()
  228.                 clock.tick(15)
  229.  
  230.        
  231.        
  232.  
  233.  
  234.  
  235. def snake(block_size, snakeList):
  236.     for XnY in snakeList:
  237.         pygame.draw.rect(gameDisplay, green, [XnY[0],XnY[1],block_size,block_size])
  238.  
  239. def text_objects(text,color,size):
  240.     if size == "small":
  241.         textSurface = smallfont.render(text, True, color)
  242.     elif size == "medium":
  243.         textSurface = medfont.render(text, True, color)
  244.     elif size == "large":
  245.         textSurface = largefont.render(text, True, color)
  246.  
  247.     return textSurface, textSurface.get_rect()
  248.  
  249. #defines message to screen and its attributes
  250. def message_to_screen(msg,color, y_displace=0, size = "small"):
  251.     textSurf, textRect = text_objects(msg,color,size)
  252.     textRect.center = (display_width / 2), (display_height / 2)+y_displace
  253.     gameDisplay.blit(textSurf, textRect)
  254.    
  255. #Defines gameloop, gameloop contains the game itself    
  256. def gameLoop():
  257.     gameExit = False
  258.     gameOver = False
  259.     pygame.mixer.music.play(-1)
  260.    
  261.     lead_x = display_width/2
  262.     lead_y = display_height/2
  263.  
  264.     lead_x_change = 0
  265.     lead_y_change = 0
  266.  
  267.     snakeList = []
  268.     snakeLength = 1
  269.     randAppleX = round(random.randrange(0, display_width-block_size)/10.0)*10.0
  270.     randAppleY = round(random.randrange(0, display_height-block_size)/10.0)*10.0
  271.  
  272.     while not gameExit:
  273.         if gameOver == True:
  274.             game_over()
  275.  
  276.      
  277.         for event in pygame.event.get():
  278.             if event.type  == pygame.QUIT:
  279.                 gameExit = True
  280.             if event.type == pygame.KEYDOWN:
  281.                 if event.key == pygame.K_LEFT:
  282.                     lead_x_change = -block_size
  283.                     lead_y_change = 0
  284.                 elif event.key == pygame.K_RIGHT:
  285.                     lead_x_change = block_size
  286.                     lead_y_change = 0
  287.  
  288.                 elif event.key == pygame.K_UP:
  289.                     lead_y_change = -block_size
  290.                     lead_x_change = 0
  291.                 elif event.key == pygame.K_DOWN:
  292.                     lead_y_change = block_size
  293.                     lead_x_change = 0
  294.                 elif event.key == pygame.K_p:
  295.                     pause()
  296.                 elif event.key == pygame.K_m:
  297.                     pygame.mixer.music.pause()
  298.                 elif event.key == pygame.K_k:
  299.                     pygame.mixer.music.unpause()
  300.                    
  301.        
  302.         #Sets boundaries              
  303.         if lead_x >= display_width or lead_x < 0 or lead_y >= display_height or lead_y < 0:
  304.             gameOver = True
  305.        
  306.         #allows for controls
  307.         lead_x += lead_x_change  
  308.         lead_y += lead_y_change
  309.        
  310.         #draws objects, and updates screen
  311.         gameDisplay.fill(white)
  312.         pygame.draw.rect(gameDisplay, red, [randAppleX, randAppleY, block_size, block_size])
  313.  
  314.         snakeHead = []
  315.         snakeHead.append(lead_x)
  316.         snakeHead.append(lead_y)
  317.         snakeList.append(snakeHead)
  318.  
  319.         if len(snakeList) > snakeLength:
  320.             del snakeList[0]
  321.  
  322.         for eachSegment in snakeList[:-1]:
  323.             if eachSegment == snakeHead:
  324.                 gameOver = True
  325.        
  326.            
  327.  
  328.         snake(block_size, snakeList)
  329.  
  330.         score(snakeLength - 1)
  331.        
  332.         pygame.display.update()
  333.  
  334.         if lead_x == randAppleX and lead_y == randAppleY:
  335.             randAppleX = round(random.randrange(0, display_width-block_size)/10.0)*10.0
  336.             randAppleY = round(random.randrange(0, display_height-block_size)/10.0)*10.0
  337.             snakeLength += 1
  338.             pygame.mixer.Sound.play(point_sound)
  339.            
  340.         #sets FPS
  341.         clock.tick(FPS)
  342.  
  343.     pygame.quit()
  344.     quit()
  345.  
  346. game_intro()
  347. gameLoop()
  348. game_over()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement