def create_feather(self, char, direction=None): if direction is None: direction = char.direction feather = sprite.oSprite() feather.attackMgr = self tw, th = core.level.tileSize feather.speed = .25 feather.direction = direction if char.direction == "N": feather.y -= th/2 if char.direction == "S": feather.y += th/2 if char.direction == "E": feather.x += tw/2 if char.direction == "W": feather.x -= tw/2 feather.life = 700 feather.AI = self.AI feather_char = core.load_image("feather.png", directory="gfx") feather.animFromStrip( feather_char, [ ["stopped","E",0,1,0,True, (3, 15)], ["stopped","N",1,1,0,True, (5,13)], ["stopped","W",2,1,0,True, (12,15)], ["stopped","S",3,1,0,True, (5,11)] ], 24, 24, feather_char) feather.changeState("stopped") feather.x = char.rx() + char.action_point()[0] - feather.hotspot()[0] feather.y = char.ry() + char.action_point()[1] - feather.hotspot()[1] core.attack_group.add(feather) core.render_group.add(feather) return feather
def update(self, lag): for bug in self.bugs: if bug.dead: self.bugs.remove(bug) if self.state < self.maxState: self.openWork += lag open_prob = 0 if self.openWork > 6000: open_prob = 1 if self.openWork > 15000: open_prob = 100 if random.randint(0,100) < open_prob: self.changeState(self.state + 1) self.openWork = 0 if self.state <= 0: return if len(self.bugs) < (self.capacity*self.state/2): self.unactive += lag if self.unactive > 5000: if random.randint(0,100) < self.state*self.state*10: # create a new bug... self.mapa.created_bugs += 1 bug = sprite.oSprite() tw, th = self.mapa.tileSize bug.x = (self.mapID % self.mapa.width) bug.y = (self.mapID / self.mapa.width) bug.speed = .1 bug.direction = random.choice(self.direction) bug.stuck = 0 if bug.direction == "N": bug.y -= 1 elif bug.direction == "W": bug.x -= 1 elif bug.direction == "S": bug.y += 1 elif bug.direction == "E": bug.x += 1 bug.AI = sprite.bugAI bug_sprite = core.load_image("bug.png", directory="gfx") bug.animFromStrip( bug_sprite, [ ["stopped","E",0,1,0,True], ["stopped","N",4,1,0,True], ["stopped","W",8,1,0,True], ["stopped","S",12,1,0,True], ["dying", "N",1,3,.01,False], ], 48, 48, bug_sprite) bug.changeState("stopped") bug.x = bug.x * tw + bug.hotspot()[0] bug.y = bug.y * th + bug.hotspot()[1] + 33 self.group.add(bug) core.render_group.add(bug) self.bugs.append(bug) self.unactive = 0
def get_ctc(): ctc = sprite.oSprite() ctc.direction = "N" ctc_sprite = core.load_image("gfx/cursor.png") ctc.animFromStrip( ctc_sprite, [ ["default","N",0,10,.01,True,(0,5)], ], 24, 24) ctc.changeState("default") return ctc
def genButton(images, pos): button = sprite.oSprite() button.x = pos[0] button.y = pos[1] button.animations = { "active": { "N": [0,True,[sprite.oFrame(images[0])]] }, "inactive": { "N": [0,True,[sprite.oFrame(images[1])]] } } core.render_group.add(button) return button
def main_game(): global mc, tutorial, hints, killed_bugs # if yesno_prompt("Play tutorial level?"): # tutorial = True # hints = 0 core.baddys_group.clear() core.render_group.clear() core.attack_group.clear() # load bg sky_bg = core.load_image("sky_bg.png", directory="gfx", auto_alpha=False) exp_bgs = core.load_image("exp_bg.png", directory="gfx") exp_bg = sprite.oSprite() exp_bg.x = 10 exp_bg.y = 545 exp_bg.animations = { "stopped": { "N": [0, True, [ sprite.oFrame(core.im_crop(exp_bgs, pygame.Rect(0,0,278,33))), sprite.oFrame(core.im_crop(exp_bgs, pygame.Rect(0,33,278,33))), sprite.oFrame(core.im_crop(exp_bgs, pygame.Rect(0,66,278,33))) ]] } } exp_bg.changeState("stopped") core.render_group.add(exp_bg) # initialize map core.level = map.oMap() # current stats level = 0 level_time = oStatsDisp(core.level.time, (700,20), useNumbers=True, formatValue=format_level_time) lives = oStatsDisp(3, (10, 20), image=core.load_image("haato.png", directory="gfx")) score = oStatsDisp(0, (0,20), xalign=0.5, useNumbers=True, formatValue=format_score) # main character mc = sprite.oSprite() mc_sprite = core.load_image("nozomi.png", directory="gfx") mc_mask = core.load_image("nozomi_mask.png", directory="gfx") mc.animFromStrip( mc_sprite, [ ["stopped","S",1,1,0,True, (23,45), (23,33)], ["stopped","W",4,1,0,True, (23,45), (17,33)], ["stopped","E",7,1,0,True, (23,45), (30,33)], ["stopped","N",10,1,0,True,(23,45), (23,33)], ["walking","S",0,3,.02,True, (23,45), (23,33)], ["walking","W",3,3,.02,True, (23,45), (17,33)], ["walking","E",6,3,.02,True, (23,45), (30,33)], ["walking","N",9,3,.02,True, (23,45), (23,33)], ["dying", "N",12,3,0.01,False, (23,45)] ], 48, 48, mc_mask ) mc.changeState("stopped") mc.AI = core.handlePlayerControls core.render_group.add(mc) available_attacks = [ featheratk.oDoubleAttack(core.load_image("double_feather.png", directory="gfx")), featheratk.oTriangleAttack(core.load_image("triangle_feather.png", directory="gfx")), featheratk.oCrossAttack(core.load_image("cross_feather.png", directory="gfx")) ] mc.attacks = [ featheratk.oBasicAttack(core.load_image("debug_feather.png", directory="gfx")) ] mc.use_attack = 0 # load map load_level(level, mc) killed_bugs = 0 lost_lives = 0 mid_rank = 0 rank_num = 0 forceUpdate = True while True: lag = core.clock.tick(core.MAX_FPS) if lag <= 0: lag = 1 level_time.update(level_time.value - lag/1000.0) if level_time.value < 0: main_char_death(mc) core.play_sound("mc_dying") lives.update(lives.value-1) lost_lives += 1 if core.handleEvents() == "quit": return dirty_rects = core.render_group.update(lag) core.level.updateNests(lag) exp_bg.animFrame = core.mathExp.state # check collisions # bugs if core.render_group.checkCollision(False, False, mc, core.baddys_group): main_char_death(mc) core.play_sound("mc_dying") lives.update(lives.value-1) lost_lives += 1 # pickups picked = core.pickup_group.checkCollision(False, False, mc, core.pickup_group) for pickup in picked: pickup = pickup[0] # new live :D if isinstance(pickup, sprite.oHeartSprite): core.play_sound("live_gain") lives.update(lives.value+1) pickup.die() # feather attacks kills = core.attack_group.checkCollision(False, False, None, core.baddys_group) score.update(score.value + 100*len(kills)) killed_bugs += len(kills) for i in kills: core.play_sound("bug_dying") if hasattr(i[0], "attackMgr"): feather, bug = i if hasattr(i[1], "attackMgr"): bug, feather = i feather.attackMgr.kills += 1 feather.die() bug.OnAnimationEnd = bug.die bug.direction = "N" bug.changeState("dying") if lives.value == 0 and mc.OnAnimationEnd is None: if rank_num == 0: _rank = 0 else: _rank = mid_rank / rank_num core.next_screen = core.curry(game_over, score.value, _rank) return # draw level / sprites core.screen.blit(sky_bg, (0,0)) dirty_rects.extend( core.level.draw(core.screen) ) core.render_group.draw(core.screen) # draw stats # lives dirty_rects.extend( lives.draw(core.screen) ) # math exp dirty_rects.extend( core.mathExp.draw(core.screen) ) # time dirty_rects.extend( level_time.draw(core.screen) ) # score dirty_rects.extend( score.draw(core.screen) ) # attacks for i, attack in enumerate(mc.attacks): attack.update(lag) dirty_rects.extend( attack.draw(core.screen, i == mc.use_attack) ) # dirty_rects.append( core.fonts.Draw(core.screen, None, 24, # str(core.clock.get_fps()), (650, 50), (255,255,255,255))) if forceUpdate: dirty_rects = None forceUpdate = False core.updateScreen(dirty_rects) # clear level =D if core.level.portalActive: _score, rank = level_clear_screen(mc, killed_bugs, level_time.value, lives.value, lost_lives, score.value) score.update(_score) mid_rank += rank rank_num += 1 lost_lives = 0 core.baddys_group.clear() core.attack_group.clear() level += 1 if not load_level(level, mc): core.next_screen = core.curry(game_ending, core.curry(hiscores_screen, score.value, mid_rank/rank_num)) return if available_attacks: new_attack = available_attacks.pop(0) feather_received(new_attack) mc.attacks.append( new_attack ) level_time.update(core.level.time) mc.changeState("stopped") for attack in mc.attacks: attack.freeze = 0 attack.used = 0 attack.kills = 0 attack.last_rects = [] forceUpdate = True