Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What am I doing wrong? Why do the actions not work while they're supposed to?

I’ve been using python for around 2 months, I’ve been mostly working with tkinter but today I’ve decided to try out turtle. I’m currently working on a clicker game where when you click a mouse(an image), you receive 1 click, what I’m having problem is that when for example, when you get 10 clicks, something to be printed out, but it isn’t working. I’ve tried:

if clicks == 10:
    print("10 clicks")
    
elif clicks == 20:
    print("20 click")

but it’s not working. They only work if I change the default start clicks in the code itself. Here’s the whole code:

#Imports
import turtle

#Window basic settings
wn = turtle.Screen()
wn.title("Clicker")
wn.bgcolor("light blue")
turtle.setup(800, 500)
wn.cv._rootwindow.resizable(False, False)

#Shape register
wn.register_shape("mouse.gif")

#Mouse
mouse = turtle.Turtle()
mouse.shape("mouse.gif")
mouse.speed(0)

#Clicks
clicks = 0

#Click counter
pen = turtle.Turtle()
pen.hideturtle()
pen.color("white")
pen.penup()
pen.goto(0, -200)
pen.write(f"Clicks:{clicks}", align="center", font=("Courier New", 32, "normal"))

def clicked(x, y):
    global clicks
    clicks += 1
    pen.clear()
    pen.write(f"Clicks:{clicks}", align="center", font=("Courier New", 32, "normal"))
    
mouse.onclick(clicked)

#Actions
if clicks == 10:
    print("10 clicks")
    
elif clicks == 20:
    print("20 click")

#Window main loop
wn.mainloop()

Is the problem the fact that when you click, it writes a whole new text and doesn’t change the current one? How do I fix it?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The if is executed only once. You have to move it to the clicked function.

import turtle

#Window basic settings
wn = turtle.Screen()
wn.title("Clicker")
wn.bgcolor("light blue")
turtle.setup(800, 500)
wn.cv._rootwindow.resizable(False, False)

#Shape register
wn.register_shape("mouse.gif")

#Mouse
mouse = turtle.Turtle()
mouse.shape("mouse.gif")
mouse.speed(0)

#Clicks
clicks = 0

#Click counter
pen = turtle.Turtle()
pen.hideturtle()
pen.color("white")
pen.penup()
pen.goto(0, -200)
pen.write(f"Clicks:{clicks}", align="center", font=("Courier New", 32, "normal"))

def clicked(x, y):
    print("clicked")
    global clicks
    clicks += 1
    pen.clear()
    pen.write(f"Clicks:{clicks}", align="center", font=("Courier New", 32, "normal"))

    if clicks == 10:
        print("10 clicks")
        
    elif clicks == 20:
        print("20 click")

    
mouse.onclick(clicked)


#Actions

#Window main loop
wn.mainloop()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading