Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
42 changes: 42 additions & 0 deletions src/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""
Main package

This package provides a simple implementation of the classic Snake game using the Pygame library.
It consists of three modules:

1. `__init__.py`: Defines the main function `snake_game()` to run the game.
2. `config_apple.py`: Contains the Apple class, which represents an apple object in the game.
3. `config_snake.py`: Contains the Snake class, which represents the snake entity in the Snake game.
"""

# Author
__author__ = "Eduardo Benatti"

# Version
__version__ = "1.1.0"

# Import
import pygame

""" Game configuration """

# Initialize Pygame
pygame.init()

# Screen dimensions
SW, SH = 650, 650

# Block size for snake and other game elements
BLOCK_SIZE = 50

# Font used for the game
FONT = pygame.font.Font(None, BLOCK_SIZE * 2)

# Create the game screen
screen = pygame.display.set_mode((SW, SH))

# Create a clock object to control frame rate
clock = pygame.time.Clock()

# Set the window caption
pygame.display.set_caption("Snake!")
16 changes: 16 additions & 0 deletions src/apple_config/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
Configuration module for the Apple class.

This module provides the Apple class, which represents an
apple object in the game. The apple is an item that the
player can collect for points.
"""

# Internal import
from .config_apple import Apple

# Author
__author__ = "Eduardo Benatti <https://github.com/eduardo2580/SnakeInPython>"

# Version
__version__ = "1.1.0"
50 changes: 50 additions & 0 deletions src/apple_config/config_apple.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"""
This module provides the Apple class, which represents an
apple object in the game. The apple is an item that the
player can collect for points.

Author: Eduardo Benatti
"""

import random
from src import (BLOCK_SIZE, SH, SW, pygame)

class Apple:
"""
A class to represent an apple in the game.

Attributes:
- rect (pygame.Rect): A rectangle representing the apple's position and size on the screen.

Methods:
- __init__(): Initializes the apple attributes and randomly places it on the screen.
- randomize(): Randomly places the apple on the screen.
- draw(screen): Draws the apple on the given screen.
"""

def __init__(self):
"""
Initialize an Apple object.

Sets the initial position of the apple to (0, 0) and then randomizes its position.
"""
self.rect = pygame.Rect(0, 0, BLOCK_SIZE, BLOCK_SIZE)
self.randomize()

def randomize(self) -> None:
"""
Randomize the position of the apple.

Randomly sets the x and y coordinates of the apple within the boundaries of the screen.
"""
self.rect.x = random.randint(0, SW - BLOCK_SIZE) // BLOCK_SIZE * BLOCK_SIZE
self.rect.y = random.randint(0, SH - BLOCK_SIZE) // BLOCK_SIZE * BLOCK_SIZE

def draw(self, screen) -> None:
"""
Draw the apple on the screen.

Args:
- screen: The surface on which the apple will be drawn.
"""
pygame.draw.rect(screen, "red", self.rect)
125 changes: 0 additions & 125 deletions src/snake.py

This file was deleted.

16 changes: 16 additions & 0 deletions src/snake_config/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
Configuration module for the Snake class.

This module provides the Snake class, which represents the snake
entity in the Snake game. The Snake class handles the snake's
movement, collision detection, and drawing on the screen.
"""

# Internal import
from .snake_config import Snake

# Author
__author__ = "Eduardo Benatti <https://github.com/eduardo2580/SnakeInPython>"

# Version
__version__ = "1.1.0"
108 changes: 108 additions & 0 deletions src/snake_config/snake_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
"""
Module containing the Snake class for the Snake game.

This module provides the Snake class, which represents the
snake entity in the Snake game.
The Snake class handles the snake's movement, collision
detection, and drawing on the screen.

Author: Eduardo Benatti
"""

# Import statements and code for the Snake class go here


import apple_config # Import the apple_config module
from src import (BLOCK_SIZE, SH, SW, pygame) # Import constants and libraries from the src module


class Snake:
"""
Class to represent the snake in the Snake game.

Attributes:
x (int): x position of the snake's head.
y (int): y position of the snake's head.
xdir (int): Direction of the snake on the x-axis (-1 for left, 1 for right).
ydir (int): Direction of the snake on the y-axis (-1 for up, 1 for down).
head (pygame.Rect): Rectangle representing the snake's head.
body (list): List of rectangles representing the snake's body.
dead (bool): Indicates whether the snake is dead (True) or alive (False).
"""

def __init__(self):
"""
Initializes the snake's attributes.

Initializes the snake's position, direction, head, and body.
"""
self.x, self.y = BLOCK_SIZE, BLOCK_SIZE
self.xdir = 1
self.ydir = 0
self.head = pygame.Rect(self.x, self.y, BLOCK_SIZE, BLOCK_SIZE)
self.body = [pygame.Rect(self.x - BLOCK_SIZE, self.y, BLOCK_SIZE, BLOCK_SIZE)]
self.dead = False

def update(self, apple: apple_config) -> None:
"""
Updates the snake's position and checks for collisions.

Args:
apple (apple_config): Apple object to check for collisions.

Returns:
None
"""
# Check for collisions with the snake's body or walls
for square in self.body:
if self.head.colliderect(square):
self.dead = True
if not (0 <= self.head.x < SW and 0 <= self.head.y < SH):
self.dead = True

# Handle snake death
if self.dead:
self.reset()

# Update the snake's body
self.body.insert(0, self.head.copy())
if self.head.colliderect(apple.rect):
apple.randomize()
else:
self.body.pop()

# Move the snake's head
self.head.x += self.xdir * BLOCK_SIZE
self.head.y += self.ydir * BLOCK_SIZE

def reset(self) -> None:
"""
Resets the snake.

Reinitializes the snake's attributes to restart the game.

Returns:
None
"""
self.x, self.y = BLOCK_SIZE, BLOCK_SIZE
self.head = pygame.Rect(self.x, self.y, BLOCK_SIZE, BLOCK_SIZE)
self.body = [pygame.Rect(self.x - BLOCK_SIZE, self.y, BLOCK_SIZE, BLOCK_SIZE)]
self.xdir = 1
self.ydir = 0
self.dead = False

def draw(self, screen) -> None:
"""
Draws the snake on the screen.

Args:
screen: Screen where the snake will be drawn.

Returns:
None
"""
# Draw the snake's head
pygame.draw.rect(screen, "green", self.head)
# Draw the snake's body
for square in self.body:
pygame.draw.rect(screen, "green", square)
Loading