Advertisement
Shlocko

Untitled

Jan 17th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.68 KB | None | 0 0
  1. import pygame
  2. import time
  3. import random
  4.  
  5. pygame.init()
  6.  
  7. # Defining colors
  8. white = (255, 255, 255)
  9. black = (0, 0, 0)
  10. red = (255, 0, 0)
  11. green = (0, 255, 0)
  12. blue = (0, 0, 255)
  13.  
  14. # Setting the "Surface" (IE game window)
  15. display_width = 800
  16. display_height = 600
  17. gameDisplay = pygame.display.set_mode((display_width, display_height))
  18. pygame.display.set_caption('Slither')
  19.  
  20. # Defining some game constants
  21. font = pygame.font.SysFont(None, 25)
  22. clock = pygame.time.Clock()
  23. fps = 15
  24.  
  25.  
  26. def snake(lead_x, lead_y, player_width, player_height):  # Snake rendering/logic(someday)
  27.     pygame.draw.rect(gameDisplay, green, [lead_x, lead_y, player_width, player_height])
  28.  
  29.  
  30. def message_to_screen(msg, color):  # Display text in center of screen
  31.     screen_text = font.render(msg, True, color)
  32.     gameDisplay.blit(screen_text, [display_width / 2, display_height / 2])
  33.  
  34.  
  35. def game_loop():  # Actual main loop
  36.    
  37.     # Defining some game variables
  38.     game_exit = False
  39.     game_over = False
  40.     player_height = 10
  41.     player_width = 10
  42.     apple_size = 10
  43.     lead_x = display_width / 2
  44.     lead_y = display_height / 2
  45.     lead_x_change = 0
  46.     lead_y_change = 0
  47.     rand_apple_x = round(random.randrange(0, display_width - apple_size) / 10.0) * 10.0
  48.     rand_apple_y = round(random.randrange(0, display_height - apple_size) / 10.0) * 10.0
  49.    
  50.     while not game_exit:  # Main Game loop
  51.        
  52.         while game_over:  # Game Over menu
  53.             gameDisplay.fill(white)
  54.             message_to_screen("Game over, press C to play again or Q to quit", red)
  55.             pygame.display.update()
  56.             for event in pygame.event.get():  # Event checking for game over
  57.                 if event.type == pygame.KEYDOWN:
  58.                     if event.key == pygame.K_c:
  59.                         game_loop()
  60.                     elif event.key == pygame.K_q:
  61.                         game_exit = True
  62.                         game_over = False
  63.        
  64.         for event in pygame.event.get():  # Checking for events
  65.             if event.type == pygame.QUIT:
  66.                 game_exit = True
  67.             if event.type == pygame.KEYDOWN:
  68.                 if event.key == pygame.K_LEFT:
  69.                     lead_x_change = -player_width
  70.                     lead_y_change = 0
  71.                 if event.key == pygame.K_RIGHT:
  72.                     lead_x_change = player_width
  73.                     lead_y_change = 0
  74.                 if event.key == pygame.K_UP:
  75.                     lead_y_change = -player_height
  76.                     lead_x_change = 0
  77.                 if event.key == pygame.K_DOWN:
  78.                     lead_y_change = player_height
  79.                     lead_x_change = 0
  80.        
  81.         if lead_x > display_width - player_width or lead_x < 0 or lead_y > display_height - player_height or lead_y < 0:  # Check if player is within bounds
  82.             game_over = True
  83.        
  84.         # Moving Player
  85.         lead_x += lead_x_change
  86.         lead_y += lead_y_change
  87.        
  88.         # Drawing Game - - - -
  89.         gameDisplay.fill(white)
  90.         pygame.draw.rect(gameDisplay, red, [rand_apple_x, rand_apple_y, apple_size, apple_size])
  91.         snake(lead_x, lead_y, player_width, player_height)
  92.         pygame.display.update()  # Render all changes to graphics since last frame
  93.        
  94.         if lead_x == rand_apple_x and lead_y == rand_apple_y:  # Snake Apple collision detection
  95.             print("om nom nom")
  96.             rand_apple_x = round(random.randrange(0, display_width - apple_size) / 10.0) * 10.0
  97.             rand_apple_y = round(random.randrange(0, display_height - apple_size) / 10.0) * 10.0
  98.        
  99.         clock.tick(fps)
  100.    
  101.     pygame.quit()
  102.     quit()
  103.  
  104.  
  105. game_loop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement