1

I am trying to create a simple tic-tac-toe game in Python using turtle graphics and I was having some trouble alternating between the circles and crosses. This is the section from my current code:

drawBoard()

tracker = True
count = 0
while count < 9:
    if tracker:
        turtle.onscreenclick(position_circle)       
        print(1)
        tracker = False
    elif not tracker:
        turtle.onscreenclick(position_cross)       
        print(2)
        tracker = True
    count += 1

turtle.done()

Whenever I run the code, the turtle always outputs circles. I added a print statement to let me know whether it was alternating and this is the output:

1
2
1
2
1
2
1
2
1

I don't know to how to "pause" the loop to actually draw the circle or cross. Any help would be appreciated!

2 Answers 2

1

I think you could fix this problem by turning your explicit loop into an implicit one:

import turtle

# ... your existing code here

count = 0

def position_circle(x, y):
    global count

    turtle.onscreenclick(None)  # disable this handler

    # ... your existing code here

    drawBoard()
    count += 1

    if count < 9:
        turtle.onscreenclick(position_cross)  # next handler

def position_cross(x, y):
    global count

    turtle.onscreenclick(None)  # disable this handler

    # ... your existing code here

    drawBoard()
    count += 1

    if count < 9:
        turtle.onscreenclick(position_circle)  # next handler

# ... your existing code here

turtle.onscreenclick(position_circle)

turtle.done()

Each onscreenclick() handler switches to the other as turns alternate, eventually turning off screen clicks when the board has been filled. We start the game off by setting one of the handlers.

Whatever you do, don't add a call to sleep(), as that will further mess up the turtle event model.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, thanks a lot for your answer, it works well! I was unsure about the purpose of disabling the handler. Could you please explain this?
@TechRooster, if you don't disable the handler, a new click could come in while you're still handling the previous one. This can both leave your code in a confused state and appear like a recursion.
0

Okay, excellent question. I'm not entirely sure, but if you want to pause the loop you would need to enact a sleep() function. This will pause the loop for a given amount of time as entered in the parentheses. Example below:

import time

print("Printed immediately.")
time.sleep(2.4)
print("Printed after 2.4 seconds.")

If you wanted to completely end the loop you should put in a "break statement." As seen below:

>>> for n in range(2, 10):
...     for x in range(2, n):
...         if n % x == 0:
...             print(n, 'equals', x, '*', n//x)
...             break
...     else:
...         # loop fell through without finding a factor
...         print(n, 'is a prime number')

Anyways... I hope this helped!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.