Exemplo n.º 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
Exemplo n.º 2
0
 def setUp(self) -> None:
     o = None
     block00 = Block.from_int_array([[o, o, o], [1, o, o], [o, o, o]])
     block01 = Block.from_int_array([[o, o, o], [0, 1, o], [o, o, o]])
     block10 = Block.from_int_array([[1, 0, 1], [1, 1, o], [1, 1, 1]])
     block11 = Block.from_int_array([[o, o, o], [1, o, o], [o, 1, o]])
     self.test_board = Board([[block00, block01], [block10, block11]])
Exemplo n.º 3
0
 def test_rotate_block(self):
     o = None
     tests = [{
         'rot': Rotation(Position(0, 0), True),
         'new': Block.from_int_array([[1, 1, o], [1, o, o], [o, o, o]])
     }, {
         'rot': Rotation(Position(0, 1), False),
         'new': Block.from_int_array([[o, o, o], [o, 1, o], [o, 0, 1]])
     }]
     for test in tests:
         new_board = self.test_board.rotate_block(test['rot'])
         self.assertEqual(new_board.get_block(test['rot'].get_block_pos()),
                          test['new'])
Exemplo n.º 4
0
    def add_block(self, block: Block, x: float, y: float, *args, **kwargs):
        """Adds a block to the game world at the grid cell that contains ('x', 'y')

        Parameters:
            block (Block): The block to add to the grid
            x (float): The x-coordinate of the position contained by the cell
            y (float): The y-coordinate of the position contained by the cell

            - See add_block_to_grid for other parameters
        """
        col, row = self.xy_to_grid(x, y)
        row -= block.get_cell_size()[1] - 1
        return self.add_block_to_grid(block, col, row, *block.get_cell_size(),
                                      *args, **kwargs)
Exemplo n.º 5
0
 def test_play_marble(self):
     o = None
     tests = [{
         'move': Placement(Position(0, 0), Position(0, 0)),
         'new': Block.from_int_array([[1, o, o], [1, o, o], [1, 1, o]]),
         'colour': 1
     }, {
         'move': Placement(Position(0, 1), Position(2, 1)),
         'new': Block.from_int_array([[o, o, o], [0, 1, o], [1, 0, o]]),
         'colour': 0
     }]
     for test in tests:
         new_board = self.test_board.play_marble(test['move'],
                                                 test['colour'])
         self.assertEqual(new_board.get_block(test['move'].get_block_pos()),
                          test['new'])
Exemplo n.º 6
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
Exemplo n.º 7
0
def create_block(world: World, block_id: str, x: int, y: int, *args):
    """Create a new block instance and add it to the world based on the block_id.

    Parameters:
        world (World): The world where the block should be added to.
        block_id (str): The block identifier of the block to create.
        x (int): The x coordinate of the block.
        y (int): The y coordinate of the block.
    """

    block_id = BLOCKS[block_id]
    if block_id == "mystery_empty":
        block = MysteryBlock()
    elif block_id == "mystery_coin":
        block = MysteryBlock(drop="coin", drop_range=(3, 6))

    elif block_id == "bounce_block":  # Adding bounce, tunnel and flag to the game world
        block = BounceBlock()
    elif block_id == "flag":
        block = FlagpoleBlock()
    elif block_id == "tunnel":
        block = TunnelBlock()
    elif block_id == "switch" :
        block = switch()

    else:
        block = Block(block_id)

    world.add_block(block, x * BLOCK_SIZE, y * BLOCK_SIZE)
Exemplo n.º 8
0
 def setUp(self) -> None:
     o = None
     self.test_block = Block.from_int_array([
         [o, o, o],
         [1, 1, 1],
         [0, 1, 0],
     ])
Exemplo n.º 9
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")
     ]
Exemplo n.º 10
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
Exemplo n.º 11
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
Exemplo n.º 12
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
Exemplo n.º 13
0
    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
Exemplo n.º 14
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
Exemplo n.º 15
0
    def init(self):

        self.allSprites = pygame.sprite.Group()
        self.walls = pygame.sprite.Group()
        self.event = pygame.sprite.Group()
        self.passing = pygame.sprite.Group()
        self.projectile = pygame.sprite.Group()

        Block(self, 20, 15, (255, 255, 255), collision=False, event=True)

        for wall in self.data["blocks"]:
            for i in range(wall["w"]):
                for j in range(wall["h"]):
                    breakable = True if "breakable" in wall.keys() else False
                    Block(self,
                          wall["x"] + i,
                          wall["y"] + j,
                          wall["color"],
                          breakable=breakable)
        self.player = Player(self, self.playerSpawn[0], self.playerSpawn[1])
        self.camera = Camera(self.width, self.height)
Exemplo n.º 16
0
 def test_clockwise_rotate(self):
     o = None
     rotated_block = Block.from_int_array([
         [0, 1, o],
         [1, 1, o],
         [0, 1, o],
     ])
     msg = 'rotate anticlockwise not performing as expected'
     self.assertEqual(self.test_block.rotate_clockwise(), rotated_block,
                      msg)
     self.assertEqual(
         rotated_block.rotate_anticlockwise().rotate_clockwise(),
         rotated_block)
Exemplo n.º 17
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
Exemplo n.º 18
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
Exemplo n.º 19
0
 def _tunnel(self, block: Block):
     '''when the player stand on a tunnel and press the <down> or s, this function will bring him to a new level'''
     self.reset_world(new_level=block.get_filename())
Exemplo n.º 20
0
 def blank(board_size, block_size) -> 'Board':
     return Board([[Block.blank(block_size) for _ in range(board_size)]
                   for _ in range(board_size)])
Exemplo n.º 21
0
    def _handle_player_collide_block(self, player: Player, block: Block, data,
                                     arbiter: pymunk.Arbiter) -> bool:

        block.on_hit(arbiter, (self._world, player))
        return True
Exemplo n.º 22
0
 def test_get_size(self):
     for i in range(1, 10):
         self.assertEqual(Block.blank(i).get_size(), i)