-
Notifications
You must be signed in to change notification settings - Fork 1
/
Code.py
281 lines (206 loc) · 10.4 KB
/
Code.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
# import the modules
import tkinter
from tkinter import*
from tkinter import CENTER, ttk, messagebox
from tkinter import Tk, Label, Button
import tkinter.messagebox
import random
import os
from tkinter.ttk import *
import tkinter as tk
import sys
from tkinter.messagebox import showinfo
# list of possible colour.
colours = ['Red','Blue','Green','Pink','Black',
'Yellow','Orange','White','Purple','Brown','Grey']
score = 1
# the game time left, initially 90 seconds.
timeleft = 90
# function that will start the game.
def startGame(event):
if timeleft == 90:
# start the countdown timer.
countdown()
# run the function to
# choose the next colour.
nextColour()
# Function to choose and
# display the next colour.
def nextColour():
# use the globally declared 'score'
# and 'play' variables above.
global score
global timeleft
# if a game is currently in play
if timeleft > 0:
# make the text entry box active.
e.focus_set()
# if the colour typed is equal
# to the colour of the text
if e.get().lower() == colours[1].lower():
score += 1
else:
score -= 1
# clear the text entry box.
e.delete(0, tkinter.END)
random.shuffle(colours)
# change the colour to type, by changing the
# text _and_ the colour to a random colour value
label.config(fg = str(colours[1]), text = str(colours[0]))
# update the points.
scoreLabel.config(text = "Points: " + str(score))
# Countdown timer function
def countdown():
global timeleft
# if a game is in play
if timeleft > 0:
# decrement the timer.
timeleft -= 1
# update the time left label
timeLabel.config(text = "Time left: "
+ str(timeleft))
# run the function again after 1 second.
timeLabel.after(1000, countdown)
# Driver Code
# create a GUI window
root = tk.Tk()
# set the title
root.title("Color Game For Linux 15.0!")
my_menu= Menu(root)
root.config(menu=my_menu)
#This will be the popup to show keyboard shortcuts.
def KeyboardShortcuts():
root = tk.Toplevel()
root.resizable(0,0)
root.attributes("-topmost", True)
root.title("Keyboard Shortcuts.")
root.tk.call('wm', 'iconphoto', root._w, tkinter.PhotoImage(file='ColorGameForLinux.png'))
labelTitle = ttk.Label(root,font=("Ubuntu", 26,"bold","underline"),anchor='center', text="Keyboard shortcuts.")
label = ttk.Label(root,font=("Ubuntu", 16,"bold",),anchor='center', text="Press the 'Escape key' to exit the game.")
labelTitle.pack(side="top",fill="x",pady=1)
label.pack(side="top", fill="x", pady=2)
B1 = tk.Button(root, text="Exit",font=("ubuntu",28),bg="pink",activebackground='#23d18b', command = root.destroy)
B1.pack()
def whoMadeThisGame():
root = tk.Toplevel()
root.attributes("-topmost", True)
root.resizable(0,0)
root.title("Who made this game?")
root.tk.call('wm', 'iconphoto', root._w, tkinter.PhotoImage(file='ColorGameForLinux.png'))
labelTitle = ttk.Label(root,font=("Ubuntu", 26,"bold","underline"),anchor='center', text="Who made this game?")
label = ttk.Label(root,font=("Ubuntu", 16,"bold",),anchor='center', text="Jonathan Steadman has made this game.")
labelTitle.pack(side="top",fill="x",pady=1)
label.pack(side="top", fill="x", pady=2)
B1 = tk.Button(root, text="Exit",font=("ubuntu",28),bg="pink",activebackground='#23d18b', command = root.destroy)
B1.pack()
def WhenWillThisGameEndSupport():
root = tk.Toplevel()
root.attributes("-topmost", True)
root.resizable(0,0)
root.title("When did this game end support?")
root.tk.call('wm', 'iconphoto', root._w, tkinter.PhotoImage(file='ColorGameForLinux.png'))
labelTitle = ttk.Label(root,font=("Ubuntu", 26,"bold","underline"),anchor='center', text="When did this game end support?")
label = ttk.Label(root,font=("Ubuntu", 16,"bold",),anchor='center', text="This game is no longer supported from the 15th Of August 2022!")
label1 = ttk.Label(root,font=("Ubuntu", 16,"bold",),anchor='center', text="The reason for this is because I want to move onto other projects.")
label2 = ttk.Label(root,font=("Ubuntu", 16,"bold",),anchor='center', text="Also I feel like this game is now completed.")
labelTitle.pack(side="top",fill="x",pady=1)
label.pack(side="top", fill="x", pady=2)
label1.pack(side="top", fill="x", pady=3)
label2.pack(side="top", fill="x", pady=3)
B1 = tk.Button(root, text="Exit",font=("ubuntu",28),bg="pink",activebackground='#23d18b', command = root.destroy)
B1.pack()
def howtoplay():
root = tk.Toplevel()
root.attributes("-topmost", True)
root.resizable(0,0)
root.title("How to play this game?")
root.tk.call('wm', 'iconphoto', root._w, tkinter.PhotoImage(file='ColorGameForLinux.png'))
labelTitle = ttk.Label(root,font=("Ubuntu", 26,"bold","underline"),anchor='center', text="How to play this game?")
label = ttk.Label(root,font=("Ubuntu", 16,"bold",),anchor='center', text="You will need to type down the color of the text. Not what the text says what the color is.")
label3 = ttk.Label(root,font=("Ubuntu", 16,"bold",),anchor='center',text="You will need to be fast to get more points but needs to be written correctly and the right color.")
labelw = ttk.Label(root,font=("Ubuntu", 16,"bold",),anchor='center',text="You will lose one point if you make a mistake.")
label4 = ttk.Label(root,font=("Ubuntu", 16,"bold",),anchor='center',text="Press the enter key to submit you answer.")
labelTitle.pack(side="top",fill="x",pady=1)
label.pack(side="top", fill="x", pady=2)
label3.pack(side="top", fill="x", pady=3)
labelw.pack(side="top",fill="x",pady=4)
label4.pack(side="top",fill="x", pady=5)
B1 = tk.Button(root, text="Exit",font=("ubuntu",28),bg="pink",activebackground='#23d18b', command = root.destroy)
B1.pack()
file_menu= Menu(my_menu,background="pink",activebackground="#23d18b")
my_menu.add_cascade(label="About:",font=("Ubuntu",18),activebackground="#23d18b", menu=file_menu)
file_menu.add_command(label="How to play this game?",font=("Ubuntu",18),activebackground="#23d18b",background="pink",command=howtoplay)
file_menu.add_command(label="Who made this game?",font=("Ubuntu",18),activebackground="#23d18b",background="pink",command=whoMadeThisGame)
file_menu.add_command(label="When did this game end support?",font=("Ubuntu",18),activebackground="#23d18b",background="pink",command=WhenWillThisGameEndSupport)
file_menu.add_command(label="Keyboard shortcuts.",font=("Ubuntu",18),activebackground="#23d18b",background="pink",command=KeyboardShortcuts)
root.tk.call('wm', 'iconphoto', root._w, tkinter.PhotoImage(file='ColorGameForLinux.png'))
# set the size
root.geometry("820x589")
#Cant maximise the software!
root.resizable(0,0)
style = Style()
Help = tkinter.Label(root, text = "Color Game For Linux 15.0!", font = ('Ubuntu', 24,"bold","underline"))
Help.pack()
# add a time left label
timeLabel = tkinter.Label(root, text = "Time left " +
str(timeleft), font = ('Ubuntu', 28))
timeLabel.pack()
# add a score label
scoreLabel = tkinter.Label(root, text = "Press enter to start:",
font = ('Ubuntu', 28))
scoreLabel.pack()
# add a label for displaying the colours
label = tkinter.Label(root, font = ('Ubuntu', 170))
label.pack()
# add a text entry box for
# typing in colours
e = tkinter.Entry(root,font= ("Ubuntu", 28))
# run the 'startGame' function
# when the enter key is pressed
root.bind('<Return>', startGame)
e.pack()
# set focus on the entry box
e.focus_set()
# Reset the game:
def restart():
"""Restarts the current program.
Note: this function does not return. Any cleanup action (like
saving data) must be done before calling this function."""
python = sys.executable
os.execl(python, python, * sys.argv)
#Function to ask the user when they press the escape key to exit the software or not.
def KeyPressEsc(event):
root = tk.Toplevel()
root.attributes("-topmost", True)
root.resizable(0,0)
root.title("Confirm to exit the game:")
root.tk.call('wm', 'iconphoto', root._w, tkinter.PhotoImage(file='ColorGameForLinux.png'))
labelTitle = ttk.Label(root,font=("Ubuntu", 26,"bold","underline"),anchor='center', text="Confirm to exit the game:")
label = ttk.Label(root,font=("Ubuntu", 16,"bold",),anchor='center', text="Are you sure you want to leave this game?")
labelTitle.pack(side="top",fill="x",pady=1)
label.pack(side="top", fill="x", pady=2)
B1 = tk.Button(root, text="Yes",font=("ubuntu",28),bg="pink",activebackground='#23d18b', command = root.quit)
B2 = tk.Button(root, text="No",font=("ubuntu",28),bg="pink",activebackground='#23d18b', command = root.destroy)
B1.pack(side=tkinter.LEFT, anchor=CENTER)
B2.pack(side=tkinter.RIGHT, anchor=CENTER)
def on_closing():
root = tk.Toplevel()
root.attributes("-topmost", True)
root.resizable(0,0)
root.title("Confirm to exit the game:")
root.tk.call('wm', 'iconphoto', root._w, tkinter.PhotoImage(file='ColorGameForLinux.png'))
labelTitle = ttk.Label(root,font=("Ubuntu", 26,"bold","underline"),anchor='center', text="Confirm to exit the game:")
label = ttk.Label(root,font=("Ubuntu", 16,"bold",),anchor='center', text="Are you sure you want to leave this game?")
labelTitle.pack(side="top",fill="x",pady=1)
label.pack(side="top", fill="x", pady=2)
B1 = tk.Button(root, text="Yes",font=("ubuntu",28),bg="pink",activebackground='#23d18b', command = root.quit)
B2 = tk.Button(root, text="No",font=("ubuntu",28),bg="pink",activebackground='#23d18b', command = root.destroy)
B1.pack(side=tkinter.LEFT, anchor=CENTER)
B2.pack(side=tkinter.RIGHT, anchor=CENTER)
root.bind('<Escape>', KeyPressEsc)
Tbutton = tkinter.Button(root,text = "Restart The Game",font=("ubuntu",28),bg="pink",activebackground='#23d18b',border=16, command = restart)
Tbutton.place(relx=0.5, rely=0.9, anchor=CENTER)
#When the user press the escape key they will see a popup to exit this game.
#When the user press the escape key the they will be able to leave the software by seeing the popup.
root.protocol("WM_DELETE_WINDOW", on_closing)
root.mainloop()