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 pathwindow.py
More file actions
Latest commit
345 lines (283 loc) · 13.5 KB
/
Copy pathwindow.py
File metadata and controls
345 lines (283 loc) · 13.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
importpygameaspg
fromsettingsimport*
fromosimportpath
fromunitimportUnit
importpygame.gfxdraw
importpygame.font
frompygame.colorimportTHECOLORS
frombuildingimport*
importpygame.font
# ================================================ Essential Classes ================================================ #
classMap:
"""The map class sets up and contains the game map."""
def__init__(self):
# Open file and append string contents.
self.name=path.join(path.dirname(__file__), MAPNAME)
self.contents= []
withopen(self.name, 'rt') asfile:
forlineinfile:
self.contents.append(line.strip())
# Count columns as width. Count rows as height.
self.count_width=len(self.contents[0])
self.count_height=len(self.contents)
self.pixel_width=self.count_width*TILESIZE
self.pixel_height=self.count_height*TILESIZE
classCamera:
"""The camera class contains screen location data."""
def__init__(self, main):
# Create camera rects.
self.camera=pg.Rect(0, 0, main.map.pixel_width, main.map.pixel_height)
self.pixel_width=main.map.pixel_width
self.pixel_height=main.map.pixel_height
# Set camera speed and location.
self.speed=CAMERA_SPEED_DEFAULT
self.x=0
self.y=0
defapply(self, entity):
returnentity.rect.move(self.camera.topleft)
defupdate(self, target):
self.x=-target.rect.x+int(SCREEN_WIDTH/2)
self.y=-target.rect.y+int(SCREEN_HEIGHT/2)
self.camera=pg.Rect(self.x, self.y, self.pixel_width, self.pixel_height)
classClock:
"""The clock class contains game time."""
def__init__(self):
# Create clock and time variables.
self.clock=pg.time.Clock()
self.delta_time=0
self.irl_time=0
self.resource_time=0
classMouse(pg.sprite.Sprite):
"""The mouse class handles the selection box."""
def__init__(self, game):
self.groups=game.g.all_sprites, game.g.mouse_group
pg.sprite.Sprite.__init__(self, self.groups)
self.game=game
# Selection box information.
self.is_selecting=False
self.quadrant=0
self.lock_x=0
self.lock_y=0
self.moved_x=0
self.moved_y=0
# Image information.
self.image=pg.Surface(NO_SURFACE)
self.size=self.image.get_rect().size
self.rect=pg.Rect(PIXEL_RECT)
self.alpha_surface=None
self.radius=CITY_MAX_RADIUS
# Unit commander.
self.is_commanding=False
defupdate(self):
"""This fires every single frame."""
ifself.is_selectingandnotself.game.is_ghost_building:
self.build_box()
else:
self.set_pos()
defselect_start(self):
"""Sets variables necessary for selecting units. Opens gate in self.update to call build_box."""
ifnotself.game.is_ghost_building:
self.is_selecting=True
self.lock_x=pg.mouse.get_pos()[0] -self.game.camera.x
self.lock_y=pg.mouse.get_pos()[1] -self.game.camera.y
self.rect.x=self.lock_x
self.rect.y=self.lock_y
defselect_finish(self):
"""Sets the rect, gets collided sprites (units), adds to selected_units, and wipes all box data."""
ifnotself.game.is_ghost_building:
self.is_selecting=False
self.rect=pg.Rect(self.rect.x, self.rect.y, self.image.get_rect().size[0], self.image.get_rect().size[1])
collided=pg.sprite.spritecollide(self, self.game.g.units, False)
self.image=pg.Surface(NO_SURFACE)
self.rect=pg.Rect(PIXEL_RECT)
# Lets units know they are selected. We don't want to select more than the max allowance.
forunitincollided:
iflen(self.game.g.selected_units) >=MAX_UNIT_SELECTION:
break
ifisinstance(unit, Unit):
unit.set_selected()
defbuild_box(self):
"""This builds a transparent selection box. Defines quadrant to call define_box."""
self.moved_x=pg.mouse.get_pos()[0] -self.game.camera.x
self.moved_y=pg.mouse.get_pos()[1] -self.game.camera.y
# Figures out which quadrant we're in.
if (0<self.moved_x-self.lock_x) and (0<self.moved_y-self.lock_y):
self.define_box(4, self.lock_x, self.lock_y, self.moved_x, self.moved_y)
elif (0<self.moved_x-self.lock_x) and (0>self.moved_y-self.lock_y):
self.define_box(1, self.lock_x, self.moved_y, self.moved_x, self.lock_y)
elif (0>self.moved_x-self.lock_x) and (0>self.moved_y-self.lock_y):
self.define_box(2, self.moved_x, self.moved_y, self.lock_x, self.lock_y)
elif (0>self.moved_x-self.lock_x) and (0<self.moved_y-self.lock_y):
self.define_box(3, self.moved_x, self.lock_y, self.lock_x, self.moved_y)
# Makes box transparent.
self.image.set_alpha(BOX_TRANSPARENCY)
defdefine_box(self, quadrant, rect_x, rect_y, base_x, base_y):
"""This creates the selection box. Box rect.x and rect.y always needs to be drawn from the top left corner."""
self.quadrant=quadrant
self.rect.x=rect_x
self.rect.y=rect_y
self.image=pg.Surface((base_x-self.rect.x, base_y-self.rect.y))
self.image.fill(LIGHTGREY)
defset_pos(self):
"""This tracks cursor position at all times not selecting units. Used for refunding building/units."""
self.rect.x=pg.mouse.get_pos()[0] -self.game.camera.x
self.rect.y=pg.mouse.get_pos()[1] -self.game.camera.y
classPlayer(pg.sprite.Sprite):
def__init__(self, game, x, y):
self.groups=game.g.all_sprites
pygame.sprite.Sprite.__init__(self, self.groups)
self.game=game
self.image=pygame.Surface((0, 0))
# self.image.fill(YELLOW)
self.rect=self.image.get_rect()
self.x=x*TILESIZE
self.y=y*TILESIZE
defget_keys(self):
"""This code is fired from get_keys, independent from game class keys."""
keys=pygame.key.get_pressed()
ifkeys[pygame.K_a] andnotkeys[pygame.K_d]:
self.x-=self.game.camera.speed
elifkeys[pygame.K_d] andnotkeys[pygame.K_a]:
self.x+=self.game.camera.speed
ifkeys[pygame.K_w] andnotkeys[pygame.K_s]:
self.y-=self.game.camera.speed
elifkeys[pygame.K_s] andnotkeys[pygame.K_w]:
self.y+=self.game.camera.speed
defupdate(self):
self.get_keys()
self.rect.x=self.x
self.rect.y=self.y
classWall(pg.sprite.Sprite):
"""I don't know what those game.x functions are."""
def__init__(self, game, x, y):
self.groups=game.g.all_sprites, game.g.collision_sprites, game.g.map_walls
pygame.sprite.Sprite.__init__(self, self.groups)
self.game=game
self.image=pygame.Surface((TILESIZE, TILESIZE))
self.image.fill(THECOLORS['cornflowerblue'])
self.size=self.image.get_rect().size
self.rect=self.image.get_rect()
self.x=x# Unknown
self.y=y# Unknown
self.rect.x=x*TILESIZE
self.rect.y=y*TILESIZE
classIron(pg.sprite.Sprite):
def__init__(self, game, x, y):
self.groups=game.g.all_sprites, game.g.collision_sprites, game.g.map_iron
pygame.sprite.Sprite.__init__(self, self.groups)
self.game=game
self.image=pygame.Surface((TILESIZE/2, TILESIZE/2), pg.SRCALPHA)
self.rect=self.image.get_rect()
self.rect.x= (x*TILESIZE+ (TILESIZE/4))
self.rect.y= (y*TILESIZE+ (TILESIZE/4))
defdraw(self):
pygame.gfxdraw.filled_circle(self.image, round(TILESIZE/4), round(TILESIZE/4),
round(TILESIZE/5), THECOLORS['gray'])
# =================================================== Button Class ================================================== #
classButton(pg.sprite.Sprite):
"""The button class is a sprite on the bottom that spawn ghost buildings when clicked."""
def__init__(self, main, selected, deselected, ghost, restricted=True):
pg.sprite.Sprite.__init__(self, main.g.all_buttons)
self.main=main
self.ghost=ghost# String of spawned ghost building
self.restricted=restricted# Button appears if city >= 1
# Calibrate button image.
self.image_deselected=pg.image.load(deselected)
self.image_selected=pg.image.load(selected)
self.image=self.image_deselected
# Calibrate button rects.
self.size=self.image.get_rect().size
self.rect=self.image.get_rect()
self.rect.x=BUTTON_BUFFER# Overwritten by child
self.rect.y=SCREEN_HEIGHT-self.size[1] -BUTTON_BUFFER
defclick(self):
"""Create ghost and select button if clicked."""
ifself.is_clicked():
self.main.destroy_ghost()
self.select_button()
self.spawn_ghost()
defis_clicked(self):
"""Return True if button rect is clicked; button sprite must be appearing."""
ifself.main.is_ctrl_pressedand (self.main.alive() ornotself.restricted):
returnself.rect.collidepoint(pg.mouse.get_pos()[0], pg.mouse.get_pos()[1])
defselect_button(self):
"""Select button and deselect all other buttons."""
forbuttoninself.main.g.all_buttons:
button.image=button.image_deselected
self.image=self.image_selected
defspawn_ghost(self):
"""Create ghost building that follows cursor."""
exec('self.main.g.ghost_building.add('+self.ghost+'(self.main))')
classCityButton(Button):
def__init__(self, main):
super().__init__(main, CITY_BUTTON_SELECTED, CITY_BUTTON_DESELECTED, 'CityGhost', False)
self.rect.x=BUTTON_BUFFER# Button appears 1st-left.
classWallButton(Button):
def__init__(self, main):
super().__init__(main, WALL_BUTTON_SELECTED, WALL_BUTTON_DESELECTED, 'WallGhost')
self.rect.x+=self.main.city_button.rect.x+self.main.city_button.size[0] # Button appears 2nd-left.
classTowerButton(Button):
def__init__(self, main):
super().__init__(main, TOWER_BUTTON_SELECTED, TOWER_BUTTON_DESELECTED, 'TowerGhost')
self.rect.x+=self.main.wall_button.rect.x+self.main.wall_button.size[0] # Button appears 3rd-left.
classKnightButton(Button):
def__init__(self, main):
super().__init__(main, KNIGHT_BUTTON_SELECTED, KNIGHT_BUTTON_DESELECTED, 'KnightGhost')
self.rect.x=SCREEN_WIDTH-self.size[0] -BUTTON_BUFFER# Button appears 1st-right.
# =================================================== Panel Class =================================================== #
classPanel:
"""The panel class is a widget on the top left that displays game data."""
def__init__(self, main, slot):
main.g.panels.append(self)
self.main=main
# Calibrate panel features.
self.background_color=NEARBLACK
self.text_color=WHITE
self.text_font=pygame.font.SysFont(None, 32, True)
# Calibrate panel rects.
self.rect=pygame.Rect(0, 0, PANEL_WIDTH, PANEL_HEIGHT)
self.rect.center= (PANEL_WIDTH/2+PANEL_BUFFER, PANEL_HEIGHT/2+PANEL_BUFFER)
self.rect.y+=PANEL_HEIGHT*slot
self.refresh_panel()
defdraw_panel(self):
"""Draw panel background first then draw message over it."""
self.main.screen.fill(self.background_color, self.rect)
self.main.screen.blit(self.msg_image, self.msg_image_rect)
defrefresh_panel(self):
"""Convert panel message string to drawable image; re-center message rect."""
self.msg_image=self.text_font.render(self.get_message(), True, self.text_color, self.background_color)
self.msg_image_rect=self.msg_image.get_rect()
self.msg_image_rect.center=self.rect.center
defis_clicked(self):
"""Return True if panel rect is clicked; prevents spawning buildings over panel."""
ifself.main.is_ctrl_pressedandself.main.is_ghost_building:
returnself.rect.collidepoint(pg.mouse.get_pos()[0], pg.mouse.get_pos()[1])
defget_message(self):
"""Get the panel message from child class."""
pass
classFoodPanel(Panel):
def__init__(self, main):
self.main=main
super(FoodPanel, self).__init__(main, 0)
defget_message(self):
return'Food: '+str(round(self.main.resource.food)) +' (+'+str(self.main.resource.food_income) +')'
classIronPanel(Panel):
def__init__(self, main):
super(IronPanel, self).__init__(main, 1)
defget_message(self):
return'Iron: '+str(round(self.main.resource.iron)) +' (+'+str(self.main.resource.iron_income) +')'
classGoldPanel(Panel):
def__init__(self, main):
super(GoldPanel, self).__init__(main, 2)
defget_message(self):
return'Gold: '+str(round(self.main.resource.gold)) +' / '+str(WINNING_CONDITION)
classCityPanel(Panel):
def__init__(self, main):
super(CityPanel, self).__init__(main, 3)
defget_message(self):
return'Cities: '+str(len(self.main.g.cities))
classUnitPanel(Panel):
def__init__(self, main):
super(UnitPanel, self).__init__(main, 4)
defget_message(self):
return'Supply: '+str(len(self.main.g.units)) +' / '+str(MAX_UNIT_CAP)