sprogissd
sprogissd

Reputation: 3075

Movement Of Multiple Images in Pygame

I'm trying to build a small game using Pygame where a user will move a helicopter up and down and press space bar to shoot bullets. I already have the bullet image installed into the program and set the starting X and Y using pygame.key.get_pressed, but I'm trying to make it so multiple bullets can be shot across the screen at the same time. Here's the two parts of the code that deal with bullets that I have made, but they only have capacity for one bullet and when I press space again, the bullet just reappears and restarts it's motion.

pygame.key.get_pressed
...
if keys[pygame.K_SPACE]:
    Bullet = pygame.image.load("Images/Bullet.png")
    BulletX = HELICOPTERX
    BulletY = HelicopterY + 15
    BulletShoot = True
...
if BulletShoot == True:
    BulletX += 5
    SURF.blit(Bullet, (BulletX, BulletY))
    if BulletX >= 800: #800 is the width of the screen
        BulletShoot = False

Upvotes: 0

Views: 215

Answers (1)

Blake O'Hare
Blake O'Hare

Reputation: 1880

Rather than storing each bullet attribute in a separate variable, you need to create a list of bullet objects. When the bullet's x coordinate goes above 800, then remove the bullet from the list.

Upvotes: 2

Related Questions