mercredi 27 novembre 2019

The for loop works for the first iteration but for the next item in the list

So I am trying to create a simple game using pygame which is kind of like 'brick breaker' except there is no bricks. You get a plank and try to hit the ball so that it doesn't drop below the plank. When the ball hits the plank it should bounce off in the opposite direction. It works perfectly for the first ball, but when I try to introduce the second ball (when your score is 10), the second ball follows all the conditions except it ignores the plank (just goes through it). Here is the bit of the code where I suspect the problem lies:

    for j in range(0, len(ball_x)):

    ball_y[j] += ball_vely[j]
    ball_x[j] += ball_velx[j]

    if ball_y[j] <= 0:
        ball_vely[j] *= -1

    if ball_y[j] + 55 > y and ball_y[j] + 40 < y and ball_x[j] > x and ball_x[j] < x + 160:
        ball_vely[j] *= -1
        score += 1

        if ball_x[j] > x + 75:
            ball_velx[j] = 10
            ball_velx[j] *= 1 + (ball_x[j] - x + 75)/60
        elif ball_x[j] < x + 75:
            ball_velx[j] = 10
            ball_velx[j] *= -1 * (1 + (x + 75 - ball_x[j])/60)

    if ball_x[j] - 25 <= 0 or ball_x[j] + 45  >= 680:
        ball_velx[j] *= -1
    if ball_y[j] > 795:
        lives -= 1

ball_y and ball_x while ball_velx and ball_vely are the velocities (all of them are stored in a list). When the second ball goes below the screen, the lives are taken off which makes no sense to me. I have tried getting rid of the for loop and instead writing the code twice but it didn't work. Any help would be appreciated.

Aucun commentaire:

Enregistrer un commentaire