简体   繁体   English

乒乓球 - 为什么球不动? (可能是 time.sleep 问题)

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

So I'm trying to improve my Python skills;所以我正在努力提高我的 Python 技能; 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.球不动,不如游戏必备的function。 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.我认为这可能与游戏循环的时间( wn.update() )有关,但我完全迷失了。 I have tried slowing the game down by importing time ( time.sleep(0.09) ) but had no luck.我曾尝试通过导入时间( time.sleep(0.09) )来减慢游戏速度,但没有运气。

I have no idea as to why the score updates, as the ball will not move.我不知道为什么分数会更新,因为球不会移动。 this was in turtle not PyGame.这是在乌龟而不是 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:你不应该在龟中使用time.sleep()因为它会停止整个游戏及其事件循环,而是使用这个:

screen.ontimer(func_name, 2000) #ms

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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