0

When looking at lines 54-59, there are two lines of code that state:

hero_health -= ENEMY_DAMAGE_AMOUNT
enemy_health -= HERO_DAMAGE_AMOUNT[damage_choice]

Whenever I add these lines of code to the if statement, the program just completely skips over this if statement and always outputs to the else statement. There aren't any syntax errors but whenever I remove these lines of code, the program runs both if conditions just fine. Also, when they are added, the code will skip to the else statement and for some reason only run one of the two print procedures. I'm not sure about what is causing this and would like a little feedback into what I might be doing wrong. Thank you!

import random
HERO_DAMAGE_AMOUNT = [15, 16, 17, 18, 19, 20]
ENEMY_DAMAGE_AMOUNT = 15

hero_health = 100
enemy_health = 100
enemy_choice = 0
hero_choice = 0

#------------------------------------------------------------------------------------------------------------------------------------

moon_background = Image("https://i.imgur.com/HWFNLRm.jpg")
moon_background.set_size(472.25, 480)
moon_background.set_position(0, 0)
add(moon_background)

hero_image = Image("https://images-wixmp-ed30a86b8c4ca887773594c2.wixmp.com/f/a8baa77f-c488-4769-bb75-a8a228144b41/debw2vq-26be5f8e-e91e-4009-b8f8-a56114f7efbe.png?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1cm46YXBwOiIsImlzcyI6InVybjphcHA6Iiwib2JqIjpbW3sicGF0aCI6IlwvZlwvYThiYWE3N2YtYzQ4OC00NzY5LWJiNzUtYThhMjI4MTQ0YjQxXC9kZWJ3MnZxLTI2YmU1ZjhlLWU5MWUtNDAwOS1iOGY4LWE1NjExNGY3ZWZiZS5wbmcifV1dLCJhdWQiOlsidXJuOnNlcnZpY2U6ZmlsZS5kb3dubG9hZCJdfQ.2s7I8FsHnWciBzXtHubWMRVKaV4l4nyQumfc2v3PAs4")
hero_image.set_size(150, 150)
hero_image.set_position(get_width()/32, get_height()/3)
add(hero_image)

enemy_image = Image("https://i5.walmartimages.com/asr/ee3dd5b3-301e-4728-8457-d1e9905a6318_1.e358b4a32891c23b850a910b35d444fd.png")
enemy_image.set_size(200, 200)
enemy_image.set_position(get_width()/2, get_height()/3.65)
add(enemy_image)

block_image = Image("https://r7y4k4n7.stackpathcdn.com/3134-large_default/captain-america-shield-marvel-silver-coin-1-fiji-2019.jpg")
block_image.set_size(75, 75)
block_image.set_position(get_width()/12, get_height()/1.27)
add(block_image)

attack_image = Image("https://img.icons8.com/emoji/452/crossed-swards.png")
attack_image.set_size(75, 75)
attack_image.set_position(get_width()/1.3, get_height()/1.258)
add(attack_image)

#------------------------------------------------------------------------------------------------------------------------------------

def attack_and_block(x,y):
    if x <= 307.6923076923077 + 75 and x >= 307.6923076923077 and y <= 381.55802861685214 + 75 and y >= 381.55802861685214:
        enemy_choice = random.randint(1, 2)
        hero_choice = 1
        if hero_choice == enemy_choice:
            damage_choice = random.randint(0,5)
            hero_health -= ENEMY_DAMAGE_AMOUNT
            enemy_health -= int(HERO_DAMAGE_AMOUNT[damage_choice])
            print("You both attack. You do " + str(HERO_DAMAGE_AMOUNT[damage_choice]) + " damage. The enemy does " + str(ENEMY_DAMAGE_AMOUNT) + " damage.")
            print("Your health is now: " + str(hero_health) + ". The enemy's health is now: " + str(enemy_health) + ".")
        else:
            print("You attacked but the enemy blocked it.")
            print("Your health is still: " + str(hero_health) + ". The enemy's health is still: " + str(enemy_health) + ".")
    elif x <= 33.333333333333335 + 75 and x >= 33.333333333333335 and y <= 369.2307692307692 + 75 and y >= 369.2307692307692:
        enemy_choice = random.randint(1, 2)
        hero_choice = 2
        if hero_choice == enemy_choice:
            print("You both blocked, nothing happened.")
            print("Your health is still: " + str(hero_health) + ". The enemy's health is still: " + str(enemy_health) + ".")
        else:
            print("The enemy attacked, but you blocked it.")
            print("Your health is still: " + str(hero_health) + ". The enemy's health is still: " + str(enemy_health) + ".")

add_mouse_click_handler(attack_and_block)
Roshin Raphel
  • 2,612
  • 4
  • 22
  • 40
  • 1
    It seems that whether the if-block or the else-block is evaluated depends on a random number 'enemy_choice'. Are you certain that the changes you saw when removing code blocks didn't depend on different random numbers being produced in the various runs? – Tarje Bargheer Apr 28 '21 at 13:50
  • @TarjeBargheer Well the "enemy_choice" determines whether to use the if or else statement. But whenever I add the two lines, it always chooses the else statement no matter what. But those two lines have nothing to do with the enemy_choice so theoretically everything should work fine but for some reason adding the two lines messes up the if statement in some way. – matthewgerraputa Apr 28 '21 at 15:19

0 Answers0