井字棋游戏的Python实现与拓展
1. 鼠标点击坐标获取
首先,我们要了解如何在 turtle 模块中获取鼠标点击的坐标。以下是实现这一功能的代码:
import turtle as t
# 定义函数get_xy(),用于打印点击的x和y坐标
def get_xy():
t.hideturtle() # 隐藏海龟,避免看到光标移动
def get_xy(x, y):
print(f'(x, y) is ({x}, {y})')
t.onscreenclick(get_xy) # 绑定鼠标点击事件到get_xy()函数
t.listen() # 检测鼠标点击和键盘按键等事件
t.done()
try:
t.bye()
except t.Terminator:
print('exit turtle')
运行 mouse_click.py ,随机点击屏幕几次,你会看到类似如下的输出:
(x, y) is (-46.0, 109.0)
(x, y) is (14.0, -9.0)
(x, y) is (-185.0, -19.0)
(x, y) is (-95.0, 109.0)
(x, y) is (13.0, -81.0)
这个过程可以用以下流程图表示:
graph TD;
A[开始] --> B[导入turtl
超级会员免费看
订阅专栏 解锁全文
1484

被折叠的 条评论
为什么被折叠?



