Skip to content

Commit f5310d8

Browse files
authored
Merge pull request #61 from aayushsinha0706/main
Snake game
2 parents efba1e9 + f87cf91 commit f5310d8

10 files changed

+267
-0
lines changed

Snake Game/README.md

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
**SNAKE GAME**
2+
3+
**GOAL**
4+
This is a classic snake game created in python programming language using pygame.
5+
6+
7+
**DESCRIPTION**
8+
Snake is a video game genre where the player maneuvers a line that grows bigger after eating something, typically apples. Know about snake game [here](https://en.wikipedia.org/wiki/Snake_(video_game_genre))
9+
10+
11+
12+
To play this game download snake.py and resources in the same directory.
13+
14+
You also need to have pygame installed in your computer. Learn how to install pygame from [here](https://www.pygame.org/wiki/GettingStarted#Pygame%20Installation)
15+
16+
Open and run the file snake.py without making any modifications to it to ensure that everything is set up correctly. By "open and run" I mean do the following:
17+
* Go to your IDE. From the File menu, choose "Open".
18+
* Find the file snake.py and choose it.
19+
* The template snake.py file should now be open. Run the file and enjoy the game :-D
20+
21+
You can play the game using up, left, right and down arrow keys to move the snake. Aim for apple and get highest score as possible :-D
22+
23+
24+
**WHAT I HAD DONE**
25+
In this game, I implemented three classes known as apples, snake and game, that will start up and carry out an interactive Snake game. The game uses a resources folder for graphics of apple, snake and game window as well as background music and other game sounds.
26+
27+
**DEMONSTRATION**
28+
![Screenshot 2022-12-07 at 3.26.27 PM](https://github.com/aayushsinha0706/Play-With-Python/blob/main/Snake%20Game/screenshots/Screenshot%202022-12-07%20at%203.26.27%20PM.png)
29+
30+
![Screenshot 2022-12-07 at 3.28.03 PM](https://github.com/aayushsinha0706/Play-With-Python/blob/main/Snake%20Game/screenshots/Screenshot%202022-12-07%20at%203.28.03%20PM.png)
31+
32+
33+
I wish you enjoy the classic snake game. :-D
34+
35+
**AAYUSH SINHA**

Snake Game/resources/apple.jpg

8.29 KB
Loading

Snake Game/resources/background.jpg

585 KB
Loading

Snake Game/resources/bg_music_1.mp3

6.56 MB
Binary file not shown.

Snake Game/resources/block.jpg

1.34 KB
Loading

Snake Game/resources/crash.mp3

121 KB
Binary file not shown.

Snake Game/resources/ding.mp3

158 KB
Binary file not shown.
Loading
Loading

Snake Game/snake.py

+232
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
import pygame
2+
from pygame.locals import *
3+
import time
4+
import random
5+
6+
SIZE = 40
7+
BACKGROUND_COLOR = (110, 110, 5)
8+
9+
class Apple:
10+
11+
'''class method for apple (food) in snake game '''
12+
13+
def __init__(self, parent_screen):
14+
self.parent_screen = parent_screen
15+
self.image = pygame.image.load("resources/apple.jpg").convert()
16+
self.x = 120
17+
self.y = 120
18+
19+
def draw(self):
20+
self.parent_screen.blit(self.image, (self.x, self.y))
21+
pygame.display.flip()
22+
23+
def move(self):
24+
self.x = random.randint(1,24)*SIZE
25+
self.y = random.randint(1,19)*SIZE
26+
27+
class Snake:
28+
29+
'''class method for snake apperance and movement in game '''
30+
31+
def __init__(self, parent_screen):
32+
self.parent_screen = parent_screen
33+
self.image = pygame.image.load("resources/block.jpg").convert()
34+
self.direction = 'down'
35+
36+
self.length = 1
37+
self.x = [40]
38+
self.y = [40]
39+
40+
def move_left(self):
41+
'''helps snake move in left direction'''
42+
self.direction = 'left'
43+
44+
def move_right(self):
45+
'''helps snake move in right direction'''
46+
self.direction = 'right'
47+
48+
def move_up(self):
49+
'''helps snake move in upwards direction'''
50+
self.direction = 'up'
51+
52+
def move_down(self):
53+
'''helps snake move in downwards direction'''
54+
self.direction = 'down'
55+
56+
def walk(self):
57+
58+
'''function helps in updating snake position in game'''
59+
60+
# update body
61+
for i in range(self.length-1,0,-1):
62+
self.x[i] = self.x[i-1]
63+
self.y[i] = self.y[i-1]
64+
65+
# update head
66+
if self.direction == 'left':
67+
self.x[0] -= SIZE
68+
if self.direction == 'right':
69+
self.x[0] += SIZE
70+
if self.direction == 'up':
71+
self.y[0] -= SIZE
72+
if self.direction == 'down':
73+
self.y[0] += SIZE
74+
75+
self.draw()
76+
77+
def draw(self):
78+
for i in range(self.length):
79+
self.parent_screen.blit(self.image, (self.x[i], self.y[i]))
80+
81+
pygame.display.flip()
82+
83+
def increase_length(self):
84+
self.length += 1
85+
self.x.append(-1)
86+
self.y.append(-1)
87+
88+
class Game:
89+
90+
'''class method for game screen appearance and running game'''
91+
92+
def __init__(self):
93+
'''initialises game window'''
94+
pygame.init()
95+
pygame.display.set_caption("SNAKE GAME")
96+
97+
pygame.mixer.init()
98+
self.play_background_music()
99+
100+
self.surface = pygame.display.set_mode((1000, 800))
101+
self.snake = Snake(self.surface)
102+
self.snake.draw()
103+
self.apple = Apple(self.surface)
104+
self.apple.draw()
105+
106+
def play_background_music(self):
107+
'''loads background music in game'''
108+
pygame.mixer.music.load('resources/bg_music_1.mp3')
109+
pygame.mixer.music.play(-1, 0)
110+
111+
def play_sound(self, sound_name):
112+
'''loads sound of snake crashing or snake eating apple'''
113+
if sound_name == "crash":
114+
sound = pygame.mixer.Sound("resources/crash.mp3")
115+
elif sound_name == 'ding':
116+
sound = pygame.mixer.Sound("resources/ding.mp3")
117+
118+
pygame.mixer.Sound.play(sound)
119+
120+
121+
122+
def reset(self):
123+
self.snake = Snake(self.surface)
124+
self.apple = Apple(self.surface)
125+
126+
127+
def is_collision(self, x1, y1, x2, y2):
128+
if x1 >= x2 and x1 < x2 + SIZE:
129+
if y1 >= y2 and y1 < y2 + SIZE:
130+
return True
131+
return False
132+
133+
def render_background(self):
134+
bg = pygame.image.load("resources/background.jpg")
135+
self.surface.blit(bg, (0,0))
136+
137+
def play(self):
138+
self.render_background()
139+
self.snake.walk()
140+
self.apple.draw()
141+
self.display_score()
142+
pygame.display.flip()
143+
144+
# snake eating apple scenario
145+
for i in range(self.snake.length):
146+
if self.is_collision(self.snake.x[i], self.snake.y[i], self.apple.x, self.apple.y):
147+
self.play_sound("ding")
148+
self.snake.increase_length()
149+
self.apple.move()
150+
151+
# snake colliding with itself
152+
for i in range(3, self.snake.length):
153+
if self.is_collision(self.snake.x[0], self.snake.y[0], self.snake.x[i], self.snake.y[i]):
154+
self.play_sound('crash')
155+
raise "Collision Occurred"
156+
157+
# snake colliding with the boundries of the window
158+
if not (0 <= self.snake.x[0] <= 1000 and 0 <= self.snake.y[0] <= 800):
159+
self.play_sound('crash')
160+
raise "Hit the boundry error"
161+
162+
def display_score(self):
163+
font = pygame.font.SysFont('arial',30)
164+
score = font.render(f"Score: {self.snake.length}",True,(200,200,200))
165+
self.surface.blit(score,(850,10))
166+
167+
def show_game_over(self):
168+
self.render_background()
169+
font = pygame.font.SysFont('arial', 30)
170+
line1 = font.render(f"Game is over! Your score is {self.snake.length}", True, (255, 255, 255))
171+
self.surface.blit(line1, (200, 300))
172+
line2 = font.render("To play again press Enter. To exit press Escape!", True, (255, 255, 255))
173+
self.surface.blit(line2, (200, 350))
174+
pygame.mixer.music.pause()
175+
pygame.display.flip()
176+
177+
def run(self):
178+
running = True
179+
pause = False
180+
181+
while running:
182+
for event in pygame.event.get():
183+
if event.type == KEYDOWN:
184+
if event.key == K_ESCAPE:
185+
running = False
186+
187+
if event.key == K_RETURN:
188+
pygame.mixer.music.unpause()
189+
pause = False
190+
191+
if not pause:
192+
if event.key == K_LEFT:
193+
self.snake.move_left()
194+
195+
if event.key == K_RIGHT:
196+
self.snake.move_right()
197+
198+
if event.key == K_UP:
199+
self.snake.move_up()
200+
201+
if event.key == K_DOWN:
202+
self.snake.move_down()
203+
204+
if event.key == K_a:
205+
self.snake.move_left()
206+
207+
if event.key == K_d:
208+
self.snake.move_right()
209+
210+
if event.key == K_w:
211+
self.snake.move_up()
212+
213+
if event.key == K_s:
214+
self.snake.move_down()
215+
216+
elif event.type == QUIT:
217+
running = False
218+
try:
219+
220+
if not pause:
221+
self.play()
222+
223+
except Exception as e:
224+
self.show_game_over()
225+
pause = True
226+
self.reset()
227+
228+
time.sleep(.50)
229+
230+
if __name__ == '__main__':
231+
game = Game()
232+
game.run()

0 commit comments

Comments
 (0)