Advertisement
Guest User

Untitled

a guest
Mar 31st, 2024
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.89 KB | None | 0 0
  1. Can you give me python code for a functional game of snake with a gui?
  2.  
  3. Sure, here is a simple implementation of the snake game using the Pygame library in Python:
  4.  
  5. import pygame
  6. import time
  7. import random
  8.  
  9. pygame.init()
  10.  
  11. white = (255, 255, 255)
  12. yellow = (255, 255, 102)
  13. black = (0, 0, 0)
  14. red = (213, 50, 80)
  15. green = (0, 255, 0)
  16. blue = (50, 153, 213)
  17.  
  18. dis_width = 800
  19. dis_height = 600
  20.  
  21. dis = pygame.display.set_mode((dis_width, dis_height))
  22. pygame.display.set_caption('Snake Game')
  23.  
  24. clock = pygame.time.Clock()
  25. snake_block = 10
  26. snake_speed = 30
  27.  
  28. font_style = pygame.font.SysFont(None, 50)
  29. score_font = pygame.font.SysFont(None, 35)
  30.  
  31.  
  32. def our_snake(snake_block, snake_List):
  33. for x in snake_List:
  34. pygame.draw.rect(dis, white, [x[0], x[1], snake_block, snake_block])
  35.  
  36.  
  37. def message(msg, color):
  38. mesg = font_style.render(msg, True, color)
  39. dis.blit(mesg, [dis_width / 2, dis_height / 2])
  40.  
  41.  
  42. def gameLoop():
  43. game_over = False
  44. game_close = False
  45.  
  46. x1 = dis_width / 2
  47. y1 = dis_height / 2
  48.  
  49. x1_change = 0
  50. y1_change = 0
  51.  
  52. snake_List = []
  53. Length_of_snake = 1
  54.  
  55. foodx = round(random.randrange(0, dis_width - snake_block) / 10.0) * 10.0
  56. foody = round(random.randrange(0, dis_height - snake_block) / 10.0) * 10.0
  57.  
  58. while not game_over:
  59.  
  60. while game_close == True:
  61. dis.fill(blue)
  62. message("You Lost! Press C-Continue or Q-Quit", red)
  63. pygame.display.update()
  64.  
  65. for event in pygame.event.get():
  66. if event.type == pygame.KEYDOWN:
  67. if event.key == pygame.K_q:
  68. game_over = True
  69. game_close = False
  70. if event.key == pygame.K_c:
  71. gameLoop()
  72.  
  73. for event in pygame.event.get():
  74. if event.type == pygame.QUIT:
  75. game_over = True
  76. game_close = False
  77. if event.type == pygame.KEYDOWN:
  78. if event.key == pygame.K_UP:
  79. y1_change = -snake_block
  80. x1_change = 0
  81. elif event.key == pygame.K_DOWN:
  82. y1_change = snake_block
  83. x1_change = 0
  84. elif event.key == pygame.K_LEFT:
  85. x1_change = -snake_block
  86. y1_change = 0
  87. elif event.key == pygame.K_RIGHT:
  88. x1_change = snake_block
  89. y1_change = 0
  90.  
  91. if x1 >= dis_width or x1 < 0 or y1 >= dis_height or y1 < 0:
  92. game_close = True
  93. x1 += x1_change
  94. y1 += y1_change
  95. dis.fill(blue)
  96. pygame.draw.rect(dis, green, [foodx, foody, snake_block, snake_block])
  97. snake_Head = []
  98. snake_Head.append(x1)
  99. snake_Head.append(y1)
  100. snake_List.append(snake_Head)
  101. if len(snake_List) > Length_of_snake:
  102. del snake_List[0]
  103.  
  104. for x in snake_List[:-1]:
  105. if x == snake_Head:
  106. game_close = True
  107.  
  108. our_snake(snake_block, snake_List)
  109.  
  110. pygame.display.update()
  111.  
  112. if x1 == foodx and y1 == foody:
  113. foodx = round(random.randrange(0, dis_width - snake_block) / 10.0) * 10.0
  114. foody = round(random.randrange(0, dis_height - snake_block) / 10.0) * 10.0
  115. Length_of_snake += 1
  116.  
  117. clock.tick(snake_speed)
  118.  
  119. pygame.quit()
  120. quit()
  121.  
  122.  
  123. gameLoop()
  124.  
  125. This code creates a simple snake game with a GUI using the Pygame library. The snake is controlled using the arrow keys and the goal is to eat the food that appears at random locations on the screen. Each time the snake eats the food, its length increases. The game ends when the snake runs into the screen border or into itself.
  126.  
  127. Please note that this is a basic implementation and there are many ways to improve and expand upon this code.<|im_end|>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement