Exemplo n.º 1
0
    def test_get_by_type(self):
        e = Entity()
        e.addartifact(TagArtifact(TagType.KOMESMAN))
        system = TagSystem()
        system.register(e)

        self.assertEqual(len(system.getentities(TagType.KOMESMAN)), 1)
        self.assertEqual(len(system.getentities(TagType.ENEMY)), 0)
Exemplo n.º 2
0
    def test_remove(self):
        e = Entity()
        e.addartifact(TagArtifact(TagType.KOMESMAN, TagSubType.BEER))
        system = TagSystem()
        system.register(e)

        self.assertEqual(len(system.getentities(TagType.KOMESMAN)), 1)
        system.remove(e)
        self.assertEqual(len(system.getentities(TagType.KOMESMAN)), 0)
Exemplo n.º 3
0
    def createresult(self, _text, _type):
        """
        Create black board with specified text.

        :param _text: text to display
        :param _type: GameState in which board should be displayed.
        :return: nothing
        """
        result = Entity()
        result.addartifact(SpriteArtifact(
            TextSprite(_text, Modifiers.CENTER_H | Modifiers.CENTER_V), 0, 0, _type))
        self.systems[DrawSystem.NAME].register(result)
        self.elements.append(result)
Exemplo n.º 4
0
    def createhud(self, _hudx, _hudy):
        """
        Create HUD (points, lives, cap status).

        :param _hudx: X position in pixels
        :param _hudy: Y position in pixels
        :return: nothing
        """
        hud = Entity()
        hud.addartifact(SpriteArtifact(HUDSprite(Modifiers.CENTER_H), _hudx, _hudy, GameState.GAME))
        self.systems[DrawSystem.NAME].register(hud)
        self.systems[HUDSystem.NAME].register(hud)
        self.elements.append(hud)
Exemplo n.º 5
0
    def createmenubackground(self, _menux, _menuy):
        """
        Create large KomesMan logo.

        :param _menux: X position of logo
        :param _menuy: Y position of logo
        :return: nothing
        """
        background = Entity()
        background.addartifact(
            SpriteArtifact(SimpleImageSprite('res/img/logo.png'), _menux, _menuy, GameState.MENU))
        self.systems[DrawSystem.NAME].register(background)
        self.elements.append(background)
Exemplo n.º 6
0
 def test_menu_up(self):
     system = MenuSystem()
     item = Entity()
     item.addartifact(SpriteArtifact(TextSprite("Sdasd"), 0, 0, GameState.MENU))
     item.addartifact(MenuArtifact())
     system.register(item)
     system.register(item)
     system.register(item)
     system.current_game_state = GameState.MENU
     
     self.assertEqual(system.current_index, 0)
     system.input(pygame.event.Event(pygame.KEYDOWN, key=pygame.K_UP))
     self.assertEqual(system.current_index, 2)
     system.input(pygame.event.Event(pygame.KEYDOWN, key=pygame.K_UP))
     self.assertEqual(system.current_index, 1)
     
     system.remove(item)
     system.remove(item)
     system.remove(item)
Exemplo n.º 7
0
    def createmenuelement(self, _elementx, _elementy, _text, _type, _parent):
        """
        Create single menu element.

        :param _elementx: X position of text
        :param _elementy: Y position of text
        :param _text: Text of element
        :param _type: Type of menu event
        :param _parent: Parent menu element.
        :return: menu entity
        """
        menu = Entity()
        menu.addartifact(SpriteArtifact(
            TextSprite(_text, Modifiers.CENTER_H), _elementx, _elementy, GameState.MENU))
        menu.addartifact(MenuArtifact(_type))
        self.systems[DrawSystem.NAME].register(menu)
        self.systems[MenuSystem.NAME].register(menu, _parent)
        self.elements.append(menu)
        return menu
