Snake game
This commit is contained in:
parent
e12b508072
commit
926cbda034
1 changed files with 112 additions and 0 deletions
112
snake.py
Normal file
112
snake.py
Normal file
|
@ -0,0 +1,112 @@
|
|||
# Generated by AI
|
||||
import pygame
|
||||
import random
|
||||
import sys
|
||||
|
||||
# Initialize pygame
|
||||
pygame.init()
|
||||
|
||||
# Constants for the game
|
||||
SCREEN_WIDTH, SCREEN_HEIGHT = 640, 480
|
||||
CELL_SIZE = 20
|
||||
GRID_WIDTH, GRID_HEIGHT = SCREEN_WIDTH // CELL_SIZE, SCREEN_HEIGHT // CELL_SIZE
|
||||
FPS = 10 # Game speed
|
||||
|
||||
# Colors
|
||||
BLACK = (0, 0, 0)
|
||||
WHITE = (255, 255, 255)
|
||||
GREEN = (0, 255, 0)
|
||||
RED = (255, 0, 0)
|
||||
|
||||
# Directions
|
||||
UP = (0, -1)
|
||||
DOWN = (0, 1)
|
||||
LEFT = (-1, 0)
|
||||
RIGHT = (1, 0)
|
||||
|
||||
class SnakeGame:
|
||||
def __init__(self):
|
||||
self.screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
|
||||
pygame.display.set_caption('Snake Game')
|
||||
self.clock = pygame.time.Clock()
|
||||
|
||||
self.snake = [(GRID_WIDTH // 2, GRID_HEIGHT // 2)]
|
||||
self.snake_direction = RIGHT
|
||||
self.grow = False # Flag for growing the snake
|
||||
self.place_food()
|
||||
|
||||
def place_food(self):
|
||||
"""Place the food at a random position on the grid."""
|
||||
self.food = (random.randint(0, GRID_WIDTH - 1), random.randint(0, GRID_HEIGHT - 1))
|
||||
while self.food in self.snake: # Ensure food is not placed on the snake
|
||||
self.food = (random.randint(0, GRID_WIDTH - 1), random.randint(0, GRID_HEIGHT - 1))
|
||||
|
||||
def move_snake(self):
|
||||
"""Move the snake one cell in the current direction."""
|
||||
head_x, head_y = self.snake[0]
|
||||
new_head = (head_x + self.snake_direction[0], head_y + self.snake_direction[1])
|
||||
if self.grow:
|
||||
self.snake.insert(0, new_head) # Grow the snake
|
||||
else:
|
||||
self.snake.insert(0, new_head) # Move the head
|
||||
self.snake.pop() # Remove the tail
|
||||
self.grow = False
|
||||
|
||||
# Detect collisions with the walls
|
||||
if not (0 <= new_head[0] < GRID_WIDTH) or not (0 <= new_head[1] < GRID_HEIGHT):
|
||||
self.game_over()
|
||||
|
||||
# Detect collisions with the snake itself
|
||||
if new_head in self.snake[1:]:
|
||||
self.game_over()
|
||||
|
||||
def check_food_collision(self):
|
||||
"""Check if the snake has eaten the food."""
|
||||
if self.snake[0] == self.food:
|
||||
self.place_food()
|
||||
self.grow = True
|
||||
|
||||
def draw(self):
|
||||
"""Draw the game state to the screen."""
|
||||
self.screen.fill(BLACK)
|
||||
for segment in self.snake:
|
||||
pygame.draw.rect(self.screen, GREEN, (segment[0] * CELL_SIZE, segment[1] * CELL_SIZE, CELL_SIZE, CELL_SIZE))
|
||||
pygame.draw.rect(self.screen, RED, (self.food[0] * CELL_SIZE, self.food[1] * CELL_SIZE, CELL_SIZE, CELL_SIZE))
|
||||
pygame.display.flip()
|
||||
|
||||
def game_over(self):
|
||||
"""Handle the game over scenario."""
|
||||
self.screen.fill(BLACK)
|
||||
font = pygame.font.SysFont(None, 72)
|
||||
text = font.render("Game Over", True, WHITE)
|
||||
text_rect = text.get_rect(center=(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2))
|
||||
self.screen.blit(text, text_rect)
|
||||
pygame.display.flip()
|
||||
pygame.time.wait(2000)
|
||||
pygame.quit()
|
||||
sys.exit()
|
||||
|
||||
def run(self):
|
||||
"""Main game loop."""
|
||||
while True:
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
self.game_over()
|
||||
elif event.type == pygame.KEYDOWN:
|
||||
if event.key == pygame.K_UP and self.snake_direction != DOWN:
|
||||
self.snake_direction = UP
|
||||
elif event.key == pygame.K_DOWN and self.snake_direction != UP:
|
||||
self.snake_direction = DOWN
|
||||
elif event.key == pygame.K_LEFT and self.snake_direction != RIGHT:
|
||||
self.snake_direction = LEFT
|
||||
elif event.key == pygame.K_RIGHT and self.snake_direction != LEFT:
|
||||
self.snake_direction = RIGHT
|
||||
|
||||
self.move_snake()
|
||||
self.check_food_collision()
|
||||
self.draw()
|
||||
self.clock.tick(FPS)
|
||||
|
||||
if __name__ == '__main__':
|
||||
game = SnakeGame()
|
||||
game.run()
|
Loading…
Add table
Reference in a new issue