Advertisement
Guest User

Untitled

a guest
Apr 15th, 2025
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. > write me a simple snake game.
  2. Sure, here is a simple version of the snake game in Python using the Pygame library. Make sure you have Pygame installed (`pip install pygame`) before running the script.
  3.  
  4. ```python
  5. import pygame
  6. import time
  7. import random
  8.  
  9. pygame.init()
  10.  
  11. # Set up display
  12. width, height = 800, 600
  13. screen = pygame.display.set_mode((width, height))
  14. pygame.display.set_caption('Snake Game')
  15.  
  16. # Define colors
  17. white = (255, 255, 255)
  18. yellow = (255, 255, 102)
  19. black = (0, 0, 0)
  20. red = (255, 0, 0)
  21. green = (0, 255, 0)
  22. blue = (0, 0, 255)
  23.  
  24. # Define snake block size
  25. snake_block = 10
  26.  
  27. # Define speed of the snake
  28. snake_speed = 15
  29.  
  30. # Initialize the snake
  31. snake_List = []
  32. length_of_snake = 1
  33.  
  34. # Set up the font
  35. font_style = pygame.font.SysFont(None, 50)
  36. score_font = pygame.font.SysFont(None, 35)
  37.  
  38. def our_snake(snake_block, snake_List):
  39. for x in snake_List:
  40. pygame.draw.rect(screen, black, [x[0], x[1], snake_block, snake_block])
  41.  
  42. def message(msg, color):
  43. mesg = font_style.render(msg, True, color)
  44. screen.blit(mesg, [width / 6, height / 3])
  45.  
  46. def gameLoop():
  47. game_over = False
  48. game_close = False
  49.  
  50. x1 = width / 2
  51. y1 = height / 2
  52.  
  53. x1_change = 0
  54. y1_change = 0
  55.  
  56. snake_List = []
  57. length_of_snake = 1
  58.  
  59. foodx = round(random.randrange(0, width - snake_block) / 10.0) * 10.0
  60. foody = round(random.randrange(0, height - snake_block) / 10.0) * 10.0
  61.  
  62. while not game_over:
  63.  
  64. while game_close:
  65. screen.fill(blue)
  66. message("You Lost! Press Q-Quit or C-Play Again", red)
  67. pygame.display.update()
  68.  
  69. for event in pygame.event.get():
  70. if event.type == pygame.KEYDOWN:
  71. if event.key == pygame.K_q:
  72. game_over = True
  73. game_close = False
  74. if event.key == pygame.K_c:
  75. gameLoop()
  76.  
  77. for event in pygame.event.get():
  78. if event.type == pygame.QUIT:
  79. game_over = True
  80. if event.type == pygame.KEYDOWN:
  81. if event.key == pygame.K_LEFT:
  82. x1_change = -snake_block
  83. y1_change = 0
  84. elif event.key == pygame.K_RIGHT:
  85. x1_change = snake_block
  86. y1_change = 0
  87. elif event.key == pygame.K_UP:
  88. y1_change = -snake_block
  89. x1_change = 0
  90. elif event.key == pygame.K_DOWN:
  91. y1_change = snake_block
  92. x1_change = 0
  93.  
  94. if x1 >= width or x1 < 0 or y1 >= height or y1 < 0:
  95. game_close = True
  96. x1 += x1_change
  97. y1 += y1_change
  98. screen.fill(blue)
  99. pygame.draw.rect(screen, green, [foodx, foody, snake_block, snake_block])
  100. snake_Head = []
  101. snake_Head.append(x1)
  102. snake_Head.append(y1)
  103. snake_List.append(snake_Head)
  104. if len(snake_List) > length_of_snake:
  105. del snake_List[0]
  106.  
  107. for x in snake_List[:-1]:
  108. if x == snake_Head:
  109. game_close = True
  110.  
  111. our_snake(snake_block, snake_List)
  112.  
  113. pygame.display.update()
  114.  
  115. if x1 == foodx and y1 == foody:
  116. foodx = round(random.randrange(0, width - snake_block) / 10.0) * 10.0
  117. foody = round(random.randrange(0, height - snake_block) / 10.0) * 10.0
  118. length_of_snake += 1
  119.  
  120. time.sleep(snake_speed)
  121.  
  122. pygame.quit()
  123. quit()
  124.  
  125. gameLoop()
  126. ```
  127.  
  128. This code creates a simple snake game. The snake grows and moves around the screen, eating the food that appears randomly. The game ends if the snake runs into the wall or itself. The score is displayed at the top of the screen.
  129.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement