如何使用tu将enter键绑定到函数

2024-04-25 23:41:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我想做一个井字游戏,你用箭头键在棋盘上移动一只乌龟,当你按回车键时,它会画一个圆圈(代码中为零)或一个十字架

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

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)

我最终想让它,一旦你画了一个十字,按下回车键,它就会在没有十字的地方画一个圆


Tags: eventmoveifbinddefscboturtle
1条回答
网友
1楼 · 发布于 2024-04-25 23:41:12

无需下拉到TkCanvas来配置密钥事件:

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()

这允许两个人玩游戏,应该是你可以建立的东西

相关问题 更多 >