-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
292 lines (223 loc) · 8.31 KB
/
main.py
File metadata and controls
292 lines (223 loc) · 8.31 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
# Dependencies
import pygame as Flav
import UI
import sound as Sound
import chessgame as ChessGame
import music as Music
# Initialisation of modules
Flav.init()
Sound.init()
ChessGame.init()
# Main variables
Screen = Flav.display.set_mode((500, 500))
Running = True
BotMusic, MenuPlaylist, SelfPlaylist = None, None, None # will be initialised at the bottom
# Start Menu Form
StartForm = UI.Form("Startup")
StartForm.BackgroundColour=(99, 197, 218)
StartHeaderLabel = UI.TextLabel()
StartHeaderLabel.Text = "Chess"
StartHeaderLabel.Position= (180, 20)
StartHeaderLabel.BackgroundColour = (99, 197, 218)
VersionLabel = UI.TextLabel()
VersionLabel.Text = "Chess v1"
VersionLabel.TextSize = 12
VersionLabel.Position = (445, 480)
VersionLabel.Size = (45, 20)
VersionLabel.BackgroundColour = (99, 197, 218)
BotSelectionButton = UI.TextLabel()
BotSelectionButton.Text = "Choose Bot"
BotSelectionButton.Position = (135, 220)
BotSelectionButton.Size = (200, 70)
BotSelectionButton.OutlineColour = (239, 239, 239)
BotSelectionButton.OutlineSize = 4
BotSelectionButton.BackgroundColour = (124, 124, 124)
ExitButton = UI.TextLabel()
ExitButton.Text = "Exit"
ExitButton.Position = (135, 310)
ExitButton.Size = (200, 70)
ExitButton.OutlineColour = (239, 239, 239)
ExitButton.OutlineSize = 4
ExitButton.BackgroundColour = (124, 124, 124)
StartForm.AddEntity(StartHeaderLabel)
StartForm.AddEntity(BotSelectionButton)
StartForm.AddEntity(ExitButton)
StartForm.AddEntity(VersionLabel)
# Choose Bot Form
ChooseForm = UI.Form("Bot Selection")
ChooseForm.BackgroundColour = (99, 197, 218)
ChooseHeaderLabel = UI.TextLabel()
ChooseHeaderLabel.Text = "Bot Selection"
ChooseHeaderLabel.Position= (180, 20)
ChooseHeaderLabel.BackgroundColour = (99, 197, 218)
RandomMoverButton = UI.TextLabel()
RandomMoverButton.Text = "Random Mover"
RandomMoverButton.Position = (105, 150)
RandomMoverButton.Size = (250, 70)
RandomMoverButton.OutlineColour = (239, 239, 239)
RandomMoverButton.OutlineSize = 4
RandomMoverButton.BackgroundColour = (124, 124, 124)
MinMaxButton = UI.TextLabel()
MinMaxButton.Text = "Mr Flav"
MinMaxButton.Position = (105, 240)
MinMaxButton.Size = (250, 70)
MinMaxButton.OutlineColour = (239, 239, 239)
MinMaxButton.OutlineSize = 4
MinMaxButton.BackgroundColour = (124, 124, 124)
PlaySelfButton = UI.TextLabel()
PlaySelfButton.Text = "Play Yourself"
PlaySelfButton.Position = (105, 330)
PlaySelfButton.Size = (250, 70)
PlaySelfButton.OutlineColour = (239, 239, 239)
PlaySelfButton.OutlineSize = 4
PlaySelfButton.BackgroundColour = (124, 124, 124)
ChooseBackButton = UI.TextLabel()
ChooseBackButton.Text = "Back"
ChooseBackButton.TextSize = 25
ChooseBackButton.Position = (10, 445)
ChooseBackButton.Size = (60, 45)
ChooseBackButton.OutlineColour = (239, 239, 239)
ChooseBackButton.OutlineSize = 4
ChooseBackButton.BackgroundColour = (124, 124, 124)
ChooseForm.AddEntity(ChooseHeaderLabel)
ChooseForm.AddEntity(RandomMoverButton)
ChooseForm.AddEntity(MinMaxButton)
ChooseForm.AddEntity(ChooseBackButton)
ChooseForm.AddEntity(PlaySelfButton)
ChooseForm.AddEntity(VersionLabel) # Notice that we're able to re-use the VersionLabel from the StartForm
# Game Form
GameForm = UI.App("Game")
GameForm.RenderFunction = ChessGame.Render
GameForm.EventFunction = ChessGame.ProcessEvents
GameBackButton = UI.TextLabel()
GameBackButton.Text = "Back"
GameBackButton.TextSize = 15
GameBackButton.Position = (10, 465)
GameBackButton.Size = (40, 25)
GameBackButton.OutlineColour = (239, 239, 239)
GameBackButton.OutlineSize = 4
GameBackButton.BackgroundColour = (124, 124, 124)
GameForm.AddEntity(GameBackButton)
# Victory Form
VictoryForm = UI.Form("Victory")
VictoryForm.BackgroundColour = (241, 241, 241)
VictoryHeaderLabel = UI.TextLabel()
VictoryHeaderLabel.Text = "Congratulations!"
VictoryHeaderLabel.TextColour = (241, 241, 241)
VictoryHeaderLabel.Position= (0,0)
VictoryHeaderLabel.Size = (500, 100)
VictoryHeaderLabel.BackgroundColour = (60, 176, 55)
GameBackToSelectionButton = UI.TextLabel()
GameBackToSelectionButton.Text = "Back To Selection"
GameBackToSelectionButton.TextColour = (241, 241, 241)
GameBackToSelectionButton.Position = (75, 220)
GameBackToSelectionButton.Size = (350, 70)
GameBackToSelectionButton.OutlineColour = (219, 219, 219)
GameBackToSelectionButton.OutlineSize = 4
GameBackToSelectionButton.BackgroundColour = (124, 124, 124)
VictoryForm.AddEntity(VictoryHeaderLabel)
VictoryForm.AddEntity(GameBackToSelectionButton)
# Defeat Form
DefeatForm = UI.Form("Defeat")
DefeatForm.BackgroundColour = (241, 241, 241)
DefeatHeaderLabel = UI.TextLabel()
DefeatHeaderLabel.Text = "Defeat"
DefeatHeaderLabel.TextColour = (241, 241, 241)
DefeatHeaderLabel.Position= (0,0)
DefeatHeaderLabel.Size = (500, 100)
DefeatHeaderLabel.BackgroundColour = (144, 13, 9)
DefeatForm.AddEntity(DefeatHeaderLabel)
DefeatForm.AddEntity(GameBackToSelectionButton)
# Connections - Start Menu
def OnBotSelectionClick(self, Event):
UI.SetCurrentForm(ChooseForm)
def ExitClick(self, Event):
global Running
Running = False
def OnButtonMouseEnter(self): # Note this (and OnButtonMouseLeave) can be re-used for several events in several objects
self.BackgroundColour = (159, 159, 159)
def OnButtonMouseLeave(self):
self.BackgroundColour = (124, 124, 124)
BotSelectionButton.Connect("OnClick", OnBotSelectionClick)
BotSelectionButton.Connect("OnMouseEnter", OnButtonMouseEnter)
BotSelectionButton.Connect("OnMouseLeave", OnButtonMouseLeave)
ExitButton.Connect("OnClick", ExitClick)
ExitButton.Connect("OnMouseEnter", OnButtonMouseEnter)
ExitButton.Connect("OnMouseLeave", OnButtonMouseLeave)
# Connections - Bot-Selection Menu
def InitialiseChessGame(Opponent):
ChessGame.Start(Opponent, Screen, (500, 500))
UI.SetCurrentForm(GameForm)
def ExitChessGame():
Music.SwitchPlaylist(MenuPlaylist)
def SwitchToRandomMover(self, Event):
InitialiseChessGame("Geg")
Music.SwitchPlaylist(BotMusic["Geg"])
def SwitchToMinMax(self, Event):
InitialiseChessGame("MrFlav")
Music.SwitchPlaylist(BotMusic["MrFlav"])
def SwitchToSelf(self, Event):
InitialiseChessGame("")
Music.SwitchPlaylist(SelfPlaylist)
def BackToStart(self, Event):
UI.SetCurrentForm(StartForm)
RandomMoverButton.Connect("OnClick", SwitchToRandomMover)
RandomMoverButton.Connect("OnMouseEnter", OnButtonMouseEnter)
RandomMoverButton.Connect("OnMouseLeave", OnButtonMouseLeave)
MinMaxButton.Connect("OnClick", SwitchToMinMax)
MinMaxButton.Connect("OnMouseEnter", OnButtonMouseEnter)
MinMaxButton.Connect("OnMouseLeave", OnButtonMouseLeave)
PlaySelfButton.Connect("OnClick", SwitchToSelf)
PlaySelfButton.Connect("OnMouseEnter", OnButtonMouseEnter)
PlaySelfButton.Connect("OnMouseLeave", OnButtonMouseLeave)
ChooseBackButton.Connect("OnClick", BackToStart)
ChooseBackButton.Connect("OnMouseEnter", OnButtonMouseEnter)
ChooseBackButton.Connect("OnMouseLeave", OnButtonMouseLeave)
# Connections - Game Form
def Victory():
UI.SetCurrentForm(VictoryForm)
def Defeat():
UI.SetCurrentForm(DefeatForm)
def BackToSelection(self, Event):
UI.SetCurrentForm(ChooseForm)
ExitChessGame()
def EventProcess(self, ReturnArg):
if ReturnArg == "Victory":
Victory()
elif ReturnArg == "Defeat":
Defeat()
GameForm.Connect("EventProcessed", EventProcess)
GameBackButton.Connect("OnClick", BackToSelection)
GameBackButton.Connect("OnMouseEnter", OnButtonMouseEnter)
GameBackButton.Connect("OnMouseLeave", OnButtonMouseLeave)
# Connections - Victory Screen
GameBackToSelectionButton.Connect("OnClick", BackToSelection)
GameBackToSelectionButton.Connect("OnMouseEnter", OnButtonMouseEnter)
GameBackToSelectionButton.Connect("OnMouseLeave", OnButtonMouseLeave)
# Music Asset Load
Music.MassPreloadSongs((
("Happy", "CantinaBandSource.mp3"),
("Tunes", "tunes.mp3"),
("Lobby", "ElevatorMusic.mp3"),
("Death", "TheOnlyThingTheyFearIsYou.mp3"),
("Contemplative", "Nocturne.mp3")
))
MenuPlaylist = Music.Playlist(tuple(["Lobby"]))
SelfPlaylist = Music.Playlist(tuple(["Contemplative"]))
BotMusic = {
"Geg":Music.Playlist(("Tunes","Happy")),
"MrFlav":Music.Playlist(tuple(["Death"]))
}
# Run Loop
UI.SetCurrentForm(StartForm)
Music.SwitchPlaylist(MenuPlaylist)
while Running:
Events = Flav.event.get()
for Event in Events:
if Event.type == Flav.QUIT:
Running = False
UI.Update(Events)
UI.Render(Screen)
Music.Update()
Flav.display.flip()
Flav.time.delay(50)