-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.py
71 lines (58 loc) · 1.78 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
import random
import time
import pygame as pg
from pygame import KEYDOWN
from pygame import constants
from Obstacle import Obstacle
from Player import Player
from constants import *
game_active = True
def game_over():
# TODO
global game_active
game_active = False
print('You lost!')
def main():
# Initialize pygame
pg.init()
screen = pg.display.set_mode((600, 480))
pg.display.set_caption("GDSC Dino")
clock = pg.time.Clock()
pg.mouse.set_visible(True)
start = int(time.time_ns())
font = pg.font.SysFont("Comic Sans", 16)
player = Player(screen)
obstacles = []
next_spawn = random.randint(SPAWN_MIN, SPAWN_MAX)
global game_active
while game_active:
dt = clock.tick(fps) / 1000.0
for event in pg.event.get():
if event.type == pg.QUIT:
game_active = False
return
if event.type == KEYDOWN:
if event.key == constants.K_SPACE:
player.jump()
next_spawn -= dt
if next_spawn <= 0:
obstacles.append(Obstacle(screen))
next_spawn = random.randint(SPAWN_MIN, SPAWN_MAX)
screen.fill(BG_RGB)
score = (int(time.time_ns()) - start) // 50000000
text = font.render(str(score), True, (0, 0, 0))
screen.blit(text, (500, 25))
player.show(screen)
player.update_coords(dt)
for obstacle in obstacles:
if obstacle.rect.right <= 0:
obstacles.remove(obstacle)
obstacle.update_coords(dt)
obstacle.show(screen)
if player.rect.collidelist(
[obstacle.rect for obstacle in obstacles]) != -1:
game_over()
pg.display.update()
if __name__ == "__main__":
main()
pg.quit()