Esempio n. 1
0
    def on_init(self):

        self.entities = {}

        self.cell_size = (10, 10)

        # Init map
        map_size = [0, 0]
        for i in (0, 1):
            map_size[i] = int(1.0 * self.screen_size[i] / self.cell_size[i])
            if map_size[i] % 2 == 0:
                map_size[i] -= 1
        self.map_size_in_pixels = tuple(map_size[i] * self.cell_size[i] for i in (0, 1))
        map_generator = generator.MazeGenerator()
        self.map = Map(self, map_size, map_generator)
        empty_cells = list(self.map.get_cells(Cell.FLOOR))

        # Init coins
        self.entities['coins'] = []
        coin_cells = random.sample(list(self.map.get_cells((Cell.FLOOR, Cell.STONE))), 25)
        for cell in coin_cells:
            coin = factory.create_coin(cell.coord)
            self.entities['coins'].append(coin)

        # Init bombs
        self.entities['bombs'] = []

        # Init player
        self.player = factory.create_player(coord=random.choice(empty_cells).coord)

        # Make sure monsters are generated at some distance from the player
        empty_cells = list(cell for cell in empty_cells
                                if 15 < math.sqrt((cell.x - self.player.location.x) ** 2 +
                                                  (cell.y - self.player.location.y) ** 2))

        # Init monsters
        self.entities['monsters'] = []

        # Agressive
        for _ in xrange(5):
            cell = random.choice(empty_cells)
            empty_cells.remove(cell)
            monster = factory.create_monster(coord=cell.coord)
            self.entities['monsters'].append(monster)

#        # Harmless
#        for _ in xrange(1):
#            cell = random.choice(empty_cells)
#            empty_cells.remove(cell)
#            monster = Player.create_harmless_monster(self, location=cell.coord)
#            monster.color = (0, 128, 255)
#            self.entities['monsters'].append(monster)
#
#        # Coward
#        for _ in xrange(1):
#            cell = random.choice(empty_cells)
#            empty_cells.remove(cell)
#            monster = Player.create_coward_monster(self, self.player, location=cell.coord)
#            monster.color = (255, 0, 255)
#            self.entities['monsters'].append(monster)

        # Init drawing surfaces
        self.redraw_cells = []
        self.redraw_map = True
        self.map_surface = pygame.Surface(self.map_size_in_pixels)
        self.draw_surface = pygame.Surface(self.map_size_in_pixels)

        # Pause functionality
        self._paused = False
Esempio n. 2
0
    def on_init(self):

        self.entities = {}

        self.cell_size = (10, 10)

        # Init map
        map_size = [0, 0]
        for i in (0, 1):
            map_size[i] = int(1.0 * self.screen_size[i] / self.cell_size[i])
            if map_size[i] % 2 == 0:
                map_size[i] -= 1
        self.map_size_in_pixels = tuple(map_size[i] * self.cell_size[i]
                                        for i in (0, 1))
        map_generator = generator.MazeGenerator()
        self.map = Map(self, map_size, map_generator)
        empty_cells = list(self.map.get_cells(Cell.FLOOR))

        # Init coins
        self.entities['coins'] = []
        coin_cells = random.sample(
            list(self.map.get_cells((Cell.FLOOR, Cell.STONE))), 25)
        for cell in coin_cells:
            coin = factory.create_coin(cell.coord)
            self.entities['coins'].append(coin)

        # Init bombs
        self.entities['bombs'] = []

        # Init player
        self.player = factory.create_player(
            coord=random.choice(empty_cells).coord)

        # Make sure monsters are generated at some distance from the player
        empty_cells = list(
            cell for cell in empty_cells
            if 15 < math.sqrt((cell.x - self.player.location.x)**2 +
                              (cell.y - self.player.location.y)**2))

        # Init monsters
        self.entities['monsters'] = []

        # Agressive
        for _ in xrange(5):
            cell = random.choice(empty_cells)
            empty_cells.remove(cell)
            monster = factory.create_monster(coord=cell.coord)
            self.entities['monsters'].append(monster)

#        # Harmless
#        for _ in xrange(1):
#            cell = random.choice(empty_cells)
#            empty_cells.remove(cell)
#            monster = Player.create_harmless_monster(self, location=cell.coord)
#            monster.color = (0, 128, 255)
#            self.entities['monsters'].append(monster)
#
#        # Coward
#        for _ in xrange(1):
#            cell = random.choice(empty_cells)
#            empty_cells.remove(cell)
#            monster = Player.create_coward_monster(self, self.player, location=cell.coord)
#            monster.color = (255, 0, 255)
#            self.entities['monsters'].append(monster)

# Init drawing surfaces
        self.redraw_cells = []
        self.redraw_map = True
        self.map_surface = pygame.Surface(self.map_size_in_pixels)
        self.draw_surface = pygame.Surface(self.map_size_in_pixels)

        # Pause functionality
        self._paused = False
