-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
321 lines (283 loc) · 10.4 KB
/
main.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
#! /usr/bin/env python3
"""
COMP16321 Coursework
Brick-breaker star-collector game
Screen resolution: 1280x720
by Felix Waller
main.py contains:
the main tkinter window
the left-hand menu
handling of essential keybinds
"""
import os
from tkinter import Button, Canvas, Label, PhotoImage, Tk
from tkinter import simpledialog, filedialog, messagebox
import pickle
from colour import Colour
from game_canvas import GameCanvas
from leaderboard import Leaderboard
def button_left():
"""Rebind key to move paddle left"""
while True:
btn = simpledialog.askstring(
title="Move Left",
prompt="Enter single key. Must be alphanumeric or space.")
if not btn:
break
if len(btn) == 1:
btn = btn.lower()
if btn.isalnum() and btn not in [canvas.paddle_right_key,
pause_key, boss_key]:
left_button["text"] = f"MOVE LEFT: {btn}"
root.unbind(canvas.paddle_left_key)
canvas.paddle_left_key = btn
root.bind(canvas.paddle_left_key, canvas.paddle_left)
break
elif btn == " ":
left_button["text"] = "MOVE LEFT: <space>"
root.unbind(canvas.paddle_left_key)
canvas.paddle_left_key = "<space>"
root.bind(canvas.paddle_left_key, canvas.paddle_left)
break
def button_right():
"""Rebind key to move paddle right"""
while True:
btn = simpledialog.askstring(
title="Move Right",
prompt="Enter single key. Must be alphanumeric or space.")
if not btn:
break
if len(btn) == 1:
btn = btn.lower()
if btn.isalnum() and btn not in [canvas.paddle_left_key,
pause_key, boss_key]:
right_button["text"] = f"MOVE RIGHT: {btn}"
root.unbind(canvas.paddle_right_key)
canvas.paddle_right_key = btn
root.bind(canvas.paddle_right_key, canvas.paddle_right)
break
elif btn == " ":
right_button["text"] = "MOVE RIGHT: <space>"
root.unbind(canvas.paddle_right_key)
canvas.paddle_right_key = "<space>"
root.bind(canvas.paddle_right_key, canvas.paddle_right)
break
def button_pause_change():
"""Rebind pause key"""
global pause_key
while True:
btn = simpledialog.askstring(
title="Pause",
prompt="Enter single key. Must be alphanumeric or space.")
if not btn:
break
if len(btn) == 1:
btn = btn.lower()
if btn.isalnum() and btn not in [canvas.paddle_left_key,
canvas.paddle_right_key,
boss_key]:
root.unbind(pause_key)
pause_button_change["text"] = f"PAUSE: {btn}"
pause_key = btn
root.bind(pause_key, pause)
break
elif btn == " ":
root.unbind(pause_key)
pause_button_change["text"] = "PAUSE: <space>"
pause_key = "<space>"
root.bind(pause_key, pause)
break
def button_boss_change():
"""Rebind boss key"""
global boss_key
while True:
btn = simpledialog.askstring(
title="Boss",
prompt="Enter single key. Must be alphanumeric or space.")
if not btn:
break
if len(btn) == 1:
btn = btn.lower()
if btn.isalnum() and btn not in [canvas.paddle_left_key,
canvas.paddle_right_key,
pause_key]:
root.unbind(boss_key)
boss_button["text"] = f"BOSS: {btn}"
boss_key = btn
root.bind(boss_key, boss)
break
elif btn == " ":
root.unbind(boss_key)
boss_button["text"] = "BOSS: <space>"
boss_key = "<space>"
root.bind(boss_key, boss)
break
def button_retry():
if canvas.current_level:
temp = canvas.current_level
canvas.ball.game_over()
if temp == 1:
canvas.level1()
elif temp == 2:
canvas.level2()
else:
canvas.level3()
def button_save():
global paused
paused = False
pause(None)
name = simpledialog.askstring(
title="Save Game", prompt="Enter save file name.")
if name:
name = name.replace(" ", "_")
data = canvas.save(pause_key, boss_key)
try:
with open(f"saves/{name}.obj", "wb") as file:
pickle.dump(data, file)
except:
os.mkdir("saves")
with open(f"saves/{name}.obj", "wb") as file:
pickle.dump(data, file)
def button_load():
global paused
global pause_key, boss_key
global left_button, right_button, pause_button_change, boss_button
global levels_unlocked
paused = False
pause(None)
file_name = filedialog.askopenfilename(initialdir="saves")
if file_name:
with open(file_name, "r+b") as file:
try:
data = pickle.load(file)
except:
messagebox.showinfo("Error", "Error opening save file")
return
left_button["text"] = "MOVE LEFT: " + data["keybinds"]["left"]
right_button["text"] = "MOVE RIGHT: " + data["keybinds"]["right"]
root.unbind(pause_key)
pause_key = data["keybinds"]["pause"]
pause_button_change["text"] = "PAUSE: " + data["keybinds"]["pause"]
root.bind(pause_key, pause)
root.unbind(boss_key)
boss_key = data["keybinds"]["boss"]
boss_button["text"] = "BOSS: " + data["keybinds"]["boss"]
root.bind(boss_key, boss)
canvas.levels_unlocked = data["unlocked"]
canvas.level_unlock()
canvas.load(data)
if not canvas.current_level and paused:
paused = False
paused_text.destroy()
pause_button.configure(text="PAUSE")
else:
paused = False
pause(None)
def button_pause():
pause(None)
def pause(event):
if canvas.current_level:
global paused
global paused_text
paused = not paused
if paused_text:
paused_text.destroy()
if paused:
paused_text = Button(
root, text="PAUSED", font=("Purisa", 40),
fg=Colour.text_colour, bg=Colour.button_colour,
command=button_pause)
paused_text.place(x=640, y=360, anchor="center")
pause_button.configure(text="UNPAUSE")
else:
pause_button.configure(text="PAUSE")
canvas.timer.pause(paused)
canvas.ball.pause(paused)
canvas.paddle.pause(paused)
def boss(event):
"""Switch to an image to disguise the game"""
if not paused:
pause(event)
global boss_bool
global boss_canvas
boss_bool = not boss_bool
if boss_bool:
root.unbind("<space>")
boss_canvas = Canvas(root, width=1280, height=720)
boss_canvas.place(x=0, y=0)
boss_canvas.create_text(10, 10, text="text")
boss_canvas.create_image(640, 360, image=boss_image)
root.title("Firefox")
else:
root.bind("<space>", pause)
boss_canvas.destroy()
root.title("Brick Breaker Star Collector")
# Keybind defaults
paddle_left_key = "a"
paddle_right_key = "d"
pause_key = "<space>"
boss_key = "b"
# Create window
root = Tk()
root.title("Brick Breaker Star Collector")
root.geometry(f"1280x720")
root.resizable(False, False)
root.configure(bg=Colour.side_backgound)
# Create leaderboard
leaderboard = Leaderboard(root)
# Create menu
menu_title = Label(
root, text="MENU", font=("Purisa", 20), fg=Colour.text_colour,
bg=Colour.side_backgound)
menu_title.place(x=130, y=25, anchor="center")
pause_button = Button(
root, text="PAUSE", font=("Purisa", 11), fg=Colour.text_colour,
bg=Colour.button_colour, command=button_pause)
pause_button.place(x=130, y=70, width=150, height=40, anchor="center")
save_button = Button(
root, text="SAVE", font=("Purisa", 11), fg=Colour.text_colour,
bg=Colour.button_colour, command=button_save)
save_button.place(x=130, y=120, width=150, height=40, anchor="center")
load_button = Button(
root, text="LOAD", font=("Purisa", 11), fg=Colour.text_colour,
bg=Colour.button_colour, command=button_load)
load_button.place(x=130, y=170, width=150, height=40, anchor="center")
retry_button = Button(
root, text="RETRY", font=("Purisa", 11), fg=Colour.text_colour,
bg=Colour.button_colour, command=button_retry)
retry_button.place(x=130, y=220, width=150, height=40, anchor="center")
controls_title = Label(
root, text="CONTROLS", font=("Purisa", 20), fg=Colour.text_colour,
bg=Colour.side_backgound)
controls_title.place(x=130, y=270, anchor="center")
left_button = Button(
root, text=f"MOVE LEFT: {paddle_left_key}", font=("Purisa", 11),
fg=Colour.text_colour, bg=Colour.button_colour, command=button_left)
left_button.place(x=130, y=320, width=150, height=40, anchor="center")
right_button = Button(
root, text=f"MOVE RIGHT: {paddle_right_key}", font=("Purisa", 11),
fg=Colour.text_colour, bg=Colour.button_colour, command=button_right)
right_button.place(x=130, y=370, width=150, height=40, anchor="center")
pause_button_change = Button(
root, text=f"PAUSE: {pause_key}", font=("Purisa", 11),
fg=Colour.text_colour, bg=Colour.button_colour,
command=button_pause_change)
pause_button_change.place(x=130, y=420, width=150, height=40, anchor="center")
boss_button = Button(
root, text=f"BOSS: {boss_key}", font=("Purisa", 11), fg=Colour.text_colour,
bg=Colour.button_colour, command=button_boss_change)
boss_button.place(x=130, y=470, width=150, height=40, anchor="center")
# Boss canvas
boss_canvas = None
boss_bool = False
boss_image = PhotoImage(file="assets/boss.png")
levels_unlocked = 1
paused = False
paused_text = None
# Main game canvas
canvas = GameCanvas(
root, levels_unlocked, leaderboard, paddle_left_key, paddle_right_key)
# Bind default keys
root.bind(pause_key, pause)
root.bind(boss_key, boss)
root.mainloop()