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')