-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathmain.py
More file actions
430 lines (369 loc) · 13.9 KB
/
Copy pathmain.py
File metadata and controls
430 lines (369 loc) · 13.9 KB
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
import pygame
import random
import os
from Snake import Snake
from Food import Food
from Block import Block
from World import worlds
from tkinter import *
import tkinter.simpledialog
head_path = os.path.join('Assets','Images','head.png')
index3_path = os.path.join('Assets','Images','index3.jpg')
python_path = os.path.join( 'Assets','Images','python.jpg')
point_path = os.path.join('Assets','Sounds','Point.wav')
pygame.mixer.pre_init(44100, -16, 2, 2048)
pygame.mixer.init()
pygame.init()
#intiating sounds
intro_sound = pygame.mixer.Sound(os.path.join('Assets','Sounds','intro.wav'))
game_sound = pygame.mixer.Sound(os.path.join('Assets','Sounds','gamesound.wav'))
pause_sound = pygame.mixer.Sound(os.path.join('Assets','Sounds','pausesound.wav'))
endgame_sound = pygame.mixer.Sound(os.path.join('Assets','Sounds','endsound.wav'))
#the volume are set such that sounds are pleasant
intro_sound.set_volume(0.1)
game_sound.set_volume(0.6)
pause_sound.set_volume(0.1)
endgame_sound.set_volume(0.12)
width, height = 1000, 600
game_display = pygame.display.set_mode((width, height))
game_display.fill((170,150,255))
blist=[]
pygame.display.set_caption('SNAKES')
img = pygame.image.load(head_path)
pygame.display.set_icon(img)
dirn = "right"
clock = pygame.time.Clock()
font = pygame.font.SysFont("comicsansms", 35)
FPS = 25
def total(score, i):
""" function for total score """
return score + i * 10
# for highscore
highscorefile = open('highscore.txt', 'rt')
highscore = highscorefile.readline()
try:
highscore = int(highscore)
except ValueError:
highscore = 0
namehighscore = highscorefile.readline()
highscorefile.close()
def pause(scorestr):
#stop in game music and play pause music
game_sound.stop()
pause_sound.play(-1)
paused = True
while paused:
showButton()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
keyp = action()
if event.type == pygame.KEYDOWN or keyp != None:
try:
if keyp == 'c' or event.key == pygame.K_c:
paused = False
except:
pass
try:
if keyp == 'q' or event.key == pygame.K_q:
print('quit')
pygame.quit()
quit()
except:
pass
message("Paused", (0, 0, 0))
message("C to continue, Q to Quit", (200, 0, 0), 40)
# display score on pause
message(scorestr, (255, 0, 0), 80)
pygame.display.update()
clock.tick(5)
#stop pause music and play in game music if game continues
if paused == False:
pause_sound.stop()
game_sound.play(-1)
def button(msg,x,y,w,h):
mouse = pygame.mouse.get_pos()
if x+w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(game_display, (0,0,250),(x,y,w,h))
else:
pygame.draw.rect(game_display, (0,0,100),(x,y,w,h))
smallText = pygame.font.Font("freesansbold.ttf",20)
text= smallText.render(msg, True, (0,200,0))
t=text.get_rect()
t.center = ( (x+(w/2)), (y+(h/2)) )
game_display.blit(text, t)
def intro():
#play intro music infinite loop
intro_sound.play(-1)
i=True
while i:
for event in pygame.event.get():
keyp=action()
if event.type==pygame.QUIT:
pygame.quit()
quit()
if keyp!=None or event.type==pygame.KEYDOWN :
try:
if keyp=='c' or event.key==pygame.K_c :
i=False
except:
continue
showButton()
image = pygame.image.load(index3_path)
game_display.blit(image,(0,0))
message(" Welcome to Snakes!!",(200,0,0),-59)
message("Press C to continue",(100,0,100),260)
pygame.display.update()
clock.tick(15)
def showButton():
global blist
pygame.draw.rect(game_display, (170, 150, 255), (800, 0, 200, 600))
button("Continue", 800, 200, 130, 30)
blist.append((800, 200, 130, 30,'c'))
button("Exit", 935, 200, 65, 30)
blist.append((935, 200, 65, 30,'q'))
button("Up", 870, 235, 50, 30)
blist.append((870, 235, 50, 30,'up'))
button("Down", 865, 305, 60, 30)
blist.append((865, 305, 60, 30,'dn'))
button("Right", 895, 270, 60, 30)
blist.append((895, 270, 60, 30,'rt'))
button("Left", 830, 270, 60, 30)
blist.append((830, 270, 60, 30,'lt'))
button("Pause",830,340,125,30)
blist.append((830,340,125,30,'p'))
button("Shift", 830, 375, 125, 30)
blist.append((830, 375, 125, 30,'st'))
pygame.draw.line(game_display,(0,200,100),(800,0),(800,600),5)
def action():
global blist
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
for tup in blist:
if tup[0] + tup[2] > mouse[0] > tup[0] and tup[1] + tup[3] > mouse[1] > tup[1]:
if click[0] == 1:
return tup[4]
return None
def message(m, color, dispy=0):
text = font.render(m, True, color)
t = text.get_rect()
t.center = (400), (300) + dispy
game_display.blit(text, t)
def food_collides_block(food_rect, blocks):
""" Returns True if any of the blocks collide with the food """
for block in blocks:
if food_rect.colliderect(block.get_rect()):
return True
return False
def get_blocks(food_rect, n):
""" Generates `n` blocks at random x, y """
blocks = list()
for i in range(n):
block_x = round(random.randrange(0, (width)) / 10.0) * 10
block_y = round(random.randrange(0, height) / 10.0) * 10
block_width, block_height = 10, 10
block = Block(block_x, block_y, block_width, block_height)
# if the block collides with food, generate at other x, y.
if food_rect.colliderect(block.get_rect()):
i -= 1
continue
blocks.append(block)
return blocks
def gameLoop():
global dirn, k, highscore, namehighscore
pyExit = False
pyOver = False
#stop intro music and play in game music infinite loop
intro_sound.stop()
game_sound.play(-1)
score = 0
world_num = 0
scorestr = "Score:0"
# Initialize the game
snake = Snake(200, 200, img)
food = Food(int(width / 2), int(height / 2))
blocks = worlds(width - 200, height, world_num)
# Keeps track of the direction of the snake.
dx, dy = 0, 0
lossreason = ''
while not pyExit:
if pyOver == True:
#play end music
endgame_sound.play(-1)
while pyOver:
image = pygame.image.load(python_path)
game_display.blit(image, (0, 0))
message("Game Over! Press C to play Again, Q to Quit",
(255, 0, 0), -20)
message(lossreason, (255, 0, 0), 30)
# display score on game over
message("Your" + scorestr, (255, 0, 0), 80)
if totalscore > highscore:
# message("Highscore!!!",(255,0,0),120)
# write new highscore
highscorefile = open('highscore.txt', 'wt')
highscorefile.write(str(totalscore) + "\n")
# name window
def namewrite():
highscorefile.write(v.get())
scorewindow.destroy()
scorewindow = Tk()
scorewindow.geometry('300x100')
frame = Frame(scorewindow, width=100, height=100)
frame.pack()
scorewindow.title("congratulations")
Label(frame, text='you\'ve made highscore!!!!').pack(side='top')
v = StringVar()
v.set("type your name")
textbox = Entry(frame, textvariable=v)
textbox.pack(side='top')
okbutton = Button(frame, text="ok", fg="black",
bg="white", command=namewrite)
okbutton.pack(side='bottom')
scorewindow.mainloop()
highscorefile.close()
# incase user wants to countinue after creating highscore
# to read his new score
highscorefile = open('highscore.txt', 'rt')
highscore = highscorefile.readline()
highscore = int(highscore)
namehighscore = highscorefile.readline()
highscorefile.close()
else:
message("Highscore by " + namehighscore +
":" + str(highscore), (255, 0, 0), 120)
pygame.display.update()
for event in pygame.event.get():
keyp = action()
if keyp != None or event.type == pygame.KEYDOWN:
try:
if keyp == 'q' or event.key == pygame.K_q:
pyExit = True
pyOver = False
except:
blank = [] # bypass the exception
try:
if keyp == 'c' or event.key == pygame.K_c:
#stop endgame music
endgame_sound.stop()
gameLoop()
except:
blank = [] # bypass the exception
""" Events """
#the conditions are modified to work with the buttons
for event in pygame.event.get():
keyp = action()
# blank is not used anywhere
# it is just used to jump the exception
if event.type == pygame.QUIT:
pyExit = True
if event.type == pygame.KEYDOWN or keyp != None:
try:
if keyp == 'lt' or event.key == pygame.K_LEFT and dirn != "right":
dirn = "left"
dx = -1
dy = 0
except:
blank = []
try:
if keyp == 'rt' or event.key == pygame.K_RIGHT and dirn != "left":
dirn = "right"
dx = 1
dy = 0
except:
blank = []
try:
if keyp == 'up' or event.key == pygame.K_UP and dirn != "down":
dirn = "up"
dy = -1
dx = 0
except:
blank = []
try:
if keyp == 'dn' or event.key == pygame.K_DOWN and dirn != "up":
dirn = "down"
dy = 1
dx = 0
except:
blank = []
try:
if keyp == 'p' or event.key == pygame.K_p:
pause(scorestr)
except:
blank = []
try:
if keyp == 'q' or event.key == pygame.K_q:
pygame.quit()
quit(0)
except:
blank = []
# level changer value
if score > 10:
score = 0
world_num += 1
blocks = worlds(width - 200, height, world_num)
food.x, food.y = int(width / 2), int(height / 2)
# Engage boost of pressing shift
keyp=action()
keyPresses = pygame.key.get_pressed()
boost_speed = keyPresses[pygame.K_LSHIFT] or keyPresses[pygame.K_RSHIFT] or keyp=='st'
# if boost_speed is true it will move 2 blocks in one gameloop
# else it will just move one block
iterations = [1]
if boost_speed == 1:
iterations.append(2)
for i in iterations:
""" Update snake """
snake.move(dx, dy, 10)
snake.check_boundary(width, height)
snake_rect = snake.get_rect()
food_rect = food.get_rect()
""" Snake-Snake collision """
if snake.ate_itself():
#stop game sound
game_sound.stop()
pyOver = True
lossreason = 'Oooops You Hit YOURSELF'
""" Snake-Block collision """
for block in blocks:
block_rect = block.get_rect()
if block_rect.colliderect(snake_rect):
#stop game sound
game_sound.stop()
pyOver = True
lossreason = 'Ooops You Hit a BLOCKER'
""" Snake-Food collision """
# if snake collides with food, increase its length.
if food_rect.colliderect(snake_rect):
score += 1
snake.increment_length()
sound = pygame.mixer.Sound(point_path)
sound.set_volume(0.3)
sound.play()
# generate food at random x, y.
food.generate_food(width, height)
# try generating the food at a position where blocks are not present.
while food_collides_block(food.get_rect(), blocks):
food.generate_food(width - food.size, height - food.size)
""" Draw """
game_display.fill((255, 255, 255))
showButton()
# draw the food and snake.
snake.draw(game_display, dirn, (0, 155, 0))
food.draw(game_display, (0, 255, 0))
# draw the blocks.
for block in blocks:
block.draw(game_display, (255, 0, 0))
# count and display score on screen
totalscore = total(score, world_num)
scorestr = 'Score: ' + str(totalscore)
font = pygame.font.SysFont(None, 30)
text = font.render(scorestr, True, (0, 0, 255))
game_display.blit(text, (0, 0, 20, 20))
pygame.display.update()
clock.tick(FPS)
pygame.quit()
quit()
intro()
gameLoop()