-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
516 lines (422 loc) · 18.3 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
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
import pygame, random, time
#Let's import the Player Class
from player import Player
from barrier import Barrier
from grass import Grass
from pygame import mixer
import requests, json
try:
response = requests.get("https://649204ac2f2c7ee6c2c94ef8.mockapi.io/topScore")
print(response)
response_info = json.loads(response.text)
print(response_info)
topScores = []
for i in response_info:
topScores.append((i["name"],i["score"]))
except:
print("Erro ao conectar com o servidor")
topScores = []
pygame.init()
GREEN = (20, 255, 140)
GREY = (210, 210 ,210)
WHITE = (255, 255, 255)
RED = (255, 100, 100)
DARKRED = (255, 0, 0)
PURPLE = (255, 0, 255)
YELLOW = (255, 255, 0)
CYAN = (0, 255, 255)
BLUE = (100, 100, 255)
DARKBLUE = (0, 0, 255)
DARKGREEN = (72,93,20)
speed = 2
posibleRange = (1,10)
colorList = (RED, GREEN, PURPLE, YELLOW, CYAN, BLUE)
SCREENWIDTH=800
SCREENHEIGHT=600
size = (SCREENWIDTH, SCREENHEIGHT)
screen = pygame.display.set_mode(size, pygame.FULLSCREEN|pygame.SCALED)
# screen = pygame.display.set_mode(size)
pygame.display.set_caption("Tabuada")
#This will be a list that will contain all the sprites we intend to use in our game.
all_sprites_list = pygame.sprite.Group()
playerPlayer1 = Player(DARKBLUE, 60, 80, 70, "girl.png")
playerPlayer1.rect.x = 130
playerPlayer1.rect.y = SCREENHEIGHT - 100
playerPlayer2 = Player(DARKRED, 60, 80, 70, "boy.png")
playerPlayer2.rect.x = 530
playerPlayer2.rect.y = SCREENHEIGHT - 100
Barrier1 = Barrier(random.choice(colorList), 60, 80, -200)
Barrier1.rect.x = 50
Barrier2 = Barrier(random.choice(colorList), 60, 80, -200)
Barrier2.rect.x = 130
Barrier3 = Barrier(random.choice(colorList), 60, 80, -200)
Barrier3.rect.x = 210
Barrier4 = Barrier(random.choice(colorList), 60, 80, -200)
Barrier4.rect.x = 290
Barrier5 = Barrier(random.choice(colorList), 60, 80, -200)
Barrier5.rect.x = 450
Barrier6 = Barrier(random.choice(colorList), 60, 80, -200)
Barrier6.rect.x = 530
Barrier7 = Barrier(random.choice(colorList), 60, 80, -200)
Barrier7.rect.x = 610
Barrier8 = Barrier(random.choice(colorList), 60, 80, -200)
Barrier8.rect.x = 690
# Add the Player to the list of objects
all_sprites_list.add(playerPlayer1)
all_sprites_list.add(playerPlayer2)
all_sprites_list.add(Barrier1)
all_sprites_list.add(Barrier2)
all_sprites_list.add(Barrier3)
all_sprites_list.add(Barrier4)
all_sprites_list.add(Barrier5)
all_sprites_list.add(Barrier6)
all_sprites_list.add(Barrier7)
all_sprites_list.add(Barrier8)
all_coming_BarriersLeft = pygame.sprite.Group()
all_coming_BarriersLeft.add(Barrier1)
all_coming_BarriersLeft.add(Barrier2)
all_coming_BarriersLeft.add(Barrier3)
all_coming_BarriersLeft.add(Barrier4)
all_coming_BarriersRight = pygame.sprite.Group()
all_coming_BarriersRight.add(Barrier5)
all_coming_BarriersRight.add(Barrier6)
all_coming_BarriersRight.add(Barrier7)
all_coming_BarriersRight.add(Barrier8)
numbersLeft = []
numbersRight = []
score=0
redbar = 0
greenbar = 0
barcolor = (0,0,0)
tutorial = 1
spacePressed = False
name = ""
for Barrier in all_coming_BarriersLeft:
number = random.randint(posibleRange[0],posibleRange[1])
Barrier.setNumber(number)
numbersLeft.append(number)
for Barrier in all_coming_BarriersRight:
number = random.randint(posibleRange[0],posibleRange[1])
Barrier.setNumber(number)
numbersRight.append(number)
anwser = random.choice(numbersLeft) * random.choice(numbersRight)
#Allowing the user to close the window...
PlayerryOn = True
clock=pygame.time.Clock()
starttime = time.time() + 300
tutorialTime = time.time() + 5
mixer.init()
mixer.music.load("ambient.mp3")
mixer.music.set_volume(0.7)
mixer.music.play(loops = -1)
grasses = pygame.sprite.Group()
for i in range(0,SCREENHEIGHT,50):
grasses.add(Grass(i, -20, SCREENWIDTH, SCREENHEIGHT))
grasses.add(Grass(i, 340, SCREENWIDTH, SCREENHEIGHT))
grasses.add(Grass(i, 380, SCREENWIDTH, SCREENHEIGHT))
grasses.add(Grass(i, 740, SCREENWIDTH, SCREENHEIGHT))
def wait():
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
elif event.type==pygame.KEYDOWN:
if event.key==pygame.K_ESCAPE:
pygame.quit()
else:
return
pygame.time.wait(20)
while PlayerryOn:
for event in pygame.event.get():
if event.type==pygame.QUIT:
PlayerryOn=False
elif event.type==pygame.KEYDOWN:
if event.key==pygame.K_ESCAPE:
PlayerryOn=False
if event.key==pygame.K_d:
if playerPlayer1.rect.x < 290:
playerPlayer1.moveRight(80)
elif event.key==pygame.K_a:
if playerPlayer1.rect.x > 50:
playerPlayer1.moveLeft(80)
elif event.key==pygame.K_LEFT:
if playerPlayer2.rect.x > 450:
playerPlayer2.moveLeft(80)
elif event.key==pygame.K_RIGHT:
if playerPlayer2.rect.x < 690:
playerPlayer2.moveRight(80)
elif event.key==pygame.K_SPACE:
spacePressed = True
for Barrier in all_coming_BarriersLeft:
Barrier.moveForward(speed)
for Barrier in all_coming_BarriersRight:
Barrier.moveForward(speed)
if Barrier1.rect.y > SCREENHEIGHT-160:
collisionsP1 = pygame.sprite.spritecollide(playerPlayer1, all_coming_BarriersLeft, False)[0].getNumber()
collisionsP2 = pygame.sprite.spritecollide(playerPlayer2, all_coming_BarriersRight, False)[0].getNumber()
if collisionsP1*collisionsP2 == anwser:
score += 1
speed += 0.1
print("CORRECT")
greenbar = 120
else:
print("WRONG")
if speed > 0.3:
speed -= 0.2
else:
speed = 0.2
print("speed: " + str(speed))
redbar = 120
print(collisionsP1*collisionsP2)
numbersLeft = []
for barrier in all_coming_BarriersLeft:
number = random.randint(posibleRange[0],posibleRange[1])
numbersLeft.append(number)
barrier.repaint(random.choice(colorList))
barrier.y = 0
barrier.setNumber(number)
numbersRight = []
for barrier in all_coming_BarriersRight:
number = random.randint(posibleRange[0],posibleRange[1])
numbersRight.append(number)
barrier.repaint(random.choice(colorList))
barrier.y = 0
barrier.setNumber(number)
numberleft = random.choice(numbersLeft)
numberright = random.choice(numbersRight)
anwser = numberleft * numberright
all_sprites_list.update()
timeleftMin = int((starttime - time.time())/60)
timeleftSec = int((starttime - time.time())%60)
#Drawing on Screen
screen.fill(DARKGREEN)
#Draw The Road
pygame.draw.rect(screen, GREY, [40,0, 320,SCREENHEIGHT])
#Draw Line painting on the road
pygame.draw.line(screen, WHITE, [120,0],[120,SCREENHEIGHT],5)
#Draw Line painting on the road
pygame.draw.line(screen, WHITE, [200,0],[200,SCREENHEIGHT],5)
#Draw Line painting on the road
pygame.draw.line(screen, WHITE, [280,0],[280,SCREENHEIGHT],5)
#Draw The Second Road
pygame.draw.rect(screen, GREY, [440,0, 320,SCREENHEIGHT])
#Draw Line painting on the road
pygame.draw.line(screen, WHITE, [520,0],[520,SCREENHEIGHT],5)
#Draw Line painting on the road
pygame.draw.line(screen, WHITE, [600,0],[600,SCREENHEIGHT],5)
#Draw Line painting on the road
pygame.draw.line(screen, WHITE, [680,0],[680,SCREENHEIGHT],5)
#Now let's draw all the sprites in one go. (For now we only have 1 sprite!)
all_sprites_list.draw(screen)
for grass in grasses:
grass.updateAndDraw(screen, speed)
# Draw the score and top score
if redbar > 0:
barcolor = (155,0,0)
redbar -= 1
elif greenbar > 0:
barcolor = (0,155,0)
greenbar -= 1
else:
barcolor = (0,0,0)
pygame.draw.rect(screen, barcolor, [0,0, SCREENWIDTH,80])
font = pygame.font.SysFont('Calibri', 25, True, False)
text = font.render("Score: " + str(score),True,WHITE)
screen.blit(text, [20, 10])
# text = font.render("Top Score: " + str(10),True,WHITE)
# screen.blit(text, [20, 40])
# Draw the time left
font = pygame.font.SysFont('Calibri', 25, True, False)
text = font.render("Tempo Restante: " + str(timeleftMin) + ":" + str(timeleftSec),True,WHITE)
screen.blit(text, [20, 40])
# Draw Rules to win match
font = pygame.font.SysFont('Calibri', 40, True, False)
text = font.render(" X = " + str(anwser),True,WHITE)
screen.blit(text, (288, 20))
pygame.draw.rect(screen, DARKBLUE, [300,20, 40,40])
pygame.draw.rect(screen, DARKRED, [380,20, 40,40])
#Draw Game Over
if timeleftMin <= 0 and timeleftSec <= 0:
newScore = {"name": name.replace('\r','') ,"score": score}
try:
requests.post("https://649204ac2f2c7ee6c2c94ef8.mockapi.io/topScore", data = newScore)
response = requests.get("https://649204ac2f2c7ee6c2c94ef8.mockapi.io/topScore")
print(response)
response_info = json.loads(response.text)
print(response_info)
topScores = []
for i in response_info:
topScores.append((i["name"],i["score"]))
except:
print("Error to save score")
topScores.append((name, score))
pygame.draw.rect(screen, (0,0,0), [200,200, SCREENWIDTH-400,SCREENHEIGHT-250])
font = pygame.font.SysFont('Calibri', 60, True, False)
text = font.render("Game Over",True,WHITE)
screen.blit(text, (250, 200))
pygame.display.flip()
#Draw Bigest Scores
threeBiggestScores = sorted(topScores, reverse=True)[:3]
font = pygame.font.SysFont('Calibri', 40, True, False)
text = font.render("Top Scores:",True,WHITE)
screen.blit(text, (250, 280))
pygame.display.flip()
for i in range(0,3):
try:
font = pygame.font.SysFont('Calibri', 30, True, False)
text = font.render(str(i+1) + " - " + str(threeBiggestScores[i][0]+' - '+threeBiggestScores[i][1]),True,WHITE)
screen.blit(text, (250, 350 + i*50))
except:
print("Error to show all top scores")
pygame.display.flip()
#Draw press any key to restart
font = pygame.font.SysFont('Calibri', 40, True, False)
text = font.render("Press any key to restart",True,WHITE)
screen.blit(text, (205, 500))
pygame.display.flip()
#Wait for any key to be pressed
wait()
#Draw restart
pygame.draw.rect(screen, (0,0,200), [200,200, SCREENWIDTH-400,SCREENHEIGHT-540])
font = pygame.font.SysFont('Calibri', 60, True, False)
text = font.render("Restarting",True,WHITE)
screen.blit(text, (250, 200))
pygame.display.flip()
pygame.time.wait(1000)
#Reset variables
score = 0
speed = 2
redbar = 0
greenbar = 0
starttime = time.time() + 120
numbersLeft = []
for barrier in all_coming_BarriersLeft:
number = random.randint(posibleRange[0],posibleRange[1])
numbersLeft.append(number)
barrier.repaint(random.choice(colorList))
barrier.y = -200
barrier.setNumber(number)
numbersRight = []
for barrier in all_coming_BarriersRight:
number = random.randint(posibleRange[0],posibleRange[1])
numbersRight.append(number)
barrier.repaint(random.choice(colorList))
barrier.y = -200
barrier.setNumber(number)
numberleft = random.choice(numbersLeft)
numberright = random.choice(numbersRight)
anwser = numberleft * numberright
if tutorial == 1:
speed = 1
if(time.time() > tutorialTime):
tutorial = 2
if tutorial == 2:
speed = 0
#draw tutorial
pygame.draw.rect(screen, (0,0,0), [200,200, SCREENWIDTH-400,SCREENHEIGHT-250])
font = pygame.font.SysFont('Calibri', 60, True, False)
text = font.render("Tutorial",True,WHITE)
screen.blit(text, (300, 200))
font = pygame.font.SysFont('Calibri', 40, True, False)
text = font.render("Mova o Bonecos Azul",True,WHITE)
screen.blit(text, (205, 300))
font = pygame.font.SysFont('Calibri', 40, True, False)
text = font.render("pelas teclas 'a' e 'd' ",True,WHITE)
screen.blit(text, (205, 350))
font = pygame.font.SysFont('Calibri', 40, True, False)
text = font.render("e Vermelho pelas setas",True,WHITE)
screen.blit(text, (205, 400))
font = pygame.font.SysFont('Calibri', 28, True, False)
text = font.render("Pressione espaço para Continuar",True,WHITE)
screen.blit(text, (205, 500))
if spacePressed:
tutorial += 1
spacePressed = False
if tutorial == 3:
pygame.draw.rect(screen, (0,0,0), [200,200, SCREENWIDTH-400,SCREENHEIGHT-250])
font = pygame.font.SysFont('Calibri', 60, True, False)
text = font.render("Tutorial",True,WHITE)
screen.blit(text, (300, 200))
font = pygame.font.SysFont('Calibri', 30, True, False)
text = font.render("Seu objetivo é juntar 2",True,WHITE)
screen.blit(text, (205, 300))
font = pygame.font.SysFont('Calibri', 30, True, False)
text = font.render("barreiras (esq e dir) de modo",True,WHITE)
screen.blit(text, (205, 350))
font = pygame.font.SysFont('Calibri', 30, True, False)
text = font.render("que multiplicadas resultem",True,WHITE)
screen.blit(text, (205, 400))
font = pygame.font.SysFont('Calibri', 30, True, False)
text = font.render("no número correto",True,WHITE)
screen.blit(text, (205, 450))
if spacePressed:
tutorial += 1
spacePressed = False
font = pygame.font.SysFont('Calibri', 28, True, False)
text = font.render("Pressione espaço para Continuar",True,WHITE)
screen.blit(text, (205, 500))
if tutorial == 4:
pygame.draw.rect(screen, (0,0,0), [200,200, SCREENWIDTH-400,SCREENHEIGHT-250])
font = pygame.font.SysFont('Calibri', 60, True, False)
text = font.render("Tutorial",True,WHITE)
screen.blit(text, (300, 200))
font = pygame.font.SysFont('Calibri', 30, True, False)
text = font.render("Voces tem 2 Minutos para fazer",True,WHITE)
screen.blit(text, (205, 300))
font = pygame.font.SysFont('Calibri', 30, True, False)
text = font.render("a maior pontuação possível",True,WHITE)
screen.blit(text, (205, 350))
font = pygame.font.SysFont('Calibri', 30, True, False)
text = font.render("Obs. Os melhores da sala vão",True,RED)
screen.blit(text, (205, 400))
font = pygame.font.SysFont('Calibri', 30, True, False)
text = font.render("aparecer no jogo dos colegas",True,RED)
screen.blit(text, (205, 450))
font = pygame.font.SysFont('Calibri', 28, True, False)
text = font.render("Pressione espaço para Continuar",True,WHITE)
screen.blit(text, (205, 500))
if spacePressed:
tutorial += 1
spacePressed = False
if tutorial == 5:
while True:
pygame.draw.rect(screen, (0,0,0), [200,200, SCREENWIDTH-400,SCREENHEIGHT-250])
font = pygame.font.SysFont('Calibri', 50, True, False)
text = font.render("Digite seu nome",True,WHITE)
screen.blit(text, (220, 200))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
if event.key == pygame.K_RETURN:
tutorial = 0
speed = 1.0
starttime = time.time() + 120
if event.key == pygame.K_BACKSPACE:
if name != "":
name = name[:-1]
else:
name += event.unicode
try:
if name != "":
font = pygame.font.SysFont('Calibri', 30, True, False)
text = font.render(name,True,WHITE)
screen.blit(text, (220, 300))
except:
pass
font = pygame.font.SysFont('Calibri', 30, True, False)
text = font.render("Quando estiver pronto",True,WHITE)
screen.blit(text, (205, 460))
font = pygame.font.SysFont('Calibri', 30, True, False)
text = font.render("Pressione Enter para Continuar",True,WHITE)
screen.blit(text, (205, 500))
pygame.display.flip()
if tutorial == 0:
break
#Refresh Screen
pygame.display.flip()
#Number of frames per secong e.g. 60
clock.tick(60)
pygame.quit()