create the code for snake game
- User
create the code for snake game
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 the code for snake game
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 Pygame:
import pygame
import random
import sys
from pygame.math import Vector2
# Initialize pygame
pygame.init()
# Constants
CELL_SIZE = 30
GRID_WIDTH = 20
GRID_HEIGHT = 15
SCREEN_WIDTH = CELL_SIZE * GRID_WIDTH
SCREEN_HEIGHT = CELL_SIZE * GRID_HEIGHT
FPS = 10
# Colors
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
WHITE = (255, 255, 255)
class Snake:
def __init__(self):
self.body = [Vector2(5, 10), Vector2(4, 10), Vector2(3, 10)]
self.direction = Vector2(1, 0)
self.new_block = False
def draw(self, screen):
for block in self.body:
rect = pygame.Rect(block.x * CELL_SIZE, block.y * CELL_SIZE, CELL_SIZE, CELL_SIZE)
pygame.draw.rect(screen, GREEN, rect)
def move(self):
if self.new_block:
body_copy = self.body[:]
body_copy.insert(0, body_copy[0] + self.direction)
self.body = body_copy
self.new_block = False
else:
body_copy = self.body[:-1]
body_copy.insert(0, body_copy[0] + self.direction)
self.body = body_copy
def add_block(self):
self.new_block = True
def check_collision(self):
# Check if snake hits wall
if not 0 <= self.body[0].x < GRID_WIDTH or not 0 <= self.body[0].y < GRID_HEIGHT:
return True
# Check if snake hits itself
for block in self.body[1:]:
if block == self.body[0]:
return True
return False
class Food:
def __init__(self):
self.position = self.randomize_position()
def draw(self, screen):
rect = pygame.Rect(self.position.x * CELL_SIZE, self.position.y * CELL_SIZE, CELL_SIZE, CELL_SIZE)
pygame.draw.rect(screen, RED, rect)
def randomize_position(self):
x = random.randint(0, GRID_WIDTH - 1)
y = random.randint(0, GRID_HEIGHT - 1)
return Vector2(x, y)
class Game:
def __init__(self):
self.snake = Snake()
self.food = Food()
self.score = 0
self.game_over = False
def update(self):
if not self.game_over:
self.snake.move()
self.check_collision()
self.check_food()
def draw(self, screen):
screen.fill(BLACK)
self.snake.draw(screen)
self.food.draw(screen)
self.draw_score(screen)
if self.game_over:
self.draw_game_over(screen)
def check_collision(self):
if self.snake.check_collision():
self.game_over = True
def check_food(self):
if self.snake.body[0] == self.food.position:
self.food.position = self.food.randomize_position()
self.snake.add_block()
self.score += 1
def draw_score(self, screen):
font = pygame.font.SysFont(None, 36)
score_text = font.render(f'Score: {self.score}', True, WHITE)
screen.blit(score_text, (10, 10))
def draw_game_over(self, screen):
font = pygame.font.SysFont(None, 72)
game_over_text = font.render('GAME OVER', True, WHITE)
screen.blit(game_over_text, (SCREEN_WIDTH//2 - game_over_text.get_width()//2,
SCREEN_HEIGHT//2 - game_over_text.get_height()//2))
font = pygame.font.SysFont(None, 36)
restart_text = font.render('Press SPACE to restart', True, WHITE)
screen.blit(restart_text, (SCREEN_WIDTH//2 - restart_text.get_width()//2,
SCREEN_HEIGHT//2 + game_over_text.get_height()))
def reset(self):
self.snake = Snake()
self.food = Food()
self.score = 0
self.game_over = False
def main():
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('Snake Game')
clock = pygame.time.Clock()
game = Game()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP and game.snake.direction.y != 1:
game.snake.direction = Vector2(0, -1)
if event.key == pygame.K_DOWN and game.snake.direction.y != -1:
game.snake.direction = Vector2(0, 1)
if event.key == pygame.K_LEFT and game.snake.direction.x != 1:
game.snake.direction = Vector2(-1, 0)
if event.key == pygame.K_RIGHT and game.snake.direction.x != -1:
game.snake.direction = Vector2(1, 0)
if event.key == pygame.K_SPACE and game.game_over:
game.reset()
game.update()
game.draw(screen)
pygame.display.update()
clock.tick(FPS)
if __name__ == "__main__":
main()
Would you like me to explain or break down the code?