Exemple #1
0
def main(screen):
    # -----------Debug------------
    enable_pathfinder_screen = False  # Draws the found pathfinder path
    # ----------------------------

    clock = pygame.time.Clock()

    floor_s = pygame.Surface((10 * 64, 20 * 64))
    world_s = pygame.Surface((10 * 64, 20 * 64))
    pathfinder_screen_s = floor_s.copy()
    pathfinder_screen_s.set_colorkey(colorBlack)
    pathfinding_screen_reset = False

    sword_surface = resource_loader.load_sprite('sword')

    main_menu = MainMenu()

    map_loader = MapLoader(MapData, NPC, Intent)
    map_data = map_loader.load_default_map()

    path_finder = PathFinder(map_data.passable_tiles)

    dirty_drawing = DirtyDrawing()
    dirty_drawing.prepare_floor(floor_s, map_data.get_texture_layer)
    dirty_drawing.issue_world_surface_redraw()

    intent = Intent()

    hero = Hero(surface=resource_loader.load_sprite('hero'), intent_instance=Intent(), inventory_instance=Inventory())
    if not map_data.tile_occupied((1, 1)):
        map_data.set_character_on_map(hero, (1, 1))
        hero.move(1, 1)
    new_weapon = Weapon(generate_item_name('sword'))
    print hero.inventory
    hero.inventory.add_item(new_weapon)

    camera = Camera(starting_position=hero.positionOnMap, viewport_size=(10, 10))
    camera.set_viewport_boundaries((0, 0), map_data.mapBoundaries)

    message_log = MessageLog(defaultFont)
    message_log.position = (screen.get_size()[0] - message_log.render.get_size()[0], 0)

    # Temporary. Combat is on at the start
    in_combat = False

    entities_in_combat = []
    for entity in map_data.get_characters_on_map:
        entities_in_combat.append(entity)
    reaction_order = []
    creature_in_turn = None
    new_event = pygame.event.Event(pygame.USEREVENT, subtype="combat", combat_situation="start")
    pygame.event.post(new_event)

    screen.fill(colorBlack)

    new_sword = Weapon(generate_item_name(), surface=sword_surface)
    map_data.set_item_on_map(new_sword, (3, 4))

    # MAINLOOP--------------------------
    while 1:
        clock.tick(40)
        hero.intent.type = 0

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_SPACE:
                    hero.intent.type = intent.WAIT
                elif event.key == pygame.K_UP or event.key == pygame.K_KP8:
                    hero.intent.type = intent.MOVE
                    hero.intent.direction = (0, -1)
                elif event.key == pygame.K_DOWN or event.key == pygame.K_KP2:
                    hero.intent.type = intent.MOVE
                    hero.intent.direction = (0, 1)
                elif event.key == pygame.K_LEFT or event.key == pygame.K_KP4:
                    hero.intent.type = intent.MOVE
                    hero.intent.direction = (-1, 0)
                elif event.key == pygame.K_RIGHT or event.key == pygame.K_KP6:
                    hero.intent.type = intent.MOVE
                    hero.intent.direction = (1, 0)
                elif event.key == pygame.K_KP7:
                    hero.intent.type = intent.MOVE
                    hero.intent.direction = (-1, -1)
                elif event.key == pygame.K_KP9:
                    hero.intent.type = intent.MOVE
                    hero.intent.direction = (1, -1)
                elif event.key == pygame.K_KP3:
                    hero.intent.type = intent.MOVE
                    hero.intent.direction = (1, 1)
                elif event.key == pygame.K_KP1:
                    hero.intent.type = intent.MOVE
                    hero.intent.direction = (-1, 1)
                elif event.key == pygame.K_KP_PLUS:
                    npc = add_monster_to_random_position(map_data, NPC(resource_loader.load_sprite('thug'),
                                                                       Intent(), Inventory()))
                    entities_in_combat.append(npc)
                elif event.key == pygame.K_KP_MINUS:
                    pygame.event.post(pygame.event.Event(pygame.USEREVENT,
                                                         subtype="menu"))
                elif event.key == pygame.K_i:
                    message_log.newline("-----Inventory:-----")
                    for item in hero.inventory.get_items:
                        assert isinstance(item, Item)
                        message_log.newline(item.name)
                elif event.key == pygame.K_COMMA:
                    pick_up_item(hero, map_data)

            if event.type == pygame.USEREVENT:
                if event.subtype is "combat":

                    if event.combat_situation == 'start':
                        in_combat = True
                        reaction_order = Dice.roll_reactions(entities_in_combat)
                        message_log.newline('RR')
                        pygame.event.post(pygame.event.Event(pygame.USEREVENT,
                                                             subtype="combat", combat_situation='first_turn'))

                    if event.combat_situation == 'first_turn':
                        creature_in_turn = reaction_order[0]
                        if creature_in_turn == hero:
                            message_log.newline("You start")
                        else:
                            message_log.newline('{} starts'.format(reaction_order[0].name))

                    if event.combat_situation == 'turn_change':
                        try:
                            reaction_order.remove(creature_in_turn)
                        except ValueError:
                            print '{} not in reaction order.'.format(creature_in_turn)

                        try:
                            creature_in_turn = reaction_order[0]
                            if creature_in_turn == hero:
                                message_log.newline("Your turn.")
                            else:
                                message_log.newline("{}'s turn.".format(reaction_order[0].name))
                        except IndexError:
                            creature_in_turn = None
                            pygame.event.post(pygame.event.Event(pygame.USEREVENT,
                                                                 subtype="combat", combat_situation='end_of_phase'))

                    if event.combat_situation == "end_of_phase" and in_combat:
                        reaction_order = Dice.roll_reactions(entities_in_combat)
                        creature_in_turn = None
                        message_log.newline('EOP- RR')
                        pathfinding_screen_reset = True
                        pygame.event.post(pygame.event.Event(pygame.USEREVENT,
                                                             subtype="combat", combat_situation='first_turn'))

                    if event.combat_situation == "end":
                        in_combat = False

                elif event.subtype is 'menu':
                    main_menu.open_menu(screen)

        if in_combat is True:

            if isinstance(creature_in_turn, Hero):
                hero_makes_decision = hero_turn(hero, map_data, message_log)
                if hero_makes_decision:
                    pygame.event.post(pygame.event.Event(pygame.USEREVENT,
                                                         subtype="combat", combat_situation="turn_change"))

                    if hero.intent.type is intent.MOVE:
                        map_data.attempt_move("char", hero.positionOnMap, direction=hero.intent.direction)
                        hero.move(*hero.intent.direction)

                    elif hero.intent.type is intent.ATTACK:
                        handle_attack(hero, hero.intent.target,
                                      message_log, map_data, entities_in_combat, reaction_order,
                                      sword_surface=sword_surface)

                    elif hero.intent.type == intent.WAIT:
                        pass
                    else:
                        raise "No Hero intention_:{}".format(hero.intent.type)

            elif isinstance(creature_in_turn, NPC):
                npc = creature_in_turn
                path = path_finder.find_path_between_points(npc.positionOnMap, hero.positionOnMap)
                if enable_pathfinder_screen == True:
                    if pathfinding_screen_reset == True:
                        pathfinder_screen_s.fill(colorBlack)
                        pathfinding_screen_reset = False
                    draw_pathfinder_path(pathfinder_screen_s, path)
                if path == 'path not found':
                    pygame.event.post(pygame.event.Event(pygame.USEREVENT,
                                                         subtype="combat", combat_situation="turn_change"))
                else:
                    move_success = map_data.attempt_move("char", npc.positionOnMap, destination=path[1])
                    if move_success is True:
                        # npc.move(*npc.intent.direction)
                        npc.set_position(path[1])
                        pygame.event.post(pygame.event.Event(pygame.USEREVENT,
                                                             subtype="combat", combat_situation="turn_change"))
                    elif isinstance(move_success, NPC):
                        npc.intent = Intent.WAIT
                        pygame.event.post(pygame.event.Event(pygame.USEREVENT,
                                                             subtype="combat", combat_situation="turn_change"))
                    elif isinstance(move_success, Hero):
                        handle_attack(npc, hero, message_log, map_data, entities_in_combat, reaction_order)
                        pygame.event.post(pygame.event.Event(pygame.USEREVENT,
                                                             subtype="combat", combat_situation="turn_change"))
                    else:
                        print "ERRORROROREREERRROR"

        camera.set_tile_position(hero.positionOnMap)

        if enable_pathfinder_screen:
            dirty_drawing.issue_world_surface_redraw()

        dirty_drawing.draw(screen, world_s, camera, floor_s, map_data)

        if message_log.get_is_dirty is True:
            dirty_drawing.draw_message_log(screen, message_log)

        if enable_pathfinder_screen:
            pass
            # dirty_drawing.draw_pathfinder_screen(screen, pathfinder_screen_s)

        pygame.display.flip()
