0
\$\begingroup\$

I am having issues where the variable action_cooldown does not update. I could remove this but then both the player and enemy attack at the same time, which is something you do not want in a turn based game. The variable action_cooldown counts up by adding one each pass and when action_cooldown reaches 90 (action_wait_time) the player can then attack. However in my code the action_cooldown does not count up, instead it adds 1 and stops counting. Im guessing this is something to do with pygame and or the fact that the code is running under a constant while loop. I've played around with while loops and adding in other files and it didn't work. I'm sure it's something simple however I just cannot seem to see it.

Relevant code is added below, however if you need more please let me know and I'll upload the whole project. Thanks!

        #control player actions
    #reset action variables
    attack = False
    potion = False
    #define other variables
    game_over = 0 # 0 = running 1 = player won -1 = player lost
    current_fighter = 1 # 1 = hero's turn 2 = enemy's turn
    total_fighters = 2
    action_cooldown = 0
    action_wait_time = 90
    enemy_list = []
    enemy_list.append(self.enemy)
    #create buttons
    restart_button = Button(self.screen, 330, 120, restart_img, 120, 30)
    #make sure mouse is visible
    pos = pygame.mouse.get_pos()
    for count, enemy in enumerate(enemy_list):
        if self.enemy.rect.collidepoint(pos):
            if clicked == True and self.enemy.alive == True:
                attack = True
                
    #Main Turn Based Logic
    if game_over == 0:
        #player action
        if self.hero.alive == True:
            if current_fighter == 1:
                action_cooldown += 1
                print(action_cooldown)
                if action_cooldown >= action_wait_time:
                    #look for player action
                    #attack
                    if attack == True:
                        self.hero.Attack()
                        current_fighter += 1
                        action_cooldown = 0

        else:
            game_over = -1


        #enemy action
        for count, enemy in enumerate(enemy_list):
            if current_fighter == 2 + count:
                if self.enemy.alive == True:
                        action_cooldown += 1
                        if action_cooldown >= action_wait_time:
                                #check if bandit needs to heal first
                                if (self.enemy.health / self.enemy.max_hp) < 0.5:
                                        #check if the potion would heal the bandit beyond max health
                                        if self.enemy.max_hp - self.enemy.health > potion_hp:
                                                heal_amount = potion_hp
                                        else:
                                                heal_amount = self.enemy.max_hp - self.enemy.health
                                        self.enemy.health += heal_amount
                                        self.enemy.potions -= 1
                                        current_fighter += 1
                                        action_cooldown = 0
                                #attack
                                else:
                                        self.enemy.Attack()
                                        current_fighter += 1
                                        action_cooldown = 0
                else:
                        current_fighter += 1

        #if all fighters have had a turn then reset
        if current_fighter > total_fighters:
            current_fighter = 1
                

    #check if all enemies are dead
    alive_enemies = 0
    if self.enemy.alive == True:
        alive_enemies += 1
    if alive_enemies == 0:
            game_over = 1

    
    #check if game is over
    if game_over != 0:
        if game_over == 1:
            self.screen.blit(victory_img, (250, 50))
        if game_over == -1:
            self.screen.blit(defeat_img, (290, 50))
        if restart_button.draw():
            self.hero.reset()
            for enemy in enemy_list:
                self.enemy.reset()
                current_fighter = 1
                action_cooldown = 0
                game_over = 0
                

    pygame.display.update()
\$\endgroup\$
1
  • \$\begingroup\$ Maybe first use print() (and print(type(...)), print(len(...)), etc.) to see which part of code is executed and what you really have in variables. It is called "print debuging" and it helps to see what code is really doing. \$\endgroup\$
    – furas
    Commented Apr 25, 2023 at 20:20

1 Answer 1

0
\$\begingroup\$

Your comment #Main Turn Based Logic implies this is all in a loop that isn't shown.

If so, this line is also inside the loop:

action_cooldown = 0

Which means it will reset to 0 every time, then add 1...

It's also worth noting that if you move the action_cooldown outside the loop (but not in a class or other encapsulating structure) you may need to use the global keyword to write to it.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .