-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui.py
353 lines (303 loc) · 15.4 KB
/
gui.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
import pyglet
import constants
import engine
from pyglet.window import mouse
class Button:
def __init__(self, pic, x, y, width, height, opLow, opMed, opHigh, batch):
self.pic = pic
self.sprite = pyglet.sprite.Sprite(pic, x, y, batch=batch)
self.sprite.opacity = opLow
self.state = 0
self.opLow = opLow
self.opMed = opMed
self.opHigh = opHigh
self.leftBound = x
self.rightBound = x + width
self.lowerBound = y
self.upperBound = y + height
def highlight(self):
self.sprite.opacity = self.opHigh
self.state = 2
def unhighlight(self):
self.sprite.opacity = self.opLow
self.state = 0
def hover(self):
self.sprite.opacity = self.opMed
self.state = 1
class StartWindow(pyglet.window.Window):
def __init__(self):
self.owidth = 805
self.oheight = 600
self.buttonPad = 5
self.xPad = 20
self.yPad = 20
self.botBatch = pyglet.graphics.Batch()
self.midBatch = pyglet.graphics.Batch()
self.topBatch = pyglet.graphics.Batch()
super().__init__(width=self.owidth, height=self.oheight)
self.players = []
self.numPlayers = 2
self.boardSize = 5
self.readPlayerData()
self.activePlayers = [0, 1]
# Lay out all the sprites
self.music = pyglet.media.Player()
for i in range(10):
self.music.queue(constants.startmusic)
self.music.play()
pic = constants.bg
self.bgSprite = pyglet.sprite.Sprite(pic, 0, 0, batch=self.botBatch)
pic = constants.title
self.titleSprite = pyglet.sprite.Sprite(pic, 200, 520, batch=self.topBatch)
pic = constants.devnames
self.devSprite = pyglet.sprite.Sprite(pic, 190, 510, batch=self.topBatch)
pic = constants.selectgamesize
self.selectgamesizeSprite = pyglet.sprite.Sprite(pic, 80, 420, batch=self.topBatch)
pic = constants.chooseplayer
self.chooseplayerSprite = pyglet.sprite.Sprite(pic, 320, 420, batch=self.topBatch)
pic = constants.winrecord
self.winrecordSprite = pyglet.sprite.Sprite(pic, 550, 100, batch=self.midBatch)
# Lay out all the buttons
pic = constants.startbutton
self.startButton = Button(pic, self.xPad, self.yPad, pic.texture.width,
pic.texture.height, 200, 255, 255, self.topBatch)
pic = constants.exitbutton
self.exitButton = Button(pic, self.owidth - (self.xPad + pic.texture.width), self.yPad, pic.texture.width,
pic.texture.height, 200, 255, 255, self.topBatch)
# These are the player number buttons
self.numButtons = []
for i in range(5):
pic = constants.numOptionPics[4 - i]
newButton = Button(pic, self.xPad + 65, 140 + self.yPad + ((pic.texture.height + self.buttonPad) * i),
pic.texture.width, pic.texture.height, 100, 180, 255, self.topBatch)
self.numButtons.append(newButton)
if pic == constants.numOptionPics[0]:
newButton.highlight()
self.playerButtons = []
self.winLabels = []
self.lossLabels = []
# These are the player buttons
for i in range(len(self.players)):
pic = constants.playerPics[self.players[i].id]
newButton = Button(pic, self.xPad + 300, 88 + self.yPad + ((pic.texture.height + self.buttonPad) * (5-i)),
pic.texture.width, pic.texture.height, 100, 180, 255, self.topBatch)
self.playerButtons.append(newButton)
if pic == constants.playerPics[0] or pic == constants.playerPics[1]:
newButton.highlight()
self.winLabels.append(pyglet.text.Label(str(self.players[i].wins), font_name='Arial', font_size=27,
bold=True, batch=self.topBatch,
x = self.xPad + 547,
y = 95 + self.yPad + ((pic.texture.height + self.buttonPad) * (5-i))))
self.lossLabels.append(pyglet.text.Label(str(self.players[i].losses), font_name='Arial', font_size=27,
bold=True, batch=self.topBatch,
x=self.xPad + 627,
y=95 + self.yPad + ((pic.texture.height + self.buttonPad) * (5 - i))))
def on_draw(self):
self.clear()
self.botBatch.draw()
self.midBatch.draw()
self.topBatch.draw()
def on_mouse_press(self, x, y, button, modifiers):
if button == mouse.LEFT:
self.checkNumButtons(x, y)
self.checkPlayerButtons(x, y)
self.highlightPlayerButtons()
self.checkMiscButtons(x, y)
def on_mouse_motion(self, x, y, dx, dy):
# num buttons
for button in self.numButtons:
if button.state == 0:
if button.leftBound <= x <= button.rightBound and button.lowerBound <= y <= button.upperBound:
button.hover()
constants.hoversound.play()
elif button.state == 1:
if not (button.leftBound <= x <= button.rightBound and button.lowerBound <= y <= button.upperBound):
button.unhighlight()
# player buttons
for button in self.playerButtons:
if button.state == 0:
if button.leftBound <= x <= button.rightBound and button.lowerBound <= y <= button.upperBound:
button.hover()
constants.hoversound.play()
elif button.state == 1:
if not (button.leftBound <= x <= button.rightBound and button.lowerBound <= y <= button.upperBound):
button.unhighlight()
# start button
button = self.startButton
if button.state == 0:
if button.leftBound <= x <= button.rightBound and button.lowerBound <= y <= button.upperBound:
button.hover()
constants.hoversound.play()
elif button.state == 1:
if not (button.leftBound <= x <= button.rightBound and button.lowerBound <= y <= button.upperBound):
button.unhighlight()
# exit button
button = self.exitButton
if button.state == 0:
if button.leftBound <= x <= button.rightBound and button.lowerBound <= y <= button.upperBound:
button.hover()
constants.hoversound.play()
elif button.state == 1:
if not (button.leftBound <= x <= button.rightBound and button.lowerBound <= y <= button.upperBound):
button.unhighlight()
def readPlayerData(self):
playerFile = open("playerdata.txt", "r")
for line in playerFile:
playerData = line.split("|")
self.players.append(engine.Player(int(playerData[0]), playerData[1], playerData[2], int(playerData[3]), int(playerData[4])))
playerFile.close()
def checkNumButtons(self, x, y):
# check num buttons
for button in self.numButtons:
if button.leftBound <= x <= button.rightBound and button.lowerBound <= y <= button.upperBound:
self.numPlayers = 6 - self.numButtons.index(button)
constants.clicksound.play()
print(str(self.numPlayers) + " player game selected!")
if self.numPlayers < 5:
self.boardSize = 5
else:
self.boardSize = 6
# highlight selected num button
for allButt in self.numButtons:
if allButt == button:
allButt.highlight()
else:
allButt.unhighlight()
def checkPlayerButtons(self, x, y):
# check player buttons
for button in self.playerButtons:
if button.leftBound <= x <= button.rightBound and button.lowerBound <= y <= button.upperBound:
playerInd = self.playerButtons.index(button)
# add selected player to list
if playerInd not in self.activePlayers:
constants.playerSounds[playerInd].play()
self.activePlayers.append(playerInd)
print(self.players[playerInd].name + " selected!")
def checkMiscButtons(self, x, y):
# check start button
button = self.startButton
if button.leftBound <= x <= button.rightBound and button.lowerBound <= y <= button.upperBound:
# start a new game
game = engine.Game(self.players, self.activePlayers, self.boardSize)
mainWindow = GameWindow(game)
constants.clicksound.play()
pyglet.clock.schedule_interval(mainWindow.update, 1 / 60)
self.music.pause()
self.close()
# check exit button
button = self.exitButton
if button.leftBound <= x <= button.rightBound and button.lowerBound <= y <= button.upperBound:
constants.clicksound.play()
pyglet.app.exit()
def highlightPlayerButtons(self):
# remove players if extra
while len(self.activePlayers) > self.numPlayers:
self.activePlayers.pop(0)
i = 0
# add more players if missing
while len(self.activePlayers) < self.numPlayers:
if i not in self.activePlayers:
self.activePlayers.append(i)
i += 1
# highlight appropriate buttons
for i in range(len(self.playerButtons)):
if i in self.activePlayers:
self.playerButtons[i].highlight()
else:
self.playerButtons[i].unhighlight()
class GameWindow(pyglet.window.Window):
def __init__(self, game):
self.game = game
self.owidth = (self.game.board.boxPad * (self.game.board.boardSize + 1)) + \
(self.game.board.boxSize * self.game.board.boardSize)
self.oheight = (self.game.board.boxPad * (self.game.board.boardSize + 1)) + \
(self.game.board.boxSize * self.game.board.boardSize)
self.botBatch = pyglet.graphics.Batch()
self.midBatch = pyglet.graphics.Batch()
self.topBatch = pyglet.graphics.Batch()
super().__init__(width=self.owidth, height=self.oheight + 120)
# Lay out all sprites
self.music = pyglet.media.Player()
for i in range(10):
self.music.queue(constants.gamemusic)
self.music.play()
pic = constants.bg
self.bgSprite = pyglet.sprite.Sprite(pic, 0, self.oheight, batch=self.botBatch)
pic = constants.title
self.titleSprite = pyglet.sprite.Sprite(pic, (self.owidth / 2) - (pic.texture.width/2), self.oheight + 50, batch=self.midBatch)
pic = constants.devnames
self.devSprite = pyglet.sprite.Sprite(pic, (self.owidth / 2) - (pic.texture.width/2), self.oheight + 40, batch=self.midBatch)
pic = constants.playerPics[self.game.getCurrPlayer().id]
self.currSprite = pyglet.sprite.Sprite(pic, 0, self.oheight, batch=self.midBatch)
self.buttons = []
pic = constants.quitbutton
self.quitButton = Button(pic, self.owidth - (pic.texture.width), self.oheight, pic.texture.width,
pic.texture.height, 200, 255, 255, self.midBatch)
self.buttons.append(self.quitButton)
self.gameOverSprite = -1
self.winSprite = -1
self.plusSprite = -1
def on_draw(self):
self.clear()
self.botBatch.draw()
self.game.board.gridBatch.draw()
self.game.board.orbBatch.draw()
self.midBatch.draw()
self.topBatch.draw()
def on_mouse_press(self, x, y, button, modifiers):
if button == mouse.LEFT:
if self.game.ongoing and self.game.board.transferring == 0:
self.checkGrid(x, y)
self.checkButtons(x, y)
def on_mouse_motion(self, x, y, dx, dy):
# quit button
button = self.quitButton
if button.state == 0:
if button.leftBound <= x <= button.rightBound and button.lowerBound <= y <= button.upperBound:
button.hover()
constants.hoversound.play()
elif button.state == 1:
if not (button.leftBound <= x <= button.rightBound and button.lowerBound <= y <= button.upperBound):
button.unhighlight()
# Check if tile was clicked
def checkGrid(self, x, y):
for row in self.game.board.grid:
for rec in row:
if rec.leftBound <= x <= rec.rightBound and rec.lowerBound <= y <= rec.upperBound:
turnResult = self.game.takeTurn(rec.row, rec.column)
if turnResult[0]:
# turn successfully made
if turnResult[1]:
# Lay out game over sprites
print("Game Over! Winner: " + turnResult[2].name)
constants.yaysound.play()
self.gameOverSprite = pyglet.sprite.Sprite(constants.gameover,
self.owidth / 65,
self.oheight / 4, batch=self.midBatch)
self.gameOverSprite.opacity = 200
self.gameOverSprite.scale = self.owidth / 2000
pic = constants.pluswin
self.plusSprite = pyglet.sprite.Sprite(pic,
self.owidth / 65,
self.oheight / 4, batch=self.topBatch)
self.plusSprite.scale = self.owidth / 300
pic = constants.playerPics[self.game.getCurrPlayer().id]
self.winSprite = pyglet.sprite.Sprite(pic,
self.owidth / 3,
self.oheight / 4.5, batch=self.topBatch)
self.winSprite.scale = self.owidth / 500
self.quitButton.sprite.image = constants.menu
else:
self.currSprite.image = constants.playerPics[turnResult[2].id]
self.game.board.bgRectangle.changeColor(turnResult[2].color)
def checkButtons(self, x, y):
for button in self.buttons:
if button.leftBound <= x <= button.rightBound and button.lowerBound <= y <= button.upperBound:
# go back to main menu
constants.clicksound.play()
self.preWindow = StartWindow()
pyglet.clock.unschedule(self.update)
self.music.pause()
self.close()
def update(self, dt):
self.game.board.update(dt)