Retry button doesn't work as expected

My retry button doesn’t work as expected and should reset my count and turn on clicking.

import turtle
import random
import pygame
turtle.hideturtle()
turtle.penup()
tap = turtle.Turtle()
tap.hideturtle()
high = turtle.Turtle()
high.hideturtle()
high.penup()
num = turtle.Turtle()
num.hideturtle()
count = 0
rando = 2
score = 0
score_dictionary = {}
pygame.init()
sound_file = r'C:\Users\Yarden\OneDrive\Documents\Python\Recording.wav'
high.goto(0, 100)
high.write('Highscore: ' + str(score), align = 'center', font = ('Comic Sans MS', 30))
def play_sound(sound_file):
    pygame.mixer.init()
    pygame.mixer.music.load(sound_file)
    pygame.mixer.music.play()
def click(x, y):
    global count
    if -100 <= x <= 100 and -100 <= y <= 100:
        count += 1
        rando = random.randint(1, 100)
        turtle.hideturtle()
        circle_number(count)
        tap.clear()
        if rando <= count:
            turtle.goto(0, -150)
            turtle.write('You Lost!', align='center', font=('Comic Sans MS', 30))
            turtle.onscreenclick(None)
            play_sound(sound_file)
            turtle.goto(-100, -200)
            turtle.fillcolor('#03fc2c')
            rectangle(200, 50)
            turtle.goto(0, -240)
            turtle.write('Retry', align='center', font=('Fixedsys', 30))
            def click2(x, y):
                if count > score:
                    score = count
                if -100 <= x <= 100 and -25 <= y <= 25:
                    turtle.Screen().onclick(click2)
                    count = 0
                    rando = 0
                scores_sorted = dict(sorted(scores.items(), key=lambda x: x[0], reverse=True))
def circle():
    turtle.tracer(0)
    turtle.hideturtle()
    turtle.goto(0, 0)
    turtle.dot(200, 'Red')
    turtle.color('Black')
    tap.goto(0,-30)
    tap.write('Tap!', align = 'center', font = ('Ink Free', 30))
    turtle.hideturtle()
def circle_number(number):
    circle()
    num.goto(0, -30)
    num.write(number, align = 'center', font = ('Ink Free', 30))
    turtle.update()
def rectangle(width, height):
    turtle.begin_fill()
    for _ in range(2):
        turtle.forward(width)
        turtle.right(90)
        turtle.forward(height)
        turtle.right(90)
    turtle.end_fill()
turtle.speed(0)
turtle.Screen().onclick(click)
circle()
turtle.mainloop()

Hi,

some small feedback:

  1. I would begin by renaming the two functions named: click and click2 to something that is descriptive of their functionality (what they do).
  2. With respect to click2, is it supposed to be nested? I don’t see it being called by the click function. If not, un-nest it.
  3. In the click2 function, it calls itself (with missing arguments, btw). Take another careful look at your code.
  4. Generally, when people post questions, they zoom in on a specific issue. If you’re going to paste a small application which other users may or may not be familiar with, it would help if you included comments so that it gets the reviewer up to speed with your code.

I guess this is the part that you intend to draw the “retry button”.

And then what? What code should run when it’s clicked; and what is your strategy for making that happen?

My retry button doesn’t work as expected and should reset my count and turn upon clicking. Can someone please give me a snippet of code to allow me to execute this properly.

import turtle
import random
import pygame
turtle.hideturtle()
turtle.penup()
tap = turtle.Turtle()
tap.hideturtle()
high = turtle.Turtle()
high.hideturtle()
high.penup()
num = turtle.Turtle()
num.hideturtle()
count = 0
rando = 2
score = 0
state = "playing"
score_dictionary = {}
pygame.init()
sound_file = r'C:\Users\Yarden\OneDrive\Documents\Python\randofolder\Recording.wav'
high.goto(0, 100)
high.write('Highscore: ' + str(score), align = 'center', font = ('Comic Sans MS', 30))
def play_sound(sound_file):
    pygame.mixer.init()
    pygame.mixer.music.load(sound_file)
    pygame.mixer.music.play()
def click(x, y):
    global count
    global state
    if state == "playing":
        if -100 <= x <= 100 and -100 <= y <= 100:
            state = "playing"
            count += 1
            rando = random.randint(1, 100)
            turtle.hideturtle()
            circle_number(count)
            tap.clear()
            if rando <= count:
                state = "lost"
                turtle.goto(0, -150)
                turtle.write('You Lost!', align='center', font=('Comic Sans MS', 30))
                turtle.onscreenclick(None)
                play_sound(sound_file)
                turtle.goto(-100, -200)
                turtle.fillcolor('#03fc2c')
                rectangle(200, 50)
                turtle.goto(0, -240)
                turtle.write('Retry', align='center', font=('Fixedsys', 30))
def click2(x, y):
    global count
    global score
    global state
    if count > score:
        score = count
        if state == "lost":
            if -100 <= x <= 100 and -25 <= y <= 25:
                state = "playing"
                num.clear()
                count = 0
                rando = 0
                scores_sorted = dict(sorted(scores.items(), key=lambda x: x[0], reverse=True))
def circle():
    turtle.tracer(0)
    turtle.hideturtle()
    turtle.goto(0, 0)
    turtle.dot(200, 'Red')
    turtle.color('Black')
    tap.goto(0,-30)
    tap.write('Tap!', align = 'center', font = ('Ink Free', 30))
    turtle.hideturtle()
def circle_number(number):
    circle()
    num.goto(0, -30)
    num.write(number, align = 'center', font = ('Ink Free', 30))
    turtle.update()
def rectangle(width, height):
    turtle.begin_fill()
    for _ in range(2):
        turtle.forward(width)
        turtle.right(90)
        turtle.forward(height)
        turtle.right(90)
    turtle.end_fill()
turtle.speed(0)
turtle.Screen().onclick(click)
circle()
turtle.mainloop()

Is the count reset in click2?

You’ve told pygame about click, but not click2. There are no references to that function, so it’ll never be called.