Getting Turtle in Python to recognize click events [duplicate]

2024/11/20 8:42:39

I'm trying to make Connect 4 in python, but I can't figure out how to get the coordinates of the screen click so I can use them. Right now, I want to draw the board, then have someone click, draw a dot, then go back to the top of the while loop, wipe the screen and try again. I've tried a couple different options but none have seemed to work for me.

def play_game():
"""
When this function runs, allows the user to play a game of Connect 4
against another person
"""
turn = 1
is_winner = False
while is_winner == False:# Clears screenclear()# Draws empty boardcenters = draw_board()# Decides whose turn it is, change color appropriatelyif turn % 2 == 0:color = REDelse:color = BLACK# Gets coordinates of clickpenup()onscreenclick(goto)dot(HOLE_SIZE, color)turn += 1
Answer

As well intentioned as the other answers are, I don't believe either addresses the actual problem. You've locked out events by introducing an infinite loop in your code:

is_winner = False
while is_winner == False:

You can't do this with turtle graphics -- you set up the event handlers and initialization code but turn control over to the main loop event handler. My following rework show how you might do so:

import turtlecolors = ["red", "black"]
HOLE_SIZE = 2turn = 0
is_winner = Falsedef draw_board():passreturn (0, 0)def dot(color):turtle.color(color, color)turtle.stamp()def goto(x, y):global turn, is_winner# add code to determine if we have a winnerif not is_winner:# Clears screenturtle.clear()turtle.penup()# Draws empty boardcenters = draw_board()turtle.goto(x, y)# Decides whose turn it is, change color appropriatelycolor = colors[turn % 2 == 0]dot(color)turn += 1else:passdef start_game():"""When this function runs, sets up a newgame of Connect 4 against another person"""global turn, is_winnerturn = 1is_winner = Falseturtle.shape("circle")turtle.shapesize(HOLE_SIZE)# Gets coordinates of clickturtle.onscreenclick(goto)start_game()turtle.mainloop()

Run it and you'll see the desired behavior you described.

https://en.xdnf.cn/q/118441.html

Related Q&A

Delete last widget from gridlayout

I have a Grid layout in which I add Qlineedits at runtime. while pushing the button I want to delete the last qline edit from the gridlaout Why does this function delete all qlinedits at the same time …

Counting unique words

Question:Devise an algorithm and write the Python code to count the number of unique words in a given passage. The paragraph may contain words with special characters such as !, ?, ., , , : and ; and …

Django cant find template dir?

Originally I had just one app in my Django project, the templates consisted of an index.html a detail.html and a layout.html ... the index and detail files extended the layout. They all lived in the sa…

Python Programming Loop

Im doing an assignment where I have to conduct a quiz for different topics. This is my code so far.print("Hello and welcome to Shahaads quiz!") #Introduction name = input("What is your n…

How to fix stale element error without refreshing the page

Trying to get details of Tyres on this page. https://eurawheels.com/fr/catalogue/INFINY-INDIVIDUAL . Each tyre has different FINITIONS. The price and other details are different for each FINITIONS. I w…

Fastest way in numpy to get distance of product of n pairs in array

I have N number of points, for example: A = [2, 3] B = [3, 4] C = [3, 3] . . .And theyre in an array like so: arr = np.array([[2, 3], [3, 4], [3, 3]])I need as output all pairwise distances in BFS (Bre…

How to get argument to ignore part of message

I just wondering how to get the if statement(if 0 < int(message.content)< 153:) to only test part of the message, not the full message.content. Eg: if I put in 1s 100, I want it to test if ONLY t…

how can I limit the access in Flask

I create a project to simulate login my companys website.And put it in my server to let others to use.But the company website has a limit with single ip can only open 2 sessions.So when more than 2 my …

Multiple images numpy array into blocks

I have a numpy array with 1000 RGB images with shape (1000, 90, 90, 3) and I need to work on each image, but sliced in 9 blocks. Ive found many solution for slicing a single image, but how can I obtai…

Python - Transpose columns to rows within data operation and before writing to file

I have developed a public and open source App for Splunk (Nmon performance monitor for Unix and Linux Systems, see https://apps.splunk.com/app/1753/)A master piece of the App is an old perl (recycled, …