如何解决Turtle 中程序变化的速度
当我尝试运行程序时,它的速度会不时变化。这是我的代码如下:
import turtle
# Making the Window
screen=turtle.Screen()
screen.title('PingPong Game made by using Turtle')
screen.bgcolor('black')
screen.setup(width=800,height=600)
screen.tracer(0)
# Sprites
# Paddle A
paddle_a=turtle.Turtle() # It make the sprite (paddle_a) an object of the turtle
paddle_a.speed(0) # Its not the speed of the movement of the sprite,its the speed of the animation. Here its set to the maximum possible speed using '0'
paddle_a.shape('square') # Sets the shape of the sprite
paddle_a.color('white') # Sets the colour of the sprite
paddle_a.shapesize(stretch_len=1,stretch_wid=5) # Increases the length and width of the sprite (Can also be done vise-versa(length is the side length,width is the height))
paddle_a.penup() # normally when we use turtle,the sprites constantly leave line fro where they move. (.penup) removes the line and leaves the background only
paddle_a.goto(-350,0)
# Paddle B
paddle_b=turtle.Turtle()
paddle_b.speed(0)
paddle_b.shape('square')
paddle_b.color('white')
paddle_b.shapesize(stretch_len=1,stretch_wid=5)
paddle_b.penup()
paddle_b.goto(350,0)
# Ball
ball=turtle.Turtle()
ball.speed(0)
ball.shape('circle')
ball.color('white')
ball.penup()
ball.goto(0,0)
ball.change_x=0
ball.change_y=0.7
# Sprite Functions
# Paddle A
def paddle_a_up(): # Moving the sprite (paddle_a) down
y=paddle_a.ycor() # Setting a var y to the Y cord of the sprite (paddle_a)
y+=20 # Increasing the y value variable (Its not increasing the Y coord its just increasing the value of the var)
paddle_a.sety(y) # Setting the Y cord to the Y level
def paddle_a_down():
y=paddle_a.ycor()
y-=20
paddle_a.sety(y)
# Paddle B
def paddle_b_up(): # Moving the sprite (paddle_a) down
y=paddle_b.ycor() # Setting a var y to the Y cord of the sprite (paddle_a)
y+=20 # Increasing the y value variable (Its not increasing the Y coord its just increasing the value of the var)
paddle_b.sety(y) # Setting the Y cord to the Y level
def paddle_b_down():
y=paddle_b.ycor()
y-=20
paddle_b.sety(y)
# Using the Keyboard
screen.listen() # Searches for a key press
screen.onkeypress(paddle_a_up,'Up') # Does the function in the first argument when the key in the second argument is pressed
screen.onkeypress(paddle_a_down,'Down')
screen.onkeypress(paddle_b_up,'w')
screen.onkeypress(paddle_b_down,'s')
# Game Loop
while True:
screen.update()
# Moving the ball
ball.setx(ball.xcor()+ ball.change_x)
ball.sety(ball.ycor()+ball.change_y)
# Borders
if ball.ycor()>=285:
ball.change_y=-0.7
if ball.ycor()<=-285:
ball.change_y=0.7
现在的问题是,当我的桨保持静止时,它运行正常。但是当我试图移动我的桨时,球的速度会降低。另外,如果我把球拍从屏幕上拿出来,不管我是否移动球拍,球的速度都会变成大约 2 倍。
解决方法
当我运行您的代码时,我没有遇到您描述的延迟。我的猜测是它可能与 Python 环境相关——在您的问题中包括您使用的 Python 平台。 (例如,Replit 站点上的这个 Python 海龟还是 Windows 上的标准 Python?)
我在代码方面的建议是,在时间关键的方法中,尽可能少做。这可能意味着要提前做更多的设置工作。例如,下面我(重塑和)在垂直维度中设置您的桨的方向,这样您就可以简单地使用 forward()
和 backward()
来移动它们,而不是询问、计算然后移动。同样,在移动球时,避免多次调用 xcor()
之类的方法,并避免通过 xcor()
一次性获取 ycor()
和 position()
:>
from turtle import Screen,Turtle
# Sprite Functions
# Paddle A
def paddle_a_up():
paddle_a.forward(20)
def paddle_a_down():
paddle_a.backward(20)
# Paddle B
def paddle_b_up():
paddle_b.forward(20)
def paddle_b_down():
paddle_b.backward(20)
def move():
x,y = ball.position()
# Borders
if not -285 < y < 285:
ball.change_y = -ball.change_y
# Move the ball
ball.goto(x + ball.change_x,y + ball.change_y)
screen.update()
screen.ontimer(move)
# Making the Window
screen = Screen()
screen.setup(width=800,height=600)
screen.title('PingPong Game made by using Turtle')
screen.bgcolor('black')
screen.tracer(False)
# Sprites
# Paddle A
paddle_a = Turtle()
paddle_a.shape('square')
paddle_a.color('white')
paddle_a.shapesize(stretch_len=5) # Increases the length of the sprite
paddle_a.penup()
paddle_a.setheading(90)
paddle_a.setx(-350)
# Paddle B
paddle_b = paddle_a.clone()
paddle_b.setx(350)
# Ball
ball = Turtle()
ball.shape('circle')
ball.color('white')
ball.penup()
ball.change_x = 0.5 # user defined properties
ball.change_y = 1
# Using the Keyboard
screen.onkeypress(paddle_a_up,'w')
screen.onkeypress(paddle_a_down,'s')
screen.onkeypress(paddle_b_up,'Up')
screen.onkeypress(paddle_b_down,'Down')
screen.listen()
move()
screen.mainloop()
我希望这些更改中的一些有所帮助,但同样,我猜这是您运行的环境的问题。
(我反转了控制你的拨片的键,因为用左手控制右边的拨片对我来说似乎并不自然,反之亦然。)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。