-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.py
132 lines (93 loc) · 3.43 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
131
132
from EngineClass import *
from DisplayClass import *
import pygame
import time
Score_Increment = 0.1
Width = 900
Height = 700
Tick_Time = 0.05
class Game:
def __init__(self, n):
self.paused = False
self.ensemble = Ensemble(np.array([[Width], [Height]]))
self.ensemble.populate(n)
self.display = Display(self.ensemble)
self.engine = Engine(self.ensemble)
self.score = 0
self.starting_bodies = 20
self.ticks_per_new_body = 30
self.ticks = 0
# --------------------------- Getters and setters
def get_ticks_per_new_body(self):
return self.ticks_per_new_body
def set_ticks_per_new_body(self, value):
self.ticks_per_new_body = value
def get_engine(self):
return self.engine
def get_ticks(self):
return self.ticks
def set_ticks(self, ticks):
self.ticks = ticks
def set_paused(self, bool):
self.paused = bool
def get_display(self):
return self.display
def get_paused(self):
return self.paused
def get_score(self):
return self.score
def set_score(self, score):
self.score = score
def get_ensemble(self):
return self.ensemble
def get_starting_bodies(self):
return self.starting_bodies
# ----------------------------- Game functions
def update_ticks_per_new_body(self):
self.set_ticks_per_new_body(int(20 + 100 * 1.1 ** -self.get_ticks()))
def update_score(self):
inc = 0.1 * self.get_ticks() ** 0.1
self.set_score(self.get_score() + inc)
def tick(self):
start_time = time.time()
if self.paused:
pass
else:
self.get_engine().tick()
self.get_display().draw_game(int(self.get_score()))
self.update_ticks_per_new_body()
if self.get_ticks() % self.get_ticks_per_new_body() == 0:
self.get_engine().get_ensemble().add_bodies_constant_mass(1, 4 * self.get_ticks() ** 0.15)
self.update_score()
self.set_ticks(self.get_ticks() + 1)
end_time = time.time()
time_remaining = Tick_Time + start_time - end_time
if time_remaining > 0:
time.sleep(time_remaining)
def reset_game(self):
self.get_ensemble().reset_ensemble(self.get_starting_bodies())
self.set_score(0)
def main_loop(self):
self.reset_game()
while True:
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
break
if self.get_ensemble().get_crashed():
self.get_display().draw_game_over()
if pygame.key.get_pressed()[pygame.K_SPACE]:
self.reset_game()
self.get_display().draw_game(int(self.get_score()))
elif self.get_paused():
for event in events:
if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
self.set_paused(False)
else:
self.tick()
for event in events:
if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
self.set_paused(True)
quit()
game = Game(20)
game.main_loop()