[Python] ball slowing down while the paddle moves using turtle module

Discussão em 'Python' iniciado por Stack, Fevereiro 7, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to make a ball bounce of a paddle using the turtle module. I am still developing the program. The issue is that when I hold down the left or right arrow key while the ball is moving then the ball is slowing down. Is there a way to fix this?

    Below is the code.

    main.py:

    import turtle as tt
    from paddletest import Paddle
    from balltest import Ball

    scrn = tt.Screen()
    scrn.setup(width=800, height=800)
    scrn.bgcolor("black")

    paddle = Paddle()
    ball = Ball(220)

    scrn.listen()
    scrn.onkeypress(fun=paddle.move_left, key="Left")
    scrn.onkeypress(fun=paddle.move_right, key="Right")

    while True:
    ball.speed(1)
    ball.forward(20)

    scrn.exitonclick()


    paddletest.py:

    MOVE_DIST = 50
    import turtle as tt

    class Paddle(tt.Turtle):
    def __init__(self):
    super().__init__()
    self.shape(name="square")
    self.hideturtle()
    self.penup()
    self.color("white")
    self.turtlesize(stretch_wid=1, stretch_len=5)
    self.speed(0)
    self.goto(x= 200, y=-100)
    self.showturtle()

    def move_left(self):
    self.backward(MOVE_DIST)

    def move_right(self):
    self.forward(MOVE_DIST)


    balltest.py:

    import turtle as tt

    class Ball(tt.Turtle):
    def __init__(self, initial_movement):
    super().__init__()

    self.shape(name="circle")
    self.hideturtle()
    self.penup()
    self.color("white")
    self.goto(x=100,y=100)
    self.showturtle()
    self.speed(1)
    self.forward(20)
    self.setheading(initial_movement)


    I suspect the ball slows down due to too many resources being used by the system, although I am not completely sure. Any help would be appreciated! Thank you

    Continue reading...

Compartilhe esta Página