Python screen displays coordinates , but won't stop when I click a specific x,y coordinate

Viewed 12

Python screen displays the coordinates after every click, but it won't break the loop when I reach a specific x,y coordinate. It's important to my game and has to be precise. This is what I've put together so far:

import turtle
from turtle import *
from turtle import Turtle, Screen
import random
import time

screen = Screen()

screen.bgcolor("green")

tur = turtle
screen.setup(width=800, height=600)
x = xcor()
y = ycor()
pen = Turtle()
tur = Turtle()
tur = Turtle(shape="turtle")
tur.color("black")
tur.speed("fastest")
tur.color("black")
tur.goto(x=16, y=-132)
while True:
  def get_mouse_click_coor(x, y):
    print(x, y)
    tur.clear()
    tur.write((x, y))
    if get_mouse_click_coor() == (16, -132):
      print("win")
  turtle.onscreenclick(get_mouse_click_coor)
  turtle.mainloop()
1 Answers

You should make the while loop stop.

One way to do this, is by defining a global flag. Put the flag in the condition of the while. When the desired coordinates are met, set the flag to False.

I also recommend to define the get_mouse_click_coor function out of the while loop, because at each loop, this function gets defined, however using as it is, does not make any problem.

play = True

def get_mouse_click_coor(x, y):
    print(x, y)
    tur.clear()
    tur.write((x, y))
    if get_mouse_click_coor() == (16, -132):
      print("win")
      play = False

while play:
  turtle.onscreenclick(get_mouse_click_coor)
  turtle.mainloop()
Related