@amasad/

color game

Tkinter

No description

fork
loading
Files
  • main.py

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.39837692736933605","path":"main.py","file":{"path":"main.py","content":{"asEncoding":{"base64":"IyBpbXBvcnQgdGhlIG1vZHVsZXMgIAppbXBvcnQgdGtpbnRlciAKaW1wb3J0IHJhbmRvbSAKICAKIyBsaXN0IG9mIHBvc3NpYmxlIGNvbG91ci4gCmNvbG91cnMgPSBbJ1JlZCcsJ0JsdWUnLCdHcmVlbicsJ1BpbmsnLCdCbGFjaycsIAogICAgICAgICAgICdZZWxsb3cnLCdPcmFuZ2UnLCdXaGl0ZScsJ1B1cnBsZScsJ0Jyb3duJ10gCnNjb3JlID0gMAogIAojIHRoZSBnYW1lIHRpbWUgbGVmdCwgaW5pdGlhbGx5IDMwIHNlY29uZHMuIAp0aW1lbGVmdCA9IDMwCiAgCiMgZnVuY3Rpb24gdGhhdCB3aWxsIHN0YXJ0IHRoZSBnYW1lLiAKZGVmIHN0YXJ0R2FtZShldmVudCk6IAogICAgICAKICAgIGlmIHRpbWVsZWZ0ID09IDMwOiAKICAgICAgICAgIAogICAgICAgICMgc3RhcnQgdGhlIGNvdW50ZG93biB0aW1lci4gCiAgICAgICAgY291bnRkb3duKCkgCiAgICAgICAgICAKICAgICMgcnVuIHRoZSBmdW5jdGlvbiB0byAKICAgICMgY2hvb3NlIHRoZSBuZXh0IGNvbG91ci4gCiAgICBuZXh0Q29sb3VyKCkgCiAgCiMgRnVuY3Rpb24gdG8gY2hvb3NlIGFuZCAKIyBkaXNwbGF5IHRoZSBuZXh0IGNvbG91ci4gCmRlZiBuZXh0Q29sb3VyKCk6IAogIAogICAgIyB1c2UgdGhlIGdsb2JhbGx5IGRlY2xhcmVkICdzY29yZScgCiAgICAjIGFuZCAncGxheScgdmFyaWFibGVzIGFib3ZlLiAKICAgIGdsb2JhbCBzY29yZSAKICAgIGdsb2JhbCB0aW1lbGVmdCAKICAKICAgICMgaWYgYSBnY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gICAgICBmb250ID0gKCdIZWx2ZXRpY2EnLCAxMikpIApzY29yZUxhYmVsLnBhY2soKSAKICAKIyBhZGQgYSB0aW1lIGxlZnQgbGFiZWwgCnRpbWVMYWJlbCA9IHRraW50ZXIuTGFiZWwocm9vdCwgdGV4dCA9ICJUaW1lIGxlZnQ6ICIgKwogICAgICAgICAgICAgIHN0cih0aW1lbGVmdCksIGZvbnQgPSAoJ0hlbHZldGljYScsIDEyKSkgCiAgICAgICAgICAgICAgICAKdGltZUxhYmVsLnBhY2soKSAKICAKIyBhZGQgYSBsYWJlbCBmb3IgZGlzcGxheWluZyB0aGUgY29sb3VycyAKbGFiZWwgPSB0a2ludGVyLkxhYmVsKHJvb3QsIGZvbnQgPSAoJ0hlbHZldGljYScsIDYwKSkgCmxhYmVsLnBhY2soKSAKICAKIyBhZGQgYSB0ZXh0IGVudHJ5IGJveCBmb3IgCiMgdHlwaW5nIGluIGNvbG91cnMgCmUgPSB0a2ludGVyLkVudHJ5KHJvb3QpIAogIAojIHJ1biB0aGUgJ3N0YXJ0R2FtZScgZnVuY3Rpb24gIAojIHdoZW4gdGhlIGVudGVyIGtleSBpcyBwcmVzc2VkIApyb290LmJpbmQoJzxSZXR1cm4+Jywgc3RhcnRHYW1lKSAKZS5wYWNrKCkgCiAgCiMgc2V0IGZvY3VzIG9uIHRoZSBlbnRyeSBib3ggCmUuZm9jdXNfc2V0KCkgCiAgCiMgc3RhcnQgdGhlIEdVSSAKcm9vdC5tYWlubG9vcCgpIA=="},"asBuffer":null},"loaded":true}}
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
# import the modules  
import tkinter 
import random 
  
# list of possible colour. 
colours = ['Red','Blue','Green','Pink','Black', 
           'Yellow','Orange','White','Purple','Brown'] 
score = 0
  
# the game time left, initially 30 seconds. 
timeleft = 30
  
# function that will start the game. 
def startGame(event): 
      
    if timeleft == 30: 
          
        # 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
  
        # 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 score. 
        scoreLabel.config(text = "Score: " + 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 = tkinter.Tk() 
  
# set the title 
root.title("COLORGAME") 
  
# set the size 
root.geometry("375x200") 
  
# add an instructions label 
instructions = tkinter.Label(root, text = "Type in the colour"
                        "of the words, and not the word text!", 
                                      font = ('Helvetica', 12)) 
instructions.pack()  
  
# add a score label 
scoreLabel = tkinter.Label(root, text = "Press enter to start", 
                                      font = ('Helvetica', 12)) 
scoreLabel.pack() 
  
# add a time left label 
timeLabel = tkinter.Label(root, text = "Time left: " +
              str(timeleft), font = ('Helvetica', 12)) 
                
timeLabel.pack() 
  
# add a label for displaying the colours 
label = tkinter.Label(root, font = ('Helvetica', 60)) 
label.pack() 
  
# add a text entry box for 
# typing in colours 
e = tkinter.Entry(root) 
  
# 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() 
  
# start the GUI 
root.mainloop()