1
def main(display):
    lives = 5
    collide = 0
    pygame.mixer.music.play(10)
    while pygame.mixer.music.get_busy():
        text = font.render(f"Lives: {lives}", False, (255, 255, 255))

        bird = pygame.Rect(450, 250, 70, 45)

        clock = pygame.time.Clock()
        run = True
        while run:
            fireballrec.y += 1
            fireballs.append(fireballrec.y)
            if bird.colliderect(fireballrec):
                print(lives)
                collide += 1
                lives -= 1
                if collide == 1:
                    collide = 0
                    break
            if fireballrec.y > HEIGHT:
                del fireballrec.y
            draw_window(bird, text, lives, display)
            pygame.display.update()
            clock.tick(FPS)

I'm trying to make multiple fireballs fall down the screen and on collision you lose a life but the fireball image doesn't move with the fireball rec (The fireballrec variable is the hitbox of the fireball)

I tried using the del and kill() attributes but it was unable to remove it from the list and the fireball did not appear to go downwards on screen

1 Answers1

1

It is very hard to tell what you are trying to do in your code. However, I can tell you how to do this in general.

Create a list for the fire balls before the application loop:

fireballs = []

Add new fireballs to the list at random positions at the top of the screen over time (also see Spawning multiple instances of the same object concurrently in python):

fire_ball_event = pygame.USEREVENT
pygame.time.set_timer(fire_ball_event, 200)
while run:
    # [...]

    for event in pygame.event.get():
        if event.type == fire_ball_event:
            x = random.randrange(10, window.get_width()-10)
            fireballs.append(pygame.Rect(x, -20, 20, 20))

Move the fireballs in a for-loop and remove the fireballs that reach the bottom of the screen (also see How to remove items from a list while iterating?):

for fireballrect in fireballs[:]:
    fireballrect.y += 1
    if fireballrect.top > window.get_height():
        fireballs.remove(fireballrect)

Draw the fire balls in another for-loop:

for fireballrect in fireballs:
    window.blit(fireball, fireballrect)

Minimal example:

import pygame, random

pygame.init()
window = pygame.display.set_mode((400, 400))
clock = pygame.time.Clock()

fire_ball_event = pygame.USEREVENT
pygame.time.set_timer(fire_ball_event, 200)

fireball = pygame.Surface((20, 20), pygame.SRCALPHA)
pygame.draw.circle(fireball, "yellow", (10, 10), 10)
pygame.draw.circle(fireball, "orange", (10, 13), 7)
pygame.draw.circle(fireball, "red", (10, 16), 4)
fireballs = []

run = True
while run:
    clock.tick(100)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False 
        if event.type == fire_ball_event:
            x = random.randrange(10, window.get_width()-10)
            fireballs.append(pygame.Rect(x, -20, 20, 20))

    for fireballrect in fireballs[:]:
        fireballrect.y += 1
        if fireballrect.top > window.get_height():
            fireballs.remove(fireballrect)

    window.fill(0)
    for fireballrect in fireballs:
        window.blit(fireball, fireballrect)
    pygame.display.flip()

pygame.quit()
exit()

If you have an object that you want to check if a fireball collides with it, you can perform the collision check in the loop in which the fireballs are moved:

for fireballrect in fireballs[:]:
    fireballrect.y += 1
    if bird.colliderect(fireballrec):
        print('hit')

Since fireballs is a list of pygame.Rect objects, you can also use pygame.Rect.collidelist:

if bird.collidelist(fireballs):
    print('hit')
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • @TheRealKurumi My sprites don't flicker and I just focused on the falling fireballs. You can do collision detection with the `pygame.Rect` objects in the `fireballs` list. Do the collision detection in the `for` loop where the fireballs are moved. I've given you a general idea of how to do it. That's what StackOverflow is all about. – Rabbid76 Dec 27 '22 at 17:02