-
Notifications
You must be signed in to change notification settings - Fork 1
/
Game.py
130 lines (113 loc) · 4.59 KB
/
Game.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
import core
import sys
import time
import random
import pygame
import sqlite3
from modules import *
def main(highest_score):
pygame.init()
screen = pygame.display.set_mode(core.SCREENSIZE)
pygame.display.set_caption('Dino Rush')
sounds = {}
for key, value in core.AUDIO_PATHS.items():
sounds[key] = pygame.mixer.Sound(value)
GameStartInterface(screen, sounds, core)
score = 0
highest_score = highest_score
dino = Dinosaur(core.IMAGE_PATHS['dino'])
ground = Ground(core.IMAGE_PATHS['ground'], position=(0, core.SCREENSIZE[1] * 0.93))
cloud_sprites_group = pygame.sprite.Group()
cactus_sprites_group = pygame.sprite.Group()
ptera_sprites_group = pygame.sprite.Group()
add_obstacle_timer = 0
score_timer = 0
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE or event.key == pygame.K_UP:
dino.jump(sounds)
elif event.key == pygame.K_DOWN:
dino.duck()
elif event.type == pygame.KEYUP and event.key == pygame.K_DOWN:
dino.unduck()
screen.fill(core.BACKGROUND_COLOR)
if len(cloud_sprites_group) < 5 and random.randrange(0, 300) == 10:
cloud_sprites_group.add(
Cloud(core.IMAGE_PATHS['cloud'], position=(core.SCREENSIZE[0], random.randrange(30, 200))))
add_obstacle_timer += 1
if add_obstacle_timer > random.randrange(80, 130):
add_obstacle_timer = 0
random_value = random.randrange(0, 10)
if random_value >= 0 and random_value <= 7:
cactus_sprites_group.add(Cactus(core.IMAGE_PATHS['cacti']))
else:
position_ys = [core.SCREENSIZE[1] * 0.82, core.SCREENSIZE[1] * 0.63, core.SCREENSIZE[1] * 0.30]
ptera_sprites_group.add(
Ptera(core.IMAGE_PATHS['ptera'], position=(core.SCREENSIZE[0], random.choice(position_ys))))
dino.update()
ground.update()
cloud_sprites_group.update()
cactus_sprites_group.update()
ptera_sprites_group.update()
score_timer += 1
if score_timer > (core.FPS // 12):
score_timer = 0
score += 1
score = min(score, 99999)
if score > highest_score:
highest_score = score
if score % 100 == 0:
sounds['point'].play()
if score % 1000 == 0:
ground.speed -= 1
for item in cloud_sprites_group:
item.speed -= 1
for item in cactus_sprites_group:
item.speed -= 1
for item in ptera_sprites_group:
item.speed -= 1
for item in cactus_sprites_group:
if pygame.sprite.collide_mask(dino, item):
dino.die(sounds)
for item in ptera_sprites_group:
if pygame.sprite.collide_mask(dino, item):
dino.die(sounds)
cloud_sprites_group.draw(screen)
dino.draw(screen)
ground.draw(screen)
cactus_sprites_group.draw(screen)
ptera_sprites_group.draw(screen)
score_board = Scoreboard(score, core.FONT_PATHS['joystix'],
position=(core.SCREENSIZE[0] * 0.88, core.SCREENSIZE[1] * 0.05))
highest_score_board = Scoreboard(highest_score, core.FONT_PATHS['joystix'],
position=(core.SCREENSIZE[0] * 0.72, core.SCREENSIZE[1] * 0.05),
is_highest=True)
score_board.draw(screen)
highest_score_board.draw(screen)
pygame.display.update()
clock.tick(core.FPS)
if dino.is_dead:
c.execute("INSERT INTO record (unix_timestamp, score) VALUES (?,?);", (time.time(), score))
conn.commit()
break
return GameEndInterface(screen, core), highest_score
if __name__ == '__main__':
conn = sqlite3.connect('history.db')
c = conn.cursor()
c.execute("CREATE TABLE IF NOT EXISTS record (unix_timestamp INT PRIMARY KEY, score SMALLINT NOT NULL);")
c.execute("SELECT MAX(score) FROM record;")
rows = c.fetchall()
for row in rows:
highest_score = row[0]
if not str(highest_score).isdigit():
highest_score = 0
while True:
flag, highest_score = main(highest_score)
if not flag: break
conn.commit()
conn.close()