在循环内的 onscreenclick 之间交替 - Python
Alternating between onscreenclick within loop - Python
我正在尝试使用海龟图形在 Python 中创建一个简单的井字游戏,但我在圆圈和十字之间交替时遇到了一些问题。这是我当前代码的部分:
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()
每当我 运行 代码时,乌龟总是输出圆圈。我添加了一个打印语句让我知道它是否交替,这是输出:
1
2
1
2
1
2
1
2
1
我不知道如何“暂停”循环以实际绘制圆或十字。如有任何帮助,我们将不胜感激!
好的,好问题。我不完全确定,但如果你想暂停循环,你需要执行一个 sleep() 函数。这将在括号中输入的给定时间量内暂停循环。示例如下:
import time
print("Printed immediately.")
time.sleep(2.4)
print("Printed after 2.4 seconds.")
如果您想完全结束循环,您应该放入“break 语句”。如下所示:
>>> 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')
无论如何...我希望这对您有所帮助!
我认为您可以通过将 显式 循环转换为 隐式 循环来解决此问题:
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()
每个 onscreenclick()
处理程序轮流切换到另一个处理程序,最终在棋盘被填满时关闭屏幕点击。我们通过设置其中一个处理程序来开始游戏。
无论您做什么,都不要添加对 sleep()
的调用,因为那样会进一步弄乱 turtle 事件模型。
我正在尝试使用海龟图形在 Python 中创建一个简单的井字游戏,但我在圆圈和十字之间交替时遇到了一些问题。这是我当前代码的部分:
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()
每当我 运行 代码时,乌龟总是输出圆圈。我添加了一个打印语句让我知道它是否交替,这是输出:
1
2
1
2
1
2
1
2
1
我不知道如何“暂停”循环以实际绘制圆或十字。如有任何帮助,我们将不胜感激!
好的,好问题。我不完全确定,但如果你想暂停循环,你需要执行一个 sleep() 函数。这将在括号中输入的给定时间量内暂停循环。示例如下:
import time
print("Printed immediately.")
time.sleep(2.4)
print("Printed after 2.4 seconds.")
如果您想完全结束循环,您应该放入“break 语句”。如下所示:
>>> 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')
无论如何...我希望这对您有所帮助!
我认为您可以通过将 显式 循环转换为 隐式 循环来解决此问题:
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()
每个 onscreenclick()
处理程序轮流切换到另一个处理程序,最终在棋盘被填满时关闭屏幕点击。我们通过设置其中一个处理程序来开始游戏。
无论您做什么,都不要添加对 sleep()
的调用,因为那样会进一步弄乱 turtle 事件模型。