Initial commit: Basic project structure and snake game implementation
This commit is contained in:
commit
90087b6cfe
52 files changed
+10474
No files matched your search
+77
@@ -0,0 +1,77 @@
|
||||
import pygame
|
||||
import sys
|
||||
from enum import Enum, auto
|
||||
|
||||
class GameState(Enum):
|
||||
MENU = auto()
|
||||
PLAYING = auto()
|
||||
GAME_OVER = auto()
|
||||
PAUSED = auto()
|
||||
|
||||
class Game:
|
||||
def __init__(self):
|
||||
# Initialize display
|
||||
self.width = 800
|
||||
self.height = 600
|
||||
self.screen = pygame.display.set_mode((self.width, self.height))
|
||||
pygame.display.set_caption("AI Snake Game")
|
||||
|
||||
# Initialize clock
|
||||
self.clock = pygame.time.Clock()
|
||||
self.fps = 60
|
||||
|
||||
# Game state
|
||||
self.state = GameState.MENU
|
||||
self.running = True
|
||||
|
||||
def handle_events(self):
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
self.running = False
|
||||
elif event.type == pygame.KEYDOWN:
|
||||
if event.key == pygame.K_ESCAPE:
|
||||
if self.state == GameState.PLAYING:
|
||||
self.state = GameState.PAUSED
|
||||
elif self.state == GameState.PAUSED:
|
||||
self.state = GameState.PLAYING
|
||||
|
||||
def update(self):
|
||||
if self.state == GameState.PLAYING:
|
||||
# Update game logic here
|
||||
pass
|
||||
elif self.state == GameState.MENU:
|
||||
# Update menu logic here
|
||||
pass
|
||||
elif self.state == GameState.GAME_OVER:
|
||||
# Update game over logic here
|
||||
pass
|
||||
elif self.state == GameState.PAUSED:
|
||||
# Update pause menu logic here
|
||||
pass
|
||||
|
||||
def render(self):
|
||||
# Clear screen
|
||||
self.screen.fill((0, 0, 0)) # Black background
|
||||
|
||||
if self.state == GameState.PLAYING:
|
||||
# Render game here
|
||||
pass
|
||||
elif self.state == GameState.MENU:
|
||||
# Render menu here
|
||||
pass
|
||||
elif self.state == GameState.GAME_OVER:
|
||||
# Render game over screen here
|
||||
pass
|
||||
elif self.state == GameState.PAUSED:
|
||||
# Render pause menu here
|
||||
pass
|
||||
|
||||
# Update display
|
||||
pygame.display.flip()
|
||||
|
||||
def run(self):
|
||||
while self.running:
|
||||
self.handle_events()
|
||||
self.update()
|
||||
self.render()
|
||||
self.clock.tick(self.fps)
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
import pygame
|
||||
import sys
|
||||
from game import Game
|
||||
|
||||
def main():
|
||||
# Initialize Pygame
|
||||
pygame.init()
|
||||
|
||||
# Create game instance
|
||||
game = Game()
|
||||
|
||||
try:
|
||||
# Run the game
|
||||
game.run()
|
||||
except Exception as e:
|
||||
print(f"Error: {e}")
|
||||
finally:
|
||||
# Clean up
|
||||
pygame.quit()
|
||||
sys.exit()
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,92 @@
|
||||
import pygame
|
||||
from enum import Enum, auto
|
||||
from typing import List, Tuple
|
||||
|
||||
class Direction(Enum):
|
||||
UP = auto()
|
||||
DOWN = auto()
|
||||
LEFT = auto()
|
||||
RIGHT = auto()
|
||||
|
||||
class Snake:
|
||||
def __init__(self, start_pos: Tuple[int, int], block_size: int):
|
||||
self.block_size = block_size
|
||||
self.direction = Direction.RIGHT
|
||||
self.body = [start_pos] # Head is at index 0
|
||||
self.growing = False
|
||||
|
||||
# Movement cooldown
|
||||
self.move_cooldown = 100 # milliseconds
|
||||
self.last_move_time = 0
|
||||
|
||||
def move(self, current_time: int) -> bool:
|
||||
"""Move the snake if enough time has passed. Returns True if moved."""
|
||||
if current_time - self.last_move_time < self.move_cooldown:
|
||||
return False
|
||||
|
||||
# Update position
|
||||
head = self.body[0]
|
||||
new_head = self._get_new_head_position(head)
|
||||
|
||||
# Insert new head
|
||||
self.body.insert(0, new_head)
|
||||
|
||||
# Remove tail if not growing
|
||||
if not self.growing:
|
||||
self.body.pop()
|
||||
else:
|
||||
self.growing = False
|
||||
|
||||
self.last_move_time = current_time
|
||||
return True
|
||||
|
||||
def _get_new_head_position(self, head: Tuple[int, int]) -> Tuple[int, int]:
|
||||
x, y = head
|
||||
if self.direction == Direction.UP:
|
||||
return (x, y - self.block_size)
|
||||
elif self.direction == Direction.DOWN:
|
||||
return (x, y + self.block_size)
|
||||
elif self.direction == Direction.LEFT:
|
||||
return (x - self.block_size, y)
|
||||
else: # Direction.RIGHT
|
||||
return (x + self.block_size, y)
|
||||
|
||||
def grow(self):
|
||||
"""Mark the snake to grow on next move"""
|
||||
self.growing = True
|
||||
|
||||
def check_collision(self, width: int, height: int) -> bool:
|
||||
"""Check if snake has collided with walls or itself"""
|
||||
head = self.body[0]
|
||||
|
||||
# Check wall collision
|
||||
if (head[0] < 0 or head[0] >= width or
|
||||
head[1] < 0 or head[1] >= height):
|
||||
return True
|
||||
|
||||
# Check self collision (skip head)
|
||||
if head in self.body[1:]:
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
def change_direction(self, new_direction: Direction):
|
||||
"""Change direction if it's not opposite to current direction"""
|
||||
opposites = {
|
||||
Direction.UP: Direction.DOWN,
|
||||
Direction.DOWN: Direction.UP,
|
||||
Direction.LEFT: Direction.RIGHT,
|
||||
Direction.RIGHT: Direction.LEFT
|
||||
}
|
||||
|
||||
if opposites[new_direction] != self.direction:
|
||||
self.direction = new_direction
|
||||
|
||||
def draw(self, screen: pygame.Surface):
|
||||
"""Draw the snake on the screen"""
|
||||
for segment in self.body:
|
||||
pygame.draw.rect(
|
||||
screen,
|
||||
(0, 255, 0), # Green color
|
||||
pygame.Rect(segment[0], segment[1], self.block_size, self.block_size)
|
||||
)
|
||||
Reference in new issue
Block a user