Guest User

Untitled

a guest
Dec 14th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.86 KB | None | 0 0
  1. """
  2. Snake Eater
  3. Made with PyGame
  4. """
  5.  
  6. import pygame, sys, time, random
  7.  
  8.  
  9. # Difficulty settings
  10. # Easy -> 10
  11. # Medium -> 25
  12. # Hard -> 40
  13. # Harder -> 60
  14. # Impossible-> 120
  15. difficulty = 25
  16.  
  17. # Window size
  18. frame_size_x = 720
  19. frame_size_y = 480
  20.  
  21. # Checks for errors encountered
  22. check_errors = pygame.init()
  23. # pygame.init() example output -> (6, 0)
  24. # second number in tuple gives number of errors
  25. if check_errors[1] > 0:
  26. print(f'[!] Had {check_errors[1]} errors when initialising game, exiting...')
  27. sys.exit(-1)
  28. else:
  29. print('[+] Game successfully initialised')
  30.  
  31.  
  32. # Initialise game window
  33. pygame.display.set_caption('Snake Eater')
  34. game_window = pygame.display.set_mode((frame_size_x, frame_size_y))
  35.  
  36.  
  37. # Colors (R, G, B)
  38. black = pygame.Color(0, 0, 0)
  39. white = pygame.Color(255, 255, 255)
  40. red = pygame.Color(255, 0, 0)
  41. green = pygame.Color(0, 255, 0)
  42. blue = pygame.Color(0, 0, 255)
  43.  
  44.  
  45. # FPS (frames per second) controller
  46. fps_controller = pygame.time.Clock()
  47.  
  48.  
  49. # Game variables
  50. snake_pos = [100, 50]
  51. snake_body = [[100, 50], [100-10, 50], [100-(2*10), 50]]
  52.  
  53. food_pos = [random.randrange(1, (frame_size_x//10)) * 10, random.randrange(1, (frame_size_y//10)) * 10]
  54. food_spawn = True
  55.  
  56. direction = 'RIGHT'
  57. change_to = direction
  58.  
  59. score = 0
  60.  
  61.  
  62. # Game Over
  63. def game_over():
  64. my_font = pygame.font.SysFont('times new roman', 90)
  65. game_over_surface = my_font.render('YOU DIED', True, red)
  66. game_over_rect = game_over_surface.get_rect()
  67. game_over_rect.midtop = (frame_size_x/2, frame_size_y/4)
  68. game_window.fill(black)
  69. game_window.blit(game_over_surface, game_over_rect)
  70. show_score(0, red, 'times', 20)
  71. pygame.display.flip()
  72. time.sleep(3)
  73. pygame.quit()
  74. sys.exit()
  75.  
  76.  
  77. # Score
  78. def show_score(choice, color, font, size):
  79. score_font = pygame.font.SysFont(font, size)
  80. score_surface = score_font.render('Score : ' + str(score), True, color)
  81. score_rect = score_surface.get_rect()
  82. if choice == 1:
  83. score_rect.midtop = (frame_size_x/10, 15)
  84. else:
  85. score_rect.midtop = (frame_size_x/2, frame_size_y/1.25)
  86. game_window.blit(score_surface, score_rect)
  87. # pygame.display.flip()
  88.  
  89.  
  90. # Main logic
  91. while True:
  92. for event in pygame.event.get():
  93. if event.type == pygame.QUIT:
  94. pygame.quit()
  95. sys.exit()
  96. # Whenever a key is pressed down
  97. elif event.type == pygame.KEYDOWN:
  98. # W -> Up; S -> Down; A -> Left; D -> Right
  99. if event.key == pygame.K_UP or event.key == ord('w'):
  100. change_to = 'UP'
  101. if event.key == pygame.K_DOWN or event.key == ord('s'):
  102. change_to = 'DOWN'
  103. if event.key == pygame.K_LEFT or event.key == ord('a'):
  104. change_to = 'LEFT'
  105. if event.key == pygame.K_RIGHT or event.key == ord('d'):
  106. change_to = 'RIGHT'
  107. # Esc -> Create event to quit the game
  108. if event.key == pygame.K_ESCAPE:
  109. pygame.event.post(pygame.event.Event(pygame.QUIT))
  110.  
  111. # Making sure the snake cannot move in the opposite direction instantaneously
  112. if change_to == 'UP' and direction != 'DOWN':
  113. direction = 'UP'
  114. if change_to == 'DOWN' and direction != 'UP':
  115. direction = 'DOWN'
  116. if change_to == 'LEFT' and direction != 'RIGHT':
  117. direction = 'LEFT'
  118. if change_to == 'RIGHT' and direction != 'LEFT':
  119. direction = 'RIGHT'
  120.  
  121. # Moving the snake
  122. if direction == 'UP':
  123. snake_pos[1] -= 10
  124. if direction == 'DOWN':
  125. snake_pos[1] += 10
  126. if direction == 'LEFT':
  127. snake_pos[0] -= 10
  128. if direction == 'RIGHT':
  129. snake_pos[0] += 10
  130.  
  131. # Snake body growing mechanism
  132. snake_body.insert(0, list(snake_pos))
  133. if snake_pos[0] == food_pos[0] and snake_pos[1] == food_pos[1]:
  134. score += 1
  135. food_spawn = False
  136. else:
  137. snake_body.pop()
  138.  
  139. # Spawning food on the screen
  140. if not food_spawn:
  141. food_pos = [random.randrange(1, (frame_size_x//10)) * 10, random.randrange(1, (frame_size_y//10)) * 10]
  142. food_spawn = True
  143.  
  144. # GFX
  145. game_window.fill(black)
  146. for pos in snake_body:
  147. # Snake body
  148. # .draw.rect(play_surface, color, xy-coordinate)
  149. # xy-coordinate -> .Rect(x, y, size_x, size_y)
  150. pygame.draw.rect(game_window, green, pygame.Rect(pos[0], pos[1], 10, 10))
  151.  
  152. # Snake food
  153. pygame.draw.rect(game_window, white, pygame.Rect(food_pos[0], food_pos[1], 10, 10))
  154.  
  155. # Game Over conditions
  156. # Getting out of bounds
  157. if snake_pos[0] < 0 or snake_pos[0] > frame_size_x-10:
  158. game_over()
  159. if snake_pos[1] < 0 or snake_pos[1] > frame_size_y-10:
  160. game_over()
  161. # Touching the snake body
  162. for block in snake_body[1:]:
  163. if snake_pos[0] == block[0] and snake_pos[1] == block[1]:
  164. game_over()
  165.  
  166. show_score(1, white, 'consolas', 20)
  167. # Refresh game screen
  168. pygame.display.update()
  169. # Refresh rate
  170. fps_controller.tick(difficulty)
Add Comment
Please, Sign In to add comment