Exemple #2
0
def main(screen):
    # -----------Debug------------

    print_keypresses = False

    dev_hero_undying = False
    # ------Init classes----------

    clock = pygame.time.Clock()
    resource_loader = resources.Resource_Loader()
    camera = Camera()
    main_menu = MainMenu()
    map_loader = MapLoader(MapData, creatures.NPC)
    dialogs = Dialogs()
    dialog_window_inst = windows.DialogWindow()
    combat_handler = CombatHandler()
    peaceful_action_handler = PeacefulActionHandler()

    message_log = MessageLog(default_font)

    map_editor = devtools.Map_editor(resource_loader)
    item_generator = generators.ItemGenerator(resource_loader)

    # ------Load variables----------

    hero = creatures.Hero(surface=resource_loader.load_sprite('hero'), inventory_instance=Inventory())

    map_data, path_finder, dirty_drawing, floor_s, world_s = load_map('default', map_loader, resource_loader, hero)

    camera.set_tile_position(hero.positionOnMap)
    camera.set_viewport_size((10, 10))
    camera.set_viewport_boundaries((0, 0), map_data.mapBoundaries)

    message_log.position = (screen.get_size()[0] - message_log.render.get_size()[0], 0)

    set_window_frames(dialog_window_inst, resource_loader, drawing)

    # Give hero an sword an put pne on the ground
    hero.inventory.add_item(item_generator.generate_sword())
    map_data.set_item_on_map(item_generator.generate_sword(), (3, 4))
    map_data.set_item_on_map(item_generator.generate_sword(), (3, 3))
    map_data.set_item_on_map(item_generator.generate_sword(), (3, 5))
    map_data.set_item_on_map(item_generator.generate_sword(), (3, 6))
    map_data.set_item_on_map(item_generator.generate_sword(), (3, 7))
    map_data.set_item_on_map(item_generator.generate_sword(), (3, 8))
    map_data.set_item_on_map(item_generator.generate_sword(), (3, 9))
    map_data.set_item_on_map(item_generator.generate_sword(), (3, 10))
    map_data.set_item_on_map(item_generator.generate_sword(), (3, 11))

    temp_sound = pygame.mixer.Sound('map_change.wav')
    default_font_inited = pygame.font.Font(default_font, 17)

    screen.fill(colorBlack)

    combat_handler.create_combat(map_data.get_characters_on_map)
    push_new_user_event('combat', 'level_init')

    # MAINLOOP--------------------------
    while 1:
        clock.tick(40)
        hero.intent.type = 0

        if dev_hero_undying:
            hero.sheet.fatigue = 0

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

            if event.type == pygame.USEREVENT:
                if event.subtype is 'map_change':
                    try:
                        if event.data[0] == '@':
                            map_load_return = load_map('map_arena.map', map_loader, resource_loader, hero)
                            map_data, path_finder, dirty_drawing, floor_s, world_s = map_load_return
                            push_new_user_event('combat', 'level_init')

                            camera.set_viewport_boundaries((0, 0), map_data.mapBoundaries)

                            temp_sound.play()
                    except IndexError:
                        print "Invalid map_change data: {}".format(event.data)

                elif event.subtype is 'combat':
                    if event.data == 'level_init':
                        combat_handler.reset_combat()
                        for creature in map_data.get_characters_on_map:
                            if creature.in_combat:
                                combat_handler.add_creature(creature)
                            else:
                                peaceful_action_handler.add_creature(creature)
                        combat_handler.create_combat()
                        if combat_handler.creature_list.__len__() != 0:
                            combat_handler.add_creature(hero)
                            hero.in_combat = True

                    elif event.data == 'start':
                        combat_handler.combat_active = True
                        combat_handler.reaction_order = dice.roll_reactions(combat_handler.creature_list)
                        message_log.newline('Entering combat.')
                        push_new_user_event('combat', 'start_phase')

                    elif event.data == 'start_phase':
                        combat_handler.combat_phase_done = False
                        combat_handler.reaction_order = dice.roll_reactions(combat_handler.creature_list)
                        if combat_handler.creature_in_turn == hero:
                            message_log.newline("You start")
                        elif isinstance(combat_handler.creature_in_turn, creatures.NPC):
                            message_log.newline('{} starts'.format(combat_handler.creature_in_turn.name))
                        else:
                            print 'ERROR: first turn invalid creature in turn {}'.format(
                                combat_handler.creature_in_turn)

                    elif event.data == 'turn_change':
                        combat_handler.next_creature_turn()

                        if combat_handler.creature_in_turn is not None:
                            if combat_handler.creature_in_turn == hero:
                                message_log.newline("Your turn.")
                            else:
                                message_log.newline("{}'s turn.".format(combat_handler.creature_in_turn.name))
                        else:
                            push_new_user_event('combat', 'end_of_phase')

                    elif event.data == "end_of_phase":
                        message_log.newline('End of combat phase.')
                        combat_handler.combat_phase_done = True
                        push_new_user_event('turn_order', 'start_phase')

                    elif event.data == "end":
                        combat_handler.combat_active = False

                    else:
                        print "Invalid combat event data:{}".format(event.data)

                # NON-COMBAT TURNS
                elif event.subtype is 'turn_order':
                    if event.data is 'start_phase':
                        peaceful_action_handler.reset_creature_index()
                    if event.data is 'turn_change':
                        peaceful_action_handler.next_creature()
                    if event.data is 'end_phase':
                        if combat_handler.combat_active == True:
                            push_new_user_event('combat', 'start_phase')
                        else:
                            push_new_user_event('turn_order', 'start_phase')

                elif event.subtype is 'menu':
                    main_menu.launch(screen)

                elif event.subtype is 'chat':
                    creatures_ = event.data
                    push_new_user_event('open_dialog', creatures_[0][1])


                elif event.subtype is 'open_dialog':
                    dialog_id = event.data
                    dialog_window_inst.set_text_lines(dialogs.get_dialog(dialog_id))
                    dialog_window_inst.set_title('Hello world.')
                    drawing.render_window(screen, dialog_window_inst, default_font_inited)

            if event.type == pygame.KEYUP:
                if print_keypresses:
                    print event.key
                if event.key == pygame.K_SPACE:
                    hero.intent.type = hero.intent.WAIT
                elif event.key == pygame.K_UP or event.key == pygame.K_KP8:
                    hero.intent.type = hero.intent.MOVE
                    hero.intent.direction = (0, -1)
                elif event.key == pygame.K_DOWN or event.key == pygame.K_KP2:
                    hero.intent.type = hero.intent.MOVE
                    hero.intent.direction = (0, 1)
                elif event.key == pygame.K_LEFT or event.key == pygame.K_KP4:
                    hero.intent.type = hero.intent.MOVE
                    hero.intent.direction = (-1, 0)
                elif event.key == pygame.K_RIGHT or event.key == pygame.K_KP6:
                    hero.intent.type = hero.intent.MOVE
                    hero.intent.direction = (1, 0)
                elif event.key == pygame.K_KP7:
                    hero.intent.type = hero.intent.MOVE
                    hero.intent.direction = (-1, -1)
                elif event.key == pygame.K_KP9:
                    hero.intent.type = hero.intent.MOVE
                    hero.intent.direction = (1, -1)
                elif event.key == pygame.K_KP3:
                    hero.intent.type = hero.intent.MOVE
                    hero.intent.direction = (1, 1)
                elif event.key == pygame.K_KP1:
                    hero.intent.type = hero.intent.MOVE
                    hero.intent.direction = (-1, 1)

                # + : Add enemy
                elif event.key == pygame.K_KP_PLUS:
                    new_creature = creatures.NPC(resource_loader.load_sprite('thug'))
                    npc = add_monster_to_random_position(map_data, new_creature)
                    combat_handler.add_creature(npc)
                # c : Chat
                elif event.key == pygame.K_c:
                    crs = map_data.get_creatures_around_tile(hero.positionOnMap)
                    if crs.__len__() != 0:
                        push_new_user_event('chat', data=crs)
                # i : Inventory
                elif event.key == pygame.K_i:
                    message_log.newline("-----Inventory:-----")
                    for item in hero.inventory.get_items:
                        try:
                            message_log.newline('{}, {}ad'.format(item.name, item.mod_attack))
                        except AttributeError:
                            message_log.newline('{}'.format(item.name))
                # , : Pick item
                elif event.key == pygame.K_COMMA:
                    pick_up_item(hero, map_data)
                # > : Change map
                elif event.key == 60 and pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    push_new_user_event('map_change', data='@')
                # F1 : GodMode
                elif event.key == pygame.K_F1:
                    dev_hero_undying = not dev_hero_undying
                    message_log.newline('God mode: {}'.format(dev_hero_undying))
                # F2: Menu
                elif event.key == pygame.K_F2:
                    push_new_user_event('menu')
                # F3: MapEditor
                elif event.key == pygame.K_F3:
                    map_editor.launch(map_data, screen)
                    screen.fill(colorBlack)
                    dirty_drawing.issue_world_surface_redraw()
                    message_log.set_is_dirty(True)
                # F4: Open chat-window
                elif event.key == pygame.K_F4:
                    push_new_user_event('open-dialog', data=1)
                # Home: Mouse position to caption.
                elif event.key == pygame.K_HOME:
                    devtools.mouse_position_to_caption()
                # TAB : Terminate program
                elif event.key == pygame.K_TAB:
                    sys.exit()

        # Handle combat
        if combat_handler.combat_active and not combat_handler.combat_phase_done:
            combat_handler.handle_turn(hero, message_log, map_data, item_generator, path_finder)
        else:
            # Handle other npc/hero actions.
            if peaceful_action_handler.handle_turn(hero, message_log, map_data, item_generator, path_finder):
                push_new_user_event('turn_order', 'turn_change')

        camera.set_tile_position(hero.positionOnMap)

        dirty_drawing.draw(screen, world_s, camera, floor_s, map_data)

        if message_log.get_is_dirty is True:
            dirty_drawing.draw_message_log(screen, message_log)

        pygame.display.flip()