python - How to allow for multiple instances of a displayed image MUDBALL in Pygame -
i have mudball image displayed on screen adding all_sprites group. mudball should triggered multiple times using shoot() method of trump class instead appears once @ the beginning of game , not displayed anymore. guess need code more instances of mudball don't know how. don't recieve error 1 meager displaying of mudball instead of multiple. have print "shoot function" statement printed showing shoot() being called still 1 image displayed. please appreciated. have been encouraged include complete version of code.
import pygame 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') open(path.join(self.dir, hs_file), 'r') 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 new game self.score = 0 self.all_sprites = pg.sprite.group() self.platforms = pg.sprite.group() self.player = player(self) #me: trump added sprites!!! self.trump = trump(self) self.all_sprites.add(self.trump)
#mudball statement needs amended 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) 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)] 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): = pg.time.get_ticks() if - self.last_update2 > 500: self.last_update2 = 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 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()
Comments
Post a Comment