Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Commenting the code #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
44 changes: 29 additions & 15 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,57 +1,69 @@
# Import necessary libraries for the game.
import pygame
import random

# Initialize Pygame and set up the game window.
pygame.init()
square_width = 750
pixel_width = 50
screen = pygame.display.set_mode([square_width] * 2)
clock = pygame.time.Clock()
running = True
square_width = 750 # Width of the game window.
pixel_width = 50 # Size of the pixels (snake and target blocks).
screen = pygame.display.set_mode([square_width] * 2) # Create the game window.
clock = pygame.time.Clock() # Clock to control the refresh rate.
running = True # Control variable for the main game loop.


# Function to generate random starting positions within the game area.
def generate_starting_position():
position_range = (pixel_width // 2, square_width - pixel_width // 2, pixel_width)
return [random.randrange(*position_range), random.randrange(*position_range)]


# Function to reset the game when the snake dies or is restarted.
def reset():
target.center = generate_starting_position()
snake_pixel.center = generate_starting_position()
return snake_pixel.copy()


# Function to check if the snake has gone out of bounds.
def is_out_of_bounds():
return snake_pixel.bottom > square_width or snake_pixel.top < 0 \
or snake_pixel.left < 0 or snake_pixel.right > square_width


# Initialize the snake and target with their positions and sizes.
snake_pixel = pygame.rect.Rect([0, 0, pixel_width - 2, pixel_width - 2])
snake_pixel.center = generate_starting_position()
snake = [snake_pixel.copy()]
snake_direction = (0, 0)
snake_length = 1
snake_direction = (0, 0) # Initial direction of the snake (stationary).
snake_length = 1 # Initial length of the snake.

target = pygame.rect.Rect([0, 0, pixel_width - 2, pixel_width - 2])
target.center = generate_starting_position()

# Main game loop.
while running:
# Process events (e.g., closing the window).
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False

screen.fill("black")
screen.fill("black") # Clear the screen.

# Check and handle if the snake goes out of bounds.
if is_out_of_bounds():
# Reset the snake and target upon colliding with the edge.
snake_length = 1
target.center = generate_starting_position()
snake_pixel.center = generate_starting_position()
snake = [snake_pixel.copy()]

# Check if the snake has reached the target.
if snake_pixel.center == target.center:
target.center = generate_starting_position()
snake_length += 1
target.center = generate_starting_position() # Move the target.
snake_length += 1 # Increase the length of the snake.
snake.append(snake_pixel.copy())

# Handle keyboard input to control the direction of the snake.
keys = pygame.key.get_pressed()
if keys[pygame.K_w]:
snake_direction = (0, - pixel_width)
Expand All @@ -62,17 +74,19 @@ def is_out_of_bounds():
if keys[pygame.K_d]:
snake_direction = (pixel_width, 0)

# Draw the snake and target on the screen.
for snake_part in snake:
pygame.draw.rect(screen, "green", snake_part)

pygame.draw.rect(screen, "red", target)

# Move the snake in the indicated direction.
snake_pixel.move_ip(snake_direction)
snake.append(snake_pixel.copy())
snake = snake[-snake_length:]
snake.append(snake_pixel.copy()) # Add a new segment to the end of the snake.
snake = snake[-snake_length:] # Maintain the length of the snake.

pygame.display.flip()
pygame.display.flip() # Update the screen.

clock.tick(10)
clock.tick(10) # Control the refresh rate at 10 FPS.

pygame.quit()
pygame.quit() # Close Pygame when the main loop ends.