I'm new to python and I programed a tic tac toe with an AI that plays against you. Everything is working but I used textboxes to inform the AI what the player chose. Now I want to upgrade my game so that the player can click on the box he wants to fill instead of typing it in the textbox. My idea was to use onscreenclick()
but I'm having some issues. onscreenclick()
returns the coordinates that have been clicked on the canvas and I want to use a function to determine in which box the player clicked.
I got this:
from turtle import *
def whichbox(x,y): #obviously i got 9 boxes but this is just an example for box 1
if x<-40 and x>-120:
if y>40 and y<120:
return 1
else:
return 0
else:
return 0
box=onscreenclick(whichbox)
print(box)
It's obvious that I want box to be 0 or 1 in this case but instead the value of box is None
. Does anyone know how to fix this? It has to do something with the variable box
because I if replace return 1
with print("1")
it works. I assume that the variable gets defined to quickly.
The second question that I have is if its possible pause the programm until the player clicked on a box but its more important to look at the first problem first.