Create Snake with Python
- User
Create Snake with 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.
Create Snake with 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 and the Pygame library.
pip install pygame)import pygame
import sys
import time
import random
# Initialize Pygame
pygame.init()
# Set up some constants
WIDTH = 800
HEIGHT = 600
BLOCK_SIZE = 20
FPS = 10
# Set up some colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
# Set up the display
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Snake Game")
# Set up the font
font = pygame.font.Font(None, 36)
# Set up the clock
clock = pygame.time.Clock()
class SnakeGame:
def __init__(self):
self.snake = [(200, 200), (220, 200), (240, 200)]
self.direction = "RIGHT"
self.apple = self.generate_apple()
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
if (x, y) not in self.snake:
return (x, y)
def draw_snake(self):
for pos in self.snake:
pygame.draw.rect(screen, GREEN, (pos[0], pos[1], BLOCK_SIZE, BLOCK_SIZE))
def draw_apple(self):
pygame.draw.rect(screen, 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.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 main():
game = SnakeGame()
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 game.direction != "DOWN":
game.direction = "UP"
elif event.key == pygame.K_DOWN and game.direction != "UP":
game.direction = "DOWN"
elif event.key == pygame.K_LEFT and game.direction != "RIGHT":
game.direction = "LEFT"
elif event.key == pygame.K_RIGHT and game.direction != "LEFT":
game.direction = "RIGHT"
screen.fill(BLACK)
game.draw_snake()
game.draw_apple()
game.update_snake()
if game.check_collision():
text = font.render("Game Over!", True, WHITE)
screen.blit(text, (WIDTH // 2 - 50, HEIGHT // 2 - 18))
pygame.display.flip()
time.sleep(2)
break
pygame.display.flip()
clock.tick(FPS)
if __name__ == "__main__":
main()
snake.py.pip install pygame in your terminal.python snake.py in your terminal.