Skip to content

Commit eed645c

Browse files
Merge pull request #2304 from Shikhar9425/patch-2
Snake_pygame.py
2 parents cdb5127 + da36a4f commit eed645c

File tree

2 files changed

+137
-0
lines changed

2 files changed

+137
-0
lines changed

Pygame_Snake/README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
Package/Script Name: Snake Game in Pygame
2+
3+
Short description of package/script:
4+
This package provides a simple implementation of the classic "Snake" game using Python's Pygame library. It allows players to control a snake and eat food to grow longer while avoiding collisions with the screen boundaries and its own body.
5+
6+
Functionalities/Scripts:
7+
8+
snake_game.py: Implements the Snake game with basic features such as snake movement, food spawning, collision detection, and scoring.
9+
Setup instructions:
10+
11+
Ensure you have Python installed on your system (Python 3.x is recommended).
12+
Install the Pygame library by running the following command:
13+
Copy code
14+
pip install pygame
15+
Download the snake_game.py script.
16+
Explain how to set up and run your package/script in the user's system:
17+
18+
Open a terminal or command prompt.
19+
Navigate to the directory where you saved snake_game.py.
20+
Run the script using the following command:
21+
Copy code
22+
python snake_game.py
23+
Detailed explanation of script:
24+
25+
The script initializes the Pygame module and sets up the game window, screen dimensions, colors, and clock for frame rate control.
26+
It sets the initial position, speed, and body of the snake and randomly spawns food on the screen.
27+
The game loop continuously checks for events such as keyboard inputs, food collision, boundaries, and self-collision.
28+
When the snake eats the food, it grows longer, and a new food spawns at a random location.
29+
If the snake collides with the screen boundaries or its own body, the game ends.
30+
The screen is updated with the snake and food positions in each iteration, creating the illusion of movement.
31+
Output:
32+
[Here, you can provide images, gifs, or videos of the game in action, demonstrating how the snake moves, eats food, and the game over scenario.]
33+
34+
Author(s):
35+
Shikhar9425

Pygame_Snake/snake_pygame.py

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import pygame
2+
import random
3+
4+
# Initialize pygame
5+
pygame.init()
6+
7+
# Set the screen dimensions
8+
screen_width = 640
9+
screen_height = 480
10+
11+
# Set the colors
12+
black = pygame.Color(0, 0, 0)
13+
white = pygame.Color(255, 255, 255)
14+
red = pygame.Color(255, 0, 0)
15+
16+
# Create the game window
17+
screen = pygame.display.set_mode((screen_width, screen_height))
18+
pygame.display.set_caption("Snake Game")
19+
20+
# Set the game clock
21+
clock = pygame.time.Clock()
22+
23+
# Set the snake's initial position and speed
24+
snake_position = [100, 50]
25+
snake_body = [[100, 50], [90, 50], [80, 50]]
26+
snake_speed = 10
27+
28+
# Set the initial food position
29+
food_position = [random.randrange(1, screen_width // 10) * 10,
30+
random.randrange(1, screen_height // 10) * 10]
31+
food_spawn = True
32+
33+
# Set the initial game score
34+
score = 0
35+
36+
# Set the game over flag
37+
game_over = False
38+
39+
# Game loop
40+
while not game_over:
41+
# Handle events
42+
for event in pygame.event.get():
43+
if event.type == pygame.QUIT:
44+
game_over = True
45+
46+
# Handle snake movement
47+
keys = pygame.key.get_pressed()
48+
for key in keys:
49+
if keys[pygame.K_LEFT]:
50+
snake_position[0] -= snake_speed
51+
if keys[pygame.K_RIGHT]:
52+
snake_position[0] += snake_speed
53+
if keys[pygame.K_UP]:
54+
snake_position[1] -= snake_speed
55+
if keys[pygame.K_DOWN]:
56+
snake_position[1] += snake_speed
57+
58+
# Check for collision with the food
59+
if pygame.Rect(snake_position[0], snake_position[1], 10, 10).colliderect(
60+
pygame.Rect(food_position[0], food_position[1], 10, 10)):
61+
score += 1
62+
food_spawn = False
63+
64+
# Spawn new food if the previous one was eaten
65+
if not food_spawn:
66+
food_position = [random.randrange(1, screen_width // 10) * 10,
67+
random.randrange(1, screen_height // 10) * 10]
68+
food_spawn = True
69+
70+
# Update the snake's body
71+
snake_body.insert(0, list(snake_position))
72+
if len(snake_body) > score + 1:
73+
snake_body.pop()
74+
75+
# Check for collision with the snake's own body
76+
if snake_position in snake_body[1:]:
77+
game_over = True
78+
79+
# Check for collision with the screen boundaries
80+
if snake_position[0] < 0 or snake_position[0] >= screen_width or \
81+
snake_position[1] < 0 or snake_position[1] >= screen_height:
82+
game_over = True
83+
84+
# Set the screen background
85+
screen.fill(black)
86+
87+
# Draw the snake
88+
for pos in snake_body:
89+
pygame.draw.rect(screen, white, pygame.Rect(pos[0], pos[1], 10, 10))
90+
91+
# Draw food
92+
pygame.draw.rect(screen, red, pygame.Rect(
93+
food_position[0], food_position[1], 10, 10))
94+
95+
# Update the screen
96+
pygame.display.flip()
97+
98+
# Set the game speed
99+
clock.tick(20)
100+
101+
# Quit the game
102+
pygame.quit()

0 commit comments

Comments
 (0)