My collision in my code for game over is not working right in pygame

Viewed 68

I am trying to make a game in pygame, and I am almost done. I am just having problems with my game over mechanics. After I die by touching my enemy my enemies dissappear, i send them to Y=2000 when I die, and now they continue moving and then comes up after a few seconds. My game over music works, but my game over text wont.

In my code I have this code for game over, because I want to lose when the enemies touch me:

# Game Over
if isCollision(enemyX[i], enemyY[i], playerX, playerY):
    for j in range(num_of_enemies):
        enemyY[j] = 2000
    game_over_text()
    break

This wont work, because what I described just now happens.

I have tried to change the code and use this:

# Game Over
if enemyY[i] > 440:
    for j in range(num_of_enemies):
        enemyY[j] = 2000
    game_over_text()
    break

This works, and I get the game over text, the game over sound and my enemies wont come back. But this is not how I want it to work.

Anyone have any ideas on how I can fix this?

this is my code for the iscollision function:

def isCollision(enemyX, enemyY, bulletX, bulletY):
    # Formel for distanse mellom to koordinater
    distance = math.sqrt((math.pow(enemyX-bulletX, 2)) + (math.pow(enemyY-bulletY, 2)))
    if distance < 30:
        return True
    else:
        return False

It detects the collision, but the collision code stops the game over text from working.

My whole code: https://gist.github.com/EkteFrokostNam/a46b8a905ec3d1f190af482887a80341

1 Answers

You only draw the game over text for a single frame. If you want to permanently display the text, you need to draw it in the application loop:

def game_over_text():
    over_text = over_font.render("GAME OVER", True, (255, 255, 255))
    screen.blit(over_text, (200, 250))
   
def game_over_sound(): 
    pygame.mixer.music.set_volume(0)
    death_music = mixer.Sound('Death.mp3')
    death_music.set_volume(0.1)
    death_music.play()
game_over = False

# Game loop
running = True 
while running:
    # [...]

    # Enemy movement

    for i in range(num_of_enemies):

        # Game Over
        if isCollision(enemyX[i], enemyY[i], playerX, playerY):
            for j in range(num_of_enemies):
                enemyY[j] = 2000
            game_over_sound()
            game_over = True
            break

    # [...]

    if game_over:
        game_over_text()
Related