Python Turtle中的onkeypress()和listen()问题 [英] Problem with onkeypress() and listen() in Python Turtle
问题描述
我对一个功能有疑问.我使用的是Python 3.7,当我尝试使用 onkeypress()
函数时,什么也没有发生.我尝试对其进行检查,但是当我按键时,turtle模块没有反应.
I have a problem with one function. I use Python 3.7, and when I try to use the onkeypress()
function, nothing happens. I try to check it, but the turtle module doesn't react when I press keys.
我尝试使用"w"键将桨向上移动.但这是行不通的.以下是我的* .py文件:
I try to move my paddle up using the 'w' key. But it doesn't work. Below are my *.py files:
main.py
import elements
import turtle
#Windows settings
window = turtle.Screen()
window.title("Pong game by Kosa")
window.bgcolor('black')
window.setup(width=800, height=600)
window.tracer(0)
paletka_1 = elements.Objects()
paletka_1.paddle_a()
window.onkeypress(paletka_1.paddle_a_up(), "w")
window.listen()
while True:
window.update()
elements.py
elements.py
import turtle
class Objects:
def __init__(self):
#Paddle A
#self.paddle_b = turtle.Turtle()
# #Paddle B
#Ball
self.ball = turtle.Turtle()
def paddle_a(self):
paddle_a_x = -350
#paddle_a_y = 0
self.paddle_a = turtle.Turtle()
self.paddle_a.speed(0)
self.paddle_a.shape("square")
self.paddle_a.shapesize(stretch_wid=5, stretch_len=1)
self.paddle_a.color('green')
self.paddle_a.penup()
self.paddle_a.goto(paddle_a_x, 0)
def paddle_b(self):
paddle_b_x = -350
paddle_b_y = 0
self.paddle_b.speed(0)
self.paddle_b.shape("square")
self.paddle_b.shapesize(stretch_wid=5, stretch_len=1)
self.paddle_b.color('green')
self.paddle_b.penup()
self.paddle_b.goto(paddle_b_x, paddle_b_y)
def ball(self):
self.ball.speed(0)
self.ball.shape("square")
self.ball.color('white')
self.ball.penup()
self.ball.goto(0, 0)
def paddle_a_up(self):
y = self.paddle_a.ycor()
y += 20
self.paddle_a.sety(y)
print(y)
def paddle_b_up(self):
y = self.paddle_b.ycor()
y += 20
self.paddle_b.sety(y)
程序启动后得到的结果是:我可以按下按键,但是拨片没有变化.你能找到我的错误吗?我在 paddle_a_up()
中添加了 print(y)
,只是为了确保它能正常工作.我得到 print()
的结果.
What I get when program starts: I can push keys, but no change in my paddle. Can you find my mistake? I added print(y)
in paddle_a_up()
just to make sure, that its works. I get the result of print()
.
这很奇怪,因为没有错误.
It's strange, because there is no error.
推荐答案
这是设置事件处理程序时常见的初学者错误:
This is a common beginner's error when setting event handlers:
window.onkeypress(paletka_1.paddle_a_up(), "w")
您不想调用 paddle_a_up
,而是想传递该事件,以便在事件发生时调用其他代码:
You don't want to call paddle_a_up
, you want to pass it on for some other code to call when the event happens:
window.onkeypress(paletka_1.paddle_a_up, "w")
尝试一下,看看它是否更适合您.至于其余的代码,我有一些建议:
Try that to see if it works better for you. As far as the rest of your code goes, I've some suggestions:
window.tracer(0)
在程序运行之前,避免使用 tracer()
和 update()
,否则只会使开发和调试过程变得复杂.仅在需要时再添加它们-如果程序令您满意,则将它们排除在外.
Avoid tracer()
and update()
until your program is working otherwise it'll just complicate the development and debug process. Only add them back if you need them -- if the program works to your satisfaction, leave them out.
while True:
window.update()
实际上,此循环应该是对 mainloop()
的调用,以将控制权移交给tkinter的事件处理程序:
This loop really should instead be a call to mainloop()
to turn control over to tkinter's event handler:
window.mainloop()
具有相同名称的成员变量和实例方法不是一个好主意:
Having member variables and instance methods with the same name is a bad idea:
self.ball = turtle.Turtle()
...
def ball(self):
与Python的其余部分一样,一个会覆盖另一个,并且会发生不好的事情.我的代码重做版本:
Like the rest of Python, one overwrites the other and bad things happen. My reworked versions of your code:
main.py
from turtle import Screen
import elements
# Windows settings
window = Screen()
window.title("Pong game by Kosa")
window.bgcolor('black')
window.setup(width=800, height=600)
paletka_1 = elements.Objects()
window.onkeypress(paletka_1.paddle_a_up, "w")
window.listen()
window.mainloop()
elements.py
elements.py
from turtle import Turtle
class Objects:
def __init__(self):
# Paddle A
self.paddle_a = Turtle("square")
self.init_paddle_a()
# Paddle B
self.paddle_b = Turtle("square")
self.init_paddle_b()
# Ball
self.ball = Turtle("square")
self.init_ball()
def init_paddle_a(self):
paddle_a_x = -350
self.paddle_a.speed('fastest')
self.paddle_a.shapesize(stretch_wid=5, stretch_len=1)
self.paddle_a.color('green')
self.paddle_a.penup()
self.paddle_a.setx(paddle_a_x)
def init_paddle_b(self):
paddle_b_x = 350
self.paddle_b.speed('fastest')
self.paddle_b.shapesize(stretch_wid=5, stretch_len=1)
self.paddle_b.color('red')
self.paddle_b.penup()
self.paddle_b.setx(paddle_b_x)
def init_ball(self):
self.ball.speed('fastest')
self.ball.color('white')
self.ball.penup()
self.ball.home()
def paddle_a_up(self):
y = self.paddle_a.ycor() + 20
self.paddle_a.sety(y)
def paddle_b_up(self):
y = self.paddle_b.ycor() + 20
self.paddle_b.sety(y)
现在这应该建立一个窗口,左右分别带有桨,中间有一个球.单击窗口,然后您可以按"w".使左桨上升.现在完成程序!
This should now put up a window with paddles on the left and right and a ball in the middle. Click on the window and after you can press "w" to make the left paddle rise up. Now finish the program!
这篇关于Python Turtle中的onkeypress()和listen()问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!