Exemplo n.º 8
0
    def createhighscores(self):
        """
        Create labels for highscore entering, and placeholder for entering player name.

        :return: nothing
        """
        new_highscore = Entity()
        new_highscore.addartifact(SpriteArtifact(
            TextSprite("New highscore!", Modifiers.CENTER_H), 100, 100, GameState.NEW_HIGHSCORE))
        self.systems[DrawSystem.NAME].register(new_highscore)
        self.elements.append(new_highscore)

        enter_your_name = Entity()
        enter_your_name.addartifact(SpriteArtifact(
            TextSprite("Enter your name:", Modifiers.CENTER_H), 100, 200, GameState.NEW_HIGHSCORE))
        self.systems[DrawSystem.NAME].register(enter_your_name)
        self.elements.append(enter_your_name)

        self.player_name = Entity()
        self.player_name.addartifact(SpriteArtifact(
            TextSprite('_'*30, Modifiers.CENTER_H), 100, 300, GameState.NEW_HIGHSCORE))
        self.systems[DrawSystem.NAME].register(self.player_name)
        self.elements.append(self.player_name)
Exemplo n.º 9
0
    def createkomesman(self, komesx, komesy):
        """
        Create our hero - KomesMan.

        :param komesx: integer X position
        :param komesy: integer Y position
        :return: nothing
        """
        komes_man = Entity()
        komes_man.addartifact(
            SpriteArtifact(KomesManSprite(self.tile_size), komesx, komesy,
                           GameState.GAME))
        komes_man.addartifact(MovementArtifact())
        komes_man.addartifact(TagArtifact(TagType.KOMESMAN))
        komes_man.addartifact(BehaviorArtifact(KomesManBehavior()))
        self.systems[TagSystem.NAME].register(komes_man)
        self.systems[UserMovementSystem.NAME].register(komes_man)
        self.systems[DrawSystem.NAME].register(komes_man)
        self.systems[CollisionSystem.NAME].register(komes_man)
        self.elements.append(komes_man)
Exemplo n.º 10
0
    def createsupercop(self, supercopx, supercopy):
        """
        Create super cop.

        :param supercopx: integer X position
        :param supercopy: integer Y position
        :return:
        """
        cop = Entity()
        cop.addartifact(
            SpriteArtifact(CopSprite(self.tile_size), supercopx, supercopy,
                           GameState.GAME))
        cop.addartifact(MovementArtifact(1.25))
        cop.addartifact(TagArtifact(TagType.ENEMY, TagSubType.SUPER_COP))
        cop.addartifact(BehaviorArtifact(SimpleCopBehavior()))
        self.systems[AiMovementSystem.NAME].register(cop)
        self.systems[DrawSystem.NAME].register(cop)
        self.systems[TagSystem.NAME].register(cop)
        self.systems[CollisionSystem.NAME].register(cop)
        self.elements.append(cop)
Exemplo n.º 11
0
    def createbeer(self, beerx, beery):
        """
        Create bottle of beer.

        :param beerx: integer X position
        :param beery: integer Y position
        :return: nothing
        """
        beer = Entity()
        beer.addartifact(
            SpriteArtifact(BeerSprite(self.tile_size), beerx, beery,
                           GameState.GAME))
        beer.addartifact(TagArtifact(TagType.ITEM, TagSubType.BEER))
        beer.addartifact(BehaviorArtifact(BeerBehavior()))
        self.systems[DrawSystem.NAME].register(beer)
        self.systems[TagSystem.NAME].register(beer)
        self.systems[CollisionSystem.NAME].register(beer)
        self.elements.append(beer)
Exemplo n.º 12
0
    def createbait(self, baitx, baity):
        """
        Create bait for cops.

        :param baitx: integer X position
        :param baity: integer Y position
        :return: nothing
        """
        bait = Entity()
        bait.addartifact(
            SpriteArtifact(BaitSprite(self.tile_size), baitx, baity,
                           GameState.GAME))
        bait.addartifact(TagArtifact(TagType.ITEM, TagSubType.BAIT))
        bait.addartifact(BehaviorArtifact(BaitBehavior()))
        self.systems[DrawSystem.NAME].register(bait)
        self.systems[TagSystem.NAME].register(bait)
        self.systems[CollisionSystem.NAME].register(bait)
        self.elements.append(bait)
