如何在Pygame中允许显示图像MUDBALL的多个实例

时间:2017-04-07 11:54:27

标签: python pygame

我有一个MUDBALL图像,通过将其添加到all_sprites组显示在屏幕上。应该使用TRUMP类的shoot()方法多次触发MUDBALL,而不是仅在游戏开始时出现一次,然后不再显示。我的猜测是我需要为MUDBALL的更多实例编写代码,但不知道如何编写代码。我没有收到一个错误只是1微小的显示MUDBALL而不是多个。我有一个打印“拍摄功能”声明,打印显示正在调用shoot()但仍然只显示1个图像。请任何帮助将不胜感激。我被鼓励包含我的代码的完整版本。

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语句

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

我希望多次触发的拍摄功能

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

0 个答案:

没有答案