def __init__(self, board, systems, registerlist, tilesize): """ Constructor. :param board: Board - 2d array of WallKind items or 0's if there is no wall. :param systems: Collection of all systems :param registerlist: List where entities should be registered. :param tilesize: Tile size of board in pixels """ super(Board, self).__init__() self.map = [[Entity() for _ in range(len(board))] for _ in range(len(board[0]))] self.board_data = board self.tile_size = tilesize i_x = 0 for row in board: i_y = 0 for cell in row: if cell != 0: tile_sprite = WallSprite(cell, tilesize) self.map[i_y][i_x].addartifact( SpriteArtifact(tile_sprite, i_y * tilesize, i_x * tilesize, GameState.GAME)) systems[DrawSystem.NAME].register(self.map[i_y][i_x]) registerlist.append(self.map[i_y][i_x]) i_y += 1 i_x += 1 self.addartifact(TagArtifact(TagType.FIXED, TagSubType.BOARD)) systems[TagSystem.NAME].register(self) registerlist.append(self)
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 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 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 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 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)
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)
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)
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)
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)
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)
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)
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 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)
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)