-1

My game shoots a bullet, but the problem is you cannot shoot more bullets than the amount you put in (imageA, imageB, imageC, etc...) So, I'd have to write "blit blah blah imageA" about 30 times for every gun for every player. This is just irritating.

Is there anyway to produce code depending on how many bullets the character has shot, and then to produce a code to blit it? I could then remove that new code when it goes off the screen. (I just want to know how to remove, not how to make it do it when it goes off the screen, I know that.)

Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
Bluetiger6001
  • 151
  • 2
  • 3
  • 9

1 Answers1

2

I believe you are approaching the problem in the wrong way. Let's say you have a player, that has a gun. So our player class will partly look like this:

class Player
    def __init__(self):
        self.position = [0,0]
        self.bullets = []

we will also need a bullet class, that will have a position too. Now, everytime the player presses spacebar for example, you will add a new bullet to the list.

def shoot(self):
    self.bullets += Bullet(self.position)

then, in the while loop, where you draw your player, you will also need to do a foreach to also draw the bullets. There is no need to name each bullet, and even have an own picture.

Bartlomiej Lewandowski
  • 10,771
  • 14
  • 44
  • 75