Esempio n. 3
0
class Game(BaseGame):

    def on_init(self):

        self.entities = {}

        self.cell_size = (10, 10)

        # Init map
        map_size = [0, 0]
        for i in (0, 1):
            map_size[i] = int(1.0 * self.screen_size[i] / self.cell_size[i])
            if map_size[i] % 2 == 0:
                map_size[i] -= 1
        self.map_size_in_pixels = tuple(map_size[i] * self.cell_size[i] for i in (0, 1))
        map_generator = generator.MazeGenerator()
        self.map = Map(self, map_size, map_generator)
        empty_cells = list(self.map.get_cells(Cell.FLOOR))

        # Init coins
        self.entities['coins'] = []
        coin_cells = random.sample(list(self.map.get_cells((Cell.FLOOR, Cell.STONE))), 25)
        for cell in coin_cells:
            coin = factory.create_coin(cell.coord)
            self.entities['coins'].append(coin)

        # Init bombs
        self.entities['bombs'] = []

        # Init player
        self.player = factory.create_player(coord=random.choice(empty_cells).coord)

        # Make sure monsters are generated at some distance from the player
        empty_cells = list(cell for cell in empty_cells
                                if 15 < math.sqrt((cell.x - self.player.location.x) ** 2 +
                                                  (cell.y - self.player.location.y) ** 2))

        # Init monsters
        self.entities['monsters'] = []

        # Agressive
        for _ in xrange(5):
            cell = random.choice(empty_cells)
            empty_cells.remove(cell)
            monster = factory.create_monster(coord=cell.coord)
            self.entities['monsters'].append(monster)

#        # Harmless
#        for _ in xrange(1):
#            cell = random.choice(empty_cells)
#            empty_cells.remove(cell)
#            monster = Player.create_harmless_monster(self, location=cell.coord)
#            monster.color = (0, 128, 255)
#            self.entities['monsters'].append(monster)
#
#        # Coward
#        for _ in xrange(1):
#            cell = random.choice(empty_cells)
#            empty_cells.remove(cell)
#            monster = Player.create_coward_monster(self, self.player, location=cell.coord)
#            monster.color = (255, 0, 255)
#            self.entities['monsters'].append(monster)

        # Init drawing surfaces
        self.redraw_cells = []
        self.redraw_map = True
        self.map_surface = pygame.Surface(self.map_size_in_pixels)
        self.draw_surface = pygame.Surface(self.map_size_in_pixels)

        # Pause functionality
        self._paused = False

    def get_entities(self, types=None, coord=None):

        if types is None:
            types = self.entities.keys()
        else:
            if not hasattr(types, '__iter__'):
                types = [types]
            types = (t for t in types if self.entities.has_key(t))

        entities = (e for t in types
                      for e in self.entities[t]
                      if not e.destroyed)

        if coord is not None:
            entities = (e for e in entities
                          if e.location and e.location.cr == coord)

        return entities

    def on_update(self):

        if self._paused:
            return

        for e in self.get_entities():
            e.update(self)

        self.player.update(self)

        #if self.player.location_changed:
        #    # Check if player collected a coin
        #    for coin in self.get_entities('coins'):
        #        if coin.location == self.player.location:
        #            coin.collect()

        #for monster in self.get_entities('monsters'):
        #    monster.update(self)

        #for bomb in self.get_entities('bombs'):
        #    bomb.update(self)

    def on_draw(self):

        self._clear_screen()

        self.map.draw(self, self.map_surface)
        self.draw_surface.blit(self.map_surface, (0, 0))

        for e in self.get_entities():
            e.draw(self, self.draw_surface)

        #for coin in self.get_entities('coins'):
        #    coin.draw(self, self.draw_surface, milliseconds)

        #for bomb in self.get_entities('bombs'):
        #    bomb.draw(self, self.draw_surface, milliseconds)

        #self._draw_monsters()
        #self._draw_player()
        self.player.draw(self, self.draw_surface)

        #self._draw_fps()

        self._update_screen()

    def _clear_screen(self):
        self.screen.fill((0, 0, 0))
        self.draw_surface.fill((0, 0, 0))

    def _update_screen(self):
        offset = [0, 0]
        for i in (0, 1):
            if self.map_size_in_pixels[i] <= self.screen_size[i]:
                offset[i] = (self.screen_size[i] - self.map_size_in_pixels[i]) / 2
            else:
                player_offset = round(self.cell_size[i] * (self.player.location[i] + self.player.offset[i]))
                offset[i] = self.screen_size[i] / 2 - player_offset
                if offset[i] > 0:
                    offset[i] = 0
                elif offset[i] < self.screen_size[i] - self.map_size_in_pixels[i]:
                    offset[i] = self.screen_size[i] - self.map_size_in_pixels[i]
        self.screen.blit(self.draw_surface, offset)

    def on_keydown(self, key):
        if key == pygame.K_ESCAPE:
            self.stop()
        if key == pygame.K_PAUSE:
            self.toggle_pause()

    def pause(self):
        self._paused = True

    def unpause(self):
        self._paused = False

    def toggle_pause(self):
        self._paused = not self._paused
Esempio n. 4
0
class Game(BaseGame):
    def on_init(self):

        self.entities = {}

        self.cell_size = (10, 10)

        # Init map
        map_size = [0, 0]
        for i in (0, 1):
            map_size[i] = int(1.0 * self.screen_size[i] / self.cell_size[i])
            if map_size[i] % 2 == 0:
                map_size[i] -= 1
        self.map_size_in_pixels = tuple(map_size[i] * self.cell_size[i]
                                        for i in (0, 1))
        map_generator = generator.MazeGenerator()
        self.map = Map(self, map_size, map_generator)
        empty_cells = list(self.map.get_cells(Cell.FLOOR))

        # Init coins
        self.entities['coins'] = []
        coin_cells = random.sample(
            list(self.map.get_cells((Cell.FLOOR, Cell.STONE))), 25)
        for cell in coin_cells:
            coin = factory.create_coin(cell.coord)
            self.entities['coins'].append(coin)

        # Init bombs
        self.entities['bombs'] = []

        # Init player
        self.player = factory.create_player(
            coord=random.choice(empty_cells).coord)

        # Make sure monsters are generated at some distance from the player
        empty_cells = list(
            cell for cell in empty_cells
            if 15 < math.sqrt((cell.x - self.player.location.x)**2 +
                              (cell.y - self.player.location.y)**2))

        # Init monsters
        self.entities['monsters'] = []

        # Agressive
        for _ in xrange(5):
            cell = random.choice(empty_cells)
            empty_cells.remove(cell)
            monster = factory.create_monster(coord=cell.coord)
            self.entities['monsters'].append(monster)

#        # Harmless
#        for _ in xrange(1):
#            cell = random.choice(empty_cells)
#            empty_cells.remove(cell)
#            monster = Player.create_harmless_monster(self, location=cell.coord)
#            monster.color = (0, 128, 255)
#            self.entities['monsters'].append(monster)
#
#        # Coward
#        for _ in xrange(1):
#            cell = random.choice(empty_cells)
#            empty_cells.remove(cell)
#            monster = Player.create_coward_monster(self, self.player, location=cell.coord)
#            monster.color = (255, 0, 255)
#            self.entities['monsters'].append(monster)

# Init drawing surfaces
        self.redraw_cells = []
        self.redraw_map = True
        self.map_surface = pygame.Surface(self.map_size_in_pixels)
        self.draw_surface = pygame.Surface(self.map_size_in_pixels)

        # Pause functionality
        self._paused = False

    def get_entities(self, types=None, coord=None):

        if types is None:
            types = self.entities.keys()
        else:
            if not hasattr(types, '__iter__'):
                types = [types]
            types = (t for t in types if self.entities.has_key(t))

        entities = (e for t in types for e in self.entities[t]
                    if not e.destroyed)

        if coord is not None:
            entities = (e for e in entities
                        if e.location and e.location.cr == coord)

        return entities

    def on_update(self):

        if self._paused:
            return

        for e in self.get_entities():
            e.update(self)

        self.player.update(self)

        #if self.player.location_changed:
        #    # Check if player collected a coin
        #    for coin in self.get_entities('coins'):
        #        if coin.location == self.player.location:
        #            coin.collect()

        #for monster in self.get_entities('monsters'):
        #    monster.update(self)

        #for bomb in self.get_entities('bombs'):
        #    bomb.update(self)

    def on_draw(self):

        self._clear_screen()

        self.map.draw(self, self.map_surface)
        self.draw_surface.blit(self.map_surface, (0, 0))

        for e in self.get_entities():
            e.draw(self, self.draw_surface)

        #for coin in self.get_entities('coins'):
        #    coin.draw(self, self.draw_surface, milliseconds)

        #for bomb in self.get_entities('bombs'):
        #    bomb.draw(self, self.draw_surface, milliseconds)

        #self._draw_monsters()
        #self._draw_player()
        self.player.draw(self, self.draw_surface)

        #self._draw_fps()

        self._update_screen()

    def _clear_screen(self):
        self.screen.fill((0, 0, 0))
        self.draw_surface.fill((0, 0, 0))

    def _update_screen(self):
        offset = [0, 0]
        for i in (0, 1):
            if self.map_size_in_pixels[i] <= self.screen_size[i]:
                offset[i] = (self.screen_size[i] -
                             self.map_size_in_pixels[i]) / 2
            else:
                player_offset = round(
                    self.cell_size[i] *
                    (self.player.location[i] + self.player.offset[i]))
                offset[i] = self.screen_size[i] / 2 - player_offset
                if offset[i] > 0:
                    offset[i] = 0
                elif offset[
                        i] < self.screen_size[i] - self.map_size_in_pixels[i]:
                    offset[
                        i] = self.screen_size[i] - self.map_size_in_pixels[i]
        self.screen.blit(self.draw_surface, offset)

    def on_keydown(self, key):
        if key == pygame.K_ESCAPE:
            self.stop()
        if key == pygame.K_PAUSE:
            self.toggle_pause()

    def pause(self):
        self._paused = True

    def unpause(self):
        self._paused = False

    def toggle_pause(self):
        self._paused = not self._paused