-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGraphicalDisplayManager.py
338 lines (290 loc) · 13.4 KB
/
GraphicalDisplayManager.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
from tkinter import *
from tkinter import ttk
from Dialog import *
import tkinter as tk
import math
import time
class GraphicalDisplayManager:
def __init__(self):
self.debug = False
self.root = Tk()
self.mainFrame = None
self.boardTileHeight = 95
self.boardTileWidth = 95
self.renderLoopRunning = False
self.scene = "main"
self.tokenImages = [
"./images/playerTokens/playerTokens_blueDisc.png",
"./images/playerTokens/playerTokens_boomerangePrawn.png",
"./images/playerTokens/playerTokens_casinoChip.png",
"./images/playerTokens/playerTokens_orangeDisc.png",
"./images/playerTokens/playerTokens_purpleDisc.png",
"./images/playerTokens/playerTokens_sherriffStar.png",
"./images/playerTokens/playerTokens_yellowDisc.png",
]
def drawSplashScreen(self, menuOptions):
self.setupPlayers = menuOptions["options"][1]["method"];
self.startGame = menuOptions["options"][0]["method"];
if self.mainFrame:
self.mainFrame.destroy()
self.root.title("Portals and Portals")
self.mainFrame = Frame(self.root)
self.mainFrame.pack()
image = PhotoImage(file="PortalsSplash.PNG")
lblPhoto = Label(self.mainFrame, image=image)
lblPhoto.pack()
imgStart = PhotoImage(file="./images/redButtons/redButtonsStartGame.png")
imgQuit = PhotoImage(file="./images/redButtons/redButtonsQuit.png")
bottomFrame = Frame(self.mainFrame)
bottomFrame.pack(side=BOTTOM)
btnStart = Button(bottomFrame, text="New Game", fg="red", command=self.setupPlayers)
btnStart.config(image=imgStart)
btnStart.pack(side=LEFT)
btnQuit = Button(bottomFrame, text="Exit", fg="black", command=self.root.destroy)
btnQuit.config(image=imgQuit)
btnQuit.pack(side=LEFT)
self.root.mainloop()
# while True:
# self.root.update()
# self.root.update_idletasks()
# menuOptions["options"][1]["method"]()
def drawPlayerSetupScreen(self, maxPlayers, playersList, tokens, selectedTokens):
if self.debug == False:
self.setNumberOfPlayers(maxPlayers, playersList)
self.setNameAndTokens(playersList, tokens, selectedTokens)
self.startGame()
else:
for index, player in enumerate(playersList):
print(index)
player.token = int(index)
player.ai = False
self.startGame()
def setNumberOfPlayers(self, maxPlayers, playersList):
d = SetPlayersDialog(self.root, "How many players?")
for index, player in enumerate(playersList):
if index < d.result:
print("set player to non-ai")
player.ai = False
else:
print("set player to ai")
player.ai = True
#
# mainFrame = ttk.Frame(root, padding="10 10 10 10")
# mainFrame.grid(column=0, row=0, sticky=(N, W, E, S))
# mainFrame.columnconfigure(0, weight=1)
# mainFrame.rowconfigure(0, weight=1)
#
# ttk.Label(mainFrame, text="How many players?").grid(column=0, row=0, sticky=(W, E))
# buttonFrame = ttk.Frame(mainFrame, padding="10 10 10 10")
# buttonFrame.grid(column=0, row=3)
#
# # draw the tokens
# for i in range(1, maxPlayers+1):
# b = ttk.Button(buttonFrame, text=i,
# command=lambda number=i: setNumPlayers(number))
# b.grid(column=i, row=0)
# root.mainloop()
def setNameAndTokens(self, playersList, tokens, selectedTokens):
for index, player in enumerate(playersList):
if player.ai == False:
d = SelectTokensDialog(self.root, "Setup Players",
data={"tokens": tokens, "selectedTokens": selectedTokens, "images": self.tokenImages })
print(d.result)
player.name = d.result[0]
player.token = d.result[1]
selectedTokens.append(d.result[2])
print("added to selected tokens")
print(selectedTokens)
else:
player.name = "Bot " + str(index)
print('Player {} is now known as {}'.format(index + 1, player.name))
for index, token in enumerate(tokens):
if token not in selectedTokens:
player.token = index;
selectedTokens.append(token);
print('{} has been allocated {} for their game token '.format(player.name,
player.token))
break
# for player in playersList:
# if player.ai == False:
# root = Tk()
# root.title("Setup " + player.name)
#
# mainFrame = ttk.Frame(root, padding="10 10 10 10")
# mainFrame.grid(column=0, row=0, sticky=(N, W, E, S))
# mainFrame.columnconfigure(0, weight=1)
# mainFrame.rowconfigure(0, weight=1)
#
# nameEntry = ttk.Entry(mainFrame, width=7)
# nameEntry.grid(column=0, row=1, sticky=(W, E))
# ttk.Label(mainFrame, text="Enter your name").grid(column=0, row=0, sticky=(W, E))
#
# ttk.Label(mainFrame, text="Select your token").grid(column=0, row=2, sticky=(W, E))
# tokenFrame = ttk.Frame(mainFrame, padding="10 10 10 10")
# tokenFrame.grid(column=0, row=3)
#
# #draw the tokens
# for index, token in enumerate(tokens):
# if token not in selectedTokens:
# print("token found: " + token)
# b = ttk.Button(tokenFrame, text=token, command=lambda token=token: setToken(player, nameEntry.get(), token))
# b.grid(column=index, row=0)
#
# root.mainloop()
# else:
# player.name = "Bot " + str(index)
# print('Player {} is now known as {}'.format(index + 1, player.name))
# for token in tokens:
# if token not in selectedTokens:
# player.token = token;
# selectedTokens.append(token);
# print('{} has been allocated {} for their game token '.format(player.name,
# player.token))
# break
def drawBoard(self, board, menuOptions=None, isAi=False):
self.board = board
if menuOptions:
self.takeTurn = menuOptions["options"][0]["method"];
if self.scene != "game":
self.mainFrame.destroy()
self.root.title("Portals and Portals")
self.mainFrame = Frame(self.root)
self.mainFrame.pack()
self.boardFrame = Frame(self.mainFrame, height=board.height * self.boardTileHeight,
width=board.width * self.boardTileWidth)
self.boardFrame.pack()
self.buttonFrame = Frame(self.mainFrame)
self.buttonFrame.pack()
btnStart = Button(self.buttonFrame, text="New Game", fg="red", command=self.takeTurn)
btnStart.image = PhotoImage(file="images/dice/diceClickToRoll.png")
btnStart.config(image=btnStart.image)
btnStart.pack(side=RIGHT)
try:
del self.tileFrames
except Exception:
pass
self.buttonFrame.nameLabels = []
for index, player in enumerate(board.players):
image = PhotoImage(file=self.tokenImages[player.token])
nameLabel = Label(self.buttonFrame, text=player.name, padx=20, image=image, compound=CENTER)
nameLabel.image = image
nameLabel.pack(side=LEFT)
self.buttonFrame.nameLabels.append(nameLabel)
if player.isActive == True:
nameLabel.config(background="green")
self.scene = "game"
if isAi:
self.drawTiles(board)
self.takeTurn()
# self.root.mainloop()
if self.renderLoopRunning == False:
while True:
#redraw tiles
self.renderLoopRunning = True
self.drawTiles(board)
for index, player in enumerate(board.players):
if player.isActive == True:
# print("setting label " + str(index) + " green")
self.buttonFrame.nameLabels[index].config(background="green")
else:
# print("setting label " + str(index) + " white")
self.buttonFrame.nameLabels[index].config(background="white")
self.root.update()
self.root.update_idletasks()
time.sleep(0.01)
def drawTiles(self, board):
try:
self.tileFrames
except AttributeError:
self.tileFrames = []
self.setupTiles(board)
for tile in board.tiles:
canvas = self.tileFrames[tile.tileNumber - 1].canvas
self.drawPortalsOnTile(tile, canvas)
self.drawPlayerTokensOnTile(tile, canvas)
def setupTiles(self, board):
for row in range(0, board.height):
for col in range(0, board.width):
tiles = board.tiles
tile = tiles[(row * board.width) + (col)]
if tile.tileNumber == 1:
tileFile = "images/tiles/tilesStartTile.png"
elif tile.tileNumber == board.width * board.height:
tileFile = "images/tiles/tilesEndTile.png"
elif tile.tileNumber % 2 == 0:
tileFile = "images/tiles/tilesBeigeTile.png"
else:
tileFile = "images/tiles/tilesBrownTile.png"
tileFrame = Frame(self.boardFrame,
height=self.boardTileHeight, width=self.boardTileWidth,
background="green", borderwidth=0, highlightthickness=0)
canvas = tk.Canvas(tileFrame, width=self.boardTileWidth, height=self.boardTileHeight,
background="blue", borderwidth=0, highlightthickness=0)
self.tileFrames.append(tileFrame)
tileFrame.canvas = canvas
canvas.pack()
canvas.image = PhotoImage(file=tileFile)
canvas.create_image(0, 0, image=canvas.image, anchor='nw')
if row % 2:
index = (board.width) - (col + 1);
x = index * self.boardTileWidth
else:
x = col * self.boardTileWidth
y = row * self.boardTileHeight
tileFrame.place(x=x, y=y)
self.drawDirectionArrows(tile, canvas)
tokenText = canvas.create_text(10, 10, anchor="nw")
canvas.itemconfig(tokenText, text=tile.tileNumber)
def drawDirectionArrows(self, tile, canvas):
if tile.tileNumber > 1 and tile.tileNumber < 40:
if tile.tileNumber % 8 == 0:
arrowPath = "images/arrows/arrowDown.png"
elif math.ceil(tile.tileNumber / 8) % 2 == 0:
arrowPath = "images/arrows/arrowLeft.png"
else:
arrowPath = "images/arrows/arrowRight.png"
canvas.arrow = PhotoImage(file=arrowPath)
canvas.create_image(90, 90, image=canvas.arrow, anchor="se")
def drawPlayerTokensOnTile(self, tile, canvas):
try:
for token in canvas.token:
canvas.delete(token)
canvas.token = []
except AttributeError:
canvas.token = []
players = tile.players
for count, player in enumerate(players):
tokenFile = self.tokenImages[player.token]
tokenImage = PhotoImage(file=tokenFile).subsample(2, 2)
player.tokenImage = tokenImage
x = count * tokenImage.width()
y = 20
if count > 1:
y += tokenImage.height()
x = (count - 2)* tokenImage.width()
image = canvas.create_image(20+x, y, image=tokenImage, anchor="nw")
canvas.token.append(image)
def drawPortalsOnTile(self, tile, canvas):
try:
canvas.delete(canvas.portal)
canvas.portal = None
except AttributeError:
pass
if tile.portal:
portalImage = PhotoImage(file=tile.portal.image)
tile.portalImage = portalImage
canvas.portal = canvas.create_image(0, 0, image=portalImage, anchor="nw")
# canvas.portal.image = portalImage
def drawEndGameScenario(self, playerList, player, menuOptions):
self.drawTiles(self.board)
self.scene = "end"
self.renderLoopRunning = False
d = EndGameScreen(self.root, "Winner Winner Chicken Dinner!", {
"player": player,
"menuOptions": menuOptions,
"players": playerList
})
def quitGame(self):
self.mainFrame.destroy()
self.root.quit()
self.root.destroy()