write the game snake in python
- User
write the game snake in python
Assistant
Here's a simple implementation of the Snake game in Python using the Pygame library:
import pygame import random import time # Initialize pygame pygame.init() # Colors WHITE = (255, 255, 255) BLACK = (0, 0, 0) RED = (255, 0, 0) GREEN = (0, 255, 0) # Screen dimensions WIDTH, HEIGHT = 600, 400 GRID_SIZE = 20 GRID_WIDTH = WIDTH // GRID_SIZE GRID_HEIGHT = HEIGHT // GRID_SIZE # Game settings SPEED = 10 # Initialize screen screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption('Snake Game') clock = pygame.time.Clock() def draw_grid(): for x in range(0, WIDTH, GRID_SIZE): pygame.draw.line(screen, WHITE, (x, 0), (x, HEIGHT), 1) for y in range(0, HEIGHT, GRID_SIZE): pygame.draw.line(screen, WHITE, (0, y), (WIDTH, y), 1) def game_loop(): # Initial snake position snake = [(GRID_WIDTH // 2, GRID_HEIGHT // 2)] snake_direction = (1, 0) # Moving right initially # Initial food position food = (random.randint(0, GRID_WIDTH - 1), random.randint(0, GRID_HEIGHT - 1)) # Score score = 0 # Game over flag game_over = False while not game_over: for event in pygame.event.get(): if event.type == pygame.QUIT: return elif event.type == pygame.KEYDOWN: if event.key == pygame.K_UP and snake_direction != (0, 1): snake_direction = (0, -1) elif event.key == pygame.K_DOWN and snake_direction != (0, -1): snake_direction = (0, 1) elif event.key == pygame.K_LEFT and snake_direction != (1, 0): snake_direction = (-1, 0) elif event.key == pygame.K_RIGHT and snake_direction != (-1, 0): snake_direction = (1, 0) # Move snake head_x, head_y = snake[0] dir_x, dir_y = snake_direction new_head = ((head_x + dir_x) % GRID_WIDTH, (head_y + dir_y) % GRID_HEIGHT) # Check if snake hits itself if new_head in snake: game_over = True break snake.insert(0, new_head) # Check if snake eats food if new_head == food: # Generate new food while food in snake: food = (random.randint(0, GRID_WIDTH - 1), random.randint(0, GRID_HEIGHT - 1)) score += 1 else: # Remove tail if no food eaten snake.pop() # Clear screen screen.fill(BLACK) # Draw grid (optional) # draw_grid() # Draw food food_rect = pygame.Rect(food[0] * GRID_SIZE, food[1] * GRID_SIZE, GRID_SIZE, GRID_SIZE) pygame.draw.rect(screen, RED, food_rect) # Draw snake for segment in snake: segment_rect = pygame.Rect(segment[0] * GRID_SIZE, segment[1] * GRID_SIZE, GRID_SIZE, GRID_SIZE) pygame.draw.rect(screen, GREEN, segment_rect) # Display score font = pygame.font.SysFont(None, 25) score_text = font.render(f"Score: {score}", True, WHITE) screen.blit(score_text, (5, 5)) pygame.display.update() clock.tick(SPEED) # Game over screen font = pygame.font.SysFont(None, 50) game_over_text = font.render("Game Over!", True, WHITE) screen.blit(game_over_text, (WIDTH//2 - 100, HEIGHT//2 - 30)) score_text = font.render(f"Final Score: {score}", True, WHITE) screen.blit(score_text, (WIDTH//2 - 100, HEIGHT//2 + 20)) pygame.display.update() time.sleep(3) # Start the game game_loop() # Quit pygame pygame.quit()Would you like me to explain or break down the code?
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.