Exemplo n.º 13
0
    def createdrug(self, drugx, drugy):
        """
        Create drug (powder).

        :param drugx: integer X position
        :param drugy: integer Y position
        :return:
        """
        drug = Entity()
        drug.addartifact(
            SpriteArtifact(DrugSprite(self.tile_size), drugx, drugy,
                           GameState.GAME))
        drug.addartifact(TagArtifact(TagType.ITEM, TagSubType.DRUG))
        drug.addartifact(BehaviorArtifact(DrugBehavior()))
        self.systems[DrawSystem.NAME].register(drug)
        self.systems[TagSystem.NAME].register(drug)
        self.systems[CollisionSystem.NAME].register(drug)
        self.elements.append(drug)
Exemplo n.º 14
0
    def createcap(self, capx, capy):
        """
        Create beer cap.

        :param capx: integer X position
        :param capy: integer Y position
        :return:
        """
        cap = Entity()
        cap.addartifact(
            SpriteArtifact(CapSprite(self.tile_size), capx, capy,
                           GameState.GAME))
        cap.addartifact(TagArtifact(TagType.ITEM, TagSubType.CAP))
        cap.addartifact(BehaviorArtifact(CapBehavior()))
        self.systems[DrawSystem.NAME].register(cap)
        self.systems[TagSystem.NAME].register(cap)
        self.systems[CollisionSystem.NAME].register(cap)
        self.elements.append(cap)
Exemplo n.º 15
0
    def createpill(self, pillx, pilly):
        """
        Create pill.

        :param pillx: integer X position
        :param pilly: integer Y position
        :return:
        """
        pill = Entity()
        pill.addartifact(
            SpriteArtifact(PillSprite(self.tile_size), pillx, pilly,
                           GameState.GAME))
        pill.addartifact(TagArtifact(TagType.ITEM, TagSubType.PILL))
        pill.addartifact(BehaviorArtifact(PillBehavior()))
        self.systems[DrawSystem.NAME].register(pill)
        self.systems[TagSystem.NAME].register(pill)
        self.systems[CollisionSystem.NAME].register(pill)
        self.elements.append(pill)
Exemplo n.º 16
0
    def createteleport(self, teleportx, teleporty):
        """
        Create teleport.

        :param teleportx: integer X position
        :param teleporty: integer Y position
        :return:
        """
        teleport = Entity()
        teleport.addartifact(
            SpriteArtifact(DummySprite(self.tile_size), teleportx, teleporty,
                           GameState.GAME))
        teleport.addartifact(TagArtifact(TagType.FIXED, TagSubType.TELEPORT))
        teleport.addartifact(BehaviorArtifact(TeleportBehavior()))
        self.systems[DrawSystem.NAME].register(teleport)
        self.systems[TagSystem.NAME].register(teleport)
        self.systems[CollisionSystem.NAME].register(teleport)
        self.elements.append(teleport)
Exemplo n.º 17
0
    def createhalloffame(self):
        """
        Create labels and placeholders for loading highscore values.

        :return: nothing
        """
        potential_size_of_one_line = (self.screen_height - 2*self.margin) \
                                     / (HighscoresManager.topscorescount) + 1
        hall_of_fame = Entity()
        hall_of_fame.addartifact(SpriteArtifact(
            TextSprite("HALL OF FAME!", Modifiers.CENTER_H), 0
            , self.margin, GameState.SHOW_HIGHSCORES))
        self.systems[DrawSystem.NAME].register(hall_of_fame)
        self.elements.append(hall_of_fame)

        self.highscorevalues.clear()
        self.highscorenames.clear()

        for i in range(HighscoresManager.topscorescount):
            highscore_player = Entity()
            highscore_player.addartifact(SpriteArtifact(
                TextSprite("x"), self.margin,
                self.margin + (i+1) * potential_size_of_one_line, GameState.SHOW_HIGHSCORES))
            self.systems[DrawSystem.NAME].register(highscore_player)
            self.elements.append(highscore_player)
            self.highscorenames.append(highscore_player)

        for i in range(HighscoresManager.topscorescount):
            highscore_value = Entity()
            highscore_value.addartifact(SpriteArtifact(
                TextSprite("1"), 900,
                self.margin + (i+1) * potential_size_of_one_line,
                GameState.SHOW_HIGHSCORES))
            self.systems[DrawSystem.NAME].register(highscore_value)
            self.elements.append(highscore_value)
            self.highscorevalues.append(highscore_value)
