1

I have a MUDBALL image that is displayed on screen by adding it to the all_sprites group. A MUDBALL should be triggered multiple times using the shoot() method of the TRUMP class but instead only appears once at the the beginning of the game and then is not displayed anymore. My guess is I need to code for more instances of the MUDBALL but don't know how. I don't recieve an error just 1 meager displaying of the MUDBALL instead of multiple. I have a print "shoot function" statement that is printed showing that shoot() is being called but still only 1 image displayed. Please any help would be appreciated. I have been encouraged to include the complete version of my code.

import pygame as pg   
      class Game:
        def __init__(self):
        # initialize game window, etc
        pg.init()
        pg.mixer.init()
        self.screen = pg.display.set_mode((WIDTH, HEIGHT))
        pg.display.set_caption(TITLE)
        self.clock = pg.time.Clock()
        self.running = True
        self.font_name = pg.font.match_font(FONT_NAME)
        self.load_data()

 def load_data(self):
        # load high score
        self.dir = path.dirname(__file__)
        img_dir = path.join(self.dir, 'img')
        with open(path.join(self.dir, HS_FILE), 'r') as f:
        try:
            self.highscore = int(f.read())
        except:
            self.highscore = 0
        # load spritesheet image
        self.spritesheet = Spritesheet(path.join(img_dir, SPRITESHEET))

  def new(self):
        # start a new game
        self.score = 0
        self.all_sprites = pg.sprite.Group()
        self.platforms = pg.sprite.Group()
        self.player = Player(self)
        #ME: TRUMP added to all sprites!!!
        self.trump = TRUMP(self)
        self.all_sprites.add(self.trump)

#MUDBALL statement that needs to be amended for multiple instances

            self.mudball = MUDBALL(self.trump.rect.centerx, 
            self.trump.rect.centery)
            self.all_sprites.add(self.mudball)


        self.all_sprites.add(self.player)
        for plat in PLATFORM_LIST:
        p = Platform(*plat)
        self.all_sprites.add(p)
        self.platforms.add(p)
        self.run()

  def run(self):
        # Game Loop
        self.playing = True
        while self.playing:
        self.clock.tick(FPS)
        self.events()
        self.update()
        self.draw()



 class TRUMP(pg.sprite.Sprite):
    def __init__(self, game):
        pg.sprite.Sprite.__init__(self)
        self.game = game
        self.current_frame2 = 0
        self.last_update2 = 0
        self.load_images()

        self.image = self.TRUMP_fingers_l

        self.rect = self.image.get_rect()
        self.rect.center = (WIDTH *3/4), (589)
        self.rect.centerx = (WIDTH *3/4)
        self.rect.centery = 589
        self.rect.centerx = (WIDTH*3/4)
        self.rect.centery = 589
        self.pos = vec((WIDTH/2), (HEIGHT/2))
        self.vel = vec(0, 0)
        self.acc = vec(0, 0)
        self.dir = 0 


        self.screen = pg.display.set_mode((WIDTH, HEIGHT))
        self.clock = pg.time.Clock()

  def load_images(self):

        self.TRUMPhail = pg.image.load("trumpHAILING.png")

        self.TRUMP_walk_cycle = [self.game.spritesheet.get_image(1, 1704, 248, 559),
        self.game.spritesheet.get_image(1, 565, 312, 561),
                                self.game.spritesheet.get_image(1, 1, 312, 562)]
        for frame in self.TRUMP_walk_cycle:
            frame.set_colorkey(BLACK)


        self.TRUMP_fingers_l = self.game.spritesheet.get_image(1, 1704, 248, 559)
        self.TRUMP_fingers_l.set_colorkey(BLACK)

        self.TRUMP_walk1_l = self.game.spritesheet.get_image(1, 565, 312, 561)
        self.TRUMP_walk1_l.set_colorkey(BLACK)

        self.TRUMP_walk2_l = self.game.spritesheet.get_image(1, 1, 312, 562)
        self.TRUMP_walk2_l.set_colorkey(BLACK)

        self.TRUMP_hailing = self.game.spritesheet.get_image(1, 2265, 248, 439)
        self.TRUMP_hailing.set_colorkey(BLACK)
        self.TRUMP_hailing = pg.transform.scale(self.TRUMP_hailing,(283, 500))


        self.TRUMP_throwing = self.game.spritesheet.get_image(1, 1128, 302, 574)
        self.TRUMP_throwing.set_colorkey(BLACK)

        self.TRUMP_smug = self.game.spritesheet.get_image(251, 1704, 247, 558)
        self.TRUMP_smug.set_colorkey(BLACK)

        self.TRUMP_flexing_l = self.game.spritesheet.get_image(251, 2264, 243, 554)
        self.TRUMP_flexing_l.set_colorkey(BLACK)


   def update(self):


           now = pg.time.get_ticks()
           if now - self.last_update2 > 500:
                self.last_update2 = now
                self.current_frame2 = (self.current_frame2 + 1) % len(self.TRUMP_walk_cycle)
                self.image = self.TRUMP_walk_cycle[self.current_frame2]
                print(self.rect.centerx, self.rect.centery)
                self.dir = random.randint(1,10)
                if self.dir == 1: #moving right
                    self.rect.centerx +=10
                    print("move right 1")
                if self.dir == 2: #Moving left
                    self.rect.centerx -=10
                    print("move left 2")
                if self.dir == 3:
                    self.image = self.TRUMP_flexing_l
                if self.dir == 4:
                    self.shoot()
                    print("shoot")
                    self.image = self.TRUMP_throwing 
                if self.dir == 5: #SMUG
                    self.image = self.TRUMP_smug

                if self.dir == 6: #flexing
                    self.image = self.TRUMP_flexing_l

                if self.dir == 7: #Moving right
                    self.rect.centerx +=10
                    print("move right 5")
                if self.dir == 8: #move left
                    self.rect.centerx -=10
                    print("move left 6")
                if self.dir == 9: #Moving right
                    self.rect.centerx +=10
                    print("move right 7")
                if self.dir == 10: #move left
                    self.rect.centerx -=10
                    print("move left 8")

