Skip to content

Commit 6785fb7

Browse files
committed
added snake game
1 parent df84125 commit 6785fb7

File tree

3 files changed

+199
-0
lines changed

3 files changed

+199
-0
lines changed

GAMES/snakeGame/Readme.md

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Snake Game
2+
3+
A classic Snake Game implemented in Python using the Pygame library.
4+
5+
![Gameplay](game.png)
6+
7+
## Table of Contents
8+
9+
- [Description](#description)
10+
- [Features](#features)
11+
- [Installation](#installation)
12+
- [How to Play](#how-to-play)
13+
- [Controls](#controls)
14+
- [Contributing](#contributing)
15+
16+
## Description
17+
18+
This is a simple implementation of the classic Snake Game using Python and Pygame. The game starts with a single snake that you control using arrow keys or WASD keys. Your goal is to eat the brown squares (food) to grow longer while avoiding collisions with the game boundaries and the snake's own body. The game ends if you run into a wall or collide with yourself.
19+
20+
## Features
21+
22+
- Classic Snake Game experience.
23+
- Simple and intuitive controls.
24+
- Score tracking to see how well you've done.
25+
- Game over screen with your final score.
26+
27+
## Installation
28+
29+
1. Clone the repository:
30+
31+
```
32+
git clone https://github.com/sahilrw/snake-game.git
33+
```
34+
35+
2. Navigate to the project directory:
36+
37+
```
38+
cd snake-game
39+
```
40+
41+
3. Install the required dependencies:
42+
43+
```
44+
pip install pygame
45+
```
46+
47+
4. Run the game:
48+
49+
```
50+
python snakeGame.py
51+
```
52+
53+
## How to Play
54+
55+
- Use the arrow keys (Up, Down, Left, Right) or WASD keys (W, A, S, D) to control the snake's direction.
56+
- Eat the brown squares (food) to grow longer.
57+
- Avoid running into the game boundaries or colliding with the snake's own body.
58+
- Try to achieve the highest score possible before the game ends.
59+
60+
## Controls
61+
62+
- Arrow Keys (Up, Down, Left, Right) or
63+
- WASD Keys (W, A, S, D)
64+
65+
## Contributing
66+
67+
Contributions are welcome! If you find any issues or have suggestions for improvement, please open an issue or create a pull request.

GAMES/snakeGame/game.png

18.5 KB
Loading

GAMES/snakeGame/snakeGame.py

+132
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
# Snake Game!
2+
3+
# Game imports
4+
import pygame, sys, random, time
5+
6+
check_errors = pygame.init()
7+
if check_errors[1] > 0:
8+
print("(!) Had {0} initializing errors, exiting...".format(check_errors[1]))
9+
sys.exit(-1)
10+
else:
11+
print("(+) PyGame successfully initialized")
12+
13+
14+
# Play Surface
15+
playSurface = pygame.display.set_mode((720, 700))
16+
pygame.display.set_caption("Snake Game!")
17+
# time.sleep(5)
18+
19+
# Colors
20+
red = pygame.Color(255, 0, 0) # gameover
21+
green = pygame.Color(0, 255, 0) # snake
22+
black = pygame.Color(0, 0, 0) # score
23+
white = pygame.Color(255, 255, 255) # background
24+
brown = pygame.Color(165, 42, 42) # food
25+
26+
# FPS controller
27+
fpsController = pygame.time.Clock()
28+
29+
# Position of the snake
30+
snakePos = [100, 50]
31+
snakeBody = [[100, 50], [90, 50], [80, 50]]
32+
33+
foodPos = [random.randrange(1, 72) * 10, random.randrange(1, 70) * 10]
34+
foodSpawn = True
35+
36+
direction = "RIGHT"
37+
changeto = direction
38+
39+
score = 0
40+
41+
42+
# Game over function
43+
def gameOver():
44+
myFont = pygame.font.SysFont("monaco", 72)
45+
GOsurf = myFont.render("Game over!", False, red)
46+
GOrect = GOsurf.get_rect()
47+
GOrect.midtop = (360, 150)
48+
playSurface.blit(GOsurf, GOrect)
49+
showScore(0)
50+
pygame.display.flip()
51+
time.sleep(5)
52+
pygame.quit() # for exiting pygame
53+
sys.exit() # for exiting from the console
54+
55+
56+
def showScore(choice=1):
57+
scoreFont = pygame.font.SysFont("monaco", 25)
58+
scoreSurf = scoreFont.render("Score : {0}".format(score), True, black)
59+
scoreRect = scoreSurf.get_rect()
60+
if choice == 1:
61+
scoreRect.midtop = (80, 10)
62+
else:
63+
scoreRect.midtop = (360, 120)
64+
playSurface.blit(scoreSurf, scoreRect)
65+
66+
67+
# Main Logic of the game
68+
while True:
69+
for event in pygame.event.get():
70+
if event.type == pygame.QUIT:
71+
pygame.quit()
72+
sys.exit()
73+
elif event.type == pygame.KEYDOWN:
74+
if event.key == pygame.K_RIGHT or event.key == ord("d"):
75+
changeto = "RIGHT"
76+
if event.key == pygame.K_LEFT or event.key == ord("a"):
77+
changeto = "LEFT"
78+
if event.key == pygame.K_UP or event.key == ord("w"):
79+
changeto = "UP"
80+
if event.key == pygame.K_DOWN or event.key == ord("s"):
81+
changeto = "DOWN"
82+
if event.key == pygame.K_ESCAPE:
83+
pygame.event.post(pygame.event.Event(pygame.QUIT))
84+
85+
# validation of direction
86+
if changeto == "RIGHT" and not direction == "LEFT":
87+
direction = "RIGHT"
88+
if changeto == "LEFT" and not direction == "RIGHT":
89+
direction = "LEFT"
90+
if changeto == "UP" and not direction == "DOWN":
91+
direction = "UP"
92+
if changeto == "DOWN" and not direction == "UP":
93+
direction = "DOWN"
94+
95+
# Update snake position [x, y]
96+
if direction == "RIGHT":
97+
snakePos[0] += 10
98+
if direction == "LEFT":
99+
snakePos[0] -= 10
100+
if direction == "UP":
101+
snakePos[1] -= 10
102+
if direction == "DOWN":
103+
snakePos[1] += 10
104+
105+
# Snake body mechanism
106+
snakeBody.insert(0, list(snakePos))
107+
if snakePos[0] == foodPos[0] and snakePos[1] == foodPos[1]:
108+
score += 1
109+
foodSpawn = False
110+
else:
111+
snakeBody.pop()
112+
113+
if foodSpawn == False:
114+
foodPos = [random.randrange(1, 72) * 10, random.randrange(1, 70) * 10]
115+
foodSpawn = True
116+
117+
playSurface.fill(white)
118+
for pos in snakeBody:
119+
pygame.draw.rect(playSurface, green, pygame.Rect(pos[0], pos[1], 10, 10))
120+
121+
pygame.draw.rect(playSurface, brown, pygame.Rect(foodPos[0], foodPos[1], 10, 10))
122+
if snakePos[0] > 710 or snakePos[0] < 0:
123+
gameOver()
124+
if snakePos[1] > 690 or snakePos[1] < 0:
125+
gameOver()
126+
127+
for block in snakeBody[1:]:
128+
if snakePos[0] == block[0] and snakePos[1] == block[1]:
129+
gameOver()
130+
showScore()
131+
pygame.display.flip()
132+
fpsController.tick(18)

0 commit comments

Comments
 (0)