Пример #1
0
    async def spawn_bullet(self, tank):
        position = tank.position
        direction = tank.direction

        size = Bullet.SIZE
        half_size = size // 2
        speed = Bullet.speed

        if direction is Direction.UP:
            x = position.centerx - half_size
            y = floor((position.top + speed) / size) * size
        elif direction is Direction.DOWN:
            x = position.centerx - half_size
            y = floor((position.bottom - speed) / size) * size
        elif direction is Direction.LEFT:
            x = floor((position.left + speed) / size) * size
            y = position.centery - half_size
        elif direction is Direction.RIGHT:
            x = floor((position.right - speed) / size) * size
            y = position.centery - half_size
        else:
            x = 0
            y = 0

        bullet = Bullet(x, y)
        bullet.set_direction(direction)
        bullet.set_parent(tank)

        data = messages.get_monster_serialized_data(bullet, action='spawn')
        await self.game.broadcast(data)

        self.game.bullets.append(bullet)
Пример #2
0
 async def do_sth_with_npcs(self):
     for npc in self.game.npcs:
         is_changed = npc.do_something()
         if not is_changed:
             continue
         npc_data = messages.get_monster_serialized_data(npc)
         await self.game.broadcast(npc_data)
Пример #3
0
    async def spawn_npc(self):
        if len(self.game.npcs) >= self.game.MAX_NPC_IN_AREA:
            return
        if random() > 0.6 / (len(self.game.npcs) + 1):
            return

        spawn = choice(self.game.npc_spawns)
        npc = NPC(*spawn)
        self.game.npcs.append(npc)
        self.game.npcs_left -= 1
        npc_data = messages.get_monster_serialized_data(npc, action='spawn')
        await self.game.broadcast(npc_data)
Пример #4
0
def test_messages_get_monster_data():
    player = Player(x=1, y=2, player_id=0)
    player.direction = Direction.UP
    player.speed = 2

    assert messages.get_monster_serialized_data(player, action='socek') == dict(
        id=player.id.hex,
        type='player',
        status='data',
        action='socek',
        is_freeze=False,
        direction='up',
        speed=2,
        parent=None,
        position={'x': 1, 'y': 2},
    )
Пример #5
0
def test_messages_get_bullet_data():
    player = Player(x=1, y=2, player_id=0)
    bullet = Bullet(x=1, y=2)
    bullet.set_direction(direction=Direction.UP)
    bullet.set_parent(player)

    assert messages.get_monster_serialized_data(bullet, action='socek') == dict(
        id=bullet.id.hex,
        type='bullet',
        status='data',
        action='socek',
        is_freeze=False,
        direction='up',
        speed=8,
        parent=player.id.hex,
        position={'x': 1, 'y': 2},
    )
Пример #6
0
 async def freeze(self, player: Player):
     player.set_freeze()
     data = messages.get_monster_serialized_data(player, action='freeze')
     await self.game.broadcast(data)
Пример #7
0
 async def set_had_action(player: Player, game: Game):
     player.set_had_action()
     data = messages.get_monster_serialized_data(player)
     await game.broadcast(data)