forked from camillezajac/snakesVersusSpace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
327 lines (247 loc) · 9.97 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
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
import time
import pygame
import pygame_menu
import requests
import concurrent.futures
from math import sqrt, pow
from random import randint, randrange
PLAYING = False
DIFFICULTY = 0
SNAKES_COUNT = 3
SHOOTING = False
USER = ""
PASS = ""
TOKEN_GEN = ""
# ~~~~ graphic sizes ~~~~
# laser.png : 32px * 32px
# lad.png : 64px * 64px
# snk.png : 64px * 64px
# ico.png : 32px * 32px
# bg.jpg : 1080px * 720px
# initial player coords
(x, y) = (500, 576)
# background sprite
bg = pygame.image.load('img/bg.jpg')
# player and enemy icons
icon_player = pygame.image.load('img/octocat.png')
icon_snake = pygame.image.load('img/snk.png')
icon_laser = pygame.image.load('img/laser.png')
# initialize pygame
pygame.init()
# scoreboard
font = pygame.font.Font(pygame.font.get_default_font(), 22)
# display, title and icon
screen = pygame.display.set_mode((1080, 720))
pygame.display.set_icon(pygame.image.load('img/ico.png'))
pygame.display.set_caption('Snakes vs. Space')
# get login access and refresh tokens
def gettokens():
global USER, PASS
res = requests.post('https://snakesvsspace.herokuapp.com/api/token/', {"username": USER, "password": PASS}).json()
return res
def updatedb(user,score,acc,timetaken):
res = requests.post('https://snakesvsspace.herokuapp.com/scores/', data={"username": user, "accuracy": acc, "score": score, "time_s": timetaken}, headers={"Authorization": f'Bearer {TOKEN_GEN}'}).json()
# player sprite locator
def player(x, y):
screen.blit(icon_player, (x, y))
# snake sprite locator
def snake(x, y):
screen.blit(icon_snake, (x, y))
# laser sprite locator
def laser(xl, yl):
global SHOOTING
SHOOTING = True
screen.blit(icon_laser, (xl+16, yl+10))
# collision mechanism
def collides(xs, ys, xl, yl):
D = sqrt(pow(xs-xl,2)+pow(ys-yl,2))
if D <= 28: return True
else: return False
# main menu
def menu():
def set_difficulty(value, difficulty):
global DIFFICULTY
DIFFICULTY = difficulty
def set_snakes_count(value, count):
global SNAKES_COUNT
SNAKES_COUNT = count
def set_user(value):
global USER
USER = value
def set_pass(value):
global PASS
PASS = value
menu_bg = pygame_menu.baseimage.BaseImage(
image_path='img/bg-menu.jpg',
drawing_mode=pygame_menu.baseimage.IMAGE_MODE_REPEAT_XY
)
menu_bg_creds = pygame_menu.baseimage.BaseImage(
image_path='img/bg-menu-creds.jpg',
drawing_mode=pygame_menu.baseimage.IMAGE_MODE_REPEAT_XY
)
pygame_menu.themes.THEME_DARK.background_color = menu_bg
pygame_menu.themes.THEME_DARK.widget_font = pygame_menu.font.FONT_MUNRO
pygame_menu.themes.THEME_DARK.widget_font_size = 36
pygame_menu.themes.THEME_DARK.title_bar_style = pygame_menu.widgets.MENUBAR_STYLE_NONE
pygame_menu.themes.THEME_DARK.title_offset = (100,100)
_menu = pygame_menu.Menu(720, 1080, '', theme=pygame_menu.themes.THEME_DARK) # main menu
pygame_menu.themes.THEME_DARK.title_offset = (350,100)
pygame_menu.themes.THEME_DARK.background_color = menu_bg_creds
_menu_sub = pygame_menu.Menu(720, 1080, '', theme=pygame_menu.themes.THEME_DARK) # credits section menu
# main menu definition
# max user length: 16, max password length: 64
_menu.add_text_input('Username: ', onchange=set_user, maxchar=16)
_menu.add_text_input('Password: ', password=True, password_char='*', onchange=set_pass, maxchar=64)
_menu.add_selector('Difficulty: ', [('Easy', 0), ('Medium', 1), ('Hard', 2)], font_color=(204,204,0), onchange=set_difficulty)
_menu.add_selector('Snakes: ', [('few snakes', 3), ('moderate snakes', 5), ('snakes all day', 6)], font_color=(204,204,0), onchange=set_snakes_count, margin=(0,40))
_menu.add_button('Play', game, font_color=(144,238,144))
_menu.add_button('Quit', pygame_menu.events.EXIT, font_color=(255,99,71), margin=(0,40))
_menu.add_button('Credits', _menu_sub, font_color=(255,51,153))
# credit section menu definition
_menu_sub.add_button('Abhinav Sinha', pygame_menu.events.NONE, font_color=(255,51,153))
_menu_sub.add_button('Camille Zajac', pygame_menu.events.NONE, font_color=(255,51,153))
_menu_sub.add_button('Kruti Sutaria', pygame_menu.events.NONE, font_color=(255,51,153))
_menu_sub.add_button('Kumar Mallikarjuna', pygame_menu.events.NONE, margin=(0,80), font_color=(255,51,153))
_menu_sub.add_button('www.snakes-vs.space', pygame_menu.events.BACK, font_color=(0,255,255), shadow=True, shadow_color=(255,0,255), shadow_offset=1)
return _menu
# game loop
def game():
tokens = gettokens()
if 'refresh' in tokens.keys():
global TOKEN_GEN
TOKEN_GEN = tokens['access']
pass
else: return
space_count = 0
SCORE = 0
finalscore = 0
snk_velocity = [1.1,1.3,1.7]
global x, y, SHOOTING
Xchange = Ychange = 0
# initial snake coords
snakes = []
(xs, ys, Xschange, Yschange) = ([],[],[],[])
global SNAKES_COUNT
for i in range(SNAKES_COUNT):
snakes.append(icon_snake)
xs.append(randint(32, 1048))
ys.append(randint(0, 200))
Xschange.append(0)
Yschange.append(0)
# laser coords
global xl, yl
(xl, yl) = (x+16, 570)
# laser movement
XLchange = 0
YLchange = 15
running = True
start_timer = time.time()
while running:
GAME_OVER = False
screen.blit(bg, (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
Xchange = -7
if event.key == pygame.K_RIGHT:
Xchange = 7
if event.key == pygame.K_SPACE:
if yl==570:
xl = x
laser(xl, yl)
space_count += 1
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
Ychange = 0
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
Xchange = 0
# player x-boundary limit
if (x + Xchange) >= 1016:
x = 1016
elif (x + Xchange) <= 0:
x = 0
else:
x += Xchange
# update snake coords
for i in range(SNAKES_COUNT):
if ys[i] >= 550:
GAME_OVER = True
break
Xsch_rng = randint(-2, 2)
while (xs[i]+Xsch_rng) < 32 or (xs[i]+Xsch_rng) > 1058:
Xsch_rng = randint(-2, 2)
Yschange[i] = float(randrange(0, int(100*snk_velocity[DIFFICULTY]))/100) # can change this at number of collisions increases to add difficulty to levels
xs[i] += Xsch_rng
ys[i] += Yschange[i]
if collides(xs[i],ys[i],xl+8,yl+8):
yl = 570
SHOOTING = False
SCORE += 5
(xs[i], ys[i]) = (randint(32, 1048), randint(0, 100))
# laser resets upon reaching the end
if yl <= 0:
yl = 570
SHOOTING = False
# laser shooting forward
if SHOOTING:
laser(xl, yl)
yl -= YLchange
# player sprite
player(x, y)
for i in range(SNAKES_COUNT):
snake(xs[i], ys[i])
# return to main menu if game is over
prevtime = time.time()
if GAME_OVER:
stop_timer = time.time()
go = pygame.font.Font(pygame.font.get_default_font(), 72)
gameov = go.render('GAME OVER', True, (255, 64, 255))
gameovbox = gameov.get_rect()
gameovbox.center = (540, 320)
finscore = font.render(f'Final Score: {finalscore}', True, (0, 255, 0))
finscorebox = finscore.get_rect()
finscorebox.center = (540, 375)
finacc = font.render(f'Final Accuracy: {round(((finalscore/5)/space_count)*100,2) if space_count != 0 else 0.0}%', True, (0, 255, 0))
finaccbox = finacc.get_rect()
finaccbox.center = (540, 400)
fintime = font.render(f'Play Time: {round(stop_timer-start_timer,2)} seconds', True, (0, 255, 0))
fintimebox = fintime.get_rect()
fintimebox.center = (540, 425)
while (time.time()-prevtime) <= 3:
screen.blit(bg, (0,0))
screen.blit(gameov,gameovbox)
screen.blit(finscore,finscorebox)
screen.blit(finacc,finaccbox)
screen.blit(fintime,fintimebox)
pygame.display.update()
updatedb(USER,SCORE,round(((finalscore/5)/space_count)*100,2),round(stop_timer-start_timer,2))
SCORE = 0
break
# tracking final score till the game gets over
finalscore = SCORE
# ui score
score = font.render(f'Score: {SCORE}', True, (0, 255, 0))
scorebox = score.get_rect()
scorebox.center = (540, 680)
# ui player name
user = font.render(f'Player: {USER}', True, (255,105,180))
userbox = user.get_rect()
userbox.midleft = (50,680)
acc_percent = ((SCORE/5)/space_count)*100 if space_count != 0 else 0.0
# ui acc
acc = font.render(f'Accuracy: {round(acc_percent,2)}%', True, (255,105,180))
accbox = acc.get_rect()
accbox.midright = (1030,680)
# ui timer
timer = font.render(f'Time Elapsed: {int(time.time()-start_timer)} sec', True, (255,255,0))
timerbox = timer.get_rect()
timerbox.midright = (1030,50)
screen.blit(pygame.image.load('img/bar.png'), (0,640))
screen.blit(user,userbox)
screen.blit(score,scorebox)
screen.blit(timer,timerbox)
screen.blit(acc,accbox)
pygame.display.update()
menu().mainloop(screen)