【问题标题】:How to bind enter key to a function using turtle如何使用乌龟将输入键绑定到函数
【发布时间】:2019-11-08 22:14:45
【问题描述】:

我正在尝试制作一个井字游戏,您可以使用箭头键在棋盘上移动一只乌龟,当您按下回车键时,它会绘制一个圆圈(代码中没有)或一个十字。

我尝试使用与用于移动键的代码类似的代码,但将其替换为 Enter 键和 C 键(用于十字),但它们不起作用并在启动时自动绘制十字。我也尝试过使用猴子,但这不起作用。

import turtle

sc = turtle.Screen()
bo = turtle.Turtle()

#screen

sc.bgcolor("black")
sc.title("tic tac toe")

#board

bo.color("white")
bo.penup()
bo.speed(0)
bo.setposition(-100, -300)
bo.pendown()
bo.setposition(-100, 300)
bo.penup()
bo.setposition(100, 300)
bo.pendown()
bo.setposition(100, -300)
bo.penup()
bo.setposition(-300, 100)
bo.pendown()
bo.setposition(300, 100)
bo.penup()
bo.setposition(300, -100)
bo.pendown()
bo.setposition(-300,-100)
bo.penup()
bo.setposition(0,0)

#naught and cross function

def c()  :
    bo.pendown()
    x = bo.xcor()-80
    y = bo.ycor()+80
    bo.penup()
    bo.setposition(x,y)
    bo.pendown()
    x = bo.xcor()+160
    y = bo.ycor()-160
    bo.setposition(x,y)
    bo.penup()
    x = bo.xcor()-160
    bo.setposition(x,y)
    bo.pendown()
    x = bo.xcor()+160
    y = bo.ycor()+160
    bo.setposition(x,y)
    bo.penup()
    bo.setposition(0,0)

def n() :
    y = bo.ycor()-80
    x = bo.xcor()
    bo.setposition(x, y)
    bo.pendown()
    bo.circle(80)
    bo.penup()
    bo.setposition(0,0)

#movment
def move_left(event):
    x = bo.xcor()
    x -= 200
    if x < -300:
        x = -200
    bo.setx(x)

def move_right(event):
    x = bo.xcor()
    x += 200
    if x > 300:
        x = 200
    bo.setx(x)

def move_down(event):
    y = bo.ycor()
    y -= 200
    if y < -300:
        y = -200
    bo.sety(y)

def move_up(event):
    y = bo.ycor()
    y += 200
    if y > 300:
        y = 200
    bo.sety(y)

#Keybinding

turtle.listen()

turtle.getcanvas().bind("<Left>", move_left)
turtle.getcanvas().bind("<Right>", move_right)
turtle.getcanvas().bind("<Up>", move_up)
turtle.getcanvas().bind("<Down>", move_down)

我最终希望它在你画一个十字的地方,然后按回车,它会在没有十字的地方画一个圆圈。

【问题讨论】:

    标签: python python-3.x turtle-graphics


    【解决方案1】:

    无需下拉至 Tk Canvas 即可配置您的关键事件:

    turtle.getcanvas().bind("<Left>", move_left)
    

    你可以从海龟本身做同样的事情:

    sc.onkey(move_left, "Left")
    

    我在下面重新编写了您的代码,为"Return" 添加了一个关键事件,该事件引起了人们的注意。由于您的游戏没有底层数据结构,因此我将其设置为简单地在两者之间交替:

    from turtle import Screen, Turtle
    
    # naught and cross functions
    
    def cross():
        x, y = board.position()
        board.penup()
        board.setposition(x - 80, y - 80)
        board.pendown()
        board.setposition(x + 80, y + 80)
        board.penup()
        board.setx(x - 80)
        board.pendown()
        board.setposition(x + 80, y - 80)
        board.penup()
        board.home()
    
    def naught():
        board.sety(board.ycor() - 80)
        board.pendown()
        board.circle(80)
        board.penup()
        board.home()
    
    first_player = True
    
    def choose():
        global first_player
    
        if first_player:
            naught()
        else:
            cross()
    
        first_player = not first_player
    
    # movement
    def move_left():
        x = board.xcor() - 200
    
        if x < -300:
            x = -200
    
        board.setx(x)
    
    def move_right():
        x = board.xcor() + 200
    
        if x > 300:
            x = 200
    
        board.setx(x)
    
    def move_down():
        y = board.ycor() - 200
    
        if y < -300:
            y = -200
    
        board.sety(y)
    
    def move_up():
        y = board.ycor() + 200
    
        if y > 300:
            y = 200
    
        board.sety(y)
    
    # screen
    
    screen = Screen()
    screen.bgcolor("black")
    screen.title("tic tac toe")
    
    # board
    
    board = Turtle()
    board.color("white")
    board.speed('fastest')
    
    # vertical lines
    board.penup()
    board.setposition(-100, -300)
    board.pendown()
    board.sety(300)
    board.penup()
    board.setx(100)
    board.pendown()
    board.sety(-300)
    board.penup()
    
    # horizontal lines
    board.setposition(-300, 100)
    board.pendown()
    board.setx(300)
    board.penup()
    board.sety(-100)
    board.pendown()
    board.setx(-300)
    board.penup()
    board.home()
    
    # Keybinding
    
    screen.onkey(move_left, "Left")
    screen.onkey(move_right, "Right")
    screen.onkey(move_up, "Up")
    screen.onkey(move_down, "Down")
    
    screen.onkey(choose, "Return")
    
    screen.listen()
    screen.mainloop()
    

    这允许两个人玩游戏,并且应该是您可以构建的东西。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-24
      相关资源
      最近更新 更多