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 pathgame.py
More file actions
Latest commit
196 lines (175 loc) · 7.31 KB
/
Copy pathgame.py
File metadata and controls
196 lines (175 loc) · 7.31 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
importpygameaspg
fromsettingsimport*
frombuildingimport*
fromunitimport*
importmath
frompygame.spriteimportSprite
classGroups:
"""The group class contains all pygame groups."""
def__init__(self):
# Draw-Collisions.
self.all_sprites=pg.sprite.Group()
self.collision_sprites=pg.sprite.Group()
self.mouse_group=pg.sprite.Group()
# Units.
self.units=pg.sprite.Group()
self.selected_units=pg.sprite.Group()
self.enemy_units=pg.sprite.Group()
self.projectiles=pg.sprite.Group()
# Ghosts.
self.ghost_building=pg.sprite.Group()
self.ghost_plate=pg.sprite.Group()
self.current_run=pg.sprite.Group()
# GUIs.
self.all_buttons=pg.sprite.Group()
self.panels= []
# Maps.
self.map_walls=pg.sprite.Group()
self.map_iron=pg.sprite.Group()
self.map_food=pg.sprite.Group()
# Buildings.
self.buildings=pg.sprite.Group()
self.construction=pg.sprite.Group()
self.cities=pg.sprite.Group()
self.walls=pg.sprite.Group()
self.plates=pg.sprite.Group()
self.towers=pg.sprite.Group()
classResource:
"""The resource class tracks all resource statistics."""
def__init__(self, main):
self.main=main
# Initialize starting resources.
self.food=FOOD_START
self.iron=IRON_START
self.gold=GOLD_START
self.creed=CREED_START
# Initialize starting resource income.
self.food_income=0
self.iron_income=0
self.gold_income=0
defincome(self):
"""Add resources to storage for each resource tick."""
ifself.main.clock.resource_time>=1:
# Distribute resources and reset tick.
self.food+=self.food_income
self.iron+=self.iron_income
self.gold+=self.gold_income
self.main.clock.resource_time=self.main.clock.resource_time-1
# Refresh and print statistics.
print("%s food, %s iron, and %s gold has distributed at %s seconds."
% (self.food_income, self.iron_income, self.gold_income, round(self.main.clock.irl_time, 1)))
self.refresh()
defdeduct(self, cost):
"""Subtract resources based on cost of object constructed."""
self.food-=cost[0]
self.iron-=cost[1]
self.gold-=cost[2]
self.refresh()
defis_valid(self, cost):
"""Check if game has minimum resources to construct object."""
returnself.food>=cost[0] andself.iron>=cost[1] andself.gold>=cost[2]
defrefresh(self):
"""Refresh resource income and GUI panels."""
# Standard update of income.
ifself.main.alive():
self.food_income=FOOD_INCOME# TODO: Configure income based on resource nodes captured.
self.iron_income=IRON_INCOME# TODO: Configure income based on resource nodes captured.
self.gold_income=len(self.main.g.cities)
else:
self.food_income=0
self.iron_income=0
self.gold_income=0
# If no cities, provide enough income to build one city.
ifself.food<CITY_Fandnotself.main.alive():
self.food_income=FOOD_INCOME
ifself.iron<CITY_Iandnotself.main.alive():
self.iron_income=IRON_INCOME
ifself.gold<CITY_Gandnotself.main.alive():
self.gold_income=GOLD_INCOME
# Update GUI panels.
forpanelinself.main.g.panels:
panel.refresh_panel()
classHotKey: # TODO: Github code cleanup.
def__init__(self, game):
self.game=game
self.hotkey_numbers= ['1', '2', '3', '4', '5', '6', '7', '8', '9']
self.hotkey_group= {}
self.new_group()
defnew_group(self):
"""Creates the hotkey groups"""
fornumberinself.hotkey_numbers:
self.hotkey_group[number] =pg.sprite.Group()
defget(self, num):
"""Gets a specific hotkey group."""
returnself.hotkey_group.get(num)
defcheck_events(self, event):
"""Checks to see if we are utilizing hotkeys."""
ifself.game.is_ctrl_pressedandpg.key.name(event.key) inself.hotkey_numbers:
self.add_unit_hotkey(pg.key.name(event.key))
elifpg.key.name(event.key) inself.hotkey_numbers:
self.select_hotkey(pg.key.name(event.key))
defadd_unit_hotkey(self, num):
"""Adds the units into the hotkey group."""
self.get(num).empty()
forunitinself.game.g.selected_units:
iflen(self.get(num)) <MAX_HOTKEY_SELECTION:
pg.sprite.Group.add(self.get(num), unit)
print(str(self.get(num)))
defselect_hotkey(self, num):
"""Selects the units in the hotkey group."""
self.game.deselect_units()
forunitinself.get(num):
unit.select_button() # TODO: Debug button knight class missing.
classRunner(Sprite):
"""This class runs from point A to point B and tried to spawn a building every frame of the way."""
def__init__(self, game):
pg.sprite.Sprite.__init__(self)
self.game=game
# Image Information
self.image=pg.image.load(RUNNER)
self.size=self.image.get_rect().size
self.rect=self.image.get_rect()
# Movement slope information
self.start_x=0
self.start_y=0
self.move_x=0
self.move_y=0
self.move_slope=0
self.move_quadrant=0
self.move_counter=0
# Spawn plate information
self.radius=RUNNER_RADIUS
self.speed=RUNNER_SPEED
self.buffer=RUNNER_BUFFER
self.collided_circle=None
self.collided_ghosts=None
self.is_running=False
defrun(self, start_x, start_y, move_x, move_y):
"""This makes the runner spawn plates."""
self.reset_runner(start_x, start_y, move_x, move_y)
whileself.is_running:
self.set_move_slope()
self.collision_detection()
defset_move_slope(self):
"""This calculates the movement slope and what quadrant our sprite is moving towards."""
# Checks to see if we made it to the move location. Stops within general vicinity (buffer).
ifself.game.arrived_at_location(self):
self.is_running=False
self.game.slope_movement_logic(self)
defcollision_detection(self):
"""We spawn a ghost plate any time the runner is not colliding with a ghost plate or ghost wall."""
self.collided_circle=pg.sprite.spritecollide(self, self.game.g.current_run, False, collided=self.game.circol)\
+pg.sprite.spritecollide(self, self.game.g.ghost_building, False)
ifnotself.collided_circle:
PlateGhost(self.game, self)
defreset_runner(self, start_x, start_y, move_x, move_y):
"""We reset the runner back to ghost-wall position and prepare it for multiple wall running"""
self.is_running=True
self.start_x=start_x
self.start_y=start_y
self.move_x=move_x
self.move_y=move_y
self.rect.centerx=start_x
self.rect.centery=start_y
self.game.g.current_run.empty()
self.move_counter=0