Advertisement
szze

OpenAI Snake

Dec 31st, 2022 (edited)
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.37 KB | None | 0 0
  1. import pygame
  2. import random
  3.  
  4. # Initialize pygame
  5. pygame.init()
  6.  
  7. # Set the window size
  8. window_size = (600, 600)
  9.  
  10. # Create the window
  11. screen = pygame.display.set_mode(window_size)
  12.  
  13. # Set the title of the window
  14. pygame.display.set_caption("Snake")
  15.  
  16. # Set the dimensions of the snake block
  17. block_size = 20
  18.  
  19. # Set the rate of the game (in milliseconds)
  20. refresh_rate = 150
  21.  
  22. # Set the font for rendering text
  23. font = pygame.font.Font(None, 36)
  24.  
  25. # Set the colors
  26. black = (0, 0, 0)
  27. white = (255, 255, 255)
  28. green = (0, 255, 0)
  29. red = (255, 0, 0)
  30.  
  31. # Set the initial position and velocity of the snake
  32. x_position = window_size[0] // 2
  33. y_position = window_size[1] // 2
  34. x_velocity = 0
  35. y_velocity = 0
  36.  
  37. # Set the initial length of the snake
  38. length = 1
  39.  
  40. # Create an empty list for storing the blocks of the snake
  41. snake = []
  42.  
  43. # Set the initial position of the food
  44. food_x = random.randrange(0, window_size[0] - block_size, block_size)
  45. food_y = random.randrange(0, window_size[1] - block_size, block_size)
  46.  
  47. # Set the initial game over flag to False
  48. game_over = False
  49.  
  50. # Set the initial score to 0
  51. score = 0
  52.  
  53. # Set the initial high score to 0
  54. high_score = 0
  55.  
  56. # Main game loop
  57. while not game_over:
  58.     # Check for user input
  59.     for event in pygame.event.get():
  60.         # Quit the game if the user closes the window
  61.         if event.type == pygame.QUIT:
  62.             game_over = True
  63.  
  64.         # Change the direction of the snake based on the arrow keys
  65.         if event.type == pygame.KEYDOWN:
  66.             if event.key == pygame.K_LEFT and x_velocity != block_size:
  67.                 x_velocity = -block_size
  68.                 y_velocity = 0
  69.             elif event.key == pygame.K_RIGHT and x_velocity != -block_size:
  70.                 x_velocity = block_size
  71.                 y_velocity = 0
  72.             elif event.key == pygame.K_UP and y_velocity != block_size:
  73.                 x_velocity = 0
  74.                 y_velocity = -block_size
  75.             elif event.key == pygame.K_DOWN and y_velocity != -block_size:
  76.                 x_velocity = 0
  77.                 y_velocity = block_size
  78.  
  79.     # Update the position of the snake
  80.     x_position += x_velocity
  81.     y_position += y_velocity
  82.  
  83.     # Check if the snake has collided with the edges of the screen
  84.     if x_position < 0 or x_position > window_size[0] - block_size or y_position < 0 or y_position > window_size[1] - block_size:
  85.         game_over = True
  86.  
  87.     # Check if the snake has collided with itself
  88.     for block in snake:
  89.         if block[0] == x_position and block[1] == y_position and len(snake) > 1:
  90.             game_over = True
  91.  
  92.     # Update the snake list
  93.     snake.append((x_position, y_position))
  94.  
  95.     # Remove the first block of the snake if the length of the snake is greater than the current length
  96.     if len(snake) > length:
  97.         snake.pop(0)
  98.  
  99.     # Check if the snake has eaten the food
  100.     if x_position == food_x and y_position == food_y:
  101.         # Increase the length of the snake
  102.         length += 1
  103.  
  104.         # Increase the score
  105.         score += 1
  106.  
  107.         # Check if the score is higher than the high score
  108.         if score > high_score:
  109.             high_score = score
  110.  
  111.         # Generate new food
  112.         food_x = random.randrange(0, window_size[0] - block_size, block_size)
  113.         food_y = random.randrange(0, window_size[1] - block_size, block_size)
  114.  
  115.     # Clear the screen
  116.     screen.fill(black)
  117.  
  118.     # Draw the food
  119.     pygame.draw.rect(screen, green, (food_x, food_y, block_size, block_size))
  120.  
  121.     # Draw the snake
  122.     for block in snake:
  123.         pygame.draw.rect(screen, white, (block[0], block[1], block_size, block_size))
  124.  
  125.     # Render the score text
  126.     text = font.render("Score: " + str(score), True, white)
  127.     screen.blit(text, (10, 10))
  128.  
  129.     # Render the high score text
  130.     text = font.render("High Score: " + str(high_score), True, white)
  131.     screen.blit(text, (10, 40))
  132.  
  133.     # Update the display
  134.     pygame.display.flip()
  135.  
  136.     # Wait for the refresh rate
  137.     pygame.time.delay(refresh_rate)
  138.  
  139. # Game over message
  140. text = font.render("Game Over", True, red)
  141. text_rect = text.get_rect()
  142. text_rect.center = (window_size[0] // 2, window_size[1] // 2)
  143. screen.blit(text, text_rect)
  144. pygame.display.flip()
  145.  
  146. # Wait for the user to quit
  147. while True:
  148.     for event in pygame.event.get():
  149.         if event.type == pygame.QUIT:
  150.             pygame.quit()
  151.             sys.exit()
  152.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement