Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Can you give me python code for a functional game of snake with a gui?
- Sure, here is a simple implementation of the snake game using the tkinter library in Python:
- import tkinter as tk
- import random
- class Snake:
- def __init__(self):
- self.direction = "down"
- self.positions = [(0, 0), (0, -20), (0, -40)]
- self.body = self.positions[:]
- def move(self):
- head = self.positions[0]
- x, y = head
- if self.direction == "up":
- y -= 20
- elif self.direction == "down":
- y += 20
- elif self.direction == "left":
- x -= 20
- elif self.direction == "right":
- x += 20
- self.positions.insert(0, (x, y))
- self.positions.pop()
- def change_direction(self, new_direction):
- if new_direction == "up" and self.direction != "down":
- self.direction = "up"
- elif new_direction == "down" and self.direction != "up":
- self.direction = "down"
- elif new_direction == "left" and self.direction != "right":
- self.direction = "left"
- elif new_direction == "right" and self.direction != "left":
- self.direction = "right"
- class Game:
- def __init__(self):
- self.width = 640
- self.height = 480
- self.window = tk.Tk()
- self.window.title("Snake Game")
- self.canvas = tk.Canvas(self.window, width=self.width, height=self.height, bg="white")
- self.canvas.pack()
- self.snake = Snake()
- self.food = self.create_food()
- self.score = 0
- self.bind_keys()
- self.game_over = False
- def create_food(self):
- x = random.randint(0, self.width - 20) // 20 * 20
- y = random.randint(0, self.height - 20) // 20 * 20
- self.canvas.create_rectangle(x, y, x + 20, y + 20, fill="green")
- return (x, y)
- def bind_keys(self):
- self.window.bind("<Key>", self.change_direction)
- def change_direction(self, event):
- if event.keysym == "Up" or event.keysym == "w":
- self.snake.change_direction("up")
- elif event.keysym == "Down" or event.keysym == "s":
- self.snake.change_direction("down")
- elif event.keysym == "Left" or event.keysym == "a":
- self.snake.change_direction("left")
- elif event.keysym == "Right" or event.keysym == "d":
- self.snake.change_direction("right")
- def check_collision(self):
- head = self.snake.positions[0]
- x, y = head
- if x < 0 or x >= self.width or y < 0 or y >= self.height:
- self.game_over = True
- if head in self.snake.body[1:]:
- self.game_over = True
- if head == self.food:
- self.score += 1
- self.food = self.create_food()
- def draw(self):
- self.canvas.delete("all")
- for pos in self.snake.body:
- x, y = pos
- self.canvas.create_rectangle(x, y, x + 20, y + 20, fill="black")
- self.canvas.create_text(self.width // 2, 30, text=f"Score: {self.score}", font=("Arial", 20), fill="black")
- self.canvas.create_rectangle(self.food[0], self.food[1], self.food[0] + 20, self.food[1] + 20, fill="green")
- def play(self):
- while not self.game_over:
- self.window.update()
- self.snake.move()
- self.check_collision()
- self.draw()
- self.window.after(100)
- self.window.destroy()
- if __name__ == "__main__":
- game = Game()
- game.play()
- 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|>
Add Comment
Please, Sign In to add comment