Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

# import the

modules import
tkinter import
random
# list of poss ible colour .
colours = [ ' Red ' , ' BLue ' , ' G reen' , ' P1nk' , Black ' ,
' YeLlow , ' 0r ange ' , White' , ' Brown ' ]
s core 0
# the gave
time left, initially *0 seconds.
timeleft = ?
0

# func toon t ha I will s tar th game.


I defstartGae (event ) :

if timeleft == 30:
# st ar I t he count down tżizer .
countdown()

# choose
run thethe next colour.
function to
nextColour()

# Func toon to choose and


# di sp ay the next colour
def nexI Co ï our ( ):

# use the globally declared


#'score'
if a play
game if is
# and 'play' currently
variables above.
timeleft
global score
ent ry box active .
# make the text
e . focu s set ( )

the
i the colour typed is
t colour equal
if e . get( ) . Lowe r ( ) == colours [1 ] . lower ( ):

s core 1

# clear the
e . de î e te( text entry box.
0, tkinter.END)

random . shuI f ł e( colour s )


# change the colour to type , by changing the
# text and the colour to a r andoir colour value
label.config(fg = str(colours[1]), text = str(colours[0] ) )
# upda te thes core .
s core Label . config(text "Score : sI r ( score) )

# Countdown timer function


del countdown ( ) :

global timeleft

# if a i s in play
game if 0:
timeleft
# dec recent the t over .
timeleft -= 1
# update the time left label
tkinter
timeLabel.config(text = "Time left:
sI r( Use le it ) )
# run the func t1on aga in after 1 secon
t iueL abel . a fter ( 1 000, countdown
)

# Dr we r Code

# create a IUI window


root = t kinter .Tk( )

# set the title


root.title("C0L0RGAME”)

# set the size


root.geometry(”?75x200”)

# add an ins I ruct ions label


inst r ucI Label(root, " Type in the
colour" and not the
word text ! " ,

font = ('Helvetica' 12) )


instructions.pack()

# add a score label


s core Label = I kinter . Label( root , text = "P res s enter to sta rt" ,
font = ('Helvetica' 12) )
scoreLabel.pack()
# add a time left label
timeLabel = tkinter.Label(root, text = ”Time left: ” *
s I r ( I ireleI I) , font = ( He1vet ca ' , 12) )

You might also like