Friday, February 28, 2025
spot_img

pygame – Rating counter in Python recreation assigning factors to each gamers


I’m making an attempt to be taught to code a easy recreation with 2 gamers capturing at horizontal-moving enemies from the underside of the display.

All the things is working fantastic apart from this:

  • When participant 1 hits enemy kind A, participant 1 will get +1 rating.

  • When participant 1 hits enemy kind B, participant 1 will get +3 rating.

  • Similar goes for participant 2.

  • When participant 1 or 2 hit enemy B 3 instances, I would love a picture and a sound to be performed for five seconds.

My code in the mean time makes the sport rely each rating to each gamers, so each recreation is a tie!

And by no means I can get to breed the picture + sound when hitting the kind B enemy 3 instances.

The next is a generalized code of what I’m engaged on and I did a little bit little bit of a translation from my mom tongue, so I hope it is sensible.

enemy hit = pygame.sprite.spritecollide(self, enemies, True)
    world score_player1, score_player2
    for p in enemy hit:
        pygame.mixer.Sound.play(enemyA_sound if not p.enemyB else enemyB_suono)
        pygame.mixer.Sound.play(hit_sound)
        if p.enemyB:
            score_player1 += 3
        else:
            score_player1 += 1
        if p.is_viola:
            score_player2 += 3
        else:
            score_player2 += 1
        self.kill()
def score_management(participant, is_enemyB):
world score_player1, score_player2, player1_enemyB_hit, player2_enemyB_hit

if participant == "player1":
    if is_enemyB:
        score_player1 += 3
        player1_enemyB_hit += 1
        if player1_enemyB_hit == 3:
            display.blit(prize_img, (width // 2 - prize_img.get_width() // 2, top // 2 - prize_img.get_height() // 2))
            pygame.show.flip()
            pygame.mixer.Sound.play(prize_sound)
            pygame.time.delay(5000)

    else:
        score_player1 += 1
        score_player2 += 0

elif participant == "player2":
    if is_EnemyB:
        score_player2 += 3
        player2_enemyB_hit += 1
        if player2_enemyB_hit == 3:
            display.blit(prize_img, (width // 2 - prize_img.get_width() // 2, top // 2 - prize_img.get_height() // 2))
            pygame.show.flip()
            pygame.mixer.Sound.play(prize_sound)
            pygame.time.delay(5000)
    else:
        score_player2 += 1
        score_player1 += 0

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisement -spot_img

Latest Articles