简体   繁体   English

Turtle-graphics onkey 事件不重复

[英]Turtle-graphics onkey event not repeating

I write this code to make a pong game.我编写这段代码是为了制作一个乒乓球游戏。 Everything works fine except for the left paddle.一切正常,除了左桨。 When I press the buttons that control the movements and I keep them pressed, the paddles moves constantly until key release.当我按下控制运动的按钮并按住它们时,桨叶会不断移动,直到按键释放。 Except for the "w" key.除了“w”键。 This key makes the paddle move for just 1 step and then stops.该键使桨叶仅移动 1 步然后停止。 If I want to move, I have to release and press again.如果我想移动,我必须松开并再次按下。 The onkey events uses the same function of the "down" button, so I assume it's correct. onkey 事件使用与“向下”按钮相同的 function,所以我认为它是正确的。 I tried to change onkey with onkeypressed but it doesn't work.我试图用 onkeypressed 改变 onkey 但它不起作用。 Below is the code: the Paddle module contains Paddle, a class that inherits from the Turtle class, and the functions go_up and go_down.下面是代码:Paddle 模块包含 Paddle、一个继承自 Turtle class 的 class 以及函数 go_up 和 go_down。

from turtle import Screen, Turtle
from paddle import Paddle

screen = Screen()
screen.bgcolor("black")
screen.setup(width=800, height=600)
screen.title("Pong")
screen.tracer(0)

l_paddle = Paddle((-350, 0))
r_paddle = Paddle((350, 0))

screen.listen()
screen.onkey(r_paddle.go_up, "Up")
screen.onkey(r_paddle.go_down, "Down")
screen.onkey(l_paddle.go_up, "s")
screen.onkey(l_paddle.go_down, "w")

game_is_on = True
while game_is_on:
    screen.update()
screen.exitonclick()

You can try using screen.onkeypress() instead of screen.onkey() ;您可以尝试使用screen.onkeypress()而不是screen.onkey() I had this issue before, and switching to onkeypress() solved it.我之前遇到过这个问题,切换到onkeypress()解决了它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM