-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui.py
More file actions
370 lines (278 loc) · 11.3 KB
/
ui.py
File metadata and controls
370 lines (278 loc) · 11.3 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
from cmu_graphics import *
from abc import ABC, abstractmethod
import PIL as pl
# Class has abstract methods for debugging purposes
class drawableUI(ABC):
@abstractmethod
def drawAll(self, app):
pass
@abstractmethod
def isInteractable(self):
pass
@abstractmethod
def getSubelements(self):
pass
@abstractmethod
def execute(self):
pass
@abstractmethod
def getTopLeftLoc(self):
pass
@abstractmethod
def getSize(self):
pass
@abstractmethod
def getExitMethod(self):
pass
def removeAll(self,app):
self.remove(app)
for element in self.getSubelements():
element.removeAll(app)
def remove(self, app):
exitMethod = self.getExitMethod()
if self in app.renderedUI:
app.renderedUI.remove(self)
if self in app.interactableUI:
app.interactableUI.remove(self)
if exitMethod != None: app.exitMethodsToSet[exitMethod].remove(self)
def display(self,app):
exitMethod = self.getExitMethod()
if self not in app.renderedUI:
app.renderedUI.append(self)
if self.isInteractable():
app.interactableUI.add(self)
for element in self.getSubelements():
element.display(app)
if exitMethod != None: app.exitMethodsToSet[exitMethod].add(self)
def inBounds(self, x, y):
elementX, elementY = self.getTopLeftLoc()
width,height = self.getSize()
return (elementX <= x <= elementX + width and
elementY <= y <= elementY + height)
class CustomLabel():
def __init__(self, text,size, screenLocation, color = 'black', time = None, bold=False):
self.text = text
self.size = size
self.time = time
self.x,self.y = screenLocation
self.color = color
self.bold = bold
def __repr__(self):
return f'CustomLabel({self.text})'
def __eq__(self,other):
return isinstance(other,CustomLabel) and other.text == self.text and other.time == self.time
def __hash__(self):
return hash(str(self))
def display(self, app):
app.renderedUI.append(self)
if self.time!=None:
app.timedUI[self] = self.time
def removeAll(self,app):
if self in app.renderedUI: app.renderedUI.remove(self)
def setText(self, newText):
self.text = newText
def getTime(self):
return self.time
def getSize(self):
return self.size
def getText(self):
return self.text
def drawAll(self, app):
drawLabel(self.text, self.x,self.y, fill = self.color, size = self.size, bold = self.bold)
class Button(drawableUI):
def __init__(self, gameManager, location, size, color, parentUI, function, text = None, textSize = None):
self.parentUI = parentUI
self.gameManager = gameManager
self.x, self.y = location
self.function = function
self.width, self.height = size
self.color = color
self.interactable = True
self.elements = set()
self.text = text
self.exitMethod = 'click off'
self.textSize = textSize
if self.parentUI != None:
parentUI.elements.add(self)
app.exitMethodsToSet[self.exitMethod].add(self)
def execute(self, app):
if self.function != None:
self.function(app)
self.removeAll(app)
def getExitMethod(self):
return None
def getSubelements(self):
return self.elements
def isInteractable(self):
return self.interactable
def getTopLeftLoc(self):
return (self.x,self.y)
def getSize(self):
return (self.width,self.height)
def drawAll(self, app):
drawRect(self.x, self.y, self.width, self.height, fill = self.color, border = 'black', borderWidth = self.height*0.1)
if self.text != None and self.textSize != None:
drawLabel(self.text, self.x + self.width//2, self.y + self.height//2, size = self.textSize, bold = True)
def setFunction(self, function):
self.function = function
class PopulationButton(Button):
def __init__(self, gameManager, location, size, color, parentUI, tile, text = None, textSize = None):
self.parentUI = parentUI
self.gameManager = gameManager
self.x, self.y = location
self.width, self.height = size
self.color = color
self.interactable = True
self.elements = set()
self.text = text
self.exitMethod = 'click off'
self.textSize = textSize
self.tile = tile
self.label = CustomLabel('Population Placed', 30, (app.width//2, 50), color = 'gold', time = 30, bold = True)
if self.parentUI != None:
parentUI.elements.add(self)
app.exitMethodsToSet[self.exitMethod].add(self)
def execute(self, app):
if self.tile.civilization == app.players[app.currentPlayerID]:
settlement = self.tile.settlement
settlement.placePopulation(self.tile, app, ui = self)
self.removeAll(app)
class SettlementButton(Button):
def __init__(self, gameManager, location, size, color, parentUI, settler, text = None, textSize = None):
self.parentUI = parentUI
self.gameManager = gameManager
self.x, self.y = location
self.width, self.height = size
self.color = color
self.interactable = True
self.elements = set()
self.text = text
self.exitMethod = 'click off'
self.textSize = textSize
self.settler = settler
self.label = CustomLabel('Created Settlement!', 30, (app.width//2, 50), color = 'gold', time = 30, bold = True)
if self.parentUI != None:
parentUI.elements.add(self)
app.exitMethodsToSet[self.exitMethod].add(self)
def execute(self, app):
self.settler.createSettlement()
self.removeAll(app)
class SettlementUI(drawableUI):
def __init__(self, app, settlement, gameManager):
self.gameManager = gameManager
self.elements = set()
self.backWidth = 400
self.backHeight = 300
self.leftBorder = app.width//2 - self.backWidth//2
self.topBorder = app.height//2 - self.backHeight//2
self.exitMethod = 'click off'
self.settlement = settlement
self.productionButton = Button(self.gameManager, (self.leftBorder + self.backWidth//2 - 75, self.topBorder - 50),
(150,50), 'brown', self, self.displayProductionMenu, text="Production Menu", textSize = 15)
self.interactable = False
self.productionMenu = ProductionMenu(app, self.settlement, self.gameManager)
app.exitMethodsToSet[self.exitMethod].add(self)
self.initializeLabelVariables(app)
def displayProductionMenu(self,app):
self.productionMenu.display(app)
def initializeLabelVariables(self, app):
# Label variables
self.resourceLabelXFromEdge = app.width//2
self.resourceLabelYSpacing = 75
def drawLabels(self):
i = 0
for resource in self.settlement.yieldsByType:
drawLabel(f'{resource.getName()}: {self.settlement.yieldsByType[resource]}',
self.resourceLabelXFromEdge, self.topBorder + (i+1)*self.resourceLabelYSpacing, size = 30)
i+=1
if self.settlement.builder.unit!=None:
drawLabel(f'Production Remaining Until Unit Made: {self.settlement.builder.getCostRemaining()}', self.resourceLabelXFromEdge, self.topBorder + (i+1)*self.resourceLabelYSpacing, size = 15)
def getExitMethod(self):
return self.exitMethod
def getSize(self):
return (self.backWidth, self.backHeight)
def getTopLeftLoc(self):
return (self.leftBorder, self.topBorder)
def getSubelements(self):
return self.elements
def isInteractable(self):
return self.interactable
def drawAll(self, app):
self.drawBackground(app)
self.drawLabels()
def drawBackground(self, app):
drawRect(self.leftBorder, app.height//2 - self.backHeight//2, self.backWidth, self.backHeight, fill = 'gray')
def execute(self):
pass
class ProductionOptionButton(Button):
def __init__(self, gameManager, location, size, color, parentUI, unitStr):
self.parentUI = parentUI
self.gameManager = gameManager
self.x, self.y = location
self.width, self.height = size
self.color = color
self.interactable = True
self.elements = set()
self.unitStr = unitStr
self.text = None
self.textSize = None
if self.parentUI != None:
parentUI.elements.add(self)
def __repr__(self):
return 'ProductionOptionButton'
def execute(self, app):
unitClass = app.unitTypes[self.unitStr]
unit = unitClass(self.parentUI.settlement.civilization, app)
self.parentUI.startMakingUnit(app, unit)
class ProductionMenu(drawableUI):
unitIcons = {'warrior' : pl.Image.open('sprites/warrior.png'), 'settler': pl.Image.open('sprites/settler.png'),
'spearman' : pl.Image.open('sprites/spearman.png')}
def __init__(self, app, settlement, gameManager):
self.gameManager = gameManager
self.elements = set()
self.backWidth = 300
self.backHeight = 500
self.leftBorder = app.width//2 - self.backWidth//2
self.topBorder = app.height//2 - self.backHeight//2
self.exitMethod = 'click off'
self.settlement = settlement
self.interactable = False
self.currentUnit = None
self.iconSpacing = 75
self.heading = 50
self.intitializeButtons(app)
app.exitMethodsToSet[self.exitMethod].add(self)
def intitializeButtons(self, app):
i = 0
for name in ProductionMenu.unitIcons:
image = ProductionMenu.unitIcons[name]
button = ProductionOptionButton(self.gameManager, (self.leftBorder + self.backWidth//2 - image.size[0]//2,
self.topBorder + self.heading + self.iconSpacing * i), image.size, None, self, name)
i+=1
def startMakingUnit(self, app, unit):
self.settlement.builder.setUnit(unit)
def drawUnitOptions(self, app):
i = 0
for icon in ProductionMenu.unitIcons:
image = ProductionMenu.unitIcons[icon]
drawImage(ProductionMenu.unitIcons[icon].filename, self.leftBorder + self.backWidth//2 - image.size[0]//2,
self.topBorder + self.heading + self.iconSpacing * i)
i+=1
def getExitMethod(self):
return self.exitMethod
def getSize(self):
return (self.backWidth, self.backHeight)
def getTopLeftLoc(self):
return (self.leftBorder, self.topBorder)
def getSubelements(self):
return self.elements
def isInteractable(self):
return self.interactable
def drawAll(self, app):
self.drawBackground(app)
self.drawUnitOptions(app)
drawLabel('Pick A Unit', self.leftBorder + self.backWidth//2, self.topBorder + 15, size = 20)
def drawBackground(self, app):
drawRect(self.leftBorder, app.height//2 - self.backHeight//2, self.backWidth, self.backHeight, fill = 'gray')
def execute(self):
pass