Why does 0% 5 return 0?

I am doing a pong game right now, and I want the ball to accelerate every 5 strokes, but when I launch the ball, it just starts to accelerate in its initial direction.

It works fine without accelerating the ball, so the problem is not in the previous code.

When trying to implement this, I made a variable in the Ball class called self.num_hitsand made it initially 0. Then in my game loop, every time the ball collides, I increase ball.num_hitsand change 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 class Ball ():

if self.num_hits % 5 == 0:
        if self.x_speed > 0:
            self.x_speed += 2
        else:
            self.x_speed -= 2

, , self.num_hits % 5, 0. , 0 % number = number, , 0% 5 0? 5 , 0% 5?

-3
7

, true

n = q & times; d + r

  • n - ( ),
  • d!= 0 - ( ),
  • q - ,
  • r > 0 - .

( , q , n d . r .)

Python, n/d == q n % d == r. n 0, q 0, r 0, d.

( , , 0: d, q r , d = 0 q r = .

+11

0% 5 0?

:

, , , .

0 % 5 = 0
12 % 5 = 2
+4

a% n a - (n * int (a/n)). 0/5 0, 5 0, 0 . 0 * 5 0. 0 0 0.

+1

, self.num_hits/5 == 0:

+1

number_1% number_2 = > - , _1 number_2

0% any_number 0.

+1

0% 5 = 0
1% 5 = 1
2% 5 = 2
3% 5 = 3
4% 5 = 4
5% 5 = 0
6% 5 = 1
7% 5 = 2
...

+1

, , . % . () 6 % 5, 1, 5 6 5 * 1 == 5. 6-5 == 1, . 0%5 5 0 () . 5*0 == 0 0-0 == 0. ,

+1

All Articles