首页 > 解决方案 > 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 倍。

标签: pythonpython-turtle

解决方案


当我运行您的代码时,我没有遇到您描述的延迟。我的猜测是它可能与 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()

我希望这些更改中的一些有所帮助,但我再次猜测这是您正在运行的环境,这就是问题所在。

(我颠倒了控制你的桨的键,因为我用左手控制右桨似乎不自然,反之亦然。)


推荐阅读