Exemplo n.º 18
0
class MenuBuilder:
    """Class handling building of menu."""

    def __init__(self, _systems):
        """
        Constructor.

        :param _systems: Reference to other systems collection.
        """
        self.dirty = True
        self.systems = _systems
        self.elements = []
        self.highscorenames = []
        self.highscorevalues = []
        self.player_name = None
        self.highscoresmanager = HighscoresManager()
        self.screen_height = 768 #todo: gdzies wyciagnac.
        self.margin = 10

    def build(self):
        """
        Create menu items, and large messages (f.e. after death).

        :return: nothing
        """
        self.createmenubackground(0, 0)
        self.createmenuelement(490, 550, "Play", MenuEventType.START_NEW_GAME, None)
        option_settings = self.createmenuelement(490, 600, "Settings"
                                                 , MenuEventType.MENU_IN, None)
        self.createmenuelement(490, 650, "Hall of fame", MenuEventType.SHOW_HIGHSCORES, None)
        self.createmenuelement(490, 550, "Maximize window", MenuEventType.MAXIMIZE, option_settings)
        self.createmenuelement(490, 600, "Change tile size", MenuEventType.CHANGE_TILE_SIZE
                               , option_settings)
        self.createmenuelement(490, 660, "Back", MenuEventType.MENU_OUT, option_settings)
        self.createmenuelement(490, 700, "Exit", MenuEventType.QUIT, None)
        self.createhud(950, 10)
        self.createresult("You win!!!", GameState.WON_GAME)
        self.createresult("You lost - restarting...!!!", GameState.LOST_LIFE)
        self.createresult("You lost!!!", GameState.LOST_GAME)
        self.createresult("Press 'P' to play!", GameState.PAUSED)
        self.createhighscores()
        self.createhalloffame()

    def clear(self):
        """
        Cleanup of menu items in other systems.

        :return: nothing
        """
        for entity in self.elements:
            for _, system in self.systems.items():
                system.remove(entity)
        self.elements.clear()
        self.systems[HUDSystem.NAME].current_caps = 0  # poprawic

    def input(self, _event):
        """
        Handle rebuilding menu elements on ceratin events.

        Handles entering of name for highscores.
        :param _event: event to process
        :return: nothing
        """
        if _event.type == GAME_STATE_CHANGE_EVENT:
            if _event.state == GameState.MENU and self.dirty:
                self.dirty = False
                self.build()
        elif _event.type == MENU_EVENT:
            if _event.action == MenuEventType.START_NEW_GAME \
                    or _event.action == MenuEventType.CONTINUE_GAME:
                self.clear()
                self.build()
            elif _event.action == MenuEventType.RESTART_GAME:
                self.clear()
                self.build()
            elif _event.action == MenuEventType.UPDATE_NAME:
                nick = copy(_event.nick)
                if len(_event.nick) < _event.maxnick:
                    underscores = "_" * (_event.maxnick - len(_event.nick))
                    nick += underscores

                self.player_name.artifacts[SpriteArtifact.NAME].sprite.changetext(''.join(nick))
            elif _event.action == MenuEventType.SHOW_HIGHSCORES:
                self.reloadhighscores()


    def createmenuelement(self, _elementx, _elementy, _text, _type, _parent):
        """
        Create single menu element.

        :param _elementx: X position of text
        :param _elementy: Y position of text
        :param _text: Text of element
        :param _type: Type of menu event
        :param _parent: Parent menu element.
        :return: menu entity
        """
        menu = Entity()
        menu.addartifact(SpriteArtifact(
            TextSprite(_text, Modifiers.CENTER_H), _elementx, _elementy, GameState.MENU))
        menu.addartifact(MenuArtifact(_type))
        self.systems[DrawSystem.NAME].register(menu)
        self.systems[MenuSystem.NAME].register(menu, _parent)
        self.elements.append(menu)
        return menu

    def createmenubackground(self, _menux, _menuy):
        """
        Create large KomesMan logo.

        :param _menux: X position of logo
        :param _menuy: Y position of logo
        :return: nothing
        """
        background = Entity()
        background.addartifact(
            SpriteArtifact(SimpleImageSprite('res/img/logo.png'), _menux, _menuy, GameState.MENU))
        self.systems[DrawSystem.NAME].register(background)
        self.elements.append(background)

    def createhud(self, _hudx, _hudy):
        """
        Create HUD (points, lives, cap status).

        :param _hudx: X position in pixels
        :param _hudy: Y position in pixels
        :return: nothing
        """
        hud = Entity()
        hud.addartifact(SpriteArtifact(HUDSprite(Modifiers.CENTER_H), _hudx, _hudy, GameState.GAME))
        self.systems[DrawSystem.NAME].register(hud)
        self.systems[HUDSystem.NAME].register(hud)
        self.elements.append(hud)

    def createresult(self, _text, _type):
        """
        Create black board with specified text.

        :param _text: text to display
        :param _type: GameState in which board should be displayed.
        :return: nothing
        """
        result = Entity()
        result.addartifact(SpriteArtifact(
            TextSprite(_text, Modifiers.CENTER_H | Modifiers.CENTER_V), 0, 0, _type))
        self.systems[DrawSystem.NAME].register(result)
        self.elements.append(result)

    def createhighscores(self):
        """
        Create labels for highscore entering, and placeholder for entering player name.

        :return: nothing
        """
        new_highscore = Entity()
        new_highscore.addartifact(SpriteArtifact(
            TextSprite("New highscore!", Modifiers.CENTER_H), 100, 100, GameState.NEW_HIGHSCORE))
        self.systems[DrawSystem.NAME].register(new_highscore)
        self.elements.append(new_highscore)

        enter_your_name = Entity()
        enter_your_name.addartifact(SpriteArtifact(
            TextSprite("Enter your name:", Modifiers.CENTER_H), 100, 200, GameState.NEW_HIGHSCORE))
        self.systems[DrawSystem.NAME].register(enter_your_name)
        self.elements.append(enter_your_name)

        self.player_name = Entity()
        self.player_name.addartifact(SpriteArtifact(
            TextSprite('_'*30, Modifiers.CENTER_H), 100, 300, GameState.NEW_HIGHSCORE))
        self.systems[DrawSystem.NAME].register(self.player_name)
        self.elements.append(self.player_name)

    def createhalloffame(self):
        """
        Create labels and placeholders for loading highscore values.

        :return: nothing
        """
        potential_size_of_one_line = (self.screen_height - 2*self.margin) \
                                     / (HighscoresManager.topscorescount) + 1
        hall_of_fame = Entity()
        hall_of_fame.addartifact(SpriteArtifact(
            TextSprite("HALL OF FAME!", Modifiers.CENTER_H), 0
            , self.margin, GameState.SHOW_HIGHSCORES))
        self.systems[DrawSystem.NAME].register(hall_of_fame)
        self.elements.append(hall_of_fame)

        self.highscorevalues.clear()
        self.highscorenames.clear()

        for i in range(HighscoresManager.topscorescount):
            highscore_player = Entity()
            highscore_player.addartifact(SpriteArtifact(
                TextSprite("x"), self.margin,
                self.margin + (i+1) * potential_size_of_one_line, GameState.SHOW_HIGHSCORES))
            self.systems[DrawSystem.NAME].register(highscore_player)
            self.elements.append(highscore_player)
            self.highscorenames.append(highscore_player)

        for i in range(HighscoresManager.topscorescount):
            highscore_value = Entity()
            highscore_value.addartifact(SpriteArtifact(
                TextSprite("1"), 900,
                self.margin + (i+1) * potential_size_of_one_line,
                GameState.SHOW_HIGHSCORES))
            self.systems[DrawSystem.NAME].register(highscore_value)
            self.elements.append(highscore_value)
            self.highscorevalues.append(highscore_value)

    def reloadhighscores(self):
        """
        Load highscores using highscoresManager into menu entities.

        :return: nothing
        """
        self.highscoresmanager.load()
        i = 0
        for highscore in self.highscoresmanager.highscores:
            self.highscorenames[i].artifacts[SpriteArtifact.NAME].sprite.changetext(
                str(i + 1) + " " + highscore.name)
            self.highscorevalues[i].artifacts[SpriteArtifact.NAME].\
                sprite.changetext(str(highscore.score))
            i += 1