Python Forum
Need Help for homework
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need Help for homework
#1
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
import tkinter
import random
 
colours = ['Red', 'Blue', 'Green', 'Pink', 'Black', 'Yellow', 'Orange', 'White', 'Purple', 'Brown']
color_hex_codes = ['#FF0000', '#0000FF', '#008000', '#FFC0CB', '#000000', '#FFFF00', '#FFA500', '#FFFFFF', '#800080', '#A52A2A']
 
score = 0
timeleft = 30
timerid = None
 
def startGame(event=None):
        global score
        global timeleft
        global timerid
     
    score = 0
    timeleft = 30
     
    if timerid is not None:
        root.after_cancel(timerid)
        timerid = None
     
    if timeleft == 30:
        countdown()
    nextColour()
 
def nextColour():
    global score
    global timeleft
 
    if timeleft > 0:
        random.shuffle(colours)
        color_to_display = colours[0]
        color_to_type = colours[1]
 
        label.config(fg=color_to_display, text=color_to_type)
        update_buttons(color_to_display)
         
def update_buttons(correct_color):
        for btn, color_hex in zip(color_buttons, color_hex_codes):
            btn.config(state=tkinter.NORMAL, bg=color_hex, command=lambda c=btn['text']: check_color(c, correct_color))
     
def check_color(selected_color, correct_color):
    global score
    global timeleft
 
    if timeleft > 0 and selected_color.lower() == correct_color.lower():
        score += 1
        scoreLabel.config(text="Score: " + str(score))
        nextColour()
 
def countdown():
    global timeleft
    global timerid
     
    if timeleft > 0:
            timeleft -= 1
            timeLabel.config(text="Time left: " + str(timeleft))
            timerid = root.after(1000, countdown)
    else:
        end_message.config(text="Game Over! Final Score: " + str(score))
        update_buttons("")
         
def on_enter_press(event):
    startGame()
 
root = tkinter.Tk()
root.title("COLORGAME")
root.geometry("600x500")
 
root.bind('<Return>', on_enter_press)  # Bind Enter key to start/restart the game
 
header_label = tkinter.Label(root, text="Color Matching Game", font=('Helvetica', 18))
header_label.pack(pady=20)
 
instructions = tkinter.Label(root, text="Click on the color"
                        " of the words, and not the word text!",
                        font=('Helvetica', 14))
instructions.pack()
 
scoreLabel = tkinter.Label(root, text="Score: " + str(score), font=('Helvetica', 16))
scoreLabel.pack(pady=10)
 
timeLabel = tkinter.Label(root, text="Time left: " + str(timeleft), font=('Helvetica', 16))
timeLabel.pack()
 
label = tkinter.Label(root, font=('Helvetica', 60))
label.pack(pady=20)
 
color_buttons_frame = tkinter.Frame(root)
color_buttons_frame.pack()
 
color_buttons = []
for color, color_hex in zip(colours, color_hex_codes):
    btn = tkinter.Button(color_buttons_frame, text=color, state=tkinter.DISABLED, bg=color_hex, padx=20, pady=10)
    btn.pack(side=tkinter.LEFT, padx=5)
    color_buttons.append(btn)
 
start_button = tkinter.Button(root, text="Start Game", font=('Helvetica', 14), command=startGame)
start_button.pack(pady=20)
 
end_message = tkinter.Label(root, text="", font=('Helvetica', 16))
end_message.pack()
 
root.mainloop()
Hi guys I have encounter some error when run the code. It show this error but I can't find where:
1
2
File <tokenize>:16
    score = 0
^
IndentationError: unindent does not match any outer indentation level

Appreciate if can give me some help Smile

Thanks,
Tristan
Reply
#2
(Jan-29-2024, 06:14 AM)Treeeeee_0923 Wrote: IndentationError: unindent does not match any outer indentation level
Look at lines 12,13,14
P.
Treeeeee_0923 likes this post
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#3
A slight problem: all text is black, so I only get a point when the colour written in large letters is black!

Maybe check that!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020