-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCivilizationGameFunctions.py
953 lines (856 loc) · 60.2 KB
/
CivilizationGameFunctions.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
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
from random import randint
import random
import colorsys
from time import sleep
from math import sqrt, ceil
from copy import copy
import CivilizationGameData as data
def showStartPage():
global title, startButton, aboutButton
startButton = Button(data.cWidth / 2 - 62, data.cHeight / 2, "Start", data.startScreenButtonSize, startGame)
aboutButton = Button(data.cWidth / 2 - 66, data.cHeight / 2 + 60, "About", data.startScreenButtonSize, startGame)
title = createText(data.cWidth / 2 - 130, 120, "Kingdom of Qalwar", 2)
def initializeGame():
townHallTop = Building(data.townHallStartingX, data.townHallStartingY, data.TOWN_HALL_TOP, alliance = data.ALLY)
townHallTop.add()
townHallLeft = Building(data.townHallStartingX, data.townHallStartingY + 1, data.TOWN_HALL_LEFT, alliance = data.ALLY)
townHallLeft.add()
townHallRight = Building(data.townHallStartingX + 1, data.townHallStartingY, data.TOWN_HALL_RIGHT, alliance = data.ALLY)
townHallRight.add()
townHallBottom = Building(data.townHallStartingX + 1, data.townHallStartingY + 1, data.TOWN_HALL_BOTTOM, alliance = data.ALLY)
townHallBottom.add()
enemyBaseTop = Building(data.enemyBaseStartingX, data.enemyBaseStartingY, data.ENEMY_BASE_TOP, alliance = data.ENEMY)
enemyBaseTop.add()
enemyBaseLeft = Building(data.enemyBaseStartingX, data.enemyBaseStartingY + 1, data.ENEMY_BASE_LEFT, alliance = data.ENEMY)
enemyBaseLeft.add()
enemyBaseRight = Building(data.enemyBaseStartingX + 1, data.enemyBaseStartingY, data.ENEMY_BASE_RIGHT, alliance = data.ENEMY)
enemyBaseRight.add()
enemyBaseBottom = Building(data.enemyBaseStartingX + 1, data.enemyBaseStartingY + 1, data.ENEMY_BASE_BOTTOM, alliance = data.ENEMY)
enemyBaseBottom.add()
print("Done Initial Setup :D :D :D HAHAHHAH")
def startGame():
global title, startButton, aboutButton, nextSeasonButton, menuButton, settingsButton
for i in range(len(title)):
data.s.delete(title[i])
startButton.destroy()
aboutButton.destroy()
nextSeasonButton = Button(data.cWidth - 200, data. cHeight - 42, "Next Season", 2, passTurn)
menuButton = Button(data.cWidth - 110, 10, "Menu", 2, showMenu)
settingsButton = Button(10, 10, "Settings", 2, showSettings)
updateResources()
updateButtons()
data.seasonIndicator = createText(data.cWidth / 2 - 100, 10, "Season " + str(data.seasonNumber), 2, addColour = [True, data.seasonTextHighlightColour, 50])
data.gameStarted = True
def buyPanelGraphics(x1, y1, x2, y2, text, size):
pixelsArray = []
pixelsArray += [data.s.create_line(x1, y2 + (y1 - y2) / 4, x2, y2 + (y1 - y2) / 4)]
pixelsArray += createText((x1 + x2 - getTextLength(text, size)) / 2, y2 + (y1 - y2) / 4 + 2, text, size)
return pixelsArray
def buyPanelFunctions(buildingType):
closeMenu()
data.placingDownBuilding = True
data.buildingBeingPlaced = buildingType
data.temporaryBuilding = Building(-1000, -1000, data.buildingBeingPlaced)
data.temporaryBuilding.add()
def showSettings():
global doneButton
createNotification(["Much is waiting", "to be done"])
doneButton = Button(data.cWidth / 2 - 46, data.cHeight * (1 / data.notificationScreenBorderY - 1) * data.notificationScreenBorderY - 60, "Done", 2, doneReading)
## data.resolution = 4
## data.reload(sprites)
def showMenu():
global menuButton, nextSeasonButton, menuScroller, backButton
data.menuOpen = True
menuButton.destroy()
nextSeasonButton.destroy()
for i in range(data.numOfMenuPanels):
data.menuPanelObjects[i] = SelectablePanel(data.cWidth - 189, i * 100 + 10, data.cWidth - 17, (i + 1) * 100, buyPanelFunctions, buyPanelGraphics, pressedExtraArgs = (data.constructableBuildings[min(i, 3)],), graphicsExtraArgs = (data.constructableBuildings[min(i, 3)], 1.5), icon = [True, data.buildingTypeImages[data.constructableBuildings[min(i, 3)]], 3])
data.menuFeatures.append(redrawMenu(0))
menuScroller = Scroller(data.cWidth - 10, 10, 5, data.cHeight - 72, data.numOfMenuPanels * 90 + 20, data.scrollerPixelSize, redrawMenu)
backButton = Button(data.cWidth - 104, data. cHeight - 42, "Back", 2, closeMenu)
updateButtons()
data.s.update()
def closeMenu():
global menuButton, nextSeasonButton, menuScroller, backButton
data.menuOpen = False
menuButton = Button(data.cWidth - 110, 10, "Menu", 2, showMenu)
nextSeasonButton = Button(data.cWidth - 200, data. cHeight - 42, "Next Season", 2, passTurn)
for i in range(len(data.menuFeatures)):
for j in range(len(data.menuFeatures[i])):
if isinstance(data.menuFeatures[i][j], list):
for k in range(len(data.menuFeatures[i][j])):
data.s.delete(data.menuFeatures[i][j][k])
else:
data.s.delete(data.menuFeatures[i][j])
for i in range(data.numOfMenuPanels):
data.menuPanelObjects[i].destroy()
menuScroller.destroy()
backButton.destroy()
data.menuIndex = 0
updateButtons()
data.s.update()
def redrawMenu(index):
data.menuIndex = index
for i in range(len(data.menuFeatures)):
for j in range(len(data.menuFeatures[i])):
if isinstance(data.menuFeatures[i][j], list):
for k in range(len(data.menuFeatures[i][j])):
data.s.delete(data.menuFeatures[i][j][k])
else:
data.s.delete(data.menuFeatures[i][j])
data.menuFeatures = []
data.menuFeatures.append([data.s.create_rectangle(data.cWidth - 200, 1, data.cWidth + 1, data.cHeight + 1, fill = "#d7d7d7", outline = "#7f7f7f", width = 3)])
for i in range(data.numOfMenuPanels):
if i * 100 + 10 - data.menuIndex > data.cHeight - 52:
break
if (i + 1) * 100 - data.menuIndex >= -1:
data.menuFeatures.append([data.s.create_rectangle(data.cWidth - 190, i * 100 + 10 - data.menuIndex, data.cWidth - 17, (i + 1) * 100 - data.menuIndex, fill = "#d7d7d7", outline = "#7f7f7f", width = 3)])
data.menuPanelObjects[i].delete()
data.menuPanelObjects[i].displayPanel(data.cWidth - 189, i * 100 + 11 - data.menuIndex, data.cWidth - 18, (i + 1) * 100 - data.menuIndex - 1)
data.menuFeatures.append([data.s.create_rectangle(data.cWidth - 200, data.cHeight - 52, data.cWidth, data.cHeight + 1, fill = "#d7d7d7", outline = "#7f7f7f", width = 3)])
updateButtons()
def placeDownBuilding():
data.placingDownBuilding = False
if data.temporaryBuilding != 0:
#Do you have enough resources? (boolean)
enoughResources = removeResource(data.buildingCosts[data.temporaryBuilding.type][0], data.buildingCosts[data.temporaryBuilding.type][1])
buildable = enoughResources
if buildable == True:
newBuilding = Building(data.temporaryBuilding.x, data.temporaryBuilding.y, data.temporaryBuilding.type, alliance = data.ALLY)
data.temporaryBuilding.deleteBuilding()
data.temporaryBuilding.destroy()
newBuilding.add()
data.temporaryBuilding = 0
newBuildingType = Building.buildingTypes[newBuilding.number]
data.economy[data.buildingCosts[data.temporaryBuilding.type][2]] += data.buildingCosts[data.temporaryBuilding.type][3]
def doneReading():
global doneButton
deleteNotification()
doneButton.destroy()
def passTurn():
global doneButton
data.seasonNumber += 1
for i in range(len(data.seasonIndicator)):
data.s.delete(data.seasonIndicator[i])
data.seasonIndicator = createText(data.cWidth / 2 - 100, 10, "Season " + str(data.seasonNumber), 2, addColour = [True, data.seasonTextHighlightColour, 50])
addResource([data.QALS, data.WOOD, data.GOLD, data.MANA], [data.qalsEconomy, data.woodEconomy, data.goldEconomy, data.manaEconomy])
createNotification(["You collected", str(data.qalsEconomy) + " " + data.QALS, str(data.woodEconomy) + " " + data.WOOD, str(data.goldEconomy) + " " + data.GOLD, str(data.manaEconomy) + " " + data.MANA])
doneButton = Button(data.cWidth / 2 - 46, data.cHeight * (1 / data.notificationScreenBorderY - 1) * data.notificationScreenBorderY - 60, "Done", 2, doneReading)
def tutorial():
##
##
##
pass
def drawBuildingIcon(x, y, building, size):
pixels = []
pixels.append(makeBitmap(x, y, size, data.buildingTypeImages[building]))
return pixels
def getLandPolygonXYLength():
polygonLandXLength = int(((data.tileSize * data.xTiles) * 2 ** 0.5)/1)
polygonLandYLength = int(((data.tileSize * data.yTiles) * 2 ** 0.5)/2)
return polygonLandXLength, polygonLandYLength
def getTileXYLength():
tileXLength = ((data.tileSize) * 2 ** 0.5)/1
tileYLength = ((data.tileSize) * 2 ** 0.5)/2
return tileXLength, tileYLength
def fixPan():
#TODO
pass
def getRandomColour():
#random.random() is a random number between 0.0 and 1.0
h = random.random() #between 0.0 and 1.0
s = 0.9 + random.random()/10 #between 0.9 and 1.0
l = 0.4 + random.random()/5.0 #between 0.4 and 0.6
r,g,b = [int(256*i) for i in colorsys.hls_to_rgb(h,l,s)]
return "#%02x%02x%02x" % (r, g, b)
def keyPressDetector(event):
#To update
pass
def keyReleaseDetector(event):
k = event.keysym
def mousePressedDetector(event):
alreadyPressedSomething = False
data.clickedXMouse = event.x
data.clickedYMouse = event.y
data.previousCurrentX = data.currentX
data.previousCurrentY = data.currentY
for i in range(len(Button.buttonBounds)):
if alreadyPressedSomething == True:
break
try: #This try except is for when a button aside from the one clicked is deleted within its function.
if Button.buttonBounds[i][0] <= data.clickedXMouse <= Button.buttonBounds[i][2] and Button.buttonBounds[i][1] <= data.clickedYMouse <= Button.buttonBounds[i][3]:
data.clickedButton = True
Button.buttonFunctions[i]() #This runs the assigned function or procedure call
alreadyPressedSomething = True
except:
pass
for i in range(len(Scroller.scrollerBounds)):
if alreadyPressedSomething == True:
break
if Scroller.scrollerBounds[i][0] <= data.clickedXMouse <= Scroller.scrollerBounds[i][2] and Scroller.scrollerBounds[i][1] <= data.clickedYMouse <= Scroller.scrollerBounds[i][3]:
data.clickedScroller = True
Scroller.scrollerObject[i].clickedScroller = True
Scroller.scrollerObject[i].scrolledOriginalPercentage = Scroller.scrollerObject[i].scrolledPercentage
Scroller.scrollerObject[i].clickedY = event.y - Scroller.scrollerObject[i].y
alreadyPressedSomething = True
for i in range(len(SelectablePanel.panelBounds)):
if alreadyPressedSomething == True:
break
try: #Same reason as button's -- See above.
if SelectablePanel.panelBounds[i][0] <= data.clickedXMouse <= SelectablePanel.panelBounds[i][2] and SelectablePanel.panelBounds[i][1] <= data.clickedYMouse <= SelectablePanel.panelBounds[i][3]:
SelectablePanel.panelFunctions[i](*SelectablePanel.panelObject[i].pressedExtraArgs) #Run whatever it is that is assigned
alreadyPressedSomething = True
except:
pass
def mouseMotionDetector(event):
for i in range(len(Button.buttonObject)):
previousState = Button.buttonObject[i].hovered
try:
if Button.buttonBounds[i][0] <= event.x <= Button.buttonBounds[i][2] and Button.buttonBounds[i][1] <= event.y <= Button.buttonBounds[i][3]:
Button.buttonObject[i].hovered = True
else:
Button.buttonObject[i].hovered = False
if previousState != Button.buttonObject[i].hovered:
Button.buttonObject[i].delete()
Button.buttonObject[i].displayButton()
except:
pass
if data.placingDownBuilding == True:
tileHoverX, tileHoverY = getTile(event.x, event.y)
if data.gameStarted == True and 0 <= tileHoverX < data.xTiles and 0 <= tileHoverY < data.yTiles and data.menuOpen == False:
data.temporaryBuilding.x = tileHoverX
data.temporaryBuilding.y = tileHoverY
data.temporaryBuilding.deleteBuilding()
data.temporaryBuilding.destroy()
data.temporaryBuilding.add()
updateScreen()
data.highlightedTile = [tileHoverX, tileHoverY]
highlightSquare(data.highlightedTile[0], data.highlightedTile[1])
def mouseDragDetector(event):
data.mouseDragged = True
if data.gameStarted == True and data.clickedButton == False and data.menuOpen == False and data.notificationOpen == False:
rawCurrentX = data.previousCurrentX + data.clickedXMouse - event.x - data.panSlipX
rawCurrentY = data.previousCurrentY + data.clickedYMouse - event.y - data.panSlipY
polygonLandXLength, polygonLandYLength = getLandPolygonXYLength()
fixPan()
data.currentX = rawCurrentX
data.currentY = rawCurrentY
if data.clickedScroller == True and data.scrollerUpdated == True:
data.scrollerUpdated = False
for i in range(len(Scroller.scrollerObject)):
if Scroller.scrollerObject[i].clickedScroller == True:
scroller = Scroller.scrollerObject[i]
break
scroller.draggedY = event.y - scroller.y - scroller.clickedY
scroller.scrolledPercentage = scroller.scrolledOriginalPercentage + float(scroller.draggedY) / float(scroller.scrollableLength) * 100
if scroller.scrolledPercentage < 0:
scroller.scrolledPercentage = 0
if scroller.scrolledPercentage > 100:
scroller.scrolledPercentage = 100
scroller.deleteScroller()
scroller.displayScroller()
data.s.update()
data.scrollerUpdated = True
def mouseReleaseDetector(event):
global menuScroller
currentXLess = False
currentYLess = False
currentXMore = False
currentYMore = False
polygonLandXLength, polygonLandYLength = getLandPolygonXYLength()
if data.currentX < 0:
currentXLess = True
elif data.currentX > polygonLandXLength - data.cWidth:
currentXMore = True
if data.currentY < 0:
currentYLess = True
if data.currentY > polygonLandYLength - data.cHeight:
currentYMore = True
while currentXLess == True or currentYLess == True or currentXMore == True or currentYMore == True:
break
if data.gameStarted == True and data.mouseDragged == False and data.clickedButton == False and data.notificationOpen == False:
if data.menuOpen == True and event.x < data.cWidth - 199 or data.menuOpen == False:
tileClickedX, tileClickedY = getTile(data.clickedXMouse, data.clickedYMouse)
if 0 <= tileClickedX < data.xTiles and 0 <= tileClickedY < data.yTiles:
if data.highlightedTile != [tileClickedX, tileClickedY]:
highlightSquare(tileClickedX, tileClickedY)
menuOpenOriginal = data.menuOpen
data.menuOpen = False
updateScreen()
if menuOpenOriginal == True:
redrawMenu(data.menuIndex)
menuScroller.deleteScroller()
menuScroller.displayScroller()
data.menuOpen = menuOpenOriginal
else:
data.highlightedTile = [-1000, -1000]
if data.mouseDragged == False and data.clickedButton == False:
placeDownBuilding()
data.mouseDragged = False
data.clickedButton = False
data.clickedScroller = False
def mouseWheelHandler(event):
if data.notificationOpen == False:
oldPolygonLandXLength, oldPolygonLandYLength = getLandPolygonXYLength()
if event.num == 5 or event.delta == -120:
data.tileSize *= 0.90
if event.num == 4 or event.delta == 120:
data.tileSize *= 1.11
if data.tileSize > data.maxTileSize:
data.tileSize = data.maxTileSize
elif data.tileSize < data.minTileSize:
data.tileSize = data.minTileSize
newPolygonLandXLength, newPolygonLandYLength = getLandPolygonXYLength()
data.currentX = (data.currentX + data.cWidth / 2) / oldPolygonLandXLength * newPolygonLandXLength - data.cWidth / 2
data.currentY = (data.currentY + data.cHeight / 2) / oldPolygonLandYLength * newPolygonLandYLength - data.cHeight / 2
def addResource(resources, amounts):
for i in range(len(resources)):
data.resourceAmounts[resources[i]] += amounts[i]
if data.resourceAmounts[resources[i]] > data.resourceMaximum[resources[i]]:
data.resourceAmounts[resources[i]] = data.resourceMaximum[resources[i]]
updateResources()
def removeResource(resources, amounts):
for i in range(len(resources)):
if data.resourceAmounts[resources[i]] - amounts[i] < 0:
return False
for i in range(len(resources)):
data.resourceAmounts[resources[i]] -= amounts[i]
updateResources()
return True
def highlightSquare(x, y):
polygonLandXLength, polygonLandYLength = getLandPolygonXYLength()
tileXLength, tileYLength = getTileXYLength()
data.s.delete(data.highlightedTileObject)
data.highlightedTileObject = 0
data.highlightedTile = [x, y]
landShapeTopX = -data.currentX + polygonLandXLength / 2
landShapeTopY = -data.currentY #Top boundary
highlightX1 = landShapeTopX + ((x - y) / 2.0) * tileXLength #x1 y1 is the top corner
highlightY1 = landShapeTopY + ((x + y) / 2.0) * tileYLength
#Check if inside screen, else don't draw it (makes game faster)
if highlightX1 > -tileXLength * (data.loadBuffer + 0.5) and highlightX1 < data.cWidth + tileXLength * (data.loadBuffer + 0.5) and highlightY1 > -tileYLength * (data.loadBuffer + 1) and highlightY1 < data.cHeight + tileYLength * data.loadBuffer:
highlightX2 = highlightX1 + tileXLength / 2
highlightY2 = highlightY1 + tileYLength / 2
highlightX3 = highlightX1
highlightY3 = highlightY2 + tileYLength / 2
highlightX4 = highlightX1 - tileXLength / 2
highlightY4 = highlightY2
data.highlightedTileObject = data.s.create_polygon(highlightX1, highlightY1, highlightX2, highlightY2, highlightX3, highlightY3, highlightX4, highlightY4, width = 0, fill = "yellow", stipple = "gray50")
def getTile(mouseX, mouseY):
polygonLandXLength, polygonLandYLength = getLandPolygonXYLength()
tileXLength, tileYLength = getTileXYLength()
landClickedX = mouseX + data.currentX
landClickedY = mouseY + data.currentY
xB = landClickedY + landClickedX / 2 - polygonLandYLength / 2
yB = -(landClickedY - landClickedX / 2 - polygonLandYLength / 2)
tileOnX = int(xB / tileYLength)
tileOnY = data.yTiles - int(yB / tileYLength) - 1
return tileOnX, tileOnY
def getTileSideNumbers(xPlusValue, yPlusValue):
if xPlusValue == 0 and yPlusValue == -1:
return 0, 1
elif xPlusValue == -1 and yPlusValue == 0:
return 0, 3
elif xPlusValue == 0 and yPlusValue == 1:
return 2, 3
elif xPlusValue == 1 and yPlusValue == 0:
return 1, 2
def updateLand():
data.s.delete(data.landPolygon)
data.s.delete(data.dirtLeft, data.dirtRight)
for i in range(len(data.allianceOutlineObjects)):
data.s.delete(data.allianceOutlineObjects[i])
polygonLandXLength, polygonLandYLength = getLandPolygonXYLength()
tileXLength, tileYLength = getTileXYLength()
landShapeX1 = -data.currentX #Left boundary
landShapeY1 = polygonLandYLength / 2 - data.currentY #Middle of shape
landShapeX2 = landShapeX1 + polygonLandXLength / 2
landShapeY2 = -data.currentY #Top boundary
landShapeX3 = landShapeX1 + polygonLandXLength
landShapeY3 = landShapeY1 #Right boundary
landShapeX4 = landShapeX2 #Bottom boundary
landShapeY4 = landShapeY2 + polygonLandYLength
allianceOutlineLineNumber = 0
for alliance in [data.ENEMY, data.NEUTRAL, data.ALLY]:
#print(alliance)
for i in range(len(data.allianceDictionary[alliance])):
allianceTileGridX = data.allianceDictionary[alliance][i][0]
allianceTileGridY = data.allianceDictionary[alliance][i][1]
data.s.delete(data.allianceHighlightObjects[i])
allianceHighlightX1 = landShapeX2 + ((allianceTileGridX - allianceTileGridY) / 2.0) * tileXLength #x1 y1 is the top corner of
allianceHighlightY1 = landShapeY2 + ((allianceTileGridX + allianceTileGridY) / 2.0) * tileYLength #the diamond shaped tile.
allianceHighlightX2 = allianceHighlightX1 + tileXLength / 2
allianceHighlightY2 = allianceHighlightY1 + tileYLength / 2
allianceHighlightX3 = allianceHighlightX1
allianceHighlightY3 = allianceHighlightY2 + tileYLength / 2
allianceHighlightX4 = allianceHighlightX1 - tileXLength / 2
allianceHighlightY4 = allianceHighlightY2
allianceTileCornersX = [allianceHighlightX1, allianceHighlightX2, allianceHighlightX3, allianceHighlightX4]
allianceTileCornersY = [allianceHighlightY1, allianceHighlightY2, allianceHighlightY3, allianceHighlightY4]
for xPlus in [-1, 0, 1]:
for yPlus in [-1, 0, 1]:
if xPlus == 0 and yPlus != 0 or yPlus == 0 and xPlus != 0:
adjacentTileGridX = allianceTileGridX + xPlus
adjacentTileGridY = allianceTileGridY + yPlus
adjacentTileCoordinates = [adjacentTileGridX, adjacentTileGridY]
if adjacentTileCoordinates not in data.allianceDictionary[alliance]:
corner1, corner2 = getTileSideNumbers(xPlus, yPlus)
data.allianceOutlineObjects[allianceOutlineLineNumber] = data.s.create_line(allianceTileCornersX[corner1], allianceTileCornersY[corner1], allianceTileCornersX[corner2], allianceTileCornersY[corner2], fill = data.allianceColours[alliance][1], width = 2)
allianceOutlineLineNumber += 1
data.allianceHighlightObjects[i] = data.s.create_polygon(allianceHighlightX1, allianceHighlightY1, allianceHighlightX2, allianceHighlightY2, allianceHighlightX3, allianceHighlightY3, allianceHighlightX4, allianceHighlightY4, width = 0, stipple = "gray50", fill = data.allianceColours[alliance][0])
#print("hi")
if alliance == data.ENEMY:
data.s.create_rectangle(5 * allianceTileGridX, 5 * allianceTileGridY, 5 * allianceTileGridX + 5, 5 * allianceTileGridY + 5, fill = data.allianceColours[data.ENEMY][0])
## print(allianceHighlightX1, allianceHighlightY1, allianceHighlightX2, allianceHighlightY2, allianceHighlightX3, allianceHighlightY3, allianceHighlightX4, allianceHighlightY4, data.allianceColours[alliance][0])
data.s.tag_lower(data.allianceHighlightObjects[i])
data.landPolygon = data.s.create_polygon(landShapeX1, landShapeY1, landShapeX2, landShapeY2, landShapeX3, landShapeY3, landShapeX4, landShapeY4, fill = data.landColour, outline = data.landOutlineColour, width = 1)
data.s.tag_lower(data.landPolygon)
data.dirtLeft = data.s.create_polygon(landShapeX1, landShapeY1, landShapeX4, landShapeY4, landShapeX4, landShapeY4 + tileYLength * data.dirtThickness, landShapeX1, landShapeY1 + tileYLength * data.dirtThickness, fill = data.dirtColour, outline = data.dirtOutlineColour, width = 1)
data.dirtRight = data.s.create_polygon(landShapeX3, landShapeY3, landShapeX4, landShapeY4, landShapeX4, landShapeY4 + tileYLength * data.dirtThickness, landShapeX3, landShapeY3 + tileYLength * data.dirtThickness, fill = data.dirtColour, outline = data.dirtOutlineColour, width = 1)
data.s.tag_lower(data.dirtLeft)
data.s.tag_lower(data.dirtRight)
class Building(): #Building is used for the various types of buildings, including your own townhall, opponents base, wall, etc.
buildingObject = [] #Stores the building class instance itself
buildingImages = [] #Stores each pixel of the building
buildingTypes = [] #What type of building is it? Residence, townhall, tower?
buildingsX = [] #X value on the grid
buildingsY = [] #Y value on the grid
def __init__(self, gridX, gridY, buildingType, alliance = data.NEUTRAL):
self.x = gridX #Set x value
self.y = gridY #Set y value
self.type = buildingType #Set building type
self.number = len(Building.buildingObject) #Index number of the building
self.alliance = alliance #Set alliance (ally, enemy, or neutral)
print(self.alliance)
def add(self):
sumOfCoordinates = self.x + self.y #Why use sum of coordinates?
for i in range(len(Building.buildingObject)): #In an isometric game the furthest items are drawn first
if sumOfCoordinates >= Building.buildingsX[i] + Building.buildingsY[i]: #The sum of the coordinates on the grid are coincidentally a good way of sorting them
self.number = i #The smaller the sum the furthest away, the bigger the sum the closer it is
print(i)
print("sumOfCoordinates: " + str(sumOfCoordinates), str(self.x), str(self.y))
print("Next Biggest Coordinate Sum: " + str(Building.buildingsX[i] + Building.buildingsY[i]))
break #i.e. furthest one is (0,0) and closest one is (n-1,n-1) for some n tiles on a square grid
Building.buildingObject.insert(self.number, self) #This part is just inserting everything into the list
Building.buildingImages.insert(self.number, []) #Should not be append because you want the buildings to be in a specific order
Building.buildingTypes.insert(self.number, self.type)
Building.buildingsX.insert(self.number, self.x)
Building.buildingsY.insert(self.number, self.y)
surroundingTiles = [[self.x - 1, self.y - 1], [self.x - 1, self.y], [self.x - 1, self.y + 1], [self.x, self.y - 1], [self.x, self.y], [self.x, self.y + 1], [self.x + 1, self.y - 1], [self.x + 1, self.y], [self.x + 1, self.y + 1]]
unrepetitiveSurroundingTiles = [adjacentTileCoordinate for adjacentTileCoordinate in surroundingTiles if adjacentTileCoordinate not in data.allianceDictionary[self.alliance]]
data.allianceDictionary[self.alliance] += unrepetitiveSurroundingTiles
for i in range(len(unrepetitiveSurroundingTiles)):
data.allianceHighlightObjects.append([0])
print(data.allianceDictionary[self.alliance])
print("X:", Building.buildingsX)
print("Y:", Building.buildingsY)
for i in range(self.number + 1, len(Building.buildingObject)): #After inserting, shift the index number of each following building
Building.buildingObject[i].number += 1
def deleteBuilding(self):
for i in range(len(Building.buildingObject)):
for j in range(len(Building.buildingImages[i])):
data.s.delete(Building.buildingImages[i][j])
def destroy(self):
del Building.buildingObject[self.number] #Delete everything
del Building.buildingTypes[self.number]
del Building.buildingImages[self.number]
del Building.buildingsX[self.number]
del Building.buildingsY[self.number]
def updateBuildings():
for i in range(len(Building.buildingObject)):
for j in range(len(Building.buildingImages[i])):
data.s.delete(Building.buildingImages[i][j])
polygonLandXLength, polygonLandYLength = getLandPolygonXYLength()
tileXLength, tileYLength = getTileXYLength()
landShapeTopX = -data.currentX + polygonLandXLength / 2
landShapeTopY = -data.currentY
for i in range(len(Building.buildingObject)):
x = Building.buildingsX[i]
y = Building.buildingsY[i]
buildingX1 = landShapeTopX + ((x - y) / 2.0) * tileXLength # Top corner of
buildingY1 = landShapeTopY + ((x + y) / 2.0) * tileYLength # quadrilateral
if buildingX1 > -tileXLength * data.loadBuffer and buildingX1 < data.cWidth + tileXLength * data.loadBuffer and buildingY1 > -tileYLength * data.loadBuffer and buildingY1 < data.cHeight + tileYLength * data.loadBuffer:
buildingX2 = buildingX1 + tileXLength / 2
buildingY2 = buildingY1 + tileYLength / 2
buildingX3 = buildingX1
buildingY3 = buildingY2 + tileYLength / 2
buildingX4 = buildingX1 - tileXLength / 2
buildingY4 = buildingY2
bitmapImage = data.buildingTypeImages[Building.buildingTypes[i]]
squareSize = tileXLength/len(bitmapImage[0])
Building.buildingImages[i] = makeBitmap(buildingX4, buildingY3 - squareSize*len(bitmapImage), squareSize, bitmapImage, toBack = True)
def updateButtons():
for i in range(len(Button.buttonObject)):
Button.buttonObject[i].delete()
Button.buttonObject[i].displayButton()
def updateResources():
for i in range(len(data.resourceObjects)):
for j in range(len(data.resourceObjects[i])):
data.s.delete(data.resourceObjects[i][j])
data.resourceObjects = []
for i in range(len(data.resourceTypes) - 1, -1, -1):
data.resourceObjects.append([data.s.create_rectangle(10, data.cHeight - (len(data.resourceTypes) - i) * 30, 10 + data.resourceIndicatorLength, data.cHeight - 10 - (len(data.resourceTypes) - 1 - i) * 30)])
resource = data.resourceTypes[i]
ratioOfResourceToMaximum = float(data.resourceAmounts[resource]) / data.resourceMaximum[resource]
#Top Left x1 y1, Bottom Right x2 y2
data.resourceObjects.append([data.s.create_rectangle(10, data.cHeight - (len(data.resourceTypes) - i) * 30, 10 + data.resourceIndicatorLength * ratioOfResourceToMaximum, data.cHeight - 10 - (len(data.resourceTypes) - 1 - i) * 30, fill = data.resourceColours[resource])])
data.resourceObjects.append(makeBitmap(8, data.cHeight - 3 - (len(data.resourceTypes) - i) * 30, 2, data.resourceIcons[resource]))
data.resourceObjects.append(createText(40, data.cHeight - (len(data.resourceTypes) - i) * 30 + 4, str(data.resourceAmounts[resource]), data.resourceTextSize, addColour = [True, data.resourceColours[resource], 80]))
def updateScreen():
if data.menuOpen == False and data.notificationOpen == False:
highlightSquare(data.highlightedTile[0], data.highlightedTile[1])
updateBuildings()
updateLand()
def createNotification(text):
data.notificationOpen = True
data.notificationPage.append([data.s.create_rectangle(0, 0, data.cWidth + 2, data.cHeight + 2, fill = "black", width = 0, stipple = "gray75")])
data.s.update()
data.notificationPage.append(makeBitmap(int(data.cWidth * data.notificationScreenBorderX), int(data.cHeight * data.notificationScreenBorderY), data.notificationPixelSize, data.paperPieces[data.PAPER_TOP_LEFT]))
data.notificationPage.append(makeBitmap(int(data.cWidth * data.notificationScreenBorderX), int((1 / data.notificationScreenBorderY - 1) * data.cHeight * data.notificationScreenBorderY) - len(data.paperPieces[data.PAPER_BOTTOM_LEFT]) * data.notificationPixelSize, data.notificationPixelSize, data.paperPieces[data.PAPER_BOTTOM_LEFT]))
data.notificationPage.append(makeBitmap(int((1 / data.notificationScreenBorderX - 1) * data.cWidth * data.notificationScreenBorderX) - len(data.paperPieces[data.PAPER_TOP_RIGHT][0]) * data.notificationPixelSize, int(data.cHeight * data.notificationScreenBorderY), data.notificationPixelSize, data.paperPieces[data.PAPER_TOP_RIGHT]))
data.notificationPage.append(makeBitmap(int((1 / data.notificationScreenBorderX - 1) * data.cWidth * data.notificationScreenBorderX) - len(data.paperPieces[data.PAPER_BOTTOM_RIGHT][0]) * data.notificationPixelSize, int((1 / data.notificationScreenBorderY - 1) * data.cHeight * data.notificationScreenBorderY) - len(data.paperPieces[data.PAPER_BOTTOM_RIGHT]) * data.notificationPixelSize, data.notificationPixelSize, data.paperPieces[data.PAPER_BOTTOM_RIGHT]))
#Top Edge
for i in range(int(data.cWidth * data.notificationScreenBorderX) + len(data.paperPieces[data.PAPER_TOP_LEFT][0]) * data.notificationPixelSize, int((1 / data.notificationScreenBorderX - 1) * data.cWidth * data.notificationScreenBorderX) - len(data.paperPieces[data.PAPER_TOP_RIGHT][0]) * data.notificationPixelSize - len(data.paperPieces[data.PAPER_UP][0]) * data.notificationPixelSize, len(data.paperPieces[data.PAPER_UP][0]) * data.notificationPixelSize):
data.notificationPage.append(makeBitmap(i, int(data.cHeight * data.notificationScreenBorderY), data.notificationPixelSize, data.paperPieces[data.PAPER_UP]))
data.notificationPage.append(makeBitmap(int((1 / data.notificationScreenBorderX - 1) * data.cWidth * data.notificationScreenBorderX) - len(data.paperPieces[data.PAPER_TOP_RIGHT][0]) * data.notificationPixelSize - len(data.paperPieces[data.PAPER_UP][0]) * data.notificationPixelSize, int(data.cHeight * data.notificationScreenBorderY), data.notificationPixelSize, data.paperPieces[data.PAPER_UP]))
#Left Edge
for i in range(int(data.cHeight * data.notificationScreenBorderY) + len(data.paperPieces[data.PAPER_TOP_LEFT]) * data.notificationPixelSize, int((1 / data.notificationScreenBorderY - 1) * data.cHeight * data.notificationScreenBorderY) - len(data.paperPieces[data.PAPER_BOTTOM_LEFT]) * data.notificationPixelSize - len(data.paperPieces[data.PAPER_LEFT]) * data.notificationPixelSize, len(data.paperPieces[data.PAPER_LEFT]) * data.notificationPixelSize):
data.notificationPage.append(makeBitmap(int(data.cWidth * data.notificationScreenBorderX), i, data.notificationPixelSize, data.paperPieces[data.PAPER_LEFT]))
data.notificationPage.append(makeBitmap(int(data.cWidth * data.notificationScreenBorderX), int((1 / data.notificationScreenBorderY - 1) * data.cHeight * data.notificationScreenBorderY) - len(data.paperPieces[data.PAPER_BOTTOM_LEFT]) * data.notificationPixelSize - len(data.paperPieces[data.PAPER_LEFT]) * data.notificationPixelSize, data.notificationPixelSize, data.paperPieces[data.PAPER_LEFT]))
#Right Edge
for i in range(int(data.cHeight * data.notificationScreenBorderY) + len(data.paperPieces[data.PAPER_TOP_RIGHT]) * data.notificationPixelSize, int((1 / data.notificationScreenBorderY - 1) * data.cHeight * data.notificationScreenBorderY) - len(data.paperPieces[data.PAPER_BOTTOM_RIGHT]) * data.notificationPixelSize - len(data.paperPieces[data.PAPER_RIGHT]) * data.notificationPixelSize, len(data.paperPieces[data.PAPER_RIGHT]) * data.notificationPixelSize):
data.notificationPage.append(makeBitmap(int((1 / data.notificationScreenBorderX - 1) * data.cWidth * data.notificationScreenBorderX) - len(data.paperPieces[data.PAPER_RIGHT][0]) * data.notificationPixelSize, i, data.notificationPixelSize, data.paperPieces[data.PAPER_RIGHT]))
data.notificationPage.append(makeBitmap(int((1 / data.notificationScreenBorderX - 1) * data.cWidth * data.notificationScreenBorderX) - len(data.paperPieces[data.PAPER_RIGHT][0]) * data.notificationPixelSize, int((1 / data.notificationScreenBorderY - 1) * data.cHeight * data.notificationScreenBorderY) - len(data.paperPieces[data.PAPER_BOTTOM_RIGHT]) * data.notificationPixelSize - len(data.paperPieces[data.PAPER_RIGHT]) * data.notificationPixelSize, data.notificationPixelSize, data.paperPieces[data.PAPER_RIGHT]))
#Bottom Edge
for i in range(int(data.cWidth * data.notificationScreenBorderX) + len(data.paperPieces[data.PAPER_BOTTOM_LEFT][0]) * data.notificationPixelSize, int((1 / data.notificationScreenBorderX - 1) * data.cWidth * data.notificationScreenBorderX) - len(data.paperPieces[data.PAPER_BOTTOM_RIGHT][0]) * data.notificationPixelSize - len(data.paperPieces[data.PAPER_DOWN][0]) * data.notificationPixelSize, len(data.paperPieces[data.PAPER_DOWN][0]) * data.notificationPixelSize):
data.notificationPage.append(makeBitmap(i, int((1 / data.notificationScreenBorderY - 1) * data.cHeight * data.notificationScreenBorderY) - len(data.paperPieces[data.PAPER_DOWN]) * data.notificationPixelSize, data.notificationPixelSize, data.paperPieces[data.PAPER_DOWN]))
data.notificationPage.append(makeBitmap(int((1 / data.notificationScreenBorderX - 1) * data.cWidth * data.notificationScreenBorderX) - len(data.paperPieces[data.PAPER_BOTTOM_RIGHT][0]) * data.notificationPixelSize - len(data.paperPieces[data.PAPER_DOWN][0]) * data.notificationPixelSize, int((1 / data.notificationScreenBorderY - 1) * data.cHeight * data.notificationScreenBorderY) - len(data.paperPieces[data.PAPER_DOWN]) * data.notificationPixelSize, data.notificationPixelSize, data.paperPieces[data.PAPER_DOWN]))
#Middle
for i in range(int(data.cWidth * data.notificationScreenBorderX) + len(data.paperPieces[data.PAPER_LEFT][0]) * data.notificationPixelSize, int((1 / data.notificationScreenBorderX - 1) * data.cWidth * data.notificationScreenBorderX) - len(data.paperPieces[data.PAPER_RIGHT][0]) * data.notificationPixelSize - len(data.paperPieces[data.PAPER_MIDDLE][0]) * data.notificationPixelSize, len(data.paperPieces[data.PAPER_MIDDLE][0]) * data.notificationPixelSize):
for j in range(int(data.cHeight * data.notificationScreenBorderY) + len(data.paperPieces[data.PAPER_UP]) * data.notificationPixelSize, int((1 / data.notificationScreenBorderY - 1) * data.cHeight * data.notificationScreenBorderY) - len(data.paperPieces[data.PAPER_DOWN]) * data.notificationPixelSize - len(data.paperPieces[data.PAPER_MIDDLE]) * data.notificationPixelSize, len(data.paperPieces[data.PAPER_MIDDLE]) * data.notificationPixelSize):
data.notificationPage.append(makeBitmap(i, j, data.notificationPixelSize, data.paperPieces[data.PAPER_MIDDLE]))
data.notificationPage.append(makeBitmap(int((1 / data.notificationScreenBorderX - 1) * data.cWidth * data.notificationScreenBorderX) - len(data.paperPieces[data.PAPER_LEFT]) * data.notificationPixelSize - len(data.paperPieces[data.PAPER_MIDDLE][0]) * data.notificationPixelSize, j, data.notificationPixelSize, data.paperPieces[data.PAPER_MIDDLE]))
data.notificationPage.append(makeBitmap(i, int((1 / data.notificationScreenBorderY - 1) * data.cHeight * data.notificationScreenBorderY) - len(data.paperPieces[data.PAPER_DOWN]) * data.notificationPixelSize - len(data.paperPieces[data.PAPER_MIDDLE]) * data.notificationPixelSize, data.notificationPixelSize, data.paperPieces[data.PAPER_MIDDLE]))
data.notificationPage.append(makeBitmap(int((1 / data.notificationScreenBorderX - 1) * data.cWidth * data.notificationScreenBorderX) - len(data.paperPieces[data.PAPER_LEFT]) * data.notificationPixelSize - len(data.paperPieces[data.PAPER_MIDDLE][0]) * data.notificationPixelSize, int((1 / data.notificationScreenBorderY - 1) * data.cHeight * data.notificationScreenBorderY) - len(data.paperPieces[data.PAPER_DOWN]) * data.notificationPixelSize - len(data.paperPieces[data.PAPER_MIDDLE]) * data.notificationPixelSize, data.notificationPixelSize, data.paperPieces[data.PAPER_MIDDLE]))
for i in range(len(text)):
textLength = getTextLength(text[i], data.notificationTextSize)
data.notificationPage.append(createText((data.cWidth - textLength) / 2, 2 * data.cHeight / 3 + (i - len(text)) * 30, text[i], data.notificationTextSize))
for shownButtonObject in Button.buttonObject:
shownButtonObject.disableButton()
def deleteNotification():
data.notificationOpen = False
for i in range(len(data.notificationPage)):
for j in range(len(data.notificationPage[i])):
data.s.delete(data.notificationPage[i][j])
data.notificationPage = []
for shownButtonObject in Button.buttonObject:
shownButtonObject.enableButton()
def makeBitmap(x, y, squareSize, bitmap, toBack = False, colourAdd = [False, "#ffffff", 50], onlyDarker = False):
#Try not to use toBack, colourAdd, and onlyDarker all together because that will be very slow.
#Actually only darker doesn't take up that much time to process. colourAdd is the real killer. Dun dun duuuunn....
skip = int(1/squareSize)
if skip < 1: #This is is to reduce how many pixels need to be drawn when zoomed out.
skip = 1 #i.e. when each pixel only takes up half a square, you can skip a pixel every other time
squaresPixelsArray = [] #This is the array that stores each pixel so they can be deleted.
for i in range(0, len(bitmap), skip):
for j in range(0, len(bitmap[i]), skip):
colourCode = bitmap[i][j]
if colourCode != "#ffffff":
colour = colourCode
if colourAdd[0] == True:
percentage = colourAdd[2] / 100.0
#Get original colour
originalColour = colour
#Get hexcode number (remove "#")
originalRGB = originalColour.lstrip('#')
#Original red green blue
oR, oG, oB = tuple(int(originalRGB[i:i+2], 16) for i in (0, 2 ,4)) #o = Original
addColour = colourAdd[1]
addRGB = addColour.lstrip('#')
#Added red green blue
aR, aG, aB = tuple(int(addRGB[i:i+2], 16) for i in (0, 2 ,4)) #a = Added
#Delta red green blue
dR, dG, dB = aR - oR, aG - oG, aB - oB #d = Difference
if onlyDarker == True:
#Getting minimum of the colours
dR, dG, dB = [min(w, 0) for w in [dR, dG, dB]]
nR, nG, nB = oR + dR * percentage, oG + dG * percentage, oB + dG * percentage #n = New
nR, nG, nB = [min(w, 255) for w in [nR, nG, nB]]
nR, nG, nB = [max(w, 0) for w in [nR, nG, nB]]
colour = "#%02x%02x%02x" % (nR, nG, nB)
## print("Original =", oR, oG, oB, " Added =", aR, aG, aB, " Difference =", dR, dG, dB, " New =", nR, nG, nB)
## print(originalColour, colour, percentage)
pixel = data.s.create_rectangle(x + squareSize * j, y + squareSize * i, x + squareSize * (j + 1), y + squareSize * (i + 1), fill = colour, width = 0)
squaresPixelsArray.append(pixel)
if toBack == True:
data.s.tag_lower(pixel)
return squaresPixelsArray #Return the aray so the pixels can be deleted
def createText(x, y, text, size, center = False, onButton = False, addColour = [False, "#ffffff", 0]):
letterIndex = x
letterArray = []
for character in text:
if character == " ":
letterIndex += 20 * size / 4.0
else:
bitmapImage = data.font.fontDictionary[character]
if onButton == True:
addColour = [True, "#af622d", 100]
letterArray += makeBitmap(letterIndex, y + (size * len(data.buttonSegments[data.BUTTON_MIDDLE_0]) - len(bitmapImage) * size / 4.0) / 2, size / 4.0, bitmapImage, colourAdd = addColour, onlyDarker = True)
else:
letterArray += makeBitmap(letterIndex, y, size / 4.0, bitmapImage, colourAdd = addColour, onlyDarker = True)
letterIndex += (len(data.font.fontDictionary[character][0]) + data.buttonLetterSpacing ) * size / 4.0
if character == "q" or character == "Q":
letterIndex -= 10 * size / 4.0
return letterArray
def getTextLength(text, size):
stringLength = 0
for character in text:
if character == " ":
stringLength += 20 * size / 4.0
else:
bitmapImage = data.font.fontDictionary[character]
stringLength += (len(data.font.fontDictionary[character][0]) + data.buttonLetterSpacing) * size / 4.0
if character == "q" or character == "Q":
stringLength -= 10 * size / 4.0
return stringLength
#Button Class
class Button():
buttonObject = [] #Stores every button object
buttons = [] #This stores the entire scope of buttons and each of their pixels
#3D array --> the primary array stores each button
# --> the secondary array stores each component of said button
# --> the tertiary array stores each pixels of component
buttonBounds = [] #This stores the boudaries of each button.
#Used in the mouse click function
buttonSegments = [] #2D array that stores each of the button's segments
buttonFunctions = [] #Stores the function called when clicked
BUTTON_ENDS_WIDTH = len(data.buttonSegments[data.BUTTON_LEFT][0]) + len(data.buttonSegments[data.BUTTON_RIGHT][0])
BUTTON_MIDDLE_WIDTH = len(data.buttonSegments[data.BUTTON_MIDDLE_0][0])
BUTTON_HEIGHT = len(data.buttonSegments[data.BUTTON_MIDDLE_0])
def __init__(self, buttonX, buttonY, text, size, function):
self.x = buttonX
self.y = buttonY
self.size = float(size)
self.text = text
self.length = 0
self.hovered = False
for character in self.text:
if character == " ":
textLength = 10
else:
textLength = len(data.font.fontDictionary[character][0])
if character == "q" or character == "Q":
textLength -= 10
self.length += (textLength + data.buttonLetterSpacing) * self.size / 4.0
self.number = len(Button.buttons)
self.numOfMiddleSectionsRequired = ceil(self.length / Button.BUTTON_MIDDLE_WIDTH)
x1 = self.x
y1 = self.y
x2 = self.x + self.size * Button.BUTTON_ENDS_WIDTH + Button.BUTTON_MIDDLE_WIDTH * self.numOfMiddleSectionsRequired
y2 = self.y + self.size * Button.BUTTON_HEIGHT
Button.buttonObject.append(self)
Button.buttons.append([])
Button.buttonSegments.append([])
Button.buttonBounds.append([x1, y1, x2, y2])
Button.buttonFunctions.append(function)
xValue = self.x #Index for where to place next segment of button
remainingLength = self.length #Variable to notify when to stop
#First Left Button Segment
bitmapImage = data.buttonSegments[data.BUTTON_LEFT]
Button.buttons[self.number].append(makeBitmap(xValue, self.y, self.size, bitmapImage))
xValue += self.size * len(bitmapImage[0])
letterIndex = xValue #This is where the letters start showing up
while remainingLength > 0:
randomSegmentNumber = random.randint(0, len(data.buttonSegments) - 3)
Button.buttonSegments[self.number].append(randomSegmentNumber)
bitmapImage = data.buttonSegments[data.BUTTON_MIDDLE_TEMPLATE + str(randomSegmentNumber)]
Button.buttons[self.number].append(makeBitmap(xValue, self.y, self.size, bitmapImage))
xValue += self.size * len(bitmapImage[0])
remainingLength -= self.size * len(bitmapImage[0])
bitmapImage = data.buttonSegments[data.BUTTON_RIGHT]
Button.buttons[self.number].append(makeBitmap(xValue, self.y, self.size, bitmapImage))
Button.buttons[self.number].append(createText(letterIndex, self.y, self.text, self.size, onButton = True))
def displayButton(self):
xValue = self.x #Index for where to place next segment of button
remainingLength = self.length #Variable to notify when to stop
if self.hovered == True:
addedColour = [True, "#dbdb36", 30]
else:
addedColour = [False, "ffffff", 0]
#First Left Button Segment
bitmapImage = data.buttonSegments[data.BUTTON_LEFT]
Button.buttons[self.number][0] = makeBitmap(xValue, self.y, self.size, bitmapImage, colourAdd = addedColour)
xValue += self.size * len(bitmapImage[0])
letterIndex = xValue #This is where the letters start showing up
segmentNumber = 0
while remainingLength > 0:
bitmapImage = data.buttonSegments[data.BUTTON_MIDDLE_TEMPLATE + str(Button.buttonSegments[self.number][segmentNumber])]
segmentNumber += 1
Button.buttons[self.number][segmentNumber] = makeBitmap(xValue, self.y, self.size, bitmapImage, colourAdd = addedColour)
xValue += self.size * len(bitmapImage[0])
remainingLength -= self.size * len(bitmapImage[0])
bitmapImage = data.buttonSegments[data.BUTTON_RIGHT]
Button.buttons[self.number][segmentNumber + 1] = (makeBitmap(xValue, self.y, self.size, bitmapImage, colourAdd = addedColour))
Button.buttons[self.number][segmentNumber + 2] = createText(letterIndex, self.y, self.text, self.size, onButton = True, addColour = addedColour)
def disableButton(self):
Button.buttonBounds[self.number] = []
def enableButton(self):
Button.buttonBounds[self.number] = [self.x, self.y, self.x + self.size * Button.BUTTON_ENDS_WIDTH + Button.BUTTON_MIDDLE_WIDTH * self.numOfMiddleSectionsRequired, self.y + self.size * Button.BUTTON_HEIGHT]
def delete(self): #For updating the screen
for i in range(len(Button.buttons[self.number])):
for j in range(len(Button.buttons[self.number][i])):
data.s.delete(Button.buttons[self.number][i][j])
def destroy(self):
try: #Try to delete everything if it hasn't already been deleted
for i in range(len(Button.buttons[self.number])):
for j in range(len(Button.buttons[self.number][i])):
data.s.delete(Button.buttons[self.number][i][j])
except:
pass
#Then delete all the data
del Button.buttonBounds[self.number] #Delete the bounds check
del Button.buttons[self.number] #Delete the pixels STORER (not the pixels themselves)
del Button.buttonFunctions[self.number] #Delete the function
del Button.buttonObject[self.number] #Delete the object HOLDER (again, not the class instance itself)
del Button.buttonSegments[self.number] #Delete the button segment data (not each segment -- its data)
for i in range(self.number, len(Button.buttonObject)):
Button.buttonObject[i].number -= 1 #Reduce all numbers of following buttons by 1 to fill up the gap.
#Scroller Class
class Scroller():
scrollerObject = [] #Stores every scroller object
scrollerPixels = [] #This stores the entire scope of scrollers and each of their pixels
#3D array --> the primary array stores each scroller
# --> the secondary array stores each component of said scroller
# --> the tertiary array stores each pixels of component
scrollerBounds = [] #This stores the boudaries of each scroller.
#Used in the mouse click function
scrollerSegments = [] #2D array that stores each of the scroller's segments
scrollerFunctions = [] #Array stores each page that the scroller is assigned to
topBitmapImage = data.scrollerSegments[data.SCROLLER_TOP]
bottomBitmapImage = data.scrollerSegments[data.SCROLLER_BOTTOM]
def __init__(self, scrollerX, scrollerY, scrollerWidth, scrollerHeight, displayedActualHeight, scrollerPixelSize, function):
self.number = len(Scroller.scrollerObject)
self.x = scrollerX
self.y = scrollerY
self.pixelSize = float(scrollerPixelSize)
self.scrollerWidth = scrollerWidth
self.scrollerHeight = scrollerHeight #Scroller height is the entire thing's height
self.actualHeight = displayedActualHeight
self.scrolledPercentage = 0
self.scrolledOriginalPercentage = 0
self.scrollerXValue = self.x - (len(data.scrollerSegments[data.SCROLLER_MIDDLE_0][0]) * self.pixelSize - self.scrollerWidth) / 2
self.clickedY = 0
self.draggedY = 0
self.clickedScroller = False
self.actualDisplayIndex = 0
Scroller.scrollerObject.append(self)
Scroller.scrollerFunctions.append(function)
Scroller.scrollerSegments.append([])
Scroller.scrollerPixels.append([])
Scroller.scrollerPixels[self.number].append([data.s.create_rectangle(self.x, self.y, self.x + self.scrollerWidth, self.y + self.scrollerHeight, fill = "#7f7f7f", width = 0)])
fractionOfActualThatsSeen = (self.scrollerHeight + 20.0) / self.actualHeight
#The size of the tab
self.scrollerTabSize = fractionOfActualThatsSeen * self.scrollerHeight
#How much you can actually scroll
self.scrollableLength = self.scrollerHeight - self.scrollerTabSize
#The top and bottom segments are additional to the actual scrolling part, which are the middle segments
Scroller.scrollerPixels[self.number].append(makeBitmap(self.scrollerXValue, self.y - len(Scroller.topBitmapImage) * self.pixelSize, self.pixelSize, Scroller.topBitmapImage))
Scroller.scrollerPixels[self.number].append(makeBitmap(self.scrollerXValue, self.y + self.scrollerTabSize, self.pixelSize, Scroller.bottomBitmapImage))
#These segments actually show where the thing is
for i in range(int(self.y), self.y + int(self.scrollerTabSize), int(len(data.scrollerSegments[data.SCROLLER_MIDDLE_0]) * self.pixelSize)):
randomSegmentNumber = randint(0, len(data.scrollerSegments) - 3)
Scroller.scrollerSegments[self.number].append(randomSegmentNumber)
bitmapImage = data.scrollerSegments[data.SCROLLER_MIDDLE_TEMPLATE + str(randomSegmentNumber)]
Scroller.scrollerPixels[self.number].append(makeBitmap(self.scrollerXValue, i, self.pixelSize, bitmapImage))
Scroller.scrollerBounds.append([self.scrollerXValue, self.y - len(Scroller.topBitmapImage) * self.pixelSize, self.scrollerXValue + len(data.scrollerSegments[data.SCROLLER_MIDDLE_0][0]) * self.pixelSize, self.y + self.scrollerTabSize + len(Scroller.bottomBitmapImage) * self.pixelSize])
def displayScroller(self):
yIndex = self.scrolledPercentage / 100.0 * self.scrollableLength
self.actualDisplayIndex = self.scrolledPercentage / 100 * (self.actualHeight - self.scrollerHeight + 20)
Scroller.scrollerFunctions[self.number](self.actualDisplayIndex)
Scroller.scrollerPixels[self.number].append([data.s.create_rectangle(self.x, self.y, self.x + self.scrollerWidth, self.y + self.scrollerHeight, fill = "#7f7f7f", width = 0)])
Scroller.scrollerPixels[self.number][1] = makeBitmap(self.scrollerXValue, self.y - len(Scroller.topBitmapImage) * self.pixelSize + yIndex, self.pixelSize, Scroller.topBitmapImage)
Scroller.scrollerPixels[self.number][2] = makeBitmap(self.scrollerXValue, self.y + self.scrollerTabSize + yIndex, self.pixelSize, Scroller.bottomBitmapImage)
segmentIndex = 0
for i in range(len(Scroller.scrollerSegments[self.number])):
segmentNumber = Scroller.scrollerSegments[self.number][i]
bitmapImage = data.scrollerSegments[data.SCROLLER_MIDDLE_TEMPLATE + str(segmentNumber)]
Scroller.scrollerPixels[self.number][3 + segmentIndex] = makeBitmap(self.scrollerXValue, self.y + i * self.pixelSize + yIndex, self.pixelSize, bitmapImage)
segmentIndex += 1
Scroller.scrollerBounds[self.number] = [self.scrollerXValue, self.y - len(Scroller.topBitmapImage) * self.pixelSize + yIndex, self.scrollerXValue + len(data.scrollerSegments[data.SCROLLER_MIDDLE_0][0]) * self.pixelSize, self.y + self.scrollerTabSize + len(Scroller.bottomBitmapImage) * self.pixelSize + yIndex]
def deleteScroller(self): #For updating the screen
for i in range(len(Scroller.scrollerPixels[self.number])):
for j in range(len(Scroller.scrollerPixels[self.number][i])):
data.s.delete(Scroller.scrollerPixels[self.number][i][j])
def destroy(self):
try: #Try to delete everything if it hasn't already been deleted
for i in range(len(Scroller.scrollerPixels[self.number])):
for j in range(len(Scroller.scrollerPixels[self.number][i])):
data.s.delete(Scroller.scrollerPixels[self.number][i][j])
except:
pass
#Then delete all the data
del Scroller.scrollerBounds[self.number] #Delete the bounds check
del Scroller.scrollerPixels[self.number] #Delete the pixels STORER (not the pixels themselves)
del Scroller.scrollerObject[self.number] #Delete the object HOLDER (again, not the class instance itself)
del Scroller.scrollerSegments[self.number] #Delete the scroller segment data (not each segment -- its data)
for i in range(self.number, len(Scroller.scrollerObject)):
Scroller.scrollerObject[i].number -= 1 #Reduce all numbers of following scrollers by 1 to fill up the gap.
#Selectable Panel Class
class SelectablePanel():
panelObject = [] #Stores every panel object
panelPixels = [] #This stores the entire scope of panels and each of their pixels
#3D array --> the primary array stores each panel
# --> the secondary array stores each component of said panel
# --> the tertiary array stores each pixels of component
panelBounds = [] #This stores the boudaries of each panel.
#Used in the mouse click function
panelFunctions = [] #Stores the function called when clicked
def __init__(self, panelX1, panelY1, panelX2, panelY2, pressedFunction, graphicsFunction, pressedExtraArgs = (), graphicsExtraArgs = (), icon = [False, "", 0]):
self.number = len(SelectablePanel.panelObject)
self.x1 = panelX1
self.y1 = panelY1
self.x2 = panelX2
self.y2 = panelY2
self.graphics = graphicsFunction
self.hasIcon = False
SelectablePanel.panelObject.append(self)
SelectablePanel.panelPixels.append([])
SelectablePanel.panelBounds.append([self.x1, self.y1, self.x2, self.y2])
SelectablePanel.panelFunctions.append(pressedFunction)
self.graphicsExtraArgs = graphicsExtraArgs
self.pressedExtraArgs = pressedExtraArgs
SelectablePanel.panelPixels[self.number].append([data.s.create_rectangle(self.x1, self.y1, self.x2, self.y2, fill = data.buildingPanelColour)])
SelectablePanel.panelPixels[self.number].append(self.graphics(self.x1, self.y1, self.x2, self.y2, *self.graphicsExtraArgs))
if icon[0] == True:
self.hasIcon = True
self.bitmapImage = icon[1]
self.pixelSize = icon[2]
SelectablePanel.panelPixels[self.number].append(makeBitmap((self.x2 + self.x1 - len(self.bitmapImage[0]) * self.pixelSize) / 2, self.y1 + 2 * (self.y2 - self.y1) / 5 - len(self.bitmapImage) * self.pixelSize / 2, self.pixelSize, self.bitmapImage))
def displayPanel(self, panelX1, panelY1, panelX2, panelY2):
self.x1 = panelX1
self.y1 = panelY1
self.x2 = panelX2
self.y2 = panelY2
SelectablePanel.panelBounds[self.number] = [self.x1, self.y1, self.x2, self.y2]
SelectablePanel.panelPixels[self.number][0] = [data.s.create_rectangle(self.x1, self.y1, self.x2, self.y2, fill = data.buildingPanelColour)]
SelectablePanel.panelPixels[self.number][1] = self.graphics(self.x1, self.y1, self.x2, self.y2, *self.graphicsExtraArgs)
if self.hasIcon == True:
SelectablePanel.panelPixels[self.number][2] = (makeBitmap((self.x2 + self.x1 - len(self.bitmapImage[0]) * self.pixelSize) / 2, self.y1 + 2 * (self.y2 - self.y1) / 5 - len(self.bitmapImage) * self.pixelSize / 2, self.pixelSize, self.bitmapImage))
def delete(self): #For updating the screen
for i in range(len(SelectablePanel.panelPixels[self.number])):
for j in range(len(SelectablePanel.panelPixels[self.number][i])):
data.s.delete(SelectablePanel.panelPixels[self.number][i][j])
def destroy(self):
try: #Try to delete everything if it hasn't already been deleted
for i in range(len(SelectablePanel.panelPixels[self.number])):
for j in range(len(SelectablePanel.panelPixels[self.number][i])):
data.s.delete(SelectablePanel.panelPixels[self.number][i][j])
except:
pass
#Then delete all the data
del SelectablePanel.panelBounds[self.number] #Delete the bounds check
del SelectablePanel.panelPixels[self.number] #Delete the pixels STORER
del SelectablePanel.panelFunctions[self.number] #Delete the function
del SelectablePanel.panelObject[self.number] #Delete the object HOLDER
for i in range(self.number, len(SelectablePanel.panelObject)):
SelectablePanel.panelObject[i].number -= 1 #Reduce all numbers of following panels by 1 to fill up the gap.