这是一个下棋的小程序,运行后,用鼠标点击左键和右键下棋
import turtle
myscreen = turtle.Screen()
mypen.speed(0)
# 定义画笔移动函数
def moveto(x,y):
mypen.penup()
mypen.goto(x,y)
mypen.pendown()
# 窗口标题、颜色、大小
myscreen.title("星罗棋布")
myscreen.bgcolor("tan")
size = myscreen.window_height()
myscreen.setup(size+50, size+100)
# 书写“五子连珠”
moveto(0,size/2)
mypen.write("五子连珠", font=("文泉驿正黑", 25), align="center") # 文泉驿正黑
# 棋盘格
moveto(-size/2,size/2)
for i in range(2):
for j in range(14):
mypen.forward(size)
mypen.backward(size)
mypen.right(90)
mypen.forward(size/14)
mypen.left(90)
mypen.forward(size)
mypen.backward(size)
mypen.left(90)
# 落黑棋
def add_black(x,y):
moveto(x,y)
mypen.dot(size/18,"black")
# 落白棋
def add_white(x, y):
moveto(x, y)
mypen.dot(size/18, "white")
#撤回功能
def un_do():
mypen.undo()
myscreen.onclick(add_black, btn=1)
myscreen.onclick(add_white, btn=3)
myscreen.listen()
myscreen.onkey(un_do,"q")
turtle.done()