/
main.py
296 lines (254 loc) · 12.2 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
from Aurora.Packages.dependencies import *
# title and icon
pygame.display.set_caption("The Helios Attack")
icon = pygame.image.load('Images\\icons\\icon.png')
pygame.display.set_icon(icon)
ray_of_sickness = Spell(sw(1.83), sh(92.86), 'ray_of_sickness.png')
incinerate = Spell(sw(4.76), sh(92.86), 'incinerate.png')
plasma_discharge = Spell(sw(7.66), sh(92.86), 'plasma_discharge.png')
god_of_chaos = Spell(sw(10.61), sh(92.86), 'god_of_chaos.png')
# The planets have been intentionally made weak, and the timer has been reduced
# so that we can present various game cases within a short period of time
mercury = Planet('mercury.png', 50)
venus = Planet('venus.png', 100)
earth = Planet('earth.png', 200)
mars = Planet('mars.png', 150)
jupiter = Planet('jupiter.png',500)
saturn = Planet('saturn.png', 450)
uranus = Planet('uranus.png', 400)
neptune = Planet('neptune.png',750)
start = Control(sh(50)-100, 'start_icon.png')
high = Control(sh(50), 'high.png')
htp = Control(sh(50)+100, 'htp.png')
login = Control(sh(50)-100, 'login.png')
register = Control(sh(50), 'register.png')
play_as_guest = Control(sh(50)+100, 'play_as_guest.png')
cont = Control(sh(80), 'continue.png')
delta = Troop(sw(97.51), sh(92.86), 'delta.png')
tardis = Troop(sw(94.51), sh(92.86), 'tardis.png')
benzamite = Troop(sw(91.58), sh(92.86), 'benzamite.png')
mandalore = Troop(sw(88.65), sh(92.86), 'mandalore.png')
nemesis = Troop(sw(97.51), sh(85.99), 'nemesis.png')
armada = Troop(sw(94.51), sh(85.99), 'armada.png')
elysium = Troop(sw(91.58), sh(85.99), 'elysium.png')
demogorgon = Troop(sw(88.65), sh(85.99), 'demogorgon.png')
controls = [start, high, htp]
auth_controls = [login, register, play_as_guest]
troops = [delta, tardis, benzamite, mandalore, nemesis, armada, elysium, demogorgon]
spells = [ray_of_sickness, incinerate, plasma_discharge, god_of_chaos]
attacks = spells + troops
attacks = attacks[::-1]
levels = [mercury, venus, earth, mars, jupiter, saturn, uranus, neptune]
# authentication input box
username = TextInput(sw(75), sh(30)-1, 64)
# how to play text
with open('./Documentation/HOWTOPLAY.txt') as text:
htp_text_list = text.read().splitlines()
cp_icon = pygame.image.load('Images\\icons\\CP_icon.png').convert_alpha()
cp_rect = cp_icon.get_rect()
cp_rect.center = (sw(87)-75, sh(7)-5)
"""
GAME LOOPS BELOW
"""
play_music()
def menu():
loop = MenuLoop()
while loop.running:
loop.set_screen()
if loop.index == 'home':
change_active_state(controls, True)
change_active_state(auth_controls, False)
batch_place(controls)
elif loop.index == 'htp':
change_active_state(controls, False)
y = sw(5)
for htp_text in htp_text_list:
render_text(htp_text, sw(50), y)
y += 40
elif loop.index == 'high':
change_active_state(controls, False)
render_text('S. No. ', sw(20), sw(5))
render_text('PLAYER NAME', sw(50), sw(5))
render_text('SCORE', sw(80), sw(5))
y = sw(15)
x = 1
for name, score in high_scores():
render_text(str(x), sw(20), y)
render_text(name, sw(50), y)
render_text(str(score), sw(80), y)
y += 40
x += 1
elif loop.index == 'start':
change_active_state(controls, False)
change_active_state(auth_controls, True)
batch_place(auth_controls)
elif loop.index == 'login':
change_active_state(auth_controls, False)
render_text('USERNAME', sw(25), sh(30), 64)
username.render()
if player_exists(username.text.upper()) and username.text.upper() != 'GUEST':
cont.is_active = True
cont.place()
else:
render_text('PLEASE ENTER YOUR USERNAME', cont.rect.center[0], cont.rect.center[1], 32)
elif loop.index == 'register':
cont.is_active = True
change_active_state(auth_controls, False)
render_text('USERNAME', sw(25), sh(30), 64)
username.render()
if username.text.upper() != '' and not player_exists(username.text.upper()):
cont.place()
elif username.text.upper() == '':
render_text('PLEASE CHOOSE A USERNAME', cont.rect.center[0], cont.rect.center[1], 32)
else:
render_text('USERNAME NOT AVAILABLE', cont.rect.center[0], cont.rect.center[1], 32)
pygame.display.update()
for event in pygame.event.get():
loop.handle_quit(event)
if loop.index in ('login', 'register'):
username.handle_events(event)
if event.type == pygame.MOUSEBUTTONDOWN:
x, y = pygame.mouse.get_pos()
if start.is_active and start.rect.collidepoint(x, y):
loop.index = 'start'
elif htp.is_active and htp.rect.collidepoint(x, y):
loop.index = 'htp'
elif high.is_active and high.rect.collidepoint(x, y):
loop.index = 'high'
elif login.is_active and login.rect.collidepoint(x, y):
loop.index = 'login'
elif register.is_active and register.rect.collidepoint(x, y):
loop.index = 'register'
elif play_as_guest.is_active and play_as_guest.rect.collidepoint(x, y):
game()
loop.running = False
elif cont.is_active and cont.rect.collidepoint(x, y):
if loop.index == 'register':
create_player(username.text)
game(username.text)
loop.running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
if loop.index in ['start', 'high', 'htp']:
loop.index = 'home'
elif loop.index in ['login', 'register']:
loop.index = 'start'
pygame.time.set_timer(pygame.USEREVENT + 1, 3000)
pygame.time.set_timer(pygame.USEREVENT + 2, 1000)
# Main loop
def game(player_name='GUEST'):
loop = GameLoop(player_name)
loop.set_attributes(spells, 'spell')
loop.set_attributes(troops, 'troop')
loop.check_unlocks(troops, 'troop')
loop.check_unlocks(spells, 'spell')
while loop.running:
loop.set_screen()
Troop.update_troops()
batch_place(attacks)
loop.set_level(levels)
if loop.index not in ['options', 'victory']:
render_text(f'LEVEL {Cache.player_level}', sw(6.5), sh(2.5), 25)
loop.planet_health_bar()
loop.energy_bar()
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
if loop.player_name != 'GUEST':
if loop.index == 'options':
loop.player_level += 1
loop.update_unlocks(troops, 'troop')
loop.update_unlocks(spells, 'spell')
loop.save_progress('troop', troops)
loop.save_progress('spell', spells)
loop.save_progress('overall')
p.close()
db.close()
loop.running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
x, y = pygame.mouse.get_pos()
clicked(attacks, x, y)
if loop.index == 'options' and sh(37.5) < y < sh(62.5):
loop.index = ''
if sw(0) < x < sw(50):
loop.index = 'upgrades'
upgrades()
else:
loop.player_level += 1
loop.update_unlocks(troops, 'troop')
loop.update_unlocks(spells, 'spell')
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
loop.index = 'upgrades'
upgrades()
elif event.type == pygame.USEREVENT+1:
Cache.recover_energy()
elif event.type == pygame.USEREVENT+2:
if loop.index == '':
Cache.time_left -= 1
if Cache.time_left < 0:
bg = pygame.image.load("Images\\icons\\bg.png")
screen.blit(bg, (0, 0))
Overlay.overlay("GAME OVER")
pygame.display.update()
pygame.time.wait(5000)
loop.running = False
def upgrades():
upgrade_screen = pygame.display.set_mode((0, 0), pygame.RESIZABLE)
bg = pygame.image.load("Images\\icons\\bg.png")
upgrade_screen.blit(bg, (0, 0))
flag = True
while flag:
x = 20
y = 20
upgrade_screen.blit(bg, (0, 0))
upgrade_screen.blit(cp_icon, cp_rect)
render_text('CP : ', sw(87), sh(7), 32)
render_text(str(int(Cache.cp)).ljust(7), sw(93), sh(7), 32)
for troop in troops:
pygame.draw.circle(screen, (255, 255, 255), [sw(x), sh(y)], 40)
troop.place(sw(x), sh(y), upgrade_screen)
if troop.is_active:
render_text(f'Attack: {troop.damage}'.ljust(12), sw(x)-55, sh(y)+70, 20)
troop.attack_upgrade_icon_rect.center = (sw(x)+55, sh(y)+70)
hover_place(troop.attack_upgrade_icon, troop.attack_upgrade_icon_rect, True, upgrade_screen, troop.is_active)
render_text(str(troop.attack_upgrade_price), sw(x)+55, sh(y)+70, 10)
render_text(f'Defence: {troop.defence}'.ljust(12), sw(x) - 55, sh(y) + 110, 20)
troop.defence_upgrade_icon_rect.center = (sw(x) + 55, sh(y) + 110)
hover_place(troop.defence_upgrade_icon, troop.defence_upgrade_icon_rect, True, upgrade_screen, troop.is_active)
render_text(str(troop.defence_upgrade_price), sw(x) + 55, sh(y) + 110, 10)
render_text(f'Health: {troop.health}'.ljust(12), sw(x) - 55, sh(y) + 150, 20)
troop.health_upgrade_icon_rect.center = (sw(x) + 55, sh(y) + 150)
hover_place(troop.health_upgrade_icon, troop.health_upgrade_icon_rect, True, upgrade_screen, troop.is_active)
render_text(str(troop.health_upgrade_price), sw(x) + 55, sh(y) + 150, 10)
if x == 80:
x = 20
y = 60
else:
x += 20
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
loop.index = ''
flag = False
elif event.type == pygame.MOUSEBUTTONDOWN:
x, y = pygame.mouse.get_pos()
for troop in troops:
if troop.is_active:
if troop.health_upgrade_icon_rect.collidepoint(x, y) and Cache.cp - troop.health_upgrade_price >= 0:
Cache.cp -= troop.health_upgrade_price
troop.upgrade_health()
troop.health_upgrade_price *= 2
elif troop.attack_upgrade_icon_rect.collidepoint(x, y) and Cache.cp - troop.attack_upgrade_price >= 0:
Cache.cp -= troop.attack_upgrade_price
troop.upgrade_damage()
troop.attack_upgrade_price *= 2
elif troop.defence_upgrade_icon_rect.collidepoint(x, y) and Cache.cp - troop.defence_upgrade_price >= 0:
Cache.cp -= troop.defence_upgrade_price
troop.upgrade_defence()
troop.defence_upgrade_price *= 2
"""
GAMES FLOW OF CONTROL
"""
menu()
pygame.quit()