Beispiel #1
0
 def handle_event(self, event):
     from game import Overworld
     State.handle_event(self, event)
     if event.type == pygame.KEYDOWN:
         old_cur_x_index, old_cur_y_index = Overworld.cur_x_index, Overworld.cur_y_index
         if event.key == pygame.K_UP:
             Overworld.cur_y_index -= 1
             self.refresh_screen = True
         elif event.key == pygame.K_DOWN:
             Overworld.cur_y_index += 1
             self.refresh_screen = True
         elif event.key == pygame.K_LEFT:
             Overworld.cur_x_index -= 1
             self.refresh_screen = True
         elif event.key == pygame.K_RIGHT:
             Overworld.cur_x_index += 1
             self.refresh_screen = True
         if self._level.isWall(Overworld.center_x_index(), Overworld.center_y_index()):
             Overworld.cur_x_index = old_cur_x_index
             Overworld.cur_y_index = old_cur_y_index
         elif self._level.isLadder(Overworld.center_x_index(), Overworld.center_y_index()):
             self._level = self._level.load_level(Overworld.center_x_index(), Overworld.center_y_index())
             self.refresh_screen = True
         elif self._level.isMonster(Overworld.center_x_index(), Overworld.center_y_index()):
             from game import set_state
             from battle_state import BattleState
             from battle import Skeleton
             new_state = BattleState(Skeleton())
             set_state(new_state)
             new_state.play_music()
     return
Beispiel #2
0
def generateBattles():
    combinations = itertools.product(xrange(MAX_INFANTRY + 1),
                                     xrange(MAX_ARTILLERY + 1),
                                     xrange(MAX_INFANTRY + 1),
                                     xrange(MAX_ARTILLERY + 1))
    normalBattles = map(lambda args: BattleState.fromUnits(*args),
                        combinations)
    trenchBattles = [b.withTrench() for b in normalBattles]
    return [b for b in normalBattles + trenchBattles if not b.hasEnded()]
def battle_simulation(moves, player_1, player_2):
    """
    Performs a battle simulation, where player1 and player2 take turns using
    moves until someone dies.
    """
    global battle_id
    p1_name, p2_name = player_1.__name__, player_2.__name__
    
    battle_state = BattleState(players = (player_1, player_2))
    turns = 0
    move_usage= Counter(dict([(id(move), 0) for move in moves]))
    absolute_value_record = [battle_state.absolute_value()]
    
    if DEBUG:
        log_battle_data("Battle %d" % (1+battle_id, ))
        log_battle_data("\tTurn 0: %s" % (str(battle_state),))
    
    while battle_state.players_alive() and turns < 30:
        move = player_1.get_move(battle_state)
        battle_state = move.apply(battle_state)
        move_usage[id(move)]+= 1
        turns += 1
        if DEBUG:
            log_battle_data("\t\tMove: %s" % (str(move),))
            log_battle_data("\tTurn %d: %s" % (turns, str(battle_state)))
        
        absolute_value_record.append(battle_state.absolute_value())
        
    if DEBUG:
        log_battle_data("\t" + battle_state.get_winner())
    
    # Calculate Metrics of success
    
    # Was the length good?
    length_good = abs(IDEAL_TURNS - turns) <= IDEAL_TURNS_TOLERANCE
    if not length_good:
        length_success = -50 
    else:
        length_success = 50. * abs(turns - IDEAL_TURNS) / float(IDEAL_TURNS)
    
    # Did someone win?
    if length_good and battle_state.is_one_winner():
        victory_success = 50
    else:
        victory_success = -50
        
    # Were attacks used evenly?
    move_usage = [usage / float(turns) for usage in move_usage.values()]
    #print sum(normalize_move_usage) , MAXIMUM_MOVE_USAGE
    move_usage_success = -50 * numpy.std(move_usage)
    
    # Did the battle progress linearly?
    if abs(IDEAL_TURNS - turns) < IDEAL_TURNS_TOLERANCE:
        def ideal_decay(time):
            return 200 - time * 200. / turns
        distance_from_ideal = sum([abs(ideal_decay(i) - actual) for i, actual in enumerate(absolute_value_record)])
        maximum = 100. * (turns + 1)
        linearity_success = -50 * distance_from_ideal / maximum
    else:
        linearity_success = -50
    
    # Summate the sucesses
    total_success = sum((length_success, victory_success, move_usage_success, linearity_success))
    if DEBUG:
        log_battle_data("\t%d, %d, %d, %d, %d" % (length_success, victory_success, move_usage_success, linearity_success, total_success))
        
    battle_id+= 1
    return total_success, battle_id
 def apply(self, state):
     new_battle_state = BattleState(state)
     new_battle_state.apply(self)
     return new_battle_state
