I am making a game of pong right now, and I want the ball to speed up every 5 hits, but when I run the ball just starts speeding off in its starting direction.
It runs fine without the speeding up of the ball so the problem isn't previous code.
When trying to implement this I made a variable in my Ball class called self.num_hits
and made it initially 0. Then in my game loop every time the ball collides, I increment the ball.num_hits
and reverse its x_speed.
collide_list = pygame.sprite.spritecollide(ball, players, False)
if collide_list != []:
ball.x_speed *= -1
hit.play()
ball.num_hits += 1
In the Ball() class:
if self.num_hits % 5 == 0:
if self.x_speed > 0:
self.x_speed += 2
else:
self.x_speed -= 2
But that made the ball speed off in its starting velocity, so I checked what self.num_hits % 5
was returning, and it always returns 0. I always thought that 0 % number = number
, so my question is why does 0 % 5 return 0? And is there any other way I can make the ball speed up every 5 hits if I can't get around the 0 % 5 problem?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…