Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_Color_Game.py
- import tkinter
- import random
- colors=['RED','BLUE','GREEN','PINK','BLACK',
- 'YELLOW','ORANGE','PURPLE']
- go=0
- score=0
- wrong=0
- time_set=20
- timeleft=time_set
- def startGame(event):
- global go
- global timeleft
- global score
- global wrong
- if timeleft == time_set:
- go=1
- nextColor('')
- countdown()
- elif timeleft < 1:
- go=0
- timeleft=time_set
- score=0
- wrong=0
- scoreLabel.config(text="Press ENTER To Start")
- wrongsLabel.config(text="Wrong: 0")
- timeLabel.config(text="Time Left: 00")
- label.config(text="---")
- def nextColor(e):
- global score
- global wrong
- global colors
- if timeleft > 0 and go:
- if e:
- if e == colors[1]:
- score += 1
- else:
- score = max(0, score-1)
- wrong += 1
- a, b = colors.pop(0), colors.pop(0)
- random.shuffle(colors)
- colors += [a, b]
- label.config(fg=colors[1], text=colors[0])
- scoreLabel.config(text="Score: " + str(score))
- wrongsLabel.config(text="Wrong: " + str(wrong))
- def countdown():
- global timeleft
- global go
- if timeleft > -1:
- go=1
- timeLabel.config(text="Time Left: " + str(timeleft).zfill(2))
- timeleft -= 1
- timeLabel.after(1000, countdown)
- else:
- label.config(fg='darkgrey', text='GAME OVER')
- root=tkinter.Tk()
- root.title("Tk COLOR GAME")
- instructions=tkinter.Label(root, text="ONLY Press The Color Of The Words", fg='purple', font=('Helvetica', 24, 'bold'))
- instructions.grid(row=0, columnspan=4)
- scoreLabel=tkinter.Label(root, text="Press ENTER To Start", font=('Helvetica', 12))
- scoreLabel.grid(row=1, column=0, padx=18, sticky='W', columnspan=2)
- wrongsLabel=tkinter.Label(root, text="Wrong: 0", font=('Helvetica', 12))
- wrongsLabel.grid(row=1, column=2, sticky='W')
- timeLabel=tkinter.Label(root, text="Time Left: 00", font=('Helvetica', 12))
- timeLabel.grid(row=1, column=3, padx=18, sticky='E')
- label=tkinter.Label(root, font=('Helvetica', 64, 'bold'))
- label.grid(columnspan=4)
- t=colors[:]
- for x in [0,1,2,3]:
- for y in [0,1]:
- color=t.pop()
- e=tkinter.Button(root, width=20, text=color, command=lambda txt=color:nextColor(txt))
- e.grid(column=x, row=y+4, sticky='EW')
- root.bind('<Return>', startGame)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement