Guest User

Untitled

a guest
Mar 31st, 2024
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 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 tkinter library in Python:
  4.  
  5. import tkinter as tk
  6. import random
  7.  
  8. class Snake:
  9. def __init__(self):
  10. self.direction = "down"
  11. self.positions = [(0, 0), (0, -20), (0, -40)]
  12. self.body = self.positions[:]
  13.  
  14. def move(self):
  15. head = self.positions[0]
  16. x, y = head
  17.  
  18. if self.direction == "up":
  19. y -= 20
  20. elif self.direction == "down":
  21. y += 20
  22. elif self.direction == "left":
  23. x -= 20
  24. elif self.direction == "right":
  25. x += 20
  26.  
  27. self.positions.insert(0, (x, y))
  28. self.positions.pop()
  29.  
  30. def change_direction(self, new_direction):
  31. if new_direction == "up" and self.direction != "down":
  32. self.direction = "up"
  33. elif new_direction == "down" and self.direction != "up":
  34. self.direction = "down"
  35. elif new_direction == "left" and self.direction != "right":
  36. self.direction = "left"
  37. elif new_direction == "right" and self.direction != "left":
  38. self.direction = "right"
  39.  
  40. class Game:
  41. def __init__(self):
  42. self.width = 640
  43. self.height = 480
  44. self.window = tk.Tk()
  45. self.window.title("Snake Game")
  46. self.canvas = tk.Canvas(self.window, width=self.width, height=self.height, bg="white")
  47. self.canvas.pack()
  48. self.snake = Snake()
  49. self.food = self.create_food()
  50. self.score = 0
  51. self.bind_keys()
  52. self.game_over = False
  53.  
  54. def create_food(self):
  55. x = random.randint(0, self.width - 20) // 20 * 20
  56. y = random.randint(0, self.height - 20) // 20 * 20
  57. self.canvas.create_rectangle(x, y, x + 20, y + 20, fill="green")
  58. return (x, y)
  59.  
  60. def bind_keys(self):
  61. self.window.bind("<Key>", self.change_direction)
  62.  
  63. def change_direction(self, event):
  64. if event.keysym == "Up" or event.keysym == "w":
  65. self.snake.change_direction("up")
  66. elif event.keysym == "Down" or event.keysym == "s":
  67. self.snake.change_direction("down")
  68. elif event.keysym == "Left" or event.keysym == "a":
  69. self.snake.change_direction("left")
  70. elif event.keysym == "Right" or event.keysym == "d":
  71. self.snake.change_direction("right")
  72.  
  73. def check_collision(self):
  74. head = self.snake.positions[0]
  75. x, y = head
  76.  
  77. if x < 0 or x >= self.width or y < 0 or y >= self.height:
  78. self.game_over = True
  79.  
  80. if head in self.snake.body[1:]:
  81. self.game_over = True
  82.  
  83. if head == self.food:
  84. self.score += 1
  85. self.food = self.create_food()
  86.  
  87. def draw(self):
  88. self.canvas.delete("all")
  89. for pos in self.snake.body:
  90. x, y = pos
  91. self.canvas.create_rectangle(x, y, x + 20, y + 20, fill="black")
  92. self.canvas.create_text(self.width // 2, 30, text=f"Score: {self.score}", font=("Arial", 20), fill="black")
  93. self.canvas.create_rectangle(self.food[0], self.food[1], self.food[0] + 20, self.food[1] + 20, fill="green")
  94.  
  95. def play(self):
  96. while not self.game_over:
  97. self.window.update()
  98. self.snake.move()
  99. self.check_collision()
  100. self.draw()
  101. self.window.after(100)
  102.  
  103. self.window.destroy()
  104.  
  105. if __name__ == "__main__":
  106. game = Game()
  107. game.play()
  108.  
  109. This code creates a simple snake game with a GUI using the tkinter library. The snake is controlled using the arrow keys or the WASD keys. The game ends when the snake collides with the border or its own body. The score increases every time the snake eats the food. The food is randomly placed on the screen. The game can be played by running the code and pressing the arrow keys or the WASD keys to move the snake.<|im_end|>
  110.  
Add Comment
Please, Sign In to add comment