Beispiel #1
0
    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
Beispiel #2
0
 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)
     return True
Beispiel #3
0
 def _draw_block(self, instance: Block, shape: pymunk.Shape,
                 view: tk.Canvas, offset: Tuple[int, int]) -> List[int]:
     image = self.load_image(self._block_images[instance.get_id()])
     return [
         view.create_image(shape.bb.center().x + offset[0],
                           shape.bb.center().y,
                           image=image,
                           tags="block")
     ]
Beispiel #4
0
 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
Beispiel #5
0
 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
Beispiel #6
0
    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
Beispiel #7
0
    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
Beispiel #8
0
 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
Beispiel #9
0
    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