def on_hit(self, event: pymunk.Arbiter, data) : ''' when the player hit the mushroom on both sides, he will loose health and the mushroom will change it's moving side. And when the player hit the mushroom on the top, the first time the mushroom will be squished and the second time it will be destroyed and removed. :param event: :param data: :return: ''' world, player = data if get_collision_direction(player, self) == "R" : player.change_health(-1) velocity = player.get_velocity() player.set_velocity((velocity.x + 50, 0)) elif get_collision_direction(player, self) == "L" : player.change_health(-1) velocity = player.get_velocity() player.set_velocity((velocity.x - 50, 0)) elif get_collision_direction(player, self) == "A" : if self._dead is not True: self._dead = True player.set_velocity((0, -120)) else: world.remove_mob(self) player.set_velocity((0, -120))
def _handle_player_collide_block(self, player: Player, block: Block, data, arbiter: pymunk.Arbiter) -> bool: if(block.get_id() == "bounce"): # Propel Mario when collide to BounceBlock self._player.set_velocity((0, -180)) if(block.get_id()== "flag"): self.addScore() #to let the player enter his name to add the score on the highscore file self.level = self.getNextLevel(self.level) #get the nextLevel using the config File self.reset_world(self.level) if(get_collision_direction(player, block) == "A"): #increase the player health in case his on top of the flag player.change_health(1) if(block.get_id()=="tunnel" and get_collision_direction(player, block) == "A" and self.down_pressed is True): #GO THE NEXT LEVEL IF the player pressed up on top of the tunnel self.level = self.getNextTunnelLevel(self.level) self.reset_world(self.level) #Removing the bricks on the left and on the right of the switch if block.get_id() == "switch" and get_collision_direction(player, block) == "A": x,y = block.get_position() block1 = self._world.get_block(x-GRID_WIDTH,y-block.getCollNum()*GRID_HEIGHT) block2 = self._world.get_block(x+GRID_WIDTH,y+block.getCollNum()*GRID_HEIGHT) self._world.remove_block(block1) self._world.remove_block(block2) block.incrementcollNum() block.on_hit(arbiter, (self._world, player)) return True
def _handle_mob_collide_block(self, mob: Mob, block: Block, data, arbiter: pymunk.Arbiter) -> bool: if(mob.get_id() == "mushroom"): #to change the mob direction when he touch a block if get_collision_direction(block, mob) == "L" or get_collision_direction(block, mob) == "R": mob.set_tempo(-1 * mob.get_tempo()) if mob.get_id() == "fireball": if block.get_id() == "brick": self._world.remove_block(block) self._world.remove_mob(mob) return True
def _handle_mob_collide_block(self, mob: Mob, block: Block, data, arbiter: pymunk.Arbiter) -> bool : ''' handle when the mob hit other block when the mob is fireball and hit a brick, the block will be destroyed when the mob is mushroom and hit a brick, it will change its direction ''' if mob.get_id() == "fireball" : if block.get_id() == "brick" : self._world.remove_block(block) self._world.remove_mob(mob) if mob.get_id() == "mushroom": if get_collision_direction(mob, block) == "L" or get_collision_direction(mob, block) == "R" : mob.set_tempo(-mob.get_tempo()) return True
def on_hit(self, event: pymunk.Arbiter, data): """Callback collision with player event handler.""" world, player = data if get_collision_direction(player, self) == "A": self.set_squished(True) player.set_velocity((0, -100)) self.set_tempo(0) # stop moving when squished # destroy the mob after 0.4 seconds. just for the animation timer = Timer(0.4, world.remove_mob, [self]) timer.start() elif get_collision_direction(player, self) == "R": player.change_health(-1) player.set_velocity((50, 0)) elif get_collision_direction(player, self) == "L": player.change_health(-1) player.set_velocity((-50, 0))
def _handle_player_collide_mob(self, player: Player, mob: Mob, data, arbiter: pymunk.Arbiter) -> bool: if self._player_star.activated(): self._world.remove_mob(mob) return False else: if mob._id == "mushroom": if mob.dead: return False if get_collision_direction(player, mob) == "A": self._renderer.kill_mushroom(mob.get_shape()) mob.freeze() self._master.after(500, lambda: self._world.remove_mob(mob)) self._can_jump = True self._jump() return True mob.on_hit(arbiter, (self._world, player)) self._status_view.set_health(self._player.get_health()) self._status_view.set_score(self._player.get_score()) if self._player.get_health() == 0: tk.messagebox.showinfo("Gameover", "You died.") self._master.quit() return True
def on_hit(self, event, data): """ Callback collision with player event handler """ world, player = data brick_list = [] if get_collision_direction(player, self) != "A": return if self.is_active(): self._active = False x, y = self.get_position() things = world.get_things_in_range(x, y, 60) for thing in things: if thing._type == 2 and thing.get_id() == 'brick': x_brick, y_brick = thing.get_position() brick_list.append((thing, x_brick, y_brick)) world.remove_block(thing) # count down 10 seconds to set the switch back to on, and bring back the bricks timer_active = Timer(10, self.set_active, [True]) timer_active.start() timer_blocks = Timer(10, self.blocks_recover, [brick_list, world]) timer_blocks.start()
def on_hit(self, event, data): """Callback collision with player event handler.""" world, player = data # Ensure the bottom of the block is being hit if get_collision_direction(player, self) != "A": return velocity = player.get_velocity() player.set_velocity((velocity.x * 0.8, -300))
def on_hit(self, event, data): """Callback collision with player event handler.""" world, player = data if get_collision_direction(player, self) == "A": self._active = True player.set_velocity((0, -400)) timer = Timer(0.5, self.set_active, [False]) # this is for the animation timer.start()
def _handle_mob_collide_block(self, mob: Mob, block: Block, data, arbiter: pymunk.Arbiter) -> bool: if mob.get_id() == "fireball": if block.get_id() == "brick": self._world.remove_block(block) self._world.remove_mob(mob) if mob.get_id() == "mushroom": if get_collision_direction(block, mob) in ("L", "R"): mob.direction *= -1 return True
def _handle_player_collide_block(self, player: Player, block: Block, data, arbiter: pymunk.Arbiter) -> bool : ''' handle when the player hit some blocks, when they are colliding, the on_hit function will run''' if block.get_id() == "switch": block.on_hit(arbiter, (self._world, player)) return False if block.get_id() == "flag": if get_collision_direction(player, block) == "L" or get_collision_direction(player, block) == "R": self.reset_world(new_level=block.get_filename()) if get_collision_direction(player, block) == "A" : block.on_hit(arbiter, (self._world, player)) if block.get_id() == "tunnel": if get_collision_direction(player, block) == "A": # self._master.bind("s", lambda a : self._tunnel(block)) self._master.bind("<Down>", lambda a : self._tunnel(block)) else: block.on_hit(arbiter, (self._world, player)) return True
def on_hit(self, event, data) : world, player = data if get_collision_direction(player, self) == "A": x, y = self.get_position() self._temp = world.get_things_in_range(x, y, 20) if self._is_active: self._is_active = False self._press_time = time.time() for i in self._temp : if isinstance(i, Block) and not isinstance(i, Switch) : world.remove_block(i)
def _handle_player_collide_block(self, player: Player, block: Block, data, arbiter: pymunk.Arbiter) -> bool: if get_collision_direction(player, block) == "A": self._can_jump = True if block._id == "bouncy": if get_collision_direction(player, block) == "A": self._renderer.activate_bouncy(block.get_shape()) elif block._id == "switch": if block.activated(): return False elif block._id == "flagpole": name = self._ask_name() score_name = self._current_level.replace(".txt", "_score.txt") self._scores.load_scores(score_name) self._scores.add_score(name, self._player.get_score()) self._scores.save_scores(score_name) self.reset_world(block._next_level) block.on_hit(arbiter, (self._world, player)) return True
def _handle_player_collide_block(self, player: Player, block: Block, data, arbiter: pymunk.Arbiter) -> bool: if get_collision_direction( player, block ) == "A": # when player touch the blocks, set jumping to false self._player.set_jumping(False) if block.get_id() == "flag": if get_collision_direction(player, block) == "A": block.on_hit(arbiter, data) else: # tell the player to input their name and see if the score records need to be updated self.update_score() if self.get_next_level( ) == 'END': # if there's no further level, ask if start over ans = messagebox.askokcancel( 'Good job, you finish the game', 'Start Over?') if ans: self.reset_world('level1.txt') self._level = 'level1.txt' self._player.clear_score() self._player.change_health( self._player.get_max_health()) self.redraw_status() else: self._master.destroy() else: self.reset_world(self.get_next_level()) self._level = self.get_next_level() elif block.get_id() == "tunnel": if get_collision_direction( player, block) == "A" and self._player.is_duck() is True: self._player.set_duck(False) self.reset_world(self.get_next_level()) elif block.get_id() == 'switches': if block.is_active(): block.on_hit(arbiter, (self._world, player)) block.on_hit(arbiter, (self._world, player)) return True
def on_hit(self, event, data): """Callback collision with player event handler.""" world, player = data # Ensure the bottom of the block is being hit if get_collision_direction(player, self) != "B": return if self._active: self._active = False # Drop items into the game world drops = self.get_drops() self._drop_items(data[0], drops)
def _handle_mob_collide_block(self, mob: Mob, block: Block, data, arbiter: pymunk.Arbiter) -> bool: if mob.get_id() == "fireball" or mob.get_id( ) == 'bullet_l' or mob.get_id() == 'bullet_r': if block.get_id() == "brick": self._world.remove_block(block) self._world.remove_mob(mob) else: self._world.remove_mob(mob) elif mob.get_id( ) == "mushroom": # mushroom bounces back a little when encountering blocks if get_collision_direction( mob, block) == "R" or get_collision_direction( mob, block) == "L": mob.set_tempo(-mob.get_tempo()) elif mob.get_id( ) == 'gang': # gang jumps over the blocks when encountering them if get_collision_direction(mob, block) == "R": mob.set_velocity((50, -350)) elif get_collision_direction(mob, block) == "L": mob.set_velocity((-50, -350)) return True
def _handle_player_collide_mob(self, player: Player, mob: Mob, data, arbiter: pymunk.Arbiter) -> bool : ''' handle when the player hit some mob, when they are colliding, the on_hit function will run''' if mob.get_id() == "mushroom" : if get_collision_direction(player, mob) == "L" or get_collision_direction(player, mob) == "R" : if self._player.get_invincible() : self._world.remove_mob(mob) elif self._powerup.get_powerup(): self._powerup.set_powerup(False) else : mob.on_hit(arbiter, (self._world, player)) mob.set_tempo(-mob.get_tempo()) if get_collision_direction(player, mob) == "A": mob.on_hit(arbiter, (self._world, player)) if mob.get_id() == "fireball" : if self._player.get_invincible() : self._world.remove_mob(mob) elif self._powerup.get_powerup(): self._powerup.set_powerup(False) else : mob.on_hit(arbiter, (self._world, player)) self._world.remove_mob(mob) return True
def on_hit(self, event, data) : """Callback collision with player event handler.""" world, player = data # Ensure the bottom of the block is being hit # if get_collision_direction(player, self) != "A" : # return if get_collision_direction(player, self) == "A": player.set_velocity((0, -240)) if self._is_active is not True: self._is_active = True self._press_time = time.time() if time.time()-self._press_time >= 1: self._is_active = False else : return
def on_hit(self, event: pymunk.Arbiter, data): world, player = data if get_collision_direction(player, self) == "A": # kill mob world.remove_mob(self) velocity = player.get_velocity() player.set_velocity((velocity.x * 0.8, -100)) return player.change_health(-1) # velocity = player.get_velocity() mob_x, mob_y = self.get_position() player_x, player_y = player.get_position() if player_x < mob_x: player.set_velocity((-50, -100)) elif player_x > mob_x: player.set_velocity((50, -100))
def _handle_player_collide_mob(self, player: Player, mob: Mob, data, arbiter: pymunk.Arbiter) -> bool: # A collision with a mob make Mario Lost a health. # When Mario is Invincible Mario Lost and gain a life. # Thus, the health don't change tough the collision heppened if player.invincible == True : player.change_health(1) if(type(mob) is MushroomMob): if get_collision_direction(player, mob) == "A": #in case if the player is above the mushroomMob player.set_velocity((0, 100)) #the player up mob.destroy() #destroy the mob else:#in case the collision is on another direction player.change_health(-1) #the player should lose health player.set_velocity((2 * mob.get_velocity()[0], 0)) , #the player should be slightly repelled away mob.on_hit(arbiter, (self._world, player)) return True
def on_hit(self, event, data): world, player = data self._world = world if get_collision_direction(player, self) != "A": return if not self._activated: self._activated = True self._start_time = time.time() # Destroy blocks within radius radius = 35 # pixels x, y = self.get_position() queries = world._space.point_query( (x, y), radius, pymunk.ShapeFilter(mask=world._thing_categories["block"])) for i in queries: if i.shape.object._id == "brick": pos = i.shape.object.get_position() self._destroyed_blocks.append( [i.shape.object, pos[0], pos[1]]) world.remove_block(i.shape.object) print(self._destroyed_blocks)
class switch(Block): _id = "switch" _counter = 0 _collisionNum = 0 def __init__(self): super().__init__() self.pressed = False self.counter = 0 def incrementcollNum(self): self._collisionNum += 1 def getCollNum(self) -> int: return self._collisionNum # Press the switch by changing the object from switch to switch_pressed dedataf on_hit(self, event, data): world, player, = if get_collision_direction(player, self) == "A": self._id = "switch_pressed" self.pressed = True else : return
def on_hit(self, event, data): world, player = data if get_collision_direction(player, self) == "A": player.change_health(1)
def on_hit(self, event, data): world, player = data # Ensure the bottom of the block is being hit if get_collision_direction(player, self) != "A": return