乌龟动画随机加速/减速

时间:2019-05-26 02:46:05

标签: python turtle-graphics

如何阻止乌龟动画随机加速/减速?

我一直在尝试用Python制作乒乓球游戏;但是,球似乎有时会随机加速和减速。如何阻止这种情况发生?

我尝试更改球的速度变量,但这无济于事。我还研究了解决此问题的方法,但找不到任何有用的方法。

<TextField>

我希望球的动画很流畅;但是,动画的速度是随机变化的。

2 个答案:

答案 0 :(得分:0)

我看不到为什么动画会加速和减速。无论如何,以下是基于以下问题我将如何编写相同的代码:

  • while True:循环在基于事件的环境中没有位置,例如 乌龟-如果当前不是问题,它将成为其中的一员。我有 替换为计时器事件。

  • 0.1的像素移动非常小,并且在移动时总是显得很慢 乌龟。

  • 避免让turtle做关键循环中不需要做的任何事情。 在对setx()sety()xcor()ycor()goto()的调用之间 您在乌龟实例上进行了十几种方法调用。在我的 重写我在关键循环中只有两个乌龟调用,position()setposition()

  • 我插入tracer()update()并插入仪器 关键循环,只有一个乌龟叫声改变了 屏幕上显示每个更新,这是tracer()的默认设置 -因此一无所获。

修改后的代码:

from turtle import Screen, Turtle

WIDTH, HEIGHT = 800, 600
CURSOR_SIZE = 20

ball_dx = 1
ball_dy = -2.5

def move():
    global ball_dx, ball_dy

    x, y = ball.position()

    y += ball_dy

    # Border collision checking
    if not CURSOR_SIZE - HEIGHT/2 < y < HEIGHT/2 - CURSOR_SIZE:
        ball_dy *= -1

    x += ball_dx

    if not CURSOR_SIZE - WIDTH/2 < x < WIDTH/2 - CURSOR_SIZE:
        x = y = 0
        ball_dx *= -1

    ball.setposition(x, y)

    screen.ontimer(move, 50)

# Window settings
screen = Screen()
screen.title('Classic Pong v1.1')
screen.setup(WIDTH, HEIGHT)
screen.bgcolor('black')

# Ball
ball = Turtle()
ball.shape('square')
ball.color('white')
ball.speed('fastest')
ball.penup()

move()
screen.mainloop()

答案 1 :(得分:0)

**

File lectureRecorded = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+lectureTitle.getText().toString()+".mp4");

                try {

                    BufferedInputStream input = new BufferedInputStream(new FileInputStream(lectureRecorded));

                    if(lectureRecorded.length() > 10*1024*1024){
                        Toast.makeText(getActivity(),"File is too big MAX (10MB)\n a loss will occur",Toast.LENGTH_LONG).show();
                    }else {
                        stream = new byte[(int)lectureRecorded.length()];

                        Log.d("AUDIOS",""+lectureRecorded.length());
                        input.read(stream, 0, (int) lectureRecorded.length());
                    }
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                recorder.stop();
                recorder.release();

**