-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.py
235 lines (182 loc) · 6.58 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import pygame as oss
from pygame.locals import *
import time
import random
SIZE = 60
BACKGROUND_COLOR = (52, 235, 82)
class WelcomeScreen:
def __init__(self, surface):
self.surface = surface
def show_welcome(self):
self.surface.fill(BACKGROUND_COLOR)
font = oss.font.SysFont('arial', 60)
welcome_message = font.render("Welcome to world of snake!", True, (255, 0, 0))
instructions1 = font.render("Press Q to start the game.", True, (255, 0, 0))
self.surface.blit(welcome_message, (200, 300))
self.surface.blit(instructions1, (200, 400))
oss.display.flip()
def wait_for_key(self):
waiting = True
while waiting:
for event in oss.event.get():
if event.type == KEYDOWN:
if event.key == K_q:
waiting = False
elif event.key == K_w:
oss.quit()
self.exit()
class Apple:
def __init__(self, parent_screen):
self.parent_screen = parent_screen
self.image = oss.image.load("resources/apple.jpg.png").convert()
self.x = 120
self.y = 120
def draw(self):
self.parent_screen.blit(self.image, (self.x, self.y))
oss.display.flip()
def move(self):
self.x = random.randint(1,16)*SIZE
self.y = random.randint(1,13)*SIZE
class Snake:
def __init__(self, parent_screen):
self.parent_screen = parent_screen
self.image = oss.image.load("resources/block.jpg.png").convert()
self.direction = 'down'
self.length = 1
self.x = [60]
self.y = [60]
def move_left(self):
self.direction = 'left'
def move_right(self):
self.direction = 'right'
def move_up(self):
self.direction = 'up'
def move_down(self):
self.direction = 'down'
def walk(self):
for i in range(self.length-1,0,-1):
self.x[i] = self.x[i-1]
self.y[i] = self.y[i-1]
if self.direction == 'left':
self.x[0] -= SIZE
if self.direction == 'right':
self.x[0] += SIZE
if self.direction == 'up':
self.y[0] -= SIZE
if self.direction == 'down':
self.y[0] += SIZE
self.draw()
def draw(self):
for i in range(self.length):
self.parent_screen.blit(self.image, (self.x[i], self.y[i]))
oss.display.flip()
def increase_length(self):
#increase length of the snake
//
class PLAY:
def __init__(self):
oss.init()
oss.mixer.init()
self.play_background_music()
self.surface = oss.display.set_mode((1000, 800))
self.snake = Snake(self.surface)
self.snake.draw()
self.apple = Apple(self.surface)
self.apple.draw()
self.speed=0.25
def play_background_music(self):
oss.mixer.music.load("resources/enigma-dream-170618.mp3")
oss.mixer.music.play(-1, 0)
def play_sound(self, sound_name):
if sound_name == 'beep':
sound = oss.mixer.Sound("resources/beep.mp3.mp3")
elif sound_name == 'beep':
sound = oss.mixer.Sound("resources/beep.mp3.mp3")
oss.mixer.Sound.play(sound)
def reset(self):
self.snake = Snake(self.surface)
self.apple = Apple(self.surface)
def is_collision(self, x1, y1, x2, y2):
#add collision of snake on boundary
def is_out_of_bounds(self):
if(
self.snake.x[0]<0
or self.snake.x[0]>=1000
or self.snake.y[0]<0
or self.snake.y[0]>=800
):
return True
return False
def render_background(self):
bg = oss.image.load("resources/background.jpg.jpg")
self.surface.blit(bg, (0,0))
def play(self):
self.render_background()
self.snake.walk()
self.apple.draw()
self.display_score()
oss.display.flip()
if self.is_collision(self.snake.x[0], self.snake.y[0], self.apple.x, self.apple.y):
self.play_sound("beep")
self.snake.increase_length()
self.apple.move()
if self.is_out_of_bounds():
self.play_sound('beep')
raise "out of bounds"
for i in range(3, self.snake.length):
if self.is_collision(self.snake.x[0], self.snake.y[0], self.snake.x[i], self.snake.y[i]):
self.play_sound('beep')
raise "Collision Occurred"
def display_score(self):
font = oss.font.SysFont('arial',60)
score = font.render(f"Score: {self.snake.length}",True,(255,0,0))
self.surface.blit(score,(650,10))
def show_game_over(self):
self.render_background()
font = oss.font.SysFont('arial', 60)
line1 = font.render(f" Your score is {self.snake.length}", True, (255, 0, 0))
self.surface.blit(line1, (200, 300))
line2 = font.render("To play again press Enter.", True, (255, 0, 0))
self.surface.blit(line2, (200, 350))
line3 = font.render("TO exit press Escape!", True ,(255,0,0))
self.surface.blit(line3, (200,400))
oss.mixer.music.pause()
oss.display.flip()
def run(self):
running = True
pause = False
while running:
for event in oss.event.get():
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
running = False
if event.key == K_RETURN:
oss.mixer.music.unpause()
pause = False
if not pause:
if event.key == K_LEFT:
self.snake.move_left()
if event.key == K_RIGHT:
self.snake.move_right()
if event.key == K_UP:
self.snake.move_up()
if event.key == K_DOWN:
self.snake.move_down()
elif event.type == QUIT:
running = False
try:
if not pause:
self.play()
except Exception as e:
self.show_game_over()
pause = True
self.reset()
time .sleep(self.speed)
if __name__ == '__main__':
oss.init()
surface = oss.display.set_mode((1000, 800))
welcome = WelcomeScreen(surface)
welcome.show_welcome()
welcome.wait_for_key()
game = PLAY()
game.run()