Advertisement
here2share

# Tk_Color_Game.py

Jan 11th, 2022
1,118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.23 KB | None | 0 0
  1. # Tk_Color_Game.py
  2.  
  3. import tkinter
  4. import random
  5.  
  6. colors=['RED','BLUE','GREEN','PINK','BLACK',
  7.         'YELLOW','ORANGE','PURPLE']
  8. go=0
  9. score=0
  10. wrong=0
  11. time_set=20
  12. timeleft=time_set
  13.  
  14. def startGame(event):
  15.     global go
  16.     global timeleft
  17.     global score
  18.     global wrong
  19.    
  20.    
  21.     if timeleft == time_set:
  22.         go=1
  23.         nextColor('')
  24.         countdown()
  25.     elif timeleft < 1:
  26.         go=0
  27.         timeleft=time_set
  28.         score=0
  29.         wrong=0
  30.         scoreLabel.config(text="Press ENTER To Start")
  31.         wrongsLabel.config(text="Wrong: 0")
  32.         timeLabel.config(text="Time Left: 00")
  33.         label.config(text="---")
  34.  
  35. def nextColor(e):
  36.  
  37.     global score
  38.     global wrong
  39.     global colors
  40.            
  41.     if timeleft > 0 and go:
  42.         if e:
  43.             if e == colors[1]:
  44.                 score += 1
  45.             else:
  46.                 score = max(0, score-1)
  47.                 wrong += 1
  48.            
  49.         a, b = colors.pop(0), colors.pop(0)
  50.         random.shuffle(colors)
  51.         colors += [a, b]
  52.        
  53.         label.config(fg=colors[1], text=colors[0])
  54.        
  55.         scoreLabel.config(text="Score: " + str(score))
  56.         wrongsLabel.config(text="Wrong: " + str(wrong))
  57.  
  58. def countdown():
  59.  
  60.     global timeleft
  61.     global go
  62.    
  63.     if timeleft > -1:
  64.         go=1
  65.         timeLabel.config(text="Time Left: " + str(timeleft).zfill(2))
  66.         timeleft -= 1
  67.         timeLabel.after(1000, countdown)
  68.     else:
  69.         label.config(fg='darkgrey', text='GAME OVER')
  70.                                
  71.  
  72.  
  73. root=tkinter.Tk()
  74. root.title("Tk COLOR GAME")
  75.  
  76. instructions=tkinter.Label(root, text="ONLY Press The Color Of The Words", fg='purple', font=('Helvetica', 24, 'bold'))
  77. instructions.grid(row=0, columnspan=4)
  78.  
  79. scoreLabel=tkinter.Label(root, text="Press ENTER To Start", font=('Helvetica', 12))
  80. scoreLabel.grid(row=1, column=0, padx=18, sticky='W', columnspan=2)
  81.  
  82. wrongsLabel=tkinter.Label(root, text="Wrong: 0", font=('Helvetica', 12))
  83. wrongsLabel.grid(row=1, column=2, sticky='W')
  84.  
  85. timeLabel=tkinter.Label(root, text="Time Left: 00", font=('Helvetica', 12))
  86. timeLabel.grid(row=1, column=3, padx=18, sticky='E')
  87.  
  88. label=tkinter.Label(root, font=('Helvetica', 64, 'bold'))
  89. label.grid(columnspan=4)
  90.  
  91. t=colors[:]
  92. for x in [0,1,2,3]:
  93.     for y in [0,1]:
  94.         color=t.pop()
  95.         e=tkinter.Button(root, width=20, text=color, command=lambda txt=color:nextColor(txt))
  96.         e.grid(column=x, row=y+4, sticky='EW')
  97.  
  98. root.bind('<Return>', startGame)
  99.  
  100. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement