make a me a snake game in python
- User
make a me a snake game in python
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
make a me a snake game in python
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Here's a simple implementation of the classic Snake game using Python's Pygame library.
pip install pygame)import pygame
import sys
import random
import time
# Game Constants
WIDTH, HEIGHT = 800, 600
BLOCK_SIZE = 20
FPS = 10
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
class SnakeGame:
def __init__(self):
pygame.init()
self.display = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('Snake Game')
self.clock = pygame.time.Clock()
self.reset_game()
def reset_game(self):
self.snake = [(200, 200), (220, 200), (240, 200)]
self.direction = 'RIGHT'
self.apple = self.generate_apple()
self.score = 0
def generate_apple(self):
while True:
x = random.randint(0, WIDTH - BLOCK_SIZE) // BLOCK_SIZE * BLOCK_SIZE
y = random.randint(0, HEIGHT - BLOCK_SIZE) // BLOCK_SIZE * BLOCK_SIZE
apple = (x, y)
if apple not in self.snake:
return apple
def draw_snake(self):
for pos in self.snake:
pygame.draw.rect(self.display, GREEN, (pos[0], pos[1], BLOCK_SIZE, BLOCK_SIZE))
def draw_apple(self):
pygame.draw.rect(self.display, RED, (self.apple[0], self.apple[1], BLOCK_SIZE, BLOCK_SIZE))
def update_snake(self):
head = self.snake[-1]
if self.direction == 'RIGHT':
new_head = (head[0] + BLOCK_SIZE, head[1])
elif self.direction == 'LEFT':
new_head = (head[0] - BLOCK_SIZE, head[1])
elif self.direction == 'UP':
new_head = (head[0], head[1] - BLOCK_SIZE)
elif self.direction == 'DOWN':
new_head = (head[0], head[1] + BLOCK_SIZE)
self.snake.append(new_head)
if self.snake[-1] == self.apple:
self.score += 1
self.apple = self.generate_apple()
else:
self.snake.pop(0)
def check_collision(self):
head = self.snake[-1]
if (head[0] < 0 or head[0] >= WIDTH or
head[1] < 0 or head[1] >= HEIGHT or
head in self.snake[:-1]):
return True
return False
def draw_score(self):
font = pygame.font.Font(None, 36)
text = font.render(f'Score: {self.score}', True, WHITE)
self.display.blit(text, (10, 10))
def run(self):
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP and self.direction != 'DOWN':
self.direction = 'UP'
elif event.key == pygame.K_DOWN and self.direction != 'UP':
self.direction = 'DOWN'
elif event.key == pygame.K_LEFT and self.direction != 'RIGHT':
self.direction = 'LEFT'
elif event.key == pygame.K_RIGHT and self.direction != 'LEFT':
self.direction = 'RIGHT'
elif event.key == pygame.K_r:
self.reset_game()
self.update_snake()
if self.check_collision():
print(f'Game Over! Your score: {self.score}')
time.sleep(2)
self.reset_game()
self.display.fill(BLACK)
self.draw_snake()
self.draw_apple()
self.draw_score()
pygame.display.flip()
self.clock.tick(FPS)
if __name__ == '__main__':
game = SnakeGame()
game.run()
snake_game.py.pip install pygame in your terminal/command prompt.python snake_game.py in your terminal/command prompt.↑, ↓, ←, →) to control the snake's direction.R to reset the game after a collision.FPS constant.Enjoy playing the Snake game!