简体   繁体   中英

Pong game - why won't ball move? (possible time.sleep issue)

So I'm trying to improve my Python skills; I made a game from a tutorial, for making the classic game pong. However my code has two major issues that I can't pinpoint what is causing them.

The ball doesn't move, not good as its an essential function of the game. the player two score counter goes up at an exponential rate, as if the ball that doesn't move has passed that game wall side.

My code is as follows for the ball and pen. I think it may be to do with the timing of the game loop ( wn.update() ) but I'm totally lost. I have tried slowing the game down by importing time ( time.sleep(0.09) ) but had no luck.

I have no idea as to why the score updates, as the ball will not move. this was in turtle not PyGame. Code below is for the game ball and pen.

ball.speed(0)
ball.shape("circle")
ball.color("white")
ball.penup()
ball.goto(0, 0)
ball.shapesize(stretch_wid=1.5)
ball.dx = 2
ball.dy = 2

pen = turtle.Turtle()
pen.speed(0)
pen.color("white")
pen.penup()#So it does not draw lines.
pen.hideturtle()
pen.goto(0, 260)
pen.write(" Player One: 0 Player Two", align="center", font=("Courier", 24, "normal"))

And in my main game loop I have:

    wn.update()
    time.sleep(0.09)  # where 0.09 is the number of seconds (note milliseconds) 
                      # to do nothing for.
    # Moving the ball 
    ball.setx(ball.xcor() + ball.dx)
    ball.sety(ball.ycor() + ball.dy)

You shouldn't be using time.sleep() in turtle because it stops the whole game and its eventloop, instead use this:

screen.ontimer(func_name, 2000) #ms

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM