示例#1
0
def get_game_variables(constants):
	fighter_component = Fighter(hp=100, defense=1, power=4)
	inventory_component = Inventory(26)
	level_component = Level()
	player = Entity(0, 0, '@', libtcod.white, 'Player', blocks=True, render_order=RenderOrder.ACTOR, fighter=fighter_component, inventory=inventory_component, level=level_component)
	entities = [player]


	game_map = GameMap(constants['map_width'], constants['map_height'])
	game_map.make_map(constants['max_rooms'], constants['room_min_size'], constants['room_max_size'], constants['map_width'], constants['map_height'], player, entities)

	message_log = MessageLog(constants['message_x'], constants['message_width'], constants['message_height'])

	game_state = GameStates.PLAYERS_TURN

	return player, entities, game_map, message_log, game_state

	return constants
示例#2
0
def main():
    screen_width = 80
    screen_height = 50

    bar_width = 20
    panel_height = 7
    panel_y = screen_height - panel_height

    message_x = bar_width + 2
    message_width = screen_width - bar_width - 2
    message_height = panel_height - 1

    map_width = 80
    map_height = 43

    room_max_size = 10
    room_min_size = 6
    max_rooms = 30

    fov_algorithm = 0
    fov_light_walls = True
    fov_radius = 10

    max_monsters_per_room = 3

    colors = {
        'dark_wall': libtcod.Color(0, 0, 100),
        'dark_ground': libtcod.Color(50, 50, 150),
        'light_wall': libtcod.Color(130, 110, 50),
        'light_ground': libtcod.Color(200, 180, 50)
    }

    fighter_component = Fighter(hp=30, defense=2, power=5)
    player = Entity(0,
                    0,
                    "@",
                    libtcod.white,
                    'Player',
                    blocks=True,
                    render_order=RenderOrder.ACTOR,
                    fighter=fighter_component)
    entities = [player]

    libtcod.console_set_custom_font(
        'arial10x10.png',
        libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD)

    libtcod.console_init_root(screen_width, screen_height, 'Paradiso', False)

    con = libtcod.console_new(screen_width, screen_height)
    panel = libtcod.console_new(screen_width, panel_height)

    game_map = GameMap(map_width, map_height)
    game_map.make_map(max_rooms, room_min_size, room_max_size, map_width,
                      map_height, player, entities, max_monsters_per_room)

    fov_recompute = True

    fov_map = initialize_fov(game_map)

    message_log = MessageLog(message_x, message_width, message_height)

    key = libtcod.Key()
    mouse = libtcod.Mouse()

    game_state = GameStates.PLAYERS_TURN

    while not libtcod.console_is_window_closed():
        libtcod.sys_check_for_event(
            libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse)

        if fov_recompute:
            recompute_fov(fov_map, player.x, player.y, fov_radius,
                          fov_light_walls, fov_algorithm)

        render_all(con, panel, entities, player, game_map, fov_map,
                   fov_recompute, message_log, screen_width, screen_height,
                   bar_width, panel_height, panel_y, mouse, colors)
        fov_recompute = False

        libtcod.console_flush()

        clear_all(con, entities)

        action = handle_keys(key)

        move = action.get('move')
        exit = action.get('exit')
        fullscreen = action.get('fullscreen')

        player_turn_results = []

        if move and game_state == GameStates.PLAYERS_TURN:
            dx, dy = move
            destination_x = player.x + dx
            destination_y = player.y + dy

            if not game_map.is_blocked(destination_x, destination_y):
                target = get_blocking_entities_at_location(
                    entities, destination_x, destination_y)

                if target:
                    attack_results = player.fighter.attack(target)
                    player_turn_results.extend(attack_results)
                else:
                    player.move(dx, dy)
                    fov_recompute = True

                game_state = GameStates.ENEMY_TURN

        if exit:
            return True

        if fullscreen:
            libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen())

        for player_turn_result in player_turn_results:
            message = player_turn_result.get('message')
            dead_entity = player_turn_result.get('dead')

            if message:
                message_log.add_message(message)

            if dead_entity:
                if dead_entity == player:
                    message, game_state = kill_player(dead_entity)
                else:
                    message = kill_monster(dead_entity)

                message_log.add_message(message)

        if game_state == GameStates.ENEMY_TURN:
            for entity in entities:
                if entity.ai:
                    enemy_turn_results = entity.ai.take_turn(
                        player, fov_map, game_map, entities)

                    for enemy_turn_result in enemy_turn_results:
                        message = enemy_turn_result.get('message')
                        dead_entity = enemy_turn_result.get('dead')

                        if message:
                            message_log.add_message(message)

                        if dead_entity:
                            if dead_entity == player:
                                message, game_state = kill_player(dead_entity)
                            else:
                                message = kill_monster(dead_entity)

                            message_log.add_message(message)

                            if game_state == GameStates.PLAYER_DEAD:
                                break

                    if game_state == GameStates.PLAYER_DEAD:
                        break
            else:
                game_state = GameStates.PLAYERS_TURN
示例#3
0
def main():
    # Importing data from config
    with open('data/config.json') as cfg:
        data=json.load(cfg)
    terminal_width=data['terminal_width']
    terminal_height=data['terminal_height']
    map_width=data['map_width']
    map_height=data['map_height']
    fov_algorithm=data['fov_algorithm']
    fov_light_walls=data['fov_light_walls']
    fov_radius=data['fov_radius']
    # Init root console
    tcod.console_set_custom_font('gfx/fonts/terminal12x16_gs_ro.png', tcod.FONT_TYPE_GREYSCALE | tcod.tcod.FONT_LAYOUT_CP437)
    console_root=tcod.console_init_root(terminal_width, terminal_height, 'Python Game Lol', False, tcod.RENDERER_SDL2, 'C', False)
    console_display=tcod.console.Console(terminal_width, terminal_height, 'C')
    with open('gfx/palette.json') as colours:
        palette=json.load(colours)
    console_display.bg[:]=palette['terminal_green']
    #console_interface=tcod.console.Console(terminal_width, map_height, 'C')
    game_map=GameMap(map_width, map_height)
    spawner=Spawner(map_width, map_height, 0)
            # Testing creatures
    generate_test_area(game_map, spawner)
    player=spawner.entities[0]
    # Then generate map
    fov_recompute=True
    # message log
    game_state=GameStates.TURN_PLAYER
    prev_game_state=game_state
    #targeting_item=None
    # Rendering for the first time
    game_map.recompute_fov(player.x, player.y, fov_radius, fov_light_walls, fov_algorithm)
    render_all(console_root, console_display, spawner.entities, game_map, fov_recompute)
    fov_recompute=False
    tcod.console_flush()
    # Game loop
    while True:
        # Render
        if fov_recompute:
            game_map.recompute_fov(player.x, player.y, fov_radius, fov_light_walls, fov_algorithm)
        render_all(console_root, console_display, spawner.entities, game_map, fov_recompute)
        fov_recompute=False
        tcod.console_flush()
        erase_entities(console_display, spawner.entities, game_map)
        # Processing action
        action=handle_event(game_state)
        move=action.get('move')
        pickup=action.get('pickup')
        take_inventory=action.get('take_inventory')
        fullscreen=action.get('fullscreen')
        exit=action.get('exit')
        # Player's Turn
        results_player=[]
        # Results: Extend if player turn ends and append if doesn't? Tcod tutorial is confusing.
        if game_state==GameStates.TURN_PLAYER:
            if move:
                dx, dy=move
                results_movement=player.handle_move(dx, dy, spawner, game_map.path_map, swappable=True)
                if results_movement:
                    results_player.extend(results_movement)
                    fov_recompute=True
                    game_state=GameStates.TURN_ALLY

            elif pickup: # Should implement a pickup list like POWDER
                for entity in spawner.entities:
                    if entity.item and entity.x==player.x and entity.y==player.y:
                        print('ADD ITEM')   # Add item
                        break
                else:
                    print('GRAB GROUND')    # Message log to grab ground
        
        if take_inventory:
            prev_game_state=game_state
            game_state=GameStates.MENU_INVENTORY
        
        if exit:
            if game_state.value>=10:  # Game states >= 10 are menus: inventory, quipment, etc.
                game_state=prev_game_state
            if game_state.value>=20:  # Game states >= 20 are targetings
                results_player.append({'targeting_cancelled': True})
            # else brings up main menu
        
        if fullscreen:
            tcod.console_set_fullscreen(not tcod.console_is_fullscreen())

        # Player turn messages: handled by an announcer (translation friendly probably)

        # Faction turns (handled by an announcer also)
        if game_state==GameStates.TURN_ALLY:
            for entity in spawner.entities:
                if entity.faction==Factions.ALLY and entity!=player:
                    results_ally=entity.ai.take_turn(spawner, game_map.path_map)
            game_state=GameStates.TURN_ENEMY
        
        if game_state==GameStates.TURN_ENEMY:
            for entity in spawner.entities:
                if entity.faction==Factions.ENEMY:
                    results_enemy=entity.ai.take_turn(spawner, game_map.path_map)
            game_state=GameStates.TURN_NEUTRAL
        
        if game_state==GameStates.TURN_NEUTRAL:
            for entity in spawner.entities:
                if entity.faction==Factions.NEUTRAL:
                    if entity.ai:
                        resutls_neutral=entity.ai.take_turn(spawner, game_map.path_map)
            game_state=GameStates.TURN_PLAYER
示例#4
0
def main():
    screen_width = 80
    screen_height = 50
    map_width = 80
    map_height = 45
    room_max_size = 10
    room_min_size = 6
    max_rooms = 30

    fov_algorithm = 0
    fov_light_walls = True
    fov_radius = 10

    max_monsters_per_room = 3

    colors = {
        'light_wall': libtcod.Color(82, 53, 52),
        'light_ground': libtcod.Color(82, 81, 52),
        'dark_wall': libtcod.Color(39, 40, 57),
        'dark_ground': libtcod.Color(50, 54, 87)
    }

    fighter_component = Fighter(hp=30, defense=2, power=5)
    player = Entity(0,
                    0,
                    '@',
                    libtcod.white,
                    'Player',
                    blocks=True,
                    fighter=fighter_component)
    entities = [player]

    libtcod.console_set_custom_font(
        'resources/arial10x10.png',
        libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD)

    libtcod.console_init_root(screen_width,
                              screen_height,
                              'pyrogue',
                              False,
                              libtcod.RENDERER_SDL2,
                              vsync=True)

    con = libtcod.console.Console(screen_width, screen_height)

    game_map = GameMap(map_width, map_height)
    game_map.make_map(max_rooms, room_min_size, room_max_size, map_width,
                      map_height, player, entities, max_monsters_per_room)

    fov_recompute = True
    fov_map = initialize_fov(game_map)

    game_state = GameStates.PLAYERS_TURN

    while True:

        if fov_recompute:
            recompute_fov(fov_map, player.x, player.y, fov_radius,
                          fov_light_walls, fov_algorithm)

        render_all(con, entities, game_map, fov_map, fov_recompute,
                   screen_width, screen_height, colors)
        fov_recompute = False
        libtcod.console_flush()
        clear_all(con, entities, fov_map)

        for event in tcod.event.wait():
            if event.type == "QUIT":
                raise SystemExit()
            elif event.type == "KEYDOWN":
                action = handle_keys(event)

                move = action.get('move')
                exit = action.get('exit')
                fullscreen = action.get('fullscreen')

                if move and game_state == GameStates.PLAYERS_TURN:
                    dx, dy = move
                    destination_x = player.x + dx
                    destination_y = player.y + dy
                    if not game_map.is_blocked(destination_x, destination_y):
                        target = get_blocking_entities_at_location(
                            entities, destination_x, destination_y)

                        if target:
                            print('You kick the ' + target.name +
                                  ' in the shins, much to its annoyance!')
                        else:
                            player.move(dx, dy)
                            fov_recompute = True

                        game_state = GameStates.ENEMY_TURN

                if exit:
                    return True

                if fullscreen:
                    libtcod.console_set_fullscreen(
                        not libtcod.console_is_fullscreen())

                if game_state == GameStates.ENEMY_TURN:
                    for entity in entities:
                        if entity.ai:
                            entity.ai.take_turn()

                    game_state = GameStates.PLAYERS_TURN
示例#5
0
def main():
    screen_width = 80
    screen_height = 50

    map_width = 80
    map_height = 45
    room_max_size = 10
    room_min_size = 6
    max_rooms = 30

    colors = {
        'dark_wall': tcod.Color(0, 0, 100),
        'dark_ground': tcod.Color(50, 50, 150),
    }

    player = Entity(int(screen_width / 2), int(screen_height / 2), '@',
                    tcod.white)
    npc = Entity(int(screen_width / 2 - 5), int(screen_height / 2), '@',
                 tcod.yellow)
    entities = [npc, player]

    tcod.console_set_custom_font(
        'arial10x10.png', tcod.FONT_TYPE_GRAYSCALE | tcod.FONT_LAYOUT_TCOD)

    con = tcod.console_init_root(screen_width, screen_height,
                                 'tcod tutorial revised', False,
                                 tcod.RENDERER_SDL2)

    game_map = GameMap(map_width, map_height)
    game_map.make_map(max_rooms, room_min_size, room_max_size, map_width,
                      map_height, player)

    tcod.console_set_char_background(con, 6, 6, tcod.red, flag=tcod.BKGND_NONE)
    while True:  # Main loop

        render_all(con, entities, game_map, screen_width, screen_height,
                   colors)
        tcod.console_flush()
        clear_all(con, entities)

        for event in tc_event.wait():
            if event.type == "QUIT":
                print(event)
                raise SystemExit()
            elif event.type == "KEYDOWN":
                action = handle_keys(event)

                move = action.get('move')
                exit = action.get('exit')
                fullscreen = action.get('fullscreen')

                if move:
                    dx, dy = move
                    if not game_map.is_blocked(player.x + dx, player.y + dy):
                        player.move(dx, dy)

                if exit:
                    raise SystemExit()

                if fullscreen:
                    tcod.console_set_fullscreen(
                        not tcod.console_is_fullscreen())
示例#6
0
    def make_map(self, destination):

        # Set debug level
        if destination == "debug":
            game_map = GameMap(20, 20, "debug")
            game_map.owner = self
            # game_map.generate_trees(0, 0, game_map.width,
            #                        game_map.height, 20, block_sight=True)
            # game_map.generate_forest()
            # game_map.generate_cavern()
            game_map.place_entities()
            # Initialize field of view
            self.player.light_source.initialize_fov(game_map)

        elif destination == "dream":
            level_params = self.generate_level_params()
            level_data = MenuData(name="choose_level", params=level_params)
            self.owner.menus.create_or_show_menu(level_data)
            if not self.owner.menus.choose_level.event:
                return
            self.world_tendency = self.params["modifier"]
            game_map = GameMap(width=50,
                               height=50,
                               name="dream",
                               title=self.params["title"])
            game_map.owner = self
            game_map.generate_map()
            self.items[game_map.name] = game_map
            self.current_map = game_map
            game_map.place_entities()
            # Initialize field of view
            self.player.light_source.initialize_fov(game_map)
            self.player.light_source.radius = self.player.fighter.fov
            self.player.light_source.recompute_fov(self.player.x,
                                                   self.player.y)
示例#7
0
def game_map() -> GameMap:
    return GameMap(width=50, height=50)
示例#8
0
def main():
    screen_width = 64
    screen_height = 32

    bar_width = 18
    panel_height = 4
    panel_y = screen_height - panel_height

    map_width = 30
    map_height = 30

    room_max_size = 10
    room_min_size = 6
    max_rooms = 30

    colors = {
        'dark_wall': libtcod.Color(50, 50, 50),
        'dark_ground': libtcod.Color(150, 150, 150)
    }

    #Player starting add-ons
    player_stats = Opponent(hp=3000, mp=1000, defense=2, power=5)
    player_inventory = Inventory(4)

    #Player instantiation
    player = Object(int(screen_width / 2),
                    int(screen_height / 2),
                    '@',
                    libtcod.white,
                    'Player',
                    blocks=True,
                    opponent=player_stats,
                    inventory=player_inventory)

    #Player stat change tests
    player.opponent.hp = 1500
    player.opponent.mp = 500

    npc = Object(int(screen_width / 2 - 5), int(screen_height / 2), '@',
                 libtcod.yellow, 'NPC')
    objects = [npc, player]

    libtcod.console_set_custom_font(
        'testfont8x16_aa_tc.png',
        libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD)
    libtcod.console_init_root(screen_width, screen_height, 'Clean Slate',
                              False)

    con = libtcod.console_new(screen_width, screen_height)
    panel = libtcod.console_new(screen_width, panel_height)

    game_map = GameMap(map_width, map_height)
    game_map.make_map(max_rooms, room_min_size, room_max_size, map_width,
                      map_height, player)

    #key = libtcod.console_wait_for_keypress(True)
    key = libtcod.console_check_for_keypress()
    mouse = libtcod.Mouse()

    game_state = GameStates.PLAYER_TURN
    previous_game_state = game_state

    while not libtcod.console_is_window_closed():

        render_all(con, panel, objects, player, game_map, screen_width,
                   screen_height, bar_width, panel_height, panel_y, colors,
                   game_state)

        libtcod.console_flush()
        clear_all(con, objects)

        #############################
        #Player Control Section
        #############################

        libtcod.sys_check_for_event(libtcod.EVENT_KEY_RELEASE, key, mouse)
        action = handle_keys(key, game_state)

        move = action.get('move')
        direction = action.get('direction')
        exit = action.get('exit')
        fullscreen = action.get('fullscreen')
        inventory = action.get('inventory')

        if inventory:
            previous_game_state = game_state
            game_state = GameStates.PLAYER_MENU
            libtcod.console_print(panel, 0, 0, "INVENTORY")

        if move:

            dx, dy = move

            if direction == 'up':
                player.setGlyph('^')

            elif direction == 'down':
                player.setGlyph('v')

            elif direction == 'left':
                player.setGlyph('<')

            else:
                player.setGlyph('>')

            if not game_map.is_blocked(player.x + dx, player.y +
                                       dy) and player.direction == direction:
                player.move(dx, dy)

            if player.direction != direction:
                player.setDirection(direction)

        if exit:
            if game_state == GameStates.PLAYER_MENU:
                game_state = previous_game_state
            else:
                return True

        if fullscreen:
            libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen())
示例#9
0
def main():
    screen_width = 80
    screen_height = 50
    map_width = 80
    map_height = 45

    room_max_size = 10
    room_min_size = 6
    max_rooms = 30

    colors = {
        'dark_wall': libtcod.Color(0, 0, 0),
        'dark_ground': libtcod.Color(128, 128, 128)
    }

    player = Entity(int(screen_width / 2), int(screen_height / 2), '@',
                    libtcod.green)
    npc = Entity(int(screen_width / 2 - 5), int(screen_height / 2), '@',
                 libtcod.yellow)
    entities = [
        # npc,
        player
    ]

    libtcod.console_set_custom_font(
        'arial10x10.png',
        libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD)

    libtcod.console_init_root(screen_width, screen_height, 'Roguelike Jam',
                              False)

    con = libtcod.console.Console(screen_width, screen_height)

    game_map = GameMap(map_width, map_height)
    game_map.make_map(max_rooms, room_min_size, room_max_size, map_width,
                      map_height, player)

    key = libtcod.Key()
    mouse = libtcod.Mouse()

    while not libtcod.console_is_window_closed():
        libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS, key, mouse)

        render_all(con, entities, game_map, screen_width, screen_height,
                   colors)
        libtcod.console_set_default_foreground(0, libtcod.green)
        libtcod.console_put_char(0, player.x, player.y, '@',
                                 libtcod.BKGND_NONE)
        libtcod.console_flush()

        clear_all(con, entities)
        libtcod.console_put_char(0, player.x, player.y, ' ',
                                 libtcod.BKGND_NONE)

        action = handle_keys(key)

        move = action.get('move')
        exit = action.get('exit')
        fullscreen = action.get('fullscreen')

        if move:
            dx, dy = move
            if not game_map.is_blocked(player.x + dx, player.y + dy):
                player.move(dx, dy)

        if exit:
            return True

        if fullscreen:
            libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen())
示例#10
0
def main():
    #initilize main variables
    screen_width = 80
    screen_height = 50

    bar_width = 20
    panel_height = 7
    panel_y = screen_height - panel_height

    message_x = bar_width + 2
    message_width = screen_width - bar_width - 2
    message_height = panel_height - 1

    map_width = 80
    map_height = 43

    room_max_size = 10
    room_min_size = 6
    max_rooms = 30

    fov_algorithm = 0
    fov_light_walls = True
    fov_radius = 10

    max_monsters_per_room = 3
    max_items_per_room = 2

    #define color palette
    colors = {
        'dark_wall': libtcod.Color(55, 50, 45),
        'dark_ground': libtcod.Color(15, 15, 15),
        'light_wall': libtcod.Color(70, 55, 40),
        'light_ground': libtcod.Color(30, 20, 15)
    }

    #Create Player
    fighter_component = Fighter(hp=30, defense=2, power=5)
    inventory_component = Inventory(26)
    player = Entity(0,
                    0,
                    '@',
                    libtcod.white,
                    'Player',
                    blocks=True,
                    render_order=RenderOrder.ACTOR,
                    fighter=fighter_component,
                    inventory=inventory_component)
    entities = [player]
    player_won = False

    #set up screen
    libtcod.console_set_custom_font(
        'arial12x12.png',
        libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD)
    libtcod.console_init_root(screen_width, screen_height, 'SimpleRogue',
                              False)
    con = libtcod.console_new(screen_width, screen_height)
    panel = libtcod.console_new(screen_width, panel_height)

    key = libtcod.Key()
    mouse = libtcod.Mouse()

    #Generate map with entities
    game_map = GameMap(map_width, map_height)
    game_map.make_map(max_rooms, room_min_size, room_max_size, map_width,
                      map_height, player, entities, max_monsters_per_room,
                      max_items_per_room)

    fov_recompute = True
    fov_map = initializ_fov(game_map)
    game_state = GameStates.PLAYERS_TURN
    previous_game_state = game_state

    message_log = MessageLog(message_x, message_width, message_height)

    # main game loop
    while not libtcod.console_is_window_closed():
        libtcod.sys_check_for_event(
            libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse)

        #Update Display
        if fov_recompute:
            recompute_fov(fov_map, player.x, player.y, fov_radius,
                          fov_light_walls, fov_algorithm)

        render_all(con, panel, entities, player, game_map, fov_map,
                   fov_recompute, message_log, screen_width, screen_height,
                   bar_width, panel_height, panel_y, mouse, colors, game_state)
        fov_recompute = False
        libtcod.console_flush()
        clear_all(con, entities)

        #Define possible actions
        action = handle_keys(key, game_state)

        move = action.get('move')
        pickup = action.get('pickup')
        show_inventory = action.get('show_inventory')
        inventory_index = action.get('inventory_index')
        exit = action.get('exit')
        fullscreen = action.get('fullscreen')

        #Clear the last set of results from actions
        player_turn_results = []

        #Handle actions

        #move the player
        if move and game_state == GameStates.PLAYERS_TURN:
            dx, dy = move
            destination_x = player.x + dx
            destination_y = player.y + dy

            if not game_map.is_blocked(destination_x, destination_y):
                target = get_blocking_entities_at_location(
                    entities, destination_x, destination_y)

                if target:
                    player.fighter.attack(target)
                    attack_results = player.fighter.attack(target)
                    player_turn_results.extend(attack_results)
                else:
                    player.move(dx, dy)
                    fov_recompute = True

            game_state = GameStates.ENEMY_TURN

        #try to pick up something
        elif pickup and game_state == GameStates.PLAYERS_TURN:
            for entity in entities:
                if entity.item and entity.x == player.x and entity.y == player.y:
                    pickup_results = player.inventory.add_item(entity)
                    player_turn_results.extend(pickup_results)

                    break
            else:
                message_log.add_message(
                    Message('There is nothing here to pick up.',
                            libtcod.yellow))

        #display the inventory screen
        if show_inventory:
            if game_state != GameStates.SHOW_INVENTORY:
                previous_game_state = game_state
            game_state = GameStates.SHOW_INVENTORY

        #use an item in the inventory
        if inventory_index is not None and previous_game_state != GameStates.PLAYER_DEAD and inventory_index < len(
                player.inventory.items):
            item = player.inventory.items[inventory_index]
            player_turn_results.extend(player.inventory.use(item))

        #exit screen or close game
        if exit:
            if game_state == GameStates.SHOW_INVENTORY:
                game_state = previous_game_state
            else:
                return True

        #toggle full screen view
        if fullscreen:
            libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen())

        #Display results of player actions
        for player_turn_result in player_turn_results:
            message = player_turn_result.get('message')
            dead_entity = player_turn_result.get('dead')
            item_added = player_turn_result.get('item_added')
            item_consumed = player_turn_result.get('consumed')

            if message:
                message_log.add_message(message)

            if dead_entity:
                if dead_entity == player:
                    message, game_states = kill_player(dead_entity)
                else:
                    message = kill_monster(dead_entity)

                message_log.add_message(message)

            if item_added:
                entities.remove(item_added)
                game_state = GameStates.ENEMY_TURN

            if item_consumed:
                game_state = GameStates.ENEMY_TURN

        #check for win (player killed everything)
        fighter_count = 0
        for entity in entities:
            if entity.fighter is not None and entity.name != 'Player':
                fighter_count += 1
        if fighter_count == 0 and player_won != True:
            player_won = True
            message_log.add_message(
                Message("I hope you're proud of yourself...", libtcod.yellow))
            message_log.add_message(Message("You monster!", libtcod.red))

        #Handle enemy actions and display results
        if game_state == GameStates.ENEMY_TURN:
            for entity in entities:
                if entity.ai:
                    enemy_turn_results = entity.ai.take_turn(
                        player, fov_map, game_map, entities)

                    for enemy_turn_result in enemy_turn_results:
                        message = enemy_turn_result.get('message')
                        dead_entity = enemy_turn_result.get('dead')

                        if message:
                            message_log.add_message(message)

                        if dead_entity:
                            if dead_entity == player:
                                message, game_state = kill_player(dead_entity)
                            else:
                                message = kill_monster(dead_entity)

                            message_log.add_message(message)

                            if game_state == GameStates.PLAYER_DEAD:
                                break
            else:
                game_state = GameStates.PLAYERS_TURN
示例#11
0
def main():
    screen_width = 80
    screen_height = 50
    map_width = 80
    map_height = 45

    room_max_size = 10
    room_min_size = 6
    max_rooms = 30

    fov_algorithm = 0
    fov_light_walls = True
    fov_radius = 10

    max_monsters_per_room = 3

    colors = {
        'dark_wall': libtcod.Color(0, 0, 100),
        'dark_ground': libtcod.Color(50, 50, 150),
        'light_wall': libtcod.Color(130, 110, 50),
        'light_ground': libtcod.Color(200, 180, 50)
    }

    player = Entity(0, 0, '@', libtcod.white, 'Player', blocks=True)
    entities = [player]

    libtcod.console_set_custom_font(
        'arial10x10.png',
        libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD)

    libtcod.console_init_root(screen_width, screen_height,
                              'libtcod tutorial revised', False)

    con = libtcod.console_new(screen_width, screen_height)

    game_map = GameMap(map_width, map_height)
    game_map.make_map(max_rooms, room_min_size, room_max_size, map_width,
                      map_height, player, entities, max_monsters_per_room)

    fov_recompute = True

    fov_map = initialize_fov(game_map)

    key = libtcod.Key()
    mouse = libtcod.Mouse()

    game_state = GameStates.PLAYERS_TURN

    while not libtcod.console_is_window_closed():
        libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS, key, mouse)

        render_all(con, entities, game_map, fov_map, fov_recompute,
                   screen_width, screen_height, colors)

        fov_recompute = False

        libtcod.console_flush()

        clear_all(con, entities)

        action = handle_keys(key)
        move = action.get('move')
        exit = action.get('exit')
        fullscreen = action.get('fullscreen')

        if move and game_state == GameStates.PLAYERS_TURN:
            dx, dy = move

            destination_x = player.x + dx
            destination_y = player.y + dy

            if not game_map.is_blocked(destination_x, destination_y):
                target = get_blocking_entities_at_location(
                    entities, destination_x, destination_y)

                if target:
                    print('You kick the ' + target.name +
                          ' in the shins, much to its annoyance!')
                else:
                    player.move(dx, dy)

                    fov_recompute = True

                game_state = GameStates.ENEMY_TURN

        if exit:
            return True

        if fullscreen:
            libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen())

        if game_state == GameStates.ENEMY_TURN:
            for entity in entities:
                if entity != player:
                    print('The ' + entity.name +
                          ' ponders the meaning of its existence.')

                game_state = GameStates.PLAYERS_TURN

        if fov_recompute:
            recompute_fov(fov_map, player.x, player.y, fov_radius,
                          fov_light_walls, fov_algorithm)
示例#12
0
文件: engine.py 项目: knurt/Mary
def main():

    """
    객체 생성
    """
    # 플레이어 객체 생성. 위치는 맵 중앙.

    fighter_component = Fighter(hp=30, defense=2, power=5)
    luminary_component = Luminary(luminosity=10)
    inventory_component = Inventory(26)
    player = Entity(int(map_width/2),int(map_height/2),'@',tcod.white, 'You', blocks=True, render_order=RenderOrder.ACTOR, _Luminary=luminary_component, _Fighter=fighter_component, _Inventory=inventory_component)
    entities = [player]

    i_comp = Item(use_function=read,about='about activities of you and your best friend, Mary')
    Journal = Entity(player.x,player.y, ':', tcod.darkest_red,
                     'Swallowstone Journal', render_order=RenderOrder.ITEM, _Item = i_comp)

    i_comp = Item(use_function=talisman)
    Talisman = Entity(player.x,player.y, '*', tcod.lighter_purple,
                      'Passionflower Talisman', render_order=RenderOrder.ITEM, _Item = i_comp)

    player._Inventory.items.append(Journal)
    player._Inventory.items.append(Talisman)

    # 지도 객체 생성: y,x 순서는 game_map 객체에서 알아서 처리
    game_map = GameMap(map_width,map_height)
    game_map.create_map_cave(entities, 3, 10, 10)

    # FOV
    fov_radius = max_fov_radius
    fov_algorithm = fov_algorithm_lit
    fov_recompute = True
    fov_map = initialize_fov(game_map)

    # 광원, light_map은 numpy 리스트

    light_recompute = True

    light_map = initialize_light(game_map, fov_map, entities)

    # 카메라 객체 생성
    camera = Camera(0,0, map_width, map_height, True)

    camera.update(player)

    """
    디버그 명령 목록
    passwall: 벽 통과 가능
    showpos: 플레이어 x,y좌표 표시. 다른 엔티티 좌표도 표시할 수 있게 고칠 것
    """
    # 디버그용 객체 생성. 디버그 기능들은 기본적으로 꺼져 있고, 인자를 넣으면 활성화
    debug = Debug()

    # 메세지 출력용 객체 생성.
    message_log = MessageLog(message_x, message_width, message_height)

    # 키보드, 마우스 입력 처리용 객체 생성
    key = tcod.Key()
    mouse = tcod.Mouse()

    # 순서 결정용 객체 생성
    game_state = GameStates.PLAYERS_TURN
    previous_game_state = game_state
    targeting_item = None

    # 콘솔, 패널 생성
    con = tcod.console.Console(screen_width, screen_height)
    panel = tcod.console_new(screen_width, panel_height)

    # 폰트 설정: 10x10파일, 이미지 파일은 그레이스케일, 배열 방식은 TCOD
    # tcod.console_set_custom_font('arial10x10.png', tcod.FONT_TYPE_GREYSCALE | tcod.FONT_LAYOUT_TCOD)

    # 폰트 설정: 32x32파일, 이미지 파일은 그레이스케일, 배열 방식은 CP437
    tcod.console_set_custom_font('terminal16x16.png', tcod.FONT_TYPE_GREYSCALE | tcod.FONT_LAYOUT_CP437)

    # 스크린 생성: 스크린 가로/세로, 이름, 전체화면 여부
    tcod.console_init_root(screen_width, screen_height, 'Mary', False, vsync=True)


    # TCOD 루프
    while not tcod.console_is_window_closed():
        """
        입력
        """
        # 사용자 입력을 받음: 키 누를 시, 키보드, 마우스
        tcod.sys_check_for_event(tcod.EVENT_KEY_PRESS | tcod.EVENT_MOUSE, key, mouse)

        """
        화면 표시
        """
        # 플레이어 시야
        if fov_recompute:
            recompute_fov(fov_map, player.x, player.y, fov_radius, fov_light_walls, fov_algorithm)


        if light_recompute:
            light_map = initialize_light(game_map, fov_map, entities)

        """
        화면 표시
        """
        # 화면 초기화
        clear_all_entities(con, entities, camera)

        # 표시할 모든 객체를 화면에 배치함
        render_all(game_state, con, panel, mouse, entities, player,
                   game_map, fov_map, light_map, camera, message_log, fov_recompute,
                   screen_width, screen_height,
                   bar_width, panel_height, panel_y, colors)

        fov_recompute = False
        light_recompute = False

        # 화면 출력
        tcod.console_flush(keep_aspect=True)

        # 화면 초기화
        clear_all_entities(con, entities, camera)

        """
        입력에 대한 상호작용
        """
        # action 변수에 키보드 입력값을 사전 형태로 받아옴
        action = handle_keys(key, game_state)
        mouse_action = handle_mouse(mouse)

        left_click = mouse_action.get('left_click')
        right_click = mouse_action.get('right_click')

        move = action.get('move')
        rest = action.get('rest')
        pickup = action.get('pickup')
        show_inventory = action.get('show_inventory')
        inventory_index = action.get('inventory_index')
        drop_inventory = action.get('drop_inventory')

        toggle_light  = action.get('toggle_light')
        create_luminary = action.get('create_light')
        toggle_wall  = action.get('toggle_wall')
        exit = action.get('exit')

        if exit:
            if game_state in (GameStates.SHOW_INVENTORY, GameStates.DROP_INVENTORY):
                game_state = previous_game_state
            elif game_state == GameStates.TARGETING:
                player_turn_results.append({'targeting_cancelled': True})
            else:
                pass
                #return True

        # 최대화면이 True일 시, 전체화면이 아니라면 콘솔을 전체화면으로 전환함
        if action.get('fullscreen'):
            tcod.console_set_fullscreen(not tcod.console_is_fullscreen())

        """
        플레이어 차례에 플레이어가 할 수 있는 행동들
        """
        player_turn_results = []

        # move변수에 대입된 값이 있을 시 이동
        if move and game_state == GameStates.PLAYERS_TURN:
            dx, dy = action.get('move')
            destix = player.x + dx
            destiy = player.y + dy

            if debug.passwall == False:
                if not game_map.is_blocked(destix, destiy):
                    target = get_blocking_entities_at_location(entities, destix, destiy)

                    if target:
                        attack_results = player._Fighter.attack(target)
                        player_turn_results.extend(attack_results)

                    else:
                        player.move(dx, dy)
                        camera.update(player)
                        fov_recompute = True
                        light_recompute = True

                    game_state = GameStates.ENEMY_TURN
            else:
                if game_map.is_blocked(player.x + dx, player.y + dy):
                    debug.dbg_msg("You magically pass through solid wall.")
                player.move(dx, dy)
                camera.update(player)

        elif pickup and game_state == GameStates.PLAYERS_TURN:
            for entity in entities:
                if entity._Item and entity.x == player.x and entity.y == player.y:
                    pickup_results = player._Inventory.add_item(entity)
                    player_turn_results.extend(pickup_results)
                    break
            else:
                message_log.log(Message('There is nothing here to pick up.', tcod.yellow))

        if toggle_light:
            if player._Luminary.luminosity:
                player._Luminary.luminosity = 0
                fov_radius = 1
                fov_algorithm = fov_algorithm_dark
            else:
                player._Luminary.luminosity = player._Luminary.init_luminosity
                fov_radius = max_fov_radius
                fov_algorithm = fov_algorithm_lit

            fov_recompute = True
            light_recompute = True

            game_state = GameStates.ENEMY_TURN

        if show_inventory:
            previous_game_state = game_state
            game_state = GameStates.SHOW_INVENTORY

        if drop_inventory:
            previous_game_state = game_state
            game_state = GameStates.DROP_INVENTORY

        if inventory_index is not None and previous_game_state != GameStates.PLAYER_DEAD and inventory_index < len(
                player._Inventory.items):
            item = player._Inventory.items[inventory_index]

            if game_state == GameStates.SHOW_INVENTORY:
                player_turn_results.extend(player._Inventory.use(item, camera=camera,
                                                                 entities=entities, fov_map=fov_map,
                                                                 screen_width = screen_width,
                                                                 screen_height = screen_height))
            elif game_state == GameStates.DROP_INVENTORY:
                player_turn_results.extend(player._Inventory.drop_item(item))

        if game_state == GameStates.TARGETING:
            if left_click:
                target_x, target_y = left_click

                item_use_results = player._Inventory.use(targeting_item, entities=entities, fov_map=fov_map,
                                                        camera=camera, screen_width = screen_width, screen_height = screen_height,
                                                        target_x=target_x, target_y=target_y)
                player_turn_results.extend(item_use_results)
            elif right_click:
                player_turn_results.append({'targeting_cancelled': True})

        if rest:
            game_state = GameStates.ENEMY_TURN

        for r in player_turn_results:
            message = r.get('message')
            dead_entity = r.get('dead')
            item_added = r.get('item_added')
            item_consumed = r.get('consumed')
            item_used = r.get('used')
            item_dropped = r.get('item_dropped')
            targeting = r.get('targeting')
            targeting_cancelled = r.get('targeting_cancelled')

            if message:
                message_log.log(message)

            if targeting_cancelled:
                game_state = previous_game_state
                message_log.log(Message('Targeting cancelled'))

            if dead_entity:
                if dead_entity == player:
                    message, game_state = kill_player(dead_entity)
                else:
                    message = kill_monster(dead_entity)

                message_log.log(message)

            if item_added:
                entities.remove(item_added)
                game_state = GameStates.ENEMY_TURN

            if item_consumed or item_used:
                game_state = GameStates.ENEMY_TURN

            if item_dropped:
                entities.append(item_dropped)
                game_state = GameStates.ENEMY_TURN

            if targeting:
                previous_game_state = GameStates.PLAYERS_TURN
                game_state = GameStates.TARGETING
                targeting_item = targeting
                message_log.log(targeting_item._Item.targeting_message)

        """
        적의 차례에 적이 할 수 있는 행동들
        """
        if game_state == GameStates.ENEMY_TURN:
            for entity in entities:
                if entity.name == 'light source':
                    pass
                    #message_log.log(F"The {entity.name} is glowing")
                elif entity._Ai:
                    enemy_turn_results = entity._Ai.take_turn(player,
                                                              fov_map, game_map, entities)

                    for er in enemy_turn_results:
                        message = er.get('message')
                        dead_entity = er.get('dead')

                        if message:
                            message_log.log(message)

                        if dead_entity:
                            if dead_entity == player:
                                message, game_state = kill_player(dead_entity)
                            else:
                                message = kill_monster(dead_entity)

                            message_log.log(message)

                            if game_state == GameStates.PLAYER_DEAD:
                                break
                    if game_state == GameStates.PLAYER_DEAD:
                                break

            else:
                game_state = GameStates.PLAYERS_TURN


        """
        디버그 기능들
        """
        # 플레이어 위치 표시
        if debug.showpos: debug.show_pos(player,'player')

        # 벽 설치
        if toggle_wall:
            game_map.toggle_wall(player.x, player.y)
            # 지형이 변했으니 새로 지형 맵을 짜야 함
            fov_map = initialize_fov(game_map)
            light_recompute = True

        if create_luminary:
            game_map.create_luminary(entities, player.x, player.y, 15)
            # 광원이 새로 생겼으니 다시 계산
            light_recompute = True
def get_new_game_variables(constants):
    race_component = None
    class_component = None
    game_state = GameStates.SELECT_SEX
    previous_game_state = game_state
    key = libtcod.Key()
    mouse = libtcod.Mouse()

    while not libtcod.console_is_window_closed():
        libtcod.sys_check_for_event(
            libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse)
        libtcod.console_flush()
        action = handle_keys(key, game_state)

        if game_state == GameStates.SELECT_SEX:
            select_sex_menu(0, 50, constants['screen_width'],
                            constants['screen_height'], constants['sexes'])

            action = handle_keys(key, game_state)

            male = action.get('male')
            female = action.get('female')

            exit = action.get('exit')

            if male:
                sex = 'Male'
                game_state = GameStates.ENTER_PLAYER_NAME

            elif female:
                sex = 'Female'
                game_state = GameStates.ENTER_PLAYER_NAME

            elif exit:
                break

        elif game_state == GameStates.ENTER_PLAYER_NAME:
            select_name_menu(0, 50, constants['screen_width'],
                             constants['screen_height'])
            libtcod.console_flush()
            name = enter_player_name(constants['screen_width'],
                                     constants['screen_height'])
            if name == None:
                game_state = GameStates.SELECT_SEX
            else:
                game_state = GameStates.SELECT_RACE

        elif game_state == GameStates.SELECT_RACE:
            select_race_menu(0, 50, constants['screen_width'],
                             constants['screen_height'], constants['races'])

            action = handle_keys(key, game_state)

            human = action.get('human')

            exit = action.get('exit')

            if human:
                race_component = Human()
                game_state = GameStates.SELECT_CLASS

            elif exit:
                game_state = GameStates.ENTER_PLAYER_NAME

        elif game_state == GameStates.SELECT_CLASS:
            select_combat_class_menu(0, 50, constants['screen_width'],
                                     constants['screen_height'],
                                     constants['combat_classes'])

            action = handle_keys(key, game_state)

            warrior = action.get('warrior')
            archer = action.get('archer')

            exit = action.get('exit')

            if warrior:
                class_component = Warrior()
                break

            if archer:
                class_component = Archer()
                break

            elif exit:
                game_state = GameStates.SELECT_RACE
    if exit:
        libtcod.console_clear(0)
        libtcod.console_flush()
        return None, None, None, None, None

    inventory_component = Inventory(26)
    level_component = Level()
    equipment_component = Equipment()

    player = Entity(0,
                    0,
                    1,
                    libtcod.white,
                    name,
                    sex,
                    player=True,
                    blocks=True,
                    render_order=RenderOrder.ACTOR,
                    combat_class=class_component,
                    race=race_component,
                    inventory=inventory_component,
                    level=level_component,
                    equipment=equipment_component)

    player = apply_class_stats_to_race(player)

    if player.combat_class.class_name == 'Warrior':
        equippable_component = Equippable(EquipmentSlots.MAIN_HAND,
                                          min_damage_bonus=0,
                                          max_damage_bonus=1)
        dagger = Entity(0,
                        0,
                        '-',
                        libtcod.sky,
                        'Dagger',
                        equippable=equippable_component)

        dagger.item.description = "Better than your bare hands."

        player.inventory.add_item(dagger)
        player.equipment.toggle_equip(dagger)

    elif player.combat_class.class_name == 'Archer':
        equippable_component = Equippable(EquipmentSlots.MAIN_HAND,
                                          min_damage_bonus=0,
                                          max_damage_bonus=1)
        dagger = Entity(0,
                        0,
                        '-',
                        libtcod.sky,
                        'Dagger',
                        equippable=equippable_component)

        dagger.item.description = "Better than your bare hands."

        player.inventory.add_item(dagger)
        player.equipment.toggle_equip(dagger)

    entities = [player]

    game_map = GameMap(constants['map_width'], constants['map_height'])

    game_map.make_map(constants['max_rooms'], constants['room_min_size'],
                      constants['room_max_size'], constants['map_width'],
                      constants['map_height'], player, entities)
    message_log = MessageLog(constants['message_log_x'],
                             constants['message_width'],
                             constants['message_panel_height'])
    game_state = GameStates.PLAYERS_TURN

    return player, entities, game_map, message_log, game_state
示例#14
0
def main():
    screen_width = 80
    screen_height = 50
    bar_width = 20
    panel_height = 7
    panel_y = screen_height - panel_height
    message_x = bar_width + 2
    message_width = screen_width - bar_width - 2
    message_height = panel_height - 1
    map_width = 80
    map_height = 43
    room_max_size = 10
    room_min_size = 6
    max_rooms = 30
    fov_algorithm = 0
    fov_light_walls = True
    fov_radius = 10

    max_monsters_per_room = 3

    colors = {
        "dark_wall": lcod.Color(0, 0, 100),
        "dark_ground": lcod.Color(50, 50, 150),
        "light_wall": lcod.Color(130, 110, 50),
        "light_ground": lcod.Color(200, 180, 50),
    }

    fighter_component = Fighter(hp=12, defense=3, power=5)
    player = Entity(0,
                    0,
                    '@',
                    lcod.white,
                    "Player", [],
                    blocks=True,
                    render_order=RenderOrder.ACTOR,
                    fighter=fighter_component)
    entities = [player]

    lcod.console_set_custom_font(
        "arial10x10.png", lcod.FONT_TYPE_GRAYSCALE | lcod.FONT_LAYOUT_TCOD)

    lcod.console_init_root(screen_width, screen_height, "TicTacRogue", False)

    con = lcod.console_new(screen_width, screen_height)
    panel = lcod.console_new(screen_width, panel_height)

    game_map = GameMap(map_width, map_height)
    game_map.make_map(max_rooms, room_min_size, room_max_size, map_width,
                      map_height, player, entities, max_monsters_per_room)

    fov_recompute = True
    fov_map = initialize_fov(game_map)

    message_log = MessageLog(message_x, message_width, message_height)

    key = lcod.Key()
    mouse = lcod.Mouse()

    game_state = GameStates.PLAYER_TURN
    power_armor = PowerArmor(player)

    while not lcod.console_is_window_closed():
        lcod.sys_check_for_event(lcod.EVENT_KEY_PRESS, key, mouse)

        if fov_recompute:
            recompute_fov(fov_map, player.x, player.y, fov_radius)

        render_all(con, panel, entities, player, game_map, fov_map,
                   fov_recompute, message_log, screen_width, screen_height,
                   bar_width, panel_height, panel_y, colors)

        lcod.console_flush()

        clear_all(con, entities)

        action = handle_keys(key)

        move = action.get("move")
        exit = action.get("exit")
        cast = action.get("cast")
        fullscreen = action.get("fullscreen")

        player_turn_results = []

        if cast and game_state == GameStates.PLAYER_TURN:
            if power_armor not in player.effects:
                print("Adding effects to list")
                player.effects.append(power_armor)

            player.evaluate_effects()
            game_state = GameStates.ENEMY_TURN

        elif move and game_state == GameStates.PLAYER_TURN:
            dx, dy = move
            destination_x = player.x + dx
            destination_y = player.y + dy

            if not game_map.is_blocked(destination_x, destination_y):
                target = get_blocking_entities_at_location(
                    entities, destination_x, destination_y)

                if target:
                    attack_results = player.fighter.attack(target)
                    player_turn_results.extend(attack_results)
                else:
                    player.move(dx, dy)
                    fov_recompute = True

                player.evaluate_effects()
                player.clean_effects()
                game_state = GameStates.ENEMY_TURN

        if exit:
            return True

        if fullscreen:
            lcod.console_set_fullscreen(not lcod.console_is_fullscreen())

        for player_turn_result in player_turn_results:
            message = player_turn_result.get("message")
            dead_entity = player_turn_result.get("dead")

            if message:
                message_log.add_message(message)

            if dead_entity:
                if dead_entity == player:
                    message, game_state = kill_player(dead_entity)
                else:
                    message = kill_monster(dead_entity)

                message_log.add_message(message)

        if game_state == GameStates.ENEMY_TURN:
            for entity in entities:
                if entity.ai:
                    enemy_turn_results = entity.ai.take_turn(
                        player, fov_map, game_map, entities)

                    for enemy_turn_result in enemy_turn_results:
                        message = enemy_turn_result.get("message")
                        dead_entity = enemy_turn_result.get("dead")

                        if message:
                            message_log.add_message(message)
                        if dead_entity:
                            if dead_entity == player:
                                message, game_state = kill_player(dead_entity)
                            else:
                                message = kill_monster(dead_entity)

                            message_log.add_message(message)

                        if game_state == GameStates.PLAYER_DEAD:
                            break

                    if game_state == GameStates.PLAYER_DEAD:
                        break

            else:
                game_state = GameStates.PLAYER_TURN
示例#15
0
def main():
    screen_width = 190
    screen_height = 100

    bar_width = 20
    panel_height = 7
    panel_y = screen_height - panel_height  # panel y informs the y axis of
    # of the panel for both message log
    # and health bar

    message_x = bar_width + 2  # message x informs the x axis
    message_width = screen_width - bar_width - 2  # of the message
    message_height = panel_height - 1

    map_width = 170
    map_height = 80

    room_max_size = 20
    room_min_size = 8
    max_rooms = 100

    fov_algorithm = 0  # shape of pov
    fov_light_walls = True  #if Walls will light or not
    fov_radius = 15

    max_monsters_per_room = 4

    colors = {
        "dark_wall": libtcod.Color(127, 127, 127),
        "dark_ground": libtcod.Color(127, 101, 63),
        "light_walls": libtcod.Color(127, 137, 127),
        "light_ground": libtcod.Color(127, 110, 63)
    }

    fighter_component = Fighter(hp=(randint(100, 200)),
                                defense=(randint(0, 3)),
                                power=(randint(20, 40)))
    player = Entity(0,
                    0,
                    "@",
                    libtcod.green,
                    "Player",
                    blocks=True,
                    render_order=RenderOrder.ACTOR,
                    fighter=fighter_component)
    entities = [player]

    libtcod.console_set_custom_font(
        "arial10x10.png",
        libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD)

    libtcod.console_init_root(screen_width, screen_height, "Ork Brawl", False)

    con = libtcod.console_new(screen_width, screen_height)
    panel = libtcod.console_new(screen_width, panel_height)

    game_map = GameMap(map_width, map_height)
    game_map.make_map(max_rooms, room_min_size, room_max_size, map_width,
                      map_height, player, entities, max_monsters_per_room)

    fov_recompute = True
    #Because FOV doesn't need to be computed every turn (standing still), only need when moving to recompute
    #True by default because it needs to be computed when the game start

    fov_map = initialize_fov(
        game_map)  # This call function and store result in fov_map

    message_log = MessageLog(message_x, message_width, message_height)

    test = Message(
        "You wake up naked and angry with an urge to kill 'someting' ",
        libtcod.red)
    message = test
    message_log.add_message(message)

    key = libtcod.Key()
    mouse = libtcod.Mouse()

    game_state = GameStates.PLAYERS_TURN

    while not libtcod.console_is_window_closed():
        libtcod.sys_check_for_event(
            libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse)

        if fov_recompute:
            recompute_fov(fov_map, player.x, player.y, fov_radius,
                          fov_light_walls, fov_algorithm)

        render_all(con, panel, entities, player, game_map, fov_map,
                   fov_recompute, message_log, screen_width, screen_height,
                   bar_width, panel_height, panel_y, mouse, colors)

        fov_recompute = False  # in theory doesn't matter, can remove code but its good practise to
        # to switch it off when you are not using it

        libtcod.console_flush()

        clear_all(con, entities)

        action = handle_keys(key)

        move = action.get("move")
        exit = action.get("exit")
        fullscreen = action.get("fullscreen")

        player_turn_results = []

        if move and game_state == GameStates.PLAYERS_TURN:
            dx, dy = move
            destination_x = player.x + dx
            destination_y = player.y + dy

            if not game_map.is_blocked(destination_x, destination_y):
                target = get_blocking_entities_at_location(
                    entities, destination_x, destination_y)
                # return entity or 'None' hence target=entity
                if target:
                    attack_results = player.fighter.attack(target)
                    player_turn_results.extend(attack_results)

                else:
                    player.move(dx, dy)

                    fov_recompute = True  # will be in False due to game loop above

                game_state = GameStates.ENEMY_TURN

        if exit:
            return True  # This could be either return False or break
            # returns a value to main hence ending the programme.

        if fullscreen:
            libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen())

        for player_turn_result in player_turn_results:
            message = player_turn_result.get("message")
            dead_entity = player_turn_result.get("dead")

            if message:
                message_log.add_message(message)

            if dead_entity:
                if dead_entity == player:
                    message, game_state = kill_player(dead_entity)
                else:
                    message = kill_monster(dead_entity)

                message_log.add_message(message)

        if game_state == GameStates.ENEMY_TURN:
            for entity in entities:
                if entity.ai:
                    enemy_turn_results = entity.ai.take_turn(
                        player, fov_map, game_map, entities)

                    for enemy_turn_result in enemy_turn_results:
                        message = enemy_turn_result.get("message")
                        dead_entity = enemy_turn_result.get("dead")

                        if message:
                            message_log.add_message(message)

                        if dead_entity:
                            if dead_entity == player:
                                message, game_state = kill_player(dead_entity)
                            else:
                                message = kill_monster(dead_entity)

                            message_log.add_message(message)

                            if game_state == GameStates.PLAYER_DEAD:
                                break

                    if game_state == GameStates.PLAYER_DEAD:
                        break

            else:
                game_state = GameStates.PLAYERS_TURN
示例#16
0
def main():
    screen_width: int = 80
    screen_height: int = 35

    bar_width: int = 20
    panel_height: int = 7
    panel_y: int = screen_height - panel_height
    ui_layer = 10

    message_x = bar_width + 2
    message_width = screen_width - bar_width - 2
    message_height = panel_height - 1

    map_width = 80
    map_height = 28
    max_monsters_per_room = 3

    con = Console(x=0, y=0, width=screen_width, height=screen_height)
    panel = Console(0, panel_y, screen_width, panel_height, layer=ui_layer)

    title = "Rogue Alchemist"
    font = "mplus-1p-regular.ttf"

    fighter_component = Fighter(hp=30, defense=2, power=5)
    player = Entity(x=0,
                    y=0,
                    char='@',
                    color=Color.BLACK,
                    name='Player',
                    blocks=True,
                    render_order=RenderOrder.ACTOR,
                    fighter=fighter_component)
    entities = [player]

    game_map = GameMap(map_width, map_height)
    game_map.generate_dungeon(player, entities, max_monsters_per_room)
    # game_map.generate_dungeon(map_width, map_height, cave=True)
    start_room = game_map.dungeon.rooms[0]

    fov_algorithm = 0
    fov_light_walls = True
    fov_radius = 10

    colors = {
        "dark_wall": Color.DARK_SLATE_GRAY,
        "dark_ground": Color.DIM_GRAY,
        "light_wall": Color.LIGHT_SLATE_GRAY,
        "light_ground": Color.LIGHT_GRAY,
        "dark_door": Color.SADDLE_BROWN,
        "light_door": Color.BROWN,
        "test": Color.GOLD,
    }

    fov_recompute = True
    fov_map = initialize_fov(game_map)

    message_log = MessageLog(message_x, message_width, message_height)

    key = None

    blt.open()  # initializes BearLib Terminal instance with default parameters
    terminal_options = f"window: title={title}, size={str(screen_width)}x{str(screen_height)}; font:{font}, size=12"
    blt.set(terminal_options)

    game_state = GameStates.PLAYERS_TURN

    while True:
        if fov_recompute:
            recompute_fov(fov_map, player.x, player.y, fov_radius,
                          fov_light_walls, fov_algorithm)

        render_all(entities, player, game_map, fov_map, fov_recompute,
                   message_log, screen_width, screen_height, bar_width,
                   panel_height, panel_y, colors)
        blt.refresh()

        fov_recompute = False

        # remove player's previous position
        clear_all(entities)

        if blt.has_input():  # if no inputs, don't wait
            key = blt.read()

        action = handle_keys(key)
        key = None

        movement = action.get("move")
        exit_game = action.get("exit")

        player_turn_results = []

        if movement and game_state == GameStates.PLAYERS_TURN:
            dx, dy = movement
            destination_x = player.x + dx
            destination_y = player.y + dy

            if not game_map.is_blocked(destination_x, destination_y):
                target = get_blocking_entities_at_location(
                    entities, destination_x, destination_y)

                if target:
                    attack_results = player.fighter.attack(target)
                    player_turn_results.extend(attack_results)
                else:
                    player.move(*movement)

                    fov_recompute = True

                game_state = GameStates.ENEMY_TURN

        if exit_game:
            blt.close()
            return True

        for player_turn_result in player_turn_results:
            message = player_turn_result.get("message")
            dead_entity = player_turn_result.get("dead")

            if message:
                message_log.add_message(message)

            if dead_entity:
                if dead_entity == player:
                    message, game_state = kill_player(dead_entity)
                else:
                    message = kill_monster(dead_entity)

                message_log.add_message(message)

        if game_state == GameStates.ENEMY_TURN:
            for entity in entities:
                visible = libtcod.map_is_in_fov(fov_map, entity.x, entity.y)
                if entity.ai:
                    enemy_turn_results = entity.ai.take_turn(
                        player, fov_map, game_map, entities)

                    for enemy_turn_result in enemy_turn_results:
                        message = enemy_turn_result.get("message")
                        dead_entity = enemy_turn_result.get("dead")

                        if message:
                            message_log.add_message(message)

                        if dead_entity:
                            if dead_entity == player:
                                message, game_state = kill_player(dead_entity)
                            else:
                                message = kill_monster(dead_entity)

                            message_log.add_message(message)

                            if game_state == GameStates.PLAYER_DEAD:
                                break

            if game_state == GameStates.PLAYER_DEAD:
                break

            game_state = GameStates.PLAYERS_TURN
def main():  # Adding the main function for Python 3 compatibility

    # Setting constants and global variables
    screen_width = 80
    screen_height = 50
    bar_width = 20
    panel_height = 7
    panel_y = screen_height - panel_height
    # Adding variables Message log display to show events.
    message_x = bar_width + 2
    message_width = screen_width - bar_width - 2
    message_height = panel_height - 1

    map_width = 80
    map_height = 43
    room_max_size = 10
    room_min_size = 6
    max_rooms = 30
    fov_algorithm = 0
    fov_light_walls = True
    fov_radius = 10
    max_monsters_per_room = 3
    brown_color = libtcod.flame * libtcod.light_blue
    colors = {
        'dark_wall': brown_color,  # Color(0, 0, 100),
        'dark_ground': libtcod.desaturated_orange,  # Color(50, 50, 150)
        'light_wall': libtcod.dark_flame,
        'light_ground': libtcod.light_orange
    }  # Coloring our tiles
    # LIMIT_FPS = 20 # Unused for now
    # Setting player coordinate starting point at center of console
    fighter_component = Fighter(hp=30, defense=2,
                                power=5)  # Setting player attributes
    player = Entity(0,
                    0,
                    '@',
                    libtcod.white,
                    'Player',
                    blocks=True,
                    render_order=RenderOrder.ACTOR,
                    fighter=fighter_component)
    entities = [player]

    # Initializing the library font
    libtcod.console_set_custom_font(
        'arial10x10.png',
        libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD)
    # Now creating the window with size constants, title, and whether fullscreen
    libtcod.console_init_root(screen_width, screen_height,
                              'python/libtcod tutorial', False)
    con = libtcod.console_new(
        screen_width,
        screen_height)  # Allows the ability to create new consoles
    panel = libtcod.console_new(
        screen_width, panel_height)  # New console to hold HP and Messages
    game_map = GameMap(map_width, map_height)  # Initialize the game map
    game_map.make_map(max_rooms, room_min_size, room_max_size, map_width,
                      map_height, player, entities, max_monsters_per_room)
    fov_recompute = True  # Whether to reset the Field of View, True for start of game
    fov_map = initialize_fov(game_map)  #Initialize the Field of View
    message_log = MessageLog(message_x, message_width, message_height)
    key = libtcod.Key()  # Setting keyboard variable for input
    mouse = libtcod.Mouse()  # Setting mouse variable for input
    game_state = GameStates.PLAYERS_TURN  # Sets initial game_state to players turn

    # Next is the main game loop.  We basically print the @ character to the screen in white
    while not libtcod.console_is_window_closed():
        libtcod.sys_check_for_event(
            libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse)
        #libtcod.console_set_default_foreground(0, libtcod.white)
        if fov_recompute:
            recompute_fov(fov_map, player.x, player.y, fov_radius,
                          fov_light_walls, fov_algorithm)
# Changing the way the console is initialized so we can reference different consoles later

        render_all(con, panel, entities, player, game_map, fov_map,
                   fov_recompute, message_log, screen_width, screen_height,
                   bar_width, panel_height, panel_y, mouse, colors)

        fov_recompute = False

        libtcod.console_flush(
        )  # Flush the console which writes any changes to the screen

        clear_all(con, entities)

        # New setup to call handle_keys() function from input_handlers.py
        action = handle_keys(key)

        move = action.get('move')
        exit = action.get('exit')
        fullscreen = action.get('fullscreen')

        player_turn_results = []  # new dictionary

        if move and game_state == GameStates.PLAYERS_TURN:
            dx, dy = move
            destination_x = player.x + dx
            destination_y = player.y + dy

            if not game_map.is_blocked(destination_x, destination_y):
                target = get_blocking_entities_at_location(
                    entities, destination_x, destination_y)

                if target:  # Are you running into a monster?
                    attack_results = player.fighter.attack(
                        target)  # Attack monster
                    player_turn_results.extend(
                        attack_results)  # Get results of attack
                else:
                    player.move(dx, dy)

                    fov_recompute = True  # Recompute the FOV upon movement

                game_state = GameStates.ENEMY_TURN  # Sets state to players turn.

        if exit:
            return True

        if fullscreen:
            libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen())


# Iterate through the results
# Player Results Loop
        for player_turn_result in player_turn_results:
            message = player_turn_result.get('message')  # Get the message part
            dead_entity = player_turn_result.get(
                'dead')  # Get the part as to whether dead or not

            if message:
                message_log.add_message(
                    message)  # Prints any messages for the player turn

            if dead_entity:  # Check is something dead this turn
                if dead_entity == player:  # Is the dead thing the player?
                    message, game_state = kill_player(
                        dead_entity)  # Run kill_player function
                else:
                    message = kill_monster(
                        dead_entity)  # Run kill_monster function

                message_log.add_message(message)
        # Enemy Results Loop
        if game_state == GameStates.ENEMY_TURN:  # Checks to see if enemy turn
            for entity in entities:  # Cycles through entities looking for monsters
                if entity.ai:  # If entity is not the player and has ai.
                    # Set a list that calls the take_turn function for the ai
                    enemy_turn_results = entity.ai.take_turn(
                        player, fov_map, game_map, entities)

                    for enemy_turn_result in enemy_turn_results:  # Iterate through the list
                        message = enemy_turn_result.get(
                            'message')  # Gather any messages for that ai
                        dead_entity = enemy_turn_result.get(
                            'dead')  # get and dead comments

                        if message:
                            message_log.add_message(
                                message
                            )  # Print any messages for the turn of the ai

                        if dead_entity:  # Check if dead entity this turn
                            if dead_entity == player:  # Is it the player?
                                message, game_state = kill_player(
                                    dead_entity
                                )  # If yes then run kill_player and show message from results
                            else:
                                message = kill_monster(
                                    dead_entity
                                )  # If it's the monster, then kill it.

                            message_log.add_message(message)

                            if game_state == GameStates.PLAYER_DEAD:  # Did the player die?
                                break  # If dead player then end game.

                    if game_state == GameStates.PLAYER_DEAD:
                        break  # Ends game if player dies and monster has died at same time.

            else:
                # Set the game_state back to players turn
                game_state = GameStates.PLAYERS_TURN
示例#18
0
def main():
    # Screen size
    screen_width = 80
    screen_height = 50
    # UI settings
    bar_width = 20
    panel_height = 7
    panel_y = screen_height - panel_height
    message_x = bar_width + 2
    message_width = screen_width - bar_width - 2
    message_height = panel_height - 1
    # Map size
    map_width = 80
    map_height = 43
    # Room definitions
    max_rooms = 30
    room_min_size = 6
    room_max_size = 10
    # FoV configurations
    fov_algorithm = 0  # use defualt algorithm
    fov_light_walls = True  # light up walls we can see
    fov_radius = 10  # radius of view
    fov_recompute = True  # flag to trigger FoV computations
    # Monster spawning settings
    max_monsters_per_room = 3
    # Define colors to be used in FoV
    colors = {
        "dark_wall": libtcod.Color(0, 0, 100),
        "dark_ground": libtcod.Color(50, 50, 150),
        "light_wall": libtcod.Color(130, 110, 50),
        "light_ground": libtcod.Color(200, 180, 50),
    }
    # Font settings
    libtcod.console_set_custom_font(
        "arial10x10.png",
        libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD)

    # Player initialization
    fighter_component = Fighter(
        hp=30, defense=2, power=5)  # define a fighter component for the player
    inventory_component = Inventory(26)  # Inventory component for the player
    player = Entity(
        0,
        0,
        "@",
        libtcod.white,
        "Player",
        blocks=True,
        render_order=RenderOrder.ACTOR,
        fighter=fighter_component,
        inventory=inventory_component,
    )
    # World entity list
    entities = [player]
    # Map object
    game_map = GameMap(map_width, map_height)
    game_map.make_map(
        max_rooms,
        room_min_size,
        room_max_size,
        player,
        entities,
        max_monsters_per_room=max_monsters_per_room,
    )
    # Fov map object
    fov_map = initialize_fov(game_map)
    # Game state
    game_state = GameStates.PLAYERS_TURN
    previous_game_state = game_state

    # For item targeting
    targeting_item = None

    # Creating screen
    libtcod.console_init_root(screen_width, screen_height,
                              "Roguelike using libtcod", False)

    # Console object
    console = libtcod.console.Console(screen_width, screen_height)
    # Panel object
    panel = libtcod.console.Console(screen_width, panel_height)
    # Message Log object
    message_log = MessageLog(message_x, message_width, message_height)

    # input objects
    key = libtcod.Key()
    mouse = libtcod.Mouse()

    # Game loop
    while not libtcod.console_is_window_closed():
        # Capture input events
        libtcod.sys_check_for_event(
            libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse)
        # Trigger FoV calculation
        if fov_recompute == True:
            recompute_fov(fov_map, player.x, player.y, fov_radius,
                          fov_light_walls, fov_algorithm)
        # Initial screen config
        render_all(
            con=console,
            panel=panel,
            entities=entities,
            player=player,
            game_map=game_map,
            fov_map=fov_map,
            fov_recompute=fov_recompute,
            message_log=message_log,
            screen_width=screen_width,
            screen_height=screen_height,
            bar_width=bar_width,
            panel_height=panel_height,
            panel_y=panel_y,
            mouse=mouse,
            colors=colors,
            gs=game_state,
        )
        fov_recompute = False
        libtcod.console_flush()

        # Clear all entities
        clear_all(console, entities)

        # Capture action for given input
        action = handle_keys(key, game_state)
        mouse_action = handle_mouse(mouse)
        # Map values for each action
        move = action.get("move")
        pickup = action.get("pickup")
        show_inventory = action.get("show_inventory")
        drop_inventory = action.get("drop_inventory")
        inv_index = action.get("inventory_index")
        left_click = mouse_action.get("left_click")
        right_click = mouse_action.get("right_click")
        _exit = action.get("exit")
        fullscreen = action.get("fullscreen")
        player_turn_results = []

        # Handle movement. Check if this is players turn
        if move and game_state == GameStates.PLAYERS_TURN:
            dx, dy = move
            dest_x, dest_y = player.x + dx, player.y + dy
            if not game_map.is_blocked(dest_x, dest_y):
                target = get_blocking_entities_at_location(
                    entities, dest_x, dest_y)
                if target:
                    attack_results = player.fighter.attack(target)
                    player_turn_results.extend(attack_results)
                else:
                    player.move(dx, dy)
                    fov_recompute = True
                # Now it is enemies turn
                game_state = GameStates.ENEMY_TURN
        elif pickup and game_state == GameStates.PLAYERS_TURN:
            for entity in entities:
                if entity.item and entity.x == player.x and entity.y == player.y:
                    pickup_results = player.inventory.add_item(entity)
                    player_turn_results.extend(pickup_results)
                    break
            else:
                message_log.add_message(
                    Message("There's nothing to pickup", color=libtcod.yellow))
        # Show player inventory
        if show_inventory:
            previous_game_state = game_state
            game_state = GameStates.SHOW_INVENTORY
        # Drop item dialog
        if drop_inventory:
            previous_game_state = game_state
            game_state = GameStates.DROP_INVENTORY
        if (inv_index is not None
                and previous_game_state != GameStates.PLAYER_DEAD
                and inv_index < len(player.inventory.items)):
            item = player.inventory.items[inv_index]
            if game_state == GameStates.SHOW_INVENTORY:
                player_turn_results.extend(
                    player.inventory.use(item,
                                         entities=entities,
                                         fov_map=fov_map))
            elif game_state == GameStates.DROP_INVENTORY:
                player_turn_results.extend(player.inventory.drop_item(item))
        if game_state == GameStates.TARGET_MODE:
            if left_click:
                target_x, target_y = left_click

                item_use_results = player.inventory.use(
                    targeting_item,
                    entities=entities,
                    fov_map=fov_map,
                    target_x=target_x,
                    target_y=target_y,
                )

                player_turn_results.extend(item_use_results)
            elif right_click:
                player_turn_results.append({"targeting_cancelled": True})
        # Handle game exit
        if _exit:
            if game_state in (GameStates.SHOW_INVENTORY,
                              GameStates.DROP_INVENTORY):
                game_state = previous_game_state
            elif game_state == GameStates.TARGET_MODE:
                player_turn_results.append({"targeting_cancelled": True})
            else:
                return True
        # toggle fullscreen
        if fullscreen:
            libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen())

        # Cycle through players action log
        for player_turn_result in player_turn_results:
            message = player_turn_result.get("message")
            dead_entity = player_turn_result.get("dead")
            item_added = player_turn_result.get("item_added")
            item_consumed = player_turn_result.get("consumed")
            item_dropped = player_turn_result.get("item_dropped")
            targeting = player_turn_result.get("targeting")
            cancelled_targeting = player_turn_result.get("targeting_cancelled")

            if message:
                message_log.add_message(message)
            if dead_entity:
                if dead_entity == player:
                    message, game_state = kill_player(player)
                else:
                    message = kill_monster(dead_entity)
                message_log.add_message(message)
            if item_added:
                entities.remove(item_added)
                game_state = GameStates.ENEMY_TURN
            if item_consumed:
                game_state = GameStates.ENEMY_TURN
            if item_dropped:
                entities.append(item_dropped)
                game_state = GameStates.ENEMY_TURN
            if targeting:
                previous_game_state = GameStates.PLAYERS_TURN
                game_state = GameStates.TARGET_MODE
                targeting_item = targeting
                message_log.add_message(targeting_item.item.targeting_message)
            if cancelled_targeting:
                game_state = previous_game_state
                player_turn_result.get("targeting")

        # After all input is handle, check if this is enemies turn
        if game_state == GameStates.ENEMY_TURN:
            for entity in entities:
                if entity.ai:
                    enemy_turn_results = entity.ai.take_turn(
                        player, fov_map, game_map, entities)

                    # Cycle through players action log
                    for enemy_turn_result in enemy_turn_results:
                        message = enemy_turn_result.get("message")
                        dead_entity = enemy_turn_result.get("dead")

                        if message:
                            message_log.add_message(message)
                        if dead_entity:
                            if dead_entity == player:
                                message, game_state = kill_player(player)
                            else:
                                message = kill_monster(dead_entity)
                            message_log.add_message(message)
                    # If player has died, no need to continue with enemies
                    if game_state == GameStates.PLAYER_DEAD:
                        break

            else:
                game_state = GameStates.PLAYERS_TURN
示例#19
0
def main():
    screen_height = 50
    screen_width = 80

    map_width = 80
    map_height = 45

    room_max_size = 10
    room_min_size = 6
    max_rooms = 30

    fov_algorithm = 0
    fov_light_walls = True
    fov_radius = 10

    max_monsters_per_room = 3
    colors = {
        'dark_wall': libtcod.Color(0, 0, 100),
        'dark_ground': libtcod.Color(50, 50, 150),
        'light_wall': libtcod.Color(130, 110, 50),
        'light_ground': libtcod.Color(200, 180, 50)
    }

    # initial position for player
    player = Entity(0, 0, '@', libtcod.white, 'Player', blocks=True)

    # creates list to hold the map's entities
    entities = [player]

    # set font for game
    libtcod.console_set_custom_font(
        'arial10x10.png',
        libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD)

    # makes root console
    libtcod.console_init_root(screen_width, screen_height,
                              'libtcod tutorial revised', False)

    # console for drawing the game
    con = libtcod.console.Console(screen_width, screen_height)

    # instance the game's map
    game_map = GameMap(map_width, map_height)
    game_map.make_map(max_rooms, room_min_size, room_max_size, map_width,
                      map_height, player, entities, max_monsters_per_room)

    # init field-of-view for player character
    fov_recompute = True
    fov_map = initialize_fov(game_map)

    key = libtcod.Key()
    mouse = libtcod.Mouse()

    while not libtcod.console_is_window_closed():
        # gets new events, and updates key and mouse
        libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS, key, mouse)

        if fov_recompute:
            compute_fov(fov_map, player.x, player.y, fov_radius,
                        fov_light_walls, fov_algorithm)
            # recomputed, until player moves, don't recomputer fov map

        libtcod.console_set_default_foreground(con, libtcod.white)

        # render all entities to desired console
        render_all(con, entities, game_map, fov_map, fov_recompute,
                   screen_width, screen_height, colors)
        fov_recompute = False

        # output to console
        libtcod.console_flush()

        # clear last position
        clear_all(con, entities)

        # obtain event
        action = handle_keys(key)

        # check and handle event
        move = action.get('move')
        exit = action.get('exit')
        fullscreen = action.get('fullscreen')

        if move:
            # from the move dictionary obtain the x and y and update player pos
            dx, dy = move

            destination_x = player.x + dx
            destination_y = player.y + dy

            # check if direction of movement is towards blocked tile
            if not game_map.is_blocked(player.x + dx, player.y + dy):
                # check if there is a blocking Entity
                target = get_blocking_entities_at_location(
                    entities, destination_x, destination_y)

                if target:
                    print("You kick the" + target.name + " in the shins!")
                else:
                    # if not blocked, move player there
                    player.move(dx, dy)

                    # on next map redraw, redraw the FOV
                    fov_recompute = True

        if exit:
            return True

        # toggles full screen based on ALT+ENTER user event
        if fullscreen:
            libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen())
def get_game_variables(constants):
    fighter_component = Fighter(hp=100,
                                defense=1,
                                power=2,
                                magic=0,
                                magic_defense=1,
                                talismanhp=0,
                                gold=0)
    inventory_component = Inventory(26)
    equipment_inventory_component = Inventory(26)
    level_component = Level()
    equipment_component = Equipment()
    player = Entity(0,
                    0,
                    '@',
                    libtcod.white,
                    'Player',
                    blocks=True,
                    render_order=RenderOrder.ACTOR,
                    fighter=fighter_component,
                    inventory=inventory_component,
                    level=level_component,
                    equipment=equipment_component,
                    equipment_inventory=equipment_inventory_component)
    entities = [player]

    gold_value = 1
    equipment_component = Equippable(EquipmentSlots.MAIN_HAND,
                                     power_bonus=1,
                                     gold=gold_value)
    dagger = Entity(0,
                    0,
                    '-',
                    libtcod.darker_orange,
                    "Terrium Dagger (+1 atk)",
                    equippable=equipment_component)
    player.equipment_inventory.add_item(dagger)
    player.equipment.toggle_equip(dagger)

    gold_value = 2
    item_component = Item(use_function=cast_magic,
                          damage=2,
                          maximum_range=3,
                          gold=gold_value)
    magic_wand = Entity(0,
                        0,
                        '|',
                        libtcod.darker_sepia,
                        "Magic Wand",
                        item=item_component)
    player.inventory.add_item(magic_wand)

    game_map = GameMap(constants['map_width'], constants['map_height'])
    game_map.make_map(constants['max_rooms'], constants['room_min_size'],
                      constants['room_max_size'], constants['map_width'],
                      constants['map_height'], player, entities)

    message_log = MessageLog(constants['message_x'],
                             constants['message_width'],
                             constants['message_height'])

    game_state = GameStates.PLAYERS_TURN

    return player, entities, game_map, message_log, game_state
示例#21
0
def main():
    screen_width = 80
    screen_height = 50

    bar_width = 20
    panel_height = 7
    panel_y = screen_height - panel_height

    message_x = bar_width + 2
    message_width = screen_width - bar_width - 2
    message_height = panel_height - 1

    map_width = 80
    map_height = 43

    room_max_size = 10
    room_min_size = 6
    max_rooms = 30

    fov_algorithm = 0
    fov_light_walls = True
    fov_radius = 10

    max_challenges_per_room = 3

    colors = {
        'dark_wall': libtcod.Color(0, 0, 100),
        'dark_ground': libtcod.Color(50, 50, 150),
        'light_wall': libtcod.Color(130, 110, 50),
        'light_ground': libtcod.Color(200, 180, 50)
    }

    player = Entity(0, 0, '@', libtcod.white, 'Player', blocks=True)
    entities = [player]

    entities = [player]

    libtcod.console_set_custom_font(
        'img01.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD)

    libtcod.console_init_root(screen_width, screen_height, 'trabalho de RV',
                              False)

    con = libtcod.console_new(screen_width, screen_height)
    panel = libtcod.console_new(screen_width, panel_height)

    game_map = GameMap(map_width, map_height)
    game_map.make_map(max_rooms, room_min_size, room_max_size, map_width,
                      map_height, player, entities, max_challenges_per_room)

    fov_recompute = True
    fov_map = initialize_fov(game_map)

    message_log = MessageLog(message_x, message_width, message_height)

    key = libtcod.Key()
    mouse = libtcod.Mouse()

    while not libtcod.console_is_window_closed():
        libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS, key, mouse)

        if fov_recompute:
            recompute_fov(fov_map, player.x, player.y, fov_radius,
                          fov_light_walls, fov_algorithm)

        render_all(con, panel, entities, player, game_map, fov_map,
                   fov_recompute, message_log, screen_width, screen_height,
                   bar_width, panel_height, panel_y, colors)

        fov_recompute = False

        libtcod.console_flush()

        clear_all(con, entities)

        action = handle_keys(key)

        move = action.get('move')
        exit = action.get('exit')
        fullscreen = action.get('fullscreen')

        if move:
            dx, dy = move

            destination_x = player.x + dx
            destination_y = player.y + dy

            if not game_map.is_blocked(destination_x, destination_y):
                target = get_blocking_entities_at_location(
                    entities, destination_x, destination_y)

                if target:
                    message_log.add_message(Message(target.question.text))
                else:
                    player.move(dx, dy)

                    fov_recompute = True

        if exit:
            return True

        if fullscreen:
            libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen())
示例#22
0
def get_game_variables(constants):
    fighter_component = Fighter(hp=100,
                                defense=1,
                                power=2,
                                magic=0,
                                magic_defense=1,
                                talismanhp=0,
                                gold=0,
                                status=None,
                                mana=100)
    inventory_component = Inventory(26)
    equipment_inventory_component = Inventory(26)
    level_component = Level()
    equipment_component = Equipment()
    player = Entity(0,
                    0,
                    constants['player_overworld_tile'],
                    libtcod.white,
                    'Player',
                    blocks=True,
                    render_order=RenderOrder.ACTOR,
                    fighter=fighter_component,
                    inventory=inventory_component,
                    level=level_component,
                    equipment=equipment_component,
                    equipment_inventory=equipment_inventory_component)
    entities = [player]

    equipment_component = Equippable(EquipmentSlots.MAIN_HAND,
                                     power_bonus=1,
                                     gold=1)
    dagger = Entity(0,
                    0,
                    constants['dagger_tile'],
                    libtcod.white,
                    "Terrium Dagger (+1 atk)",
                    equippable=equipment_component)
    player.equipment_inventory.add_item(dagger)
    player.equipment.toggle_equip(dagger)

    item_component = Item(use_function=cast_magic,
                          damage=2,
                          maximum_range=3,
                          gold=2)
    magic_wand = Entity(0,
                        0,
                        constants['magic_wand_tile'],
                        libtcod.white,
                        "Magic Wand",
                        item=item_component)
    player.inventory.add_item(magic_wand)

    game_map = GameMap(constants['map_width'], constants['map_height'])
    game_map.make_map(
        constants['max_rooms'], constants['room_min_size'],
        constants['room_max_size'], constants['map_width'],
        constants['map_height'], player, entities, constants['orc_tile'],
        constants['healing_potion_tile'], constants['scroll_tile'],
        constants['troll_tile'], constants['stairs_tile'],
        constants['sword_tile'], constants['shield_tile'],
        constants['dagger_tile'], constants['magic_wand_tile'],
        constants['greater_healing_potion_tile'], constants['ghost_tile'],
        constants['slime_tile'], constants['corpse_tile'],
        constants['goblin_tile'], constants['baby_slime_tile'],
        constants['skeleton_tile'], constants['slime_corpse_tile'],
        constants['baby_slime_corpse_tile'], constants['skeleton_corpse_tile'],
        constants['mana_potion_tile'], constants['wizard_staff_tile'],
        constants['health_talisman_tile'], constants['basilisk_tile'],
        constants['treasure_tile'], constants['chestplate_tile'],
        constants['leg_armor_tile'], constants['helmet_tile'],
        constants['amulet_tile'], constants['floor_tile'],
        constants['long_bow_tile'], constants['arrow_tile'],
        constants['wall_tile'], constants['grass_tile'],
        constants['path_tile'], constants['roof_tile'],
        constants['brick_tile'], constants['player_overworld_tile'],
        constants['player_tile'], constants['forest_tile'],
        constants['door_tile'], constants['sign_tile'])

    item_descriptors = [
        'Valor', 'Power', 'Ingenuity', 'Glory', 'Strength', 'Speed', 'Wealth',
        'Divinity', 'Energy', 'Honor', 'Resistance', 'Greatness', 'Courage',
        'Intelligence'
    ]

    all_shop_items = []

    item_component = Item(use_function=heal, amount=20, gold=20)
    item = Entity(0,
                  0,
                  constants['healing_potion_tile'],
                  libtcod.white,
                  "Health Potion (+20 HP)",
                  render_order=RenderOrder.ITEM,
                  item=item_component)
    all_shop_items.append(item)

    item_component = Item(use_function=recover_mana, amount=20, gold=10)
    item = Entity(0,
                  0,
                  constants['mana_potion_tile'],
                  libtcod.white,
                  "Mana Potion (+20 MANA)",
                  render_order=RenderOrder.ITEM,
                  item=item_component)
    all_shop_items.append(item)

    all_shop_equipment = []

    sword_amount = randint(2, 4)
    equippable_component = Equippable(EquipmentSlots.MAIN_HAND,
                                      power_bonus=sword_amount,
                                      gold=10)
    item = Entity(0,
                  0,
                  constants['sword_tile'],
                  libtcod.white,
                  "Terrium Sword of " + random.choice(item_descriptors) +
                  " (+" + str(sword_amount) + " atk)",
                  equippable=equippable_component)
    all_shop_equipment.append(item)

    shield_amount = randint(1, 2)
    equippable_component = Equippable(EquipmentSlots.OFF_HAND,
                                      defense_bonus=shield_amount,
                                      gold=7)
    item = Entity(0,
                  0,
                  constants['shield_tile'],
                  libtcod.white,
                  "Terrium Shield of " + random.choice(item_descriptors) +
                  " (+" + str(shield_amount) + " def)",
                  equippable=equippable_component)
    all_shop_equipment.append(item)

    chestplate_amount = randint(2, 3)
    equippable_component = Equippable(EquipmentSlots.CHEST,
                                      defense_bonus=chestplate_amount,
                                      gold=20)
    item = Entity(0,
                  0,
                  constants['chestplate_tile'],
                  libtcod.darker_grey,
                  "Terrium Chestplate of " + random.choice(item_descriptors) +
                  " (+" + str(chestplate_amount) + " def)",
                  equippable=equippable_component)
    all_shop_equipment.append(item)

    leg_amount = randint(1, 3)
    equippable_component = Equippable(EquipmentSlots.LEGS,
                                      defense_bonus=leg_amount,
                                      gold=15)
    item = Entity(0,
                  0,
                  constants['leg_armor_tile'],
                  libtcod.darker_grey,
                  "Terrium Leg Armor of " + random.choice(item_descriptors) +
                  " (+" + str(leg_amount) + " def)",
                  equippable=equippable_component)
    all_shop_equipment.append(item)

    helmet_amount = randint(1, 2)
    equippable_component = Equippable(EquipmentSlots.HEAD,
                                      defense_bonus=helmet_amount,
                                      gold=5)
    item = Entity(0,
                  0,
                  constants['helmet_tile'],
                  libtcod.darker_grey,
                  "Terrium Helmet of " + random.choice(item_descriptors) +
                  " (+" + str(helmet_amount) + " def)",
                  equippable=equippable_component)
    all_shop_equipment.append(item)

    amulet_amount = randint(1, 4)
    equippable_component = Equippable(EquipmentSlots.AMULET,
                                      magic_bonus=amulet_amount,
                                      gold=6)
    item = Entity(0,
                  0,
                  constants['amulet_tile'],
                  libtcod.darker_grey,
                  "Terrium Amulet of " + random.choice(item_descriptors) +
                  " (+" + str(amulet_amount) + " mgk)",
                  equippable=equippable_component)
    all_shop_equipment.append(item)

    number_of_shop_items = randint(1, 3)
    for i in range(number_of_shop_items):
        random_item = randint(0, len(all_shop_items) - 1)
        game_map.shop_items.append(all_shop_items[random_item])

    number_of_shop_equipment = randint(1, 2)
    for i in range(number_of_shop_equipment):
        random_equipment = randint(0, len(all_shop_equipment) - 1)
        game_map.shop_equipment_items.append(
            all_shop_equipment[random_equipment])

    message_log = MessageLog(constants['message_x'],
                             constants['message_width'],
                             constants['message_height'])

    game_state = GameStates.PLAYERS_TURN

    return player, entities, game_map, message_log, game_state
示例#23
0
def main():
    SCREEN_WIDTH = 80
    SCREEN_HEIGHT = 50

    MAP_WIDTH = 80
    MAP_HEIGHT = 45

    ROOM_MAX_SIZE = 10
    ROOM_MIN_SIZE = 6
    MAX_ROOMS = 30

    fov_algorithm = 0
    fov_light_walls = True
    fov_radius = 5

    max_monsters_per_room = 3

    colors = {
        'dark_wall': libtcod.Color(0, 0, 100),
        'dark_ground': libtcod.Color(50, 50, 150),
        'light_wall': libtcod.Color(130, 110, 50),
        'light_ground': libtcod.Color(200, 180, 50)
    }

    fighter_component = Fighter(hp=30, defense=2, power=5)
    player = Entity(0,
                    0,
                    '@',
                    libtcod.white,
                    'Player',
                    blocks=True,
                    render_order=RenderOrder.ACTOR,
                    fighter=fighter_component)
    entities = [player]

    libtcod.console_set_custom_font(
        'arial10x10.png',
        libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD)
    libtcod.console_init_root(SCREEN_WIDTH, SCREEN_HEIGHT,
                              'libtcod tutorial revised', False)

    con = libtcod.console_new(SCREEN_WIDTH, SCREEN_HEIGHT)

    game_map = GameMap(MAP_WIDTH, MAP_HEIGHT)
    game_map.make_map(MAX_ROOMS, ROOM_MIN_SIZE, ROOM_MAX_SIZE, MAP_WIDTH,
                      MAP_HEIGHT, player, entities, max_monsters_per_room)

    fov_recompute = True

    fov_map = initialize_fov(game_map)

    key = libtcod.Key()
    mouse = libtcod.Mouse()

    game_state = GameStates.PLAYERS_TURN

    # gameplay loop
    while not libtcod.console_is_window_closed():
        # this function captures new "events" - user input
        libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS, key, mouse)

        if fov_recompute:
            recompute_fov(fov_map, player.x, player.y, fov_radius,
                          fov_light_walls, fov_algorithm)

        render_all(con, entities, player, game_map, fov_map, fov_recompute,
                   SCREEN_WIDTH, SCREEN_HEIGHT, colors)

        fov_recompute = False

        libtcod.console_flush()  # presents everything to screen

        clear_all(con, entities)

        action = handle_keys(key)

        move = action.get('move')
        exit = action.get('exit')
        fullscreen = action.get('fullscreen')

        player_turn_results = []

        if move and game_state == GameStates.PLAYERS_TURN:
            dx, dy = move
            destination_x = player.x + dx
            destination_y = player.y + dy

            if not game_map.is_blocked(destination_x, destination_y):
                target = get_blocking_entities_at_location(
                    entities, destination_x, destination_y)

                if target:
                    attack_results = player.fighter.attack(target)
                    player_turn_results.extend(attack_results)
                else:
                    player.move(dx, dy)

                    fov_recompute = True

                game_state = GameStates.ENEMY_TURN

        if exit:
            return True

        if fullscreen:
            libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen())

        for player_turn_result in player_turn_results:
            message = player_turn_result.get('message')
            dead_entity = player_turn_result.get('dead')

            if message:
                print(message)

            if dead_entity:
                if dead_entity == player:
                    message, game_state = kill_player(dead_entity)
                else:
                    message = kill_monster(dead_entity)

                print(message)

        if game_state == GameStates.ENEMY_TURN:
            for entity in entities:
                if entity.ai:
                    enemy_turn_results = entity.ai.take_turn(
                        player, fov_map, game_map, entities)

                    for enemy_turn_result in enemy_turn_results:
                        message = enemy_turn_result.get('message')
                        dead_entity = enemy_turn_result.get('dead')

                        if message:
                            print(message)

                        if dead_entity:
                            if dead_entity == player:
                                message, game_state = kill_player(dead_entity)
                            else:
                                message = kill_monster(dead_entity)

                            print(message)

                            if game_state == GameStates.PLAYER_DEAD:
                                break

                    if game_state == GameStates.PLAYER_DEAD:
                        break

            else:
                game_state = GameStates.PLAYERS_TURN
示例#24
0
def main():
    screen_width = 80
    screen_height = 50

    bar_width = 20
    panel_height = 7
    panel_y = screen_height - panel_height

    message_x = bar_width + 2
    message_width = screen_width - bar_width - 2
    message_height = panel_height - 1

    map_width = 80
    map_height = 43

    room_max_size = 10
    room_min_size = 6
    max_rooms = 30

    fov_algorithm = 0
    fov_light_walls = True
    fov_radius = 10

    max_monsters_per_room = 3
    max_items_per_room = 2

    colors = {
        'dark_wall': libtcod.Color(0, 0, 100),
        'dark_ground': libtcod.Color(50, 50, 150),
        'light_wall': libtcod.Color(130, 110, 50),
        'light_ground': libtcod.Color(200, 180, 50)
    }

    fighter_component = Fighter(hp=30, defense=2, power=5)
    inventory_component = Inventory(26)
    player = Entity(0,
                    0,
                    '@',
                    libtcod.white,
                    "Player",
                    blocks=True,
                    render_order=RenderOrder.ACTOR,
                    fighter=fighter_component,
                    inventory=inventory_component)
    entities = [player]

    libtcod.console_set_custom_font(
        'arial10x10.png',
        libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD)

    libtcod.console_init_root(screen_width, screen_height,
                              'libtcod tutorial revised', False)

    con = libtcod.console_new(screen_width, screen_height)
    panel = libtcod.console_new(screen_width, panel_height)

    game_map = GameMap(map_width, map_height)
    game_map.make_map(max_rooms, room_min_size, room_max_size, map_width,
                      map_height, player, entities, max_monsters_per_room,
                      max_items_per_room)

    fov_recompute = True

    fov_map = initialize_fov(game_map)

    message_log = MessageLog(message_x, message_width, message_height)

    key = libtcod.Key()
    mouse = libtcod.Mouse()

    game_state = GameStates.PLAYERS_TURN
    previous_game_state = game_state

    targeting_item = None

    while not libtcod.console_is_window_closed():
        libtcod.sys_check_for_event(
            libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse)

        if fov_recompute:
            recompute_fov(fov_map, player.x, player.y, fov_radius,
                          fov_light_walls, fov_algorithm)

        render_all(con, panel, entities, player, game_map, fov_map,
                   fov_recompute, message_log, screen_width, screen_height,
                   bar_width, panel_height, panel_y, mouse, colors, game_state)

        libtcod.console_flush()

        clear_all(con, entities)

        action = handle_keys(key, game_state)
        mouse_action = handle_mouse(mouse)

        move = action.get('move')
        pickup = action.get('pickup')
        show_inventory = action.get('show_inventory')
        drop_inventory = action.get('drop_inventory')
        inventory_index = action.get('inventory_index')
        exit = action.get('exit')
        fullscreen = action.get('fullscreen')

        left_click = mouse_action.get('left_click')
        right_click = mouse_action.get('right_click')

        player_turn_results = []

        if move and game_state == GameStates.PLAYERS_TURN:
            dx, dy = move
            destination_x = player.x + dx
            destination_y = player.y + dy

            if not game_map.is_blocked(destination_x, destination_y):
                target = get_blocking_entities_at_location(
                    entities, destination_x, destination_y)

                if target:
                    attack_results = player.fighter.attack(target)
                    player_turn_results.extend(attack_results)
                else:
                    player.move(dx, dy)

                    fov_recompute = True

                game_state = GameStates.ENEMY_TURN
        elif pickup and game_state == GameStates.PLAYERS_TURN:
            for entity in entities:
                if entity.item and entity.x == player.x and entity.y == player.y:
                    pickup_results = player.inventory.add_item(entity)
                    player_turn_results.extend(pickup_results)

                    break
            else:
                message_log.add_message(
                    Message('There is nothing here to pick up.',
                            libtcod.yellow))

        if show_inventory:
            previous_game_state = game_state
            game_state = GameStates.SHOW_INVENTORY

        if drop_inventory:
            previous_game_state = game_state
            game_state = GameStates.DROP_INVENTORY

        if inventory_index is not None and previous_game_state != GameStates.PLAYER_DEAD and inventory_index < len(
                player.inventory.items):
            item = player.inventory.items[inventory_index]

            if game_state == GameStates.SHOW_INVENTORY:
                player_turn_results.extend(
                    player.inventory.use(item,
                                         entities=entities,
                                         fov_map=fov_map))
            elif game_state == GameStates.DROP_INVENTORY:
                player_turn_results.extend(player.inventory.drop_item(item))

        if game_state == GameStates.TARGETING:
            if left_click:
                target_x, target_y = left_click

                item_use_results = player.inventory.use(targeting_item,
                                                        entities=entities,
                                                        fov_map=fov_map,
                                                        target_x=target_x,
                                                        target_y=target_y)
                player_turn_results.extend(item_use_results)
            elif right_click:
                player_turn_results.append({'targeting_cancelled': True})
        if exit:
            if game_state in (GameStates.SHOW_INVENTORY,
                              GameStates.DROP_INVENTORY):
                game_state = previous_game_state
            elif game_state == GameStates.TARGETING:
                player_turn_results.append({'targeting_cancelled': True})
            else:
                return True

        if fullscreen:
            libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen())

        for player_turn_result in player_turn_results:
            message = player_turn_result.get('message')
            dead_entity = player_turn_result.get('dead')
            item_added = player_turn_result.get('item_added')
            item_consumed = player_turn_result.get('consumed')
            item_dropped = player_turn_result.get('item_dropped')
            targeting = player_turn_result.get('targeting')
            targeting_cancelled = player_turn_result.get('targeting_cancelled')

            if message:
                message_log.add_message(message)

            if targeting_cancelled:
                game_state = previous_game_state

                message_log.add_message(Message('Targeting cancelled'))
            if dead_entity:
                if dead_entity == player:
                    message, game_state = kill_player(dead_entity)
                else:
                    message = kill_monster(dead_entity)

                message_log.add_message(message)

            if item_added:
                entities.remove(item_added)

                game_state = GameStates.ENEMY_TURN

            if item_consumed:
                game_state = GameStates.ENEMY_TURN

            if targeting:
                previous_game_state = GameStates.PLAYERS_TURN
                game_state = GameStates.TARGETING

                targeting_item = targeting
                message_log.add_message(targeting_item.item.targeting_message)
            if item_dropped:
                entities.append(item_dropped)

                game_state = GameStates.ENEMY_TURN

        if game_state == GameStates.ENEMY_TURN:
            for entity in entities:
                if entity.ai:
                    enemy_turn_results = entity.ai.take_turn(
                        player, fov_map, game_map, entities)

                    for enemy_turn_result in enemy_turn_results:
                        message = enemy_turn_result.get('message')
                        dead_entity = enemy_turn_result.get('dead')

                        if message:
                            message_log.add_message(message)

                        if dead_entity:
                            if dead_entity == player:
                                message, game_state = kill_player(dead_entity)
                            else:
                                message = kill_monster(dead_entity)

                            message_log.add_message(message)

                            if game_state == GameStates.PLAYER_DEAD:
                                break

                    if game_state == GameStates.PLAYER_DEAD:
                        break

            else:
                game_state = GameStates.PLAYERS_TURN
示例#25
0
def main():
    paused = True

    screen_width = 60
    screen_height = 40
    map_width = 60
    map_height = 40

    room_max_size = 10
    room_min_size = 6
    max_rooms = 20

    player = Entity(int(screen_width / 2), int(screen_height / 2), '@', "DEV", tcod.white, "")
    entities = [player]

    tcod.console_set_custom_font('my20x20.png', tcod.FONT_TYPE_GREYSCALE | tcod.FONT_LAYOUT_TCOD)
    tcod.console_init_root(screen_width, screen_height, 'PERMADEV', False)
    tcod.sys_set_fps(30)

    con = tcod.console.Console(screen_width, screen_height)

    glo.game_map = GameMap(map_width, map_height, entities, glo.items)
    glo.game_map.make_map(max_rooms, room_min_size, room_max_size, map_width, map_height, player)

    key = tcod.Key()
    mouse = tcod.Mouse()

    while not tcod.console_is_window_closed():
        con.clear()

        tcod.sys_check_for_event(tcod.EVENT_KEY_PRESS, key, mouse)

        if not paused:
            for entity in entities:
                entity.ai_step()

        render_all(con, entities, screen_width, screen_height, colors)

        if paused:
            con.print(0, 0, "PAUSED", tcod.red, tcod.yellow)
            glo.uis.append(ui.MenuBar(7, 0, [
                    {
                        "name": "Action",
                        "cb": lambda: glo.uis.append(ui.Dropdown(7, 1, [
                            {
                                "name": "Mark zone",
                                "cb": lambda: glo.uis.append(ui.Dropdown(19, 1, [
                                    {
                                        "name": "Storage",
                                        "cb": lambda: print("foo")
                                    },
                                    {
                                        "name": "Living",
                                        "cb": lambda: print("foo")
                                    },
                                    {
                                        "name": "Danger",
                                        "cb": lambda: print("foo")
                                    },
                                ]))
                            },
                            {
                                "name": "Build",
                                "cb": lambda: glo.uis.append(ui.Dropdown(19, 2, [
                                    {
                                        "name": "Wall",
                                        "cb": lambda: print("foo")
                                    },
                                    {
                                        "name": "Door",
                                        "cb": lambda: print("foo")
                                    },
                                    {
                                        "name": "Wire",
                                        "cb": lambda: print("foo")
                                    },
                                    {
                                        "name": "Panel",
                                        "cb": lambda: print("foo")
                                    },
                                ]))
                            },
                            {
                                "name": "Dig",
                                "cb": lambda: print("foo")
                            },
                        ]))
                    },
                    {
                        "name": "Units",
                        "cb": lambda: print("foo")
                    },
                    {
                        "name": "Stats",
                        "cb": lambda: print("foo")
                    },
                    {
                        "name": "Zones",
                        "cb": lambda: print("foo")
                    },
                ]))
        else:
            con.print(0, 0, "RUNNING")

        if paused:
            for entity in entities:
                if entity.x == mouse.cx and entity.y == mouse.cy:
                    tcod.console_set_char_background(con, mouse.cx, mouse.cy, tcod.grey)
                    con.print(mouse.cx+1, mouse.cy, entity.name, tcod.black, tcod.green)

        if paused and mouse.lbutton:
            for entity in entities:
                if entity.x == mouse.cx and entity.y == mouse.cy:
                    glo.uis.append(ui.Editor(3, 4, entity))

        for u in glo.uis:
            u.update(mouse, key)

        for u in glo.uis:
            u.draw(con, mouse)

        tcod.console_blit(con, 0, 0, screen_width, screen_height, 0, 0, 0)

        tcod.console_flush()

        action = handle_keys(key)

        move = action.get('move')
        exit = action.get('exit')
        fullscreen = action.get('fullscreen')
        toggle = action.get('toggle')

        if toggle:
            paused = not paused
            glo.uis = []

        if not paused:
            if move:
                dx, dy = move
                if not glo.game_map.is_blocked(player.x + dx, player.y + dy):
                    player.move(dx, dy)

        if exit:
            return True

        if fullscreen:
            tcod.console_set_fullscreen(not tcod.console_is_fullscreen())
示例#26
0
    def main(self):

        # Set font
        libtcod.console_set_custom_font(
            'dejavu_wide16x16_gs_tc.png',
            libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD)
        # Create game window
        libtcod.console_init_root(self.constants['screen_width'],
                                  self.constants['screen_height'],
                                  self.constants['window_title'], False)

        # Create a console (Drawing Layer?)
        con = libtcod.console_new(self.constants['screen_width'],
                                  self.constants['screen_height'])

        # Holds keyboard and mouse input
        key = libtcod.Key()
        mouse = libtcod.Mouse()

        entities = []
        players = get_players(self.constants, entities)

        game_map = GameMap(self.constants['map_width'],
                           self.constants['map_height'])
        make_town(game_map, self.constants['max_rooms'],
                  self.constants['room_min_size'],
                  self.constants['room_max_size'], self.constants['map_width'],
                  self.constants['map_height'], players, entities)

        render_map(con,
                   players,
                   game_map,
                   self.constants['screen_width'],
                   self.constants['screen_height'],
                   all_visible=True)
        render_entities(con,
                        entities,
                        players,
                        game_map,
                        self.constants['screen_width'],
                        self.constants['screen_height'],
                        all_visible=True)
        libtcod.console_flush()

        # Render loop
        while not libtcod.console_is_window_closed():
            libtcod.sys_check_for_event(
                libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse)

            if mouse.lbutton_pressed:
                # Clear all entities except players
                game_map.dungeon_level += 1
                entities = entities[:self.constants['player_count']]
                make_town(game_map, self.constants['max_rooms'],
                          self.constants['room_min_size'],
                          self.constants['room_max_size'],
                          self.constants['map_width'],
                          self.constants['map_height'], players, entities)

                render_map(con,
                           players,
                           game_map,
                           self.constants['screen_width'],
                           self.constants['screen_height'],
                           all_visible=True)
                render_entities(con,
                                entities,
                                players,
                                game_map,
                                self.constants['screen_width'],
                                self.constants['screen_height'],
                                all_visible=True)
                libtcod.console_flush()

            if key.vk == libtcod.KEY_ESCAPE:
                return True
示例#27
0
def main():
    screen_width = 80
    screen_height = 50
    map_width = 80
    map_height = 45
    blocked_way = 0

    colors = {
        'dark_wall': libtcod.Color(0, 0, 100),
        'dark_ground': libtcod.Color(50, 50, 150)
    }

    player = Entity(int(screen_width / 2), int(screen_height / 2), '@',
                    libtcod.white, False)
    npc = Entity(int(screen_width / 2 - 5), int(screen_height / 2), '@',
                 libtcod.yellow, True)
    entities = [npc, player]

    libtcod.console_set_custom_font(
        'arial10x10.png',
        libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD)

    libtcod.console_init_root(screen_width, screen_height,
                              'dumplings dungeons', False)

    con = libtcod.console.Console(screen_width, screen_height)

    game_map = GameMap(map_width, map_height)

    key = libtcod.Key()
    mouse = libtcod.Mouse()

    while not libtcod.console_is_window_closed():
        libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS, key, mouse)

        render_all(con, entities, game_map, screen_width, screen_height,
                   colors)

        libtcod.console_flush()

        clear_all(con, entities)

        action = handle_keys(key)

        move = action.get('move')
        exit = action.get('exit')
        fullscreen = action.get('fullscreen')

        if move:
            dx, dy = move
            if not game_map.is_blocked(player.x + dx, player.y + dy):
                blocked_way = 0
                for i in range(len(entities)):
                    if player.x + dx == entities[
                            i].x and player.y + dy == entities[i].y:
                        if entities[i].is_blocked(entities[i].x,
                                                  entities[i].y):
                            blocked_way = 1
                if not blocked_way:
                    player.move(dx, dy)

        if exit:
            return True

        if fullscreen:
            libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen())
示例#28
0
def get_game_variables(constants):
    fighter_component = Fighter(hp=100,
                                defense=1,
                                power=5,
                                agility=1,
                                job=0,
                                mana=10,
                                nutrition=500,
                                base_psyche=2,
                                starvation_bonus=0)
    inventory_component = Inventory(26)
    skills_component = Skills(15)
    level_component = Level()
    equipment_component = Equipment()
    player = Entity(0,
                    0,
                    '@',
                    libtcod.white,
                    'Player',
                    blocks=True,
                    render_order=RenderOrder.ACTOR,
                    fighter=fighter_component,
                    inventory=inventory_component,
                    level=level_component,
                    equipment=equipment_component,
                    skills=skills_component)
    entities = [player]

    equippable_component = Equippable(EquipmentSlots.MAIN_HAND, power_bonus=4)
    item_component = Item(use_function=None)
    dagger = Entity(0,
                    0,
                    '/',
                    libtcod.sky,
                    'Carving Knife',
                    equippable=equippable_component,
                    item=item_component)
    equippable_component = Equippable(EquipmentSlots.OFF_HAND,
                                      defense_bonus=1,
                                      agility_bonus=-1)
    item_component = Item(use_function=None)
    buckler = Entity(0,
                     0,
                     '{',
                     libtcod.sky,
                     'Buckler',
                     equippable=equippable_component,
                     item=item_component)
    player.inventory.add_item(dagger)
    player.equipment.toggle_equip(dagger)
    player.inventory.add_item(buckler)
    player.equipment.toggle_equip(buckler)
    game_map = GameMap(constants['map_width'], constants['map_height'])
    game_map.make_map(constants['max_rooms'], constants['room_min_size'],
                      constants['room_max_size'], constants['max_maze_rooms'],
                      constants['maze_min_size'], constants['maze_max_size'],
                      constants['map_width'], constants['map_height'], player,
                      entities)

    message_log = MessageLog(constants['message_x'],
                             constants['message_width'],
                             constants['message_height'])

    game_state = GameStates.CHARACTER_CREATION
    ggender = Gender.male

    return player, entities, game_map, message_log, game_state, ggender
示例#29
0
def main():
    # Initiate important variables
    screen_width = 80
    screen_height = 50

    bar_width = 20
    panel_height = 7
    panel_y = screen_height - panel_height

    message_x = bar_width + 2
    message_width = screen_width - bar_width - 2
    message_height = panel_height - 1

    map_width = 80
    map_height = 43

    room_max_size = 10
    room_min_size = 6
    max_rooms = 30

    fov_algorithm = 0
    fov_light_walls = True
    fov_radius = 10

    max_monsters_per_room = 3
    max_items_per_room = 2

    colors = {
        'dark_wall': libtcod.Color(0, 0, 100),
        'dark_ground': libtcod.Color(50, 50, 150),
        'light_wall': libtcod.Color(130, 110, 50),
        'light_ground': libtcod.Color(200, 180, 50),
    }

    # Initiate the objects that will be important in rendering and the map
    fighter_component = Fighter(hp=30, defense=2, power=5)
    inventory_component = Inventory(26)
    player = Entity(0, 0, '@', libtcod.white, 'Player', blocks=True, render_order=RenderOrder.ACTOR, fighter=fighter_component, inventory=inventory_component)
    entities = [player]

    libtcod.console_set_custom_font('arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD)

    libtcod.console_init_root(screen_width, screen_height, 'Cardinal Code', False) #boolean is fullscreen or not

    con = libtcod.console_new(screen_width, screen_height)
    panel = libtcod.console_new(screen_width, panel_height)

    game_map = GameMap(map_width, map_height)
    game_map.make_map(max_rooms, room_min_size, room_max_size, map_width, map_height, player, entities, max_monsters_per_room, max_items_per_room)

    fov_recompute = True

    fov_map = initialize_fov(game_map)

    message_log = MessageLog(message_x, message_width, message_height)

    key = libtcod.Key()
    mouse = libtcod.Mouse()

    game_state = GameStates.PLAYERS_TURN

    # Main game loop
    while not libtcod.console_is_window_closed():
        libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse)

        # fov_recompute tells if the render function should recompute the FOV
        # recompute_fov will recompute the FOV from render_functions.py based on the initialized variables
        if(fov_recompute):
            recompute_fov(fov_map, player.x, player.y, fov_radius, fov_light_walls, fov_algorithm)

        # Renders the map and the screens
        render_all(con, panel, entities, player, game_map, fov_map, fov_recompute, message_log, screen_width, screen_height, bar_width, panel_height, panel_y, mouse, colors)

        libtcod.console_flush()

        # Clears entities whose position changed
        clear_all(con, entities)

        # Get what key was pressed, from sets of dictionaries
        action = handle_keys(key)

        # Then get the action from the sets of dictionaries established in input_handlers.py
        move = action.get('move')
        pickup = action.get('pickup')
        exit = action.get('exit')
        fullscreen = action.get('fullscreen')

        player_turn_results = []

        # If move has a value and the game_state is the player's state
        if move and game_state == GameStates.PLAYERS_TURN:
            dx, dy = move
            destination_x = player.x + dx
            destination_y = player.y + dy

            # If the player's destination is not blocked, do something
            if not game_map.is_blocked(destination_x, destination_y):
                target = get_blocking_entities_at_location(entities, destination_x, destination_y)

                # If there is an entity at the destination, do this
                if target:
                    player.fighter.attack(target)
                    attack_results = player.fighter.attack(target)
                    player_turn_results.extend(attack_results)
                else:
                    player.move(dx, dy)

                    fov_recompute = True

                    game_state = GameStates.ENEMY_TURN

        elif pickup and game_state == GameStates.PLAYERS_TURN:
            for entity in entities:
                if entity.item and entity.x == player.x and entity.y == player.y:
                    pickup_results = player.inventory.add_item(entity)
                    player_turn_results.extend(pickup_results)

                    break
            else:
                message_log.add_message(Message('There is nothing here to pickup.', libtcod.yellow))

        if exit:
            return True

        if fullscreen:
            libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen)

        for player_turn_result in player_turn_results:
            message = player_turn_result.get('message')
            dead_entity = player_turn_result.get('dead')
            item_added = player_turn_result.get('item_added')

            if message:
                message_log.add_message(message)
            if dead_entity:
                if dead_entity == player:
                    message, game_state = kill_player(dead_entity)
                else:
                    message = kill_monster(dead_entity)

                message_log.add_message(message)

            if item_added:
                entities.remove(item_added)

                game_state = GameStates.ENEMY_TURN

        if game_state == GameStates.ENEMY_TURN:
            for entity in entities:
                if entity.ai:
                    entity.ai.take_turn(player, fov_map, game_map, entities)
                    enemy_turn_results = entity.ai.take_turn(player,fov_map, game_map, entities)

                    for enemy_turn_result in enemy_turn_results:
                        message = enemy_turn_result.get('message')
                        dead_entity = enemy_turn_result.get('dead')

                        if message:
                            message_log.add_message(message)
                        if dead_entity:
                            if dead_entity == player:
                                message, game_state = kill_player(dead_entity)
                            else:
                                message = kill_monster(dead_entity)

                            message_log.add_message(message)

                            if game_state == GameStates.PLAYER_DEAD:
                                break
                        if game_state == GameStates.PLAYER_DEAD:
                            break
            else:
                game_state = GameStates.PLAYERS_TURN
示例#30
0
def main():
    screen_width = 80
    screen_height = 50
    map_width = 80
    map_height = 45

    room_max_size = 10
    room_min_size = 6
    max_rooms = 30

    fov_algorithm = 0
    fov_light_walls = True
    fov_radius = 10

    colors = {
        'dark_wall': libtcod.Color(0, 0, 100),
        'dark_ground': libtcod.Color(50, 50, 150),
        'light_wall': libtcod.Color(130, 110, 50),
        'light_ground': libtcod.Color(200, 180, 50)
    }

    player = Entity(int(screen_width / 2), int(screen_height / 2), '@', libtcod.white)
    npc = Entity(int(screen_width / 2 - 5), int(screen_height / 2), '@', libtcod.yellow)
    entities = [npc, player]

    libtcod.console_set_custom_font('arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD)

    libtcod.console_init_root(screen_width, screen_height, 'PyRogue', False)

    con = libtcod.console_new(screen_width, screen_height)

    game_map = GameMap(map_width, map_height)
    game_map.make_map(max_rooms, room_min_size, room_max_size, map_width, map_height, player)

    fov_recompute = True

    fov_map = initialize_fov(game_map)

    key = libtcod.Key()
    mouse = libtcod.Mouse()

    while not libtcod.console_is_window_closed():
        libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS, key, mouse)

        if fov_recompute:
            recompute_fov(fov_map, player.x, player.y, fov_radius, fov_light_walls,
                          fov_algorithm)

        render_all(con, entities, game_map, fov_map, fov_recompute, screen_width, screen_height, colors)

        fov_recompute = False

        libtcod.console_flush()

        clear_all(con, entities)

        action = handle_keys(key)

        move = action.get('move')
        exit = action.get('exit')
        fullscreen = action.get('fullscreen')

        if move:
            dx, dy = move
            if not game_map.is_blocked(player.x + dx, player.y + dy):
                player.move(dx, dy)

                fov_recompute = True

        if exit:
            return True

        if fullscreen:
            libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen())
示例#31
0
def main():
    name = "pythonRL"

    screenWidth = 80
    screenHeight = 50

    bar_width = 20
    panel_height = 7
    panel_y = screenHeight - panel_height

    message_x = bar_width + 2
    message_width = screenWidth - bar_width - 1
    message_height = panel_height - 1

    mapWidth = 80
    mapHeight = 43

    room_min_size = 6
    room_max_size = 10
    max_rooms = 30

    fov_algorithm = 0
    fov_light_walls = True
    fov_radius = 10

    max_monsters_per_room = 3

    colors = {
        'dark_wall': tcod.Color(61, 31, 0),
        'dark_ground': tcod.Color(41, 21, 0),
        'light_wall': tcod.Color(77, 38, 0),
        'light_ground': tcod.Color(56, 28, 0),
        'nothing': tcod.Color(0, 0, 0)
    }

    fighter_component = Fighter(hp=30, defense=2, power=5)
    player = Entity(0,
                    0,
                    "@",
                    tcod.white,
                    "Player",
                    blocks=True,
                    render_order=RenderOrder.ACTOR,
                    fighter=fighter_component)
    entities = [player]

    tcod.console_set_custom_font(
        'arial10x10.png', tcod.FONT_TYPE_GREYSCALE | tcod.FONT_LAYOUT_TCOD)

    tcod.console_init_root(screenWidth, screenHeight, name, False,
                           tcod.RENDERER_SDL2, "F", True)

    con = tcod.console.Console(screenWidth, screenHeight, "F")
    panel = tcod.console.Console(screenWidth, panel_height)

    game_map = GameMap(mapWidth, mapHeight)
    game_map.make_map(max_rooms, room_min_size, room_max_size, mapWidth,
                      mapHeight, player, entities, max_monsters_per_room)

    fov_recompute = True

    fov_map = initialize_fov(game_map)

    message_log = MessageLog(message_x, message_width, message_height)

    key = tcod.Key()
    mouse = tcod.Mouse()

    game_state = GameStates.PLAYERS_TURN

    while not tcod.console_is_window_closed():
        tcod.sys_check_for_event(tcod.EVENT_KEY_PRESS | tcod.EVENT_MOUSE, key,
                                 mouse)

        if fov_recompute:
            recompute_fov(fov_map, player.x, player.y, fov_radius,
                          fov_light_walls, fov_algorithm)

        render_all(con, panel, entities, player, game_map, fov_map,
                   fov_recompute, message_log, screenWidth, screenHeight,
                   bar_width, panel_height, panel_y, mouse, colors)

        fov_recompute = False

        tcod.console_flush()

        clear_all(con, entities)

        action = handle_keys(key)

        move = action.get("move")
        exit = action.get("exit")
        fullscreen = action.get("fullscreen")
        generate = action.get("gen")

        player_turn_results = []

        if move and game_state == GameStates.PLAYERS_TURN:
            dx, dy = move
            dest_x = player.x + dx
            dest_y = player.y + dy

            if not game_map.is_blocked(dest_x, dest_y):
                target = get_blocking_entities_at_location(
                    entities, dest_x, dest_y)

                if target:
                    attack_results = player.fighter.attack(target)
                    player_turn_results.extend(attack_results)
                else:
                    player.move(dx, dy)
                    fov_recompute = True

                game_state = GameStates.ENEMY_TURN

        if exit:
            return True

        if fullscreen:
            tcod.console_set_fullscreen(not tcod.console_is_fullscreen())

        for player_turn_result in player_turn_results:
            message = player_turn_result.get("message")
            dead_entity = player_turn_result.get("dead")

            if message:
                message_log.add_message(message)

            if dead_entity:
                if dead_entity == player:
                    message, game_state = kill_player(dead_entity)
                else:
                    message = kill_monster(dead_entity)

                message_log.add_message(message)

        if game_state == GameStates.ENEMY_TURN:
            for entity in entities:
                if entity.ai:
                    enemy_turn_results = entity.ai.take_turn(
                        player, fov_map, game_map, entities)

                    for enemy_turn_result in enemy_turn_results:
                        message = enemy_turn_result.get("message")
                        dead_entity = enemy_turn_result.get("dead")

                        if message:
                            message_log.add_message(message)

                        if dead_entity:
                            if dead_entity == player:
                                message, game_state = kill_player(dead_entity)
                            else:
                                message = kill_monster(dead_entity)

                            message_log.add_message(message)

                            if game_state == GameStates.PLAYER_DEAD:
                                break

                    if game_state == GameStates.PLAYER_DEAD:
                        break

            else:
                game_state = GameStates.PLAYERS_TURN

        if generate:
            game_map.clear()
            game_map.make_map(max_rooms, room_min_size, room_max_size,
                              mapWidth, mapHeight, player)
            fov_map = initialize_fov(game_map)
            fov_recompute = True