Search code examples
pythononclickpython-turtle

How do I make the whole program stop completely after 5 tries (clicks)


How do I make the whole program stop compleatly after 5 trys (click) doesn't matter are they are on turtle or not, but the program after writing you are out of trys should break and when pressed dont put black or red dot just do nothing. Here is the code:

from turtle import *
from random import *
import time
reset()


penup() 
hideturtle() 
speed(0)


bandymai = 0 #tries
taskai = 0   #points
info = Turtle() 
info.penup()
info.goto(-180, 160) 
info.color("Blue") 

def gaudom(x, y):
    goto(x, y)
    if distance(beglys.pos()) < 10:
        color('red') #hit color
        global taskai
        global bandymai
        taskai = taskai + 1
        info.clear()
        info.write(taskai, font = ("Arial", 20, "bold"))
        bandymai = bandymai + 1
     else:
        color('black') #miss color 
        dot(20)
        bandymai = bandymai + 1

screen = getscreen()
screen.onclick( gaudom )


beglys = Turtle()
beglys.color('green')
beglys.shape('square')


for n in range(100):
    x = randint(-200, 200) 
    y = randint(-200, 200) 
    beglys.penup()
    beglys.clear() 
    beglys.goto(x, y)
    time.sleep(1) 
    if taskai == 3:
        info.write('you win!!!', font = ("Arial", 80, "bold"))
        break
    elif bandymai == 5:
        info.write('you are out of trys', font = ("Arial", 50, "bold"))
        break

Solution

  • Add 2 lines at the end of your code.

    screen.onclick(None) #Do nothing when clicking the screen.
    
    screen.mainloop()    #Must be last statement in a turtle graphics program.