Beispiel #5
0
def getValueMatrix(attArmies, defArmies, mapper, hasTrench):
    values = zeros((len(defArmies),len(attArmies)))
    for i,defender in enumerate(defArmies):
        for j,attacker in enumerate(attArmies):
            values[i,j] = mapper(BattleState(attacker, defender, hasTrench))
    return values
Beispiel #6
0
def setup_loop():
    setup = True

    pygame.mixer.music.load("audio/music/prep_music_placeholder.wav")
    pygame.mixer.music.play(-1)

    cardDict = cards.getCardDict()  # card dictionary to map card IDs onto card names

    playerCards = []  # list that contains the five cards displayed on the screen
    oppOrder = []  # list that contains the predetermined randomized order of the opponent's cards
    for i in range(0, 5):  # randomizes cards (without repetition)
        cardID = random.randint(1, 7)
        while cardID in playerCards:
            cardID = random.randint(1, 7)
        cardID2 = random.randint(1, 7)
        while cardID2 in oppOrder:
            cardID2 = random.randint(1, 7)
        playerCards.append(cardID)
        oppOrder.append(cardID2)

    playerOrder = []  # list that contains the player's selected cards in order

    selections = [0, 0, 0, 0, 0]  # each element will change from zero to display the order in which cards have been selected

    while setup:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        screen.fill((0, 0, 0))

        reset = miscButton("RESET", screen_width * 0.02, screen_height * 0.85, screen_width * 0.13, screen_height * 0.1, (0, 0, 100), (0, 0, 200))
        if reset:  # removes all selections to allow the player to start over
            reset = False
            playerOrder = []
            selections = [0, 0, 0, 0, 0]

        confirm = miscButton("CONFIRM", screen_width * 0.85, screen_height * 0.85, screen_width * 0.13, screen_height * 0.1, (0, 100, 0), (0, 200, 0))
        if confirm and len(playerOrder) == 5:  # exits loop if the right number of selections has been made
            setup = False

        text = pygame.font.Font('SourceSansPro-Regular.ttf', 60)
        TextSurf, TextRect = text_objects("SHOOT PREPARATION", text)
        TextRect.center = ((screen_width / 2), (screen_height * 0.1))
        screen.blit(TextSurf, TextRect)

        text2 = pygame.font.Font('SourceSansPro-Regular.ttf', 35)
        TextSurf2, TextRect2 = text_objects("Select the order of your ability cards!", text2)
        TextRect2.center = ((screen_width / 2), (screen_height * 0.9))
        screen.blit(TextSurf2, TextRect2)

        current = 0.025  # value will increment to space cards out across the screen
        for i in range(0, 5):
            selections[i] = cardButton(selections[i], len(playerOrder), cardDict[playerCards[i]], screen_width * current,
                       screen_height * 0.25, screen_width * 0.15, screen_height * 0.1, (100, 0, 0), (255, 0, 0))
            if selections[i] > 0:  # card has been selected - add it to the player's card order
                if playerCards[i] not in playerOrder:
                    playerOrder.append(playerCards[i])
                buttonImage = pygame.image.load("images/buttons/" + str(selections[i]) + ".png")  # adds the button image to show selection
                screen.blit(buttonImage, (screen_width * current + 8, screen_height * 0.14))

            charImage = pygame.image.load('images/descriptions/' + str(playerCards[i]) + '.png')  # shows description underneath each card
            screen.blit(charImage, (screen_width * current, screen_height * 0.38))

            current = current + 0.2

        pygame.display.update()

    # creates new player objects with their starting attributes (HP and card order)
    player = Player("Player", 10, playerOrder)
    opponent = Player("Lord Mike", 10, oppOrder)
    battle = BattleState(player, opponent)  # starts new battle with the two players

    screen.fill((0, 0, 0))
    pygame.mixer.music.stop()
    select_sound = pygame.mixer.Sound("audio/sfx/card_confirm.wav")
    pygame.mixer.Sound.play(select_sound)
    time.sleep(1)
    pygame.event.clear()
    pygame.mixer.music.load("audio/music/battle_music_placeholder.wav")
    pygame.mixer.music.play(-1)
    rps_loop(battle, player, opponent)