Uh oh!
There was an error while loading. Please reload this page.
forked from RyanKruse/Python-RTS
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
Latest commit
394 lines (352 loc) · 14.5 KB
/
Copy pathmain.py
File metadata and controls
394 lines (352 loc) · 14.5 KB
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
importsys
importpygameaspg
importwindow
importgame
fromosimportpath
frompygame.spriteimportGroup
fromsettingsimport*
fromunitimport*
frompygame.mathimportVector2
classMain:
def__init__(self):
# Initialize game.
pg.init()
pg.display.set_caption(GAME_TITLE) # Program title
self.g=game.Groups()
self.resource=game.Resource(self)
# Initialize window.
self.screen=pg.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
self.map=window.Map()
self.camera=window.Camera(self)
self.clock=window.Clock()
self.mouse=window.Mouse(self)
self.city_button=window.CityButton(self)
self.wall_button=window.WallButton(self)
self.tower_button=window.TowerButton(self)
self.knight_button=window.KnightButton(self)
self.food_panel=window.FoodPanel(self)
self.iron_panel=window.IronPanel(self)
self.gold_panel=window.GoldPanel(self)
self.city_panel=window.CityPanel(self)
self.unit_panel=window.UnitPanel(self)
# Initialize menu buttons & logic
self.is_ctrl_pressed=False
self.is_shift_pressed=False
self.is_alt_pressed=False
self.is_ghost_building=False
self.is_debugging=False
self.mouse1_gate=False
self.pass_length=0
self.random_number=0
# Initialize other code
self.hotkey=game.HotKey(self)
self.runner=game.Runner(self)
defalive(self):
returnlen(self.g.cities) >0
defnew(self):
"""Initialize the map string for a new game. Creates walls and player (which camera follows)."""
forrow, tilesinenumerate(self.map.contents):
forcol, tileinenumerate(tiles):
iftile=='1':
window.Wall(self, col, row)
iftile=='P':
self.player=window.Player(self, col, row)
iftile=='i':
window.Iron(self, col, row)
defrun(self):
whileTrue:
self.events()
self.update()
self.draw()
# ================================================= User Events ================================================ #
defevents(self):
"""Catches all events here and calls appropriate function."""
foreventinpg.event.get():
ifevent.type==pg.KEYDOWN:
self.key_down(event)
elifevent.type==pg.KEYUP:
self.key_up(event)
elifevent.type==pg.MOUSEBUTTONDOWN:
self.mouse_down(event)
elifevent.type==pg.MOUSEBUTTONUP:
self.mouse_up(event)
elifevent.type==pg.MOUSEMOTION:
self.mouse_move(event)
elifevent.type==pg.QUIT:
self.exit_game()
defkey_down(self, event):
# C
ifevent.key==pg.K_c:
EnemyKnight(self)
self.resource.refresh()
# R & CTRL+R
elifevent.key==pg.K_r:
ifself.is_ctrl_pressedandself.is_ghost_building:
self.destroy_ghost()
elifself.g.selected_units:
self.deselect_units()
elifself.is_ctrl_pressed:
self.refund_building()
# SHIFT
elifevent.key==pg.K_LSHIFT:
self.is_shift_pressed=True
self.delete_ghost_plates()
self.camera.speed=CAMERA_SPEED_FAST
# CTRL
elifevent.key==pg.K_LCTRL:
self.hide_ghost(True)
# ALT
elifevent.key==pg.K_LALT:
pass# TODO: This code currently crashes game.
# self.is_alt_pressed = True
# self.random_number = random.randint(1, self.pass_length)
# Quit
elifevent.key==pg.K_ESCAPE:
self.exit_game()
# Q
elifevent.key==pg.K_q:
self.is_debugging=notself.is_debugging
# 1-9
else:
self.hotkey.check_events(event)
defkey_up(self, event):
# SHIFT
ifevent.key==pg.K_LSHIFT:
self.is_shift_pressed=False
self.camera.speed=CAMERA_SPEED_DEFAULT
# CTRL
elifevent.key==pg.K_LCTRL:
self.hide_ghost(False)
# ALT
elifevent.key==pg.K_LALT:
self.is_alt_pressed=False
defmouse_down(self, event):
# Construct building, construct ghost, or select units.
ifevent.button==1:
self.mouse1_gate=True
self.construct_building()
self.construct_ghost()
self.mouse.select_start()
# Destroys ghost or commands units.
elifevent.button==3:
ifself.is_ctrl_pressedandself.is_ghost_building:
self.destroy_ghost()
else:
self.command_units()
defmouse_up(self, event):
# Finishes box selection.
ifevent.button==1:
self.mouse1_gate=False
self.mouse.select_finish()
# Stops commanding
elifevent.button==3:
self.mouse.is_commanding=False
defmouse_move(self, event):
pass
# ================================================ Main Functions =============================================== #
defexit_game(self):
"""Quits game."""
pg.quit()
sys.exit()
defdeselect_units(self):
"""Deselects all units."""
forunitinself.g.selected_units:
unit.set_deselected()
defdestroy_ghost(self):
"""Destroys ghost and deselects all buttons."""
forghostinself.g.ghost_building:
ghost.delete()
forbuttoninself.g.all_buttons:
button.image=button.image_deselected
button.selected=False
self.is_ghost_building=False
self.delete_ghost_plates()
defhide_ghost(self, is_ctrl_pressed):
"""Adds or hides ghost from view."""
self.is_ctrl_pressed=is_ctrl_pressed
ifself.is_ctrl_pressed:
forghostinself.g.ghost_building:
self.g.all_sprites.add(ghost)
self.is_ghost_building=True
else:
forghostinself.g.ghost_building:
self.g.all_sprites.remove(ghost)
self.is_ghost_building=False
self.delete_ghost_plates()
defconstruct_building(self):
"""Construct building if ghost is selected."""
ifself.is_ctrl_pressedandself.is_ghost_building:
gate=True
# Allows construction only if not clicking on button or panel.
forbuttoninself.g.all_buttons:
ifbutton.is_clicked():
gate=False
forpanelinself.g.panels:
ifpanel.is_clicked():
gate=False
ifgate:
forghostinself.g.ghost_building:
ghost.construct_building()
defconstruct_ghost(self):
"""Creates building ghost when button is clicked."""
forbuttoninself.g.all_buttons:
button.click()
defcommand_units(self):
"""Defines move_x and move_y for selected units."""
self.mouse.is_commanding=True
forunitinself.g.selected_units:
unit.set_move_location()
defrefund_building(self):
"""Refund building mouse hovering over."""
mouse_collided=pg.sprite.spritecollide(self.mouse, self.g.buildings, False)
ifmouse_collided:
refund_amount=mouse_collided[0].refund_amount
mouse_collided[0].delete()
self.resource.deduct(refund_amount)
mouse_collided[0].delete()
self.resource.refresh()
defdelete_ghost_plates(self):
"""Delete all ghost plates."""
forplateinself.g.ghost_plate:
pg.sprite.spritecollide(plate, self.g.ghost_plate, True)
defcircol(self, left, right):
"""Detects circle collision."""
ifleft!=right:
distance=Vector2(left.rect.center).distance_to(right.rect.center)
returndistance<left.radius
else:
returnFalse
defarrived_at_location(self, obj):
"""Detects if sprite has arrived at move_x and move_y."""
return (obj.move_x-obj.buffer<=obj.rect.centerx<=obj.move_x+obj.buffer) and \
(obj.move_y-obj.buffer<=obj.rect.centery<=obj.move_y+obj.buffer)
defslope_movement_logic(self, obj):
"""Defines slope and which quadrant sprite is moving towards."""
# Figures out what our slope is. This makes the runner smoother when calculated every loop.
ifnotobj.move_x-obj.rect.centerx==0:
obj.move_slope= (obj.move_y-obj.rect.centery) / (obj.move_x-obj.rect.centerx)
# Checks to see what quadrant we'll move towards.
ifobj.rect.centerx<obj.move_x:
# Quadrant 4
ifobj.rect.centery<obj.move_y:
self.move_finite_slope(obj.speed, obj.move_slope, obj.speed, obj)
# Quadrant 1
else:
self.move_finite_slope(obj.speed, -obj.move_slope, -obj.speed, obj)
elifobj.rect.centerx>obj.move_x:
# Quadrant 3
ifobj.rect.centery<obj.move_y:
self.move_finite_slope(-obj.speed, -obj.move_slope, obj.speed, obj)
# Quadrant 2
else:
self.move_finite_slope(-obj.speed, obj.move_slope, -obj.speed, obj)
else:
# Quadrant 5
ifobj.rect.centery<obj.move_y:
self.move_infinite_slope(obj.move_y, obj.rect.centery, obj.speed, obj)
# Quadrant 6
elifobj.rect.centery>obj.move_y:
self.move_infinite_slope(obj.rect.centery, obj.move_y, -obj.speed, obj)
defmove_finite_slope(self, s1, s2, s3, obj):
"""An algorithm to increase or decrease rect.x and rect.y."""
ifobj.move_counter<1:
obj.rect.x+=s1
obj.move_counter+=s2
else:
obj.rect.y+=s3
obj.move_counter-=1
defmove_infinite_slope(self, y1, y2, s1, obj):
"""An algorithm to increase or decrease rect.y only."""
ify1>y2:
obj.rect.y+=s1
defcollision_bump(self, obj, other):
"""This code is costly and doesn't work well with corner collision."""
ifother.rect.centerx==obj.rect.centerxorother.rect.centery==obj.rect.centery:
# When units bump into each other, bumps diagonally. Prevents units from clumping over each other.
ifother.rect.centerx>obj.rect.centerx:
obj.rect.x-=KNIGHT_SPEED
elifother.rect.centerx<obj.rect.centerx:
obj.rect.x+=KNIGHT_SPEED
ifother.rect.centery>obj.rect.centery:
obj.rect.y-=KNIGHT_SPEED
elifother.rect.centery<obj.rect.centery:
obj.rect.y+=KNIGHT_SPEED
else:
# When units bump into each other, bumps diagonally, horizontally, or vertically.
ifother.rect.centerx<obj.rect.x+obj.size[0]:
obj.rect.x+=KNIGHT_SPEED
ifother.rect.centerx>obj.rect.x:
obj.rect.x-=KNIGHT_SPEED
ifother.rect.centery<obj.rect.y+obj.size[1]:
obj.rect.y+=KNIGHT_SPEED
ifother.rect.centery>obj.rect.y:
obj.rect.y-=KNIGHT_SPEED
# =============================================== Clock & Update =============================================== #
defupdate(self):
"""Updates clock, all sprites, camera, and resources."""
self.update_clock()
self.g.all_sprites.update()
self.camera.update(self.player)
self.resource.income()
defupdate_clock(self):
"""Updates all the time variables in the game."""
self.clock.delta_time=self.clock.clock.tick(FPS) /1000
self.clock.irl_time+=self.clock.delta_time
self.clock.resource_time+=RESOURCE_TICK
# ==================================================== Draw ==================================================== #
defdraw(self):
"""This draws everything onto screen."""
# Draws first --> last
self.screen.fill(BGCOLOR)
self.draw_grid()
self.draw_circles()
self.draw_ghost_plates()
forspriteinself.g.all_sprites:
self.screen.blit(sprite.image, self.camera.apply(sprite))
self.draw_resources()
self.draw_gui()
pg.display.flip()
defdraw_grid(self):
"""Draws the grid locked to map. Tile size multiplied to reduce clutter."""
forxinrange(-self.player.rect[0], SCREEN_WIDTH, int(TILESIZE/2)):
pg.draw.line(self.screen, LIGHTGREY, (x, 0), (x, SCREEN_HEIGHT))
foryinrange(-self.player.rect[1], SCREEN_HEIGHT, int(TILESIZE/2)):
pg.draw.line(self.screen, LIGHTGREY, (0, y), (SCREEN_WIDTH, y))
defdraw_gui(self):
"""This draws the GUI."""
forpanelinself.g.panels:
panel.draw_panel()
ifself.is_ctrl_pressed:
forbuttoninself.g.all_buttons:
ifnotbutton.restrictedorself.alive():
self.screen.blit(button.image, button.rect)
defdraw_circles(self):
"""Draws circular territory and borders."""
ifself.is_debugging:
ifself.is_ctrl_pressed:
forghostinself.g.ghost_building:
ghost.draw_circle()
forconstructioninself.g.construction:
construction.draw_circle()
forcityinself.g.cities:
mouse_collide=pg.sprite.spritecollide(city, self.g.mouse_group, False, collided=self.circol)
ifmouse_collide:
city.draw_territory()
forcityinself.g.cities:
city.draw_circle()
defdraw_resources(self):
"""Draws the map food and iron."""
forironinself.g.map_iron:
iron.draw()
forfoodinself.g.map_food:
food.draw()
defdraw_ghost_plates(self):
"""Draws our wall plates."""
forplateinself.g.ghost_plate:
self.screen.blit(plate.image, self.camera.apply(plate))
forplateinself.g.plates:
self.screen.blit(plate.image, self.camera.apply(plate))
# Creates and runs game class.
game=Main()
game.new()
game.run()