the shoot function that I would like to be triggered multiple times

def shoot(self):
           mudballs = pg.sprite.Group()
           mudball = MUDBALL(self.rect.centerx, self.rect.centery)
           print("SHOOT function")
           mudballs.add(mudball)

class MUDBALL(pg.sprite.Sprite):
    def __init__(self, x, y):
        pg.sprite.Sprite.__init__(self)
        self.image = pg.image.load("0MUDBALL200.png")
        self.rect = self.image.get_rect()
        self.rect.bottom = y
        self.rect.centerx = x
        self.speedx = -30

def update(self):
        self.rect.x += self.speedx
        if self.rect.centerx < 0:
            self.kill()
Matt Macy
  • 141
  • 1
  • 2
  • 13
  • Your `shoot()` function initializes the variable `mudballs`, and then you add your new `mudball` to that group. This means that there is *never* more than one mudball in `mudballs`. Probably you have to move the first line from `shoot()`, i.e. `mudballs = pg.sprite.Group()` to somewhere else where it makes more sense. – Schmuddi Apr 07 '17 at 15:32
  • I remove mudballs = pg.sprite.Group and I still only get 1 instance. Thanks for your help! What else do I need to change? – Matt Macy Apr 07 '17 at 15:36
  • Hold on... `mudballs` is a local variable of `shoot()`. This means that your `Game` instance doesn't know about this sprite group in whatever happens in `Game.event()`, `Game.update()` and `Game.draw()`. The `MUDBALL` instances that you create in `shoot()` probably have to be added to `all_sprites` from your `Game` instance. – Schmuddi Apr 07 '17 at 15:52
  • That was an approach I considered originally. I couldn't figure out the code for it. Can you tell me how I would code for that? is it Game.all_sprites.add(self.mudball)? When I code that in my shoot function it says that Game is not defined. Please if you could tell me what to code? Am I correct to place the statement in my shoot() method? – Matt Macy Apr 07 '17 at 15:58
  • I needed to put self.game.all_sprites.add(mudball). I don't know why this works but it does! Thanks! – Matt Macy Apr 07 '17 at 16:20
  • This works because you pass a `Game` instance as one of the arguments to `TRUMP.__init__()`, and you store that instance in the object attribute `self.game`. This is why you can call `self.game.all_sprites.add()` to access that `Game` instance. – Schmuddi Apr 07 '17 at 16:48

0 Answers0