最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - ball slowing down while the paddle moves using turtle module - Stack Overflow

programmeradmin5浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论