Little Python Combat Game

Asked

Viewed 160 times

8

I’m a beginner in programming and I’m trying to make a simple little game to apply my knowledge about conditionals, repetition and the Random module, but I’m having some semantic problems.

My goal is to attack the monster with a random number between 1 and 6, until the HP reduce it to a value equal to or less than zero and in doing so indicate that the monster has been defeated, but I am having basically two problems:

  • I want the player to have the option of attacking or not and present a message in case he decides not to attack.
  • I want to present a victory message, which appears only when the player beats the monster.

Here’s the code:

from random import randint
print('Um monstro apareceu !')
hp=10
a=randint(1,6)
while hp-a>0:
  hp=hp-a
  d=input('Deseja atacar o monstro (S/N) ? ')
  if d=='S':
    print(hp-a,)
  if hp<=0:
    print('Você venceu o monstro !')
if d=='N':
  print('Você fugiu !')

The problem is that the victory message is not being presented and the message indicating that the player does not want to attack is always appearing. Could someone help me?

2 answers

9


Basically you have 3 major problems:

  1. a=randint(1,6)

This Line only happens once before the while cycle, so the value of a will not change, it seems to me that it should be within the cycle and it is the first thing to do when the user responds 'S'.

  1. You’re going too far a to hp, which may lead to false data being displayed, and logic being bugged:

while hp-a>0:
...
hp=hp-a
...
print(hp-a)

  1. As to "victory message is not being presented" has also to do with point 2 when the while hp-a>0 he leaves the loop without hp=hp-a, and so you don’t even get into if hp<=0: when really hp <= 0.

That said, you can simplify it to:

from random import randint

hp = 10
while hp > 0:
    d = input('Deseja atacar o monstro (S/N) ? ')
    if(d == 'S'):
        a = randint(1,6)
        hp = hp - a # novo valor do hp
        print('ataque de {} pontos, {} de hp restante do monstro'.format(a, hp)) # imprimir hp restante
    else:
        print('Você fugiu !')
        break
else: # se saiu do ciclo while sem haver break e porque o monstro ficou com hp <= 0
    print('Você venceu o monstro !')

DEMONSTRATION

  • Consider adding an explanation of why the OP application does not work, since it is using application to apply knowledge of python cycles and conditions. Other than that good answer +1

  • 1

    @lazyFox, I was just adding that, edited reply. I am leaving the computer now, in case you see that you are missing/improving you are welcome to edit my answer. Thank you

0

I added the monster’s turn to the game

from random import randint

hpm = 10 #Hp monstro
hpg = 10 # Hp guerreiro
while hpm > 0 and hpg > 0:
    d = input('Deseja atacar o monstro (S/N) ? ')
    if(d == 'S' or 's'):
        a = randint(1,6)
        hpm = hpm - a # novo valor do hp_mosntro
        print('ataque de {} pontos, {} de hp restante do monstro'.format(a, hpm)) # 
imprimir hp restante
        #Vez do monstro
        if (hpm <= 0):
            break
        b = randint(1,6)
        hpg = hpg - b # novo valor do hp_guerreiro
        print(f'O monstro te acertou com {b} pontos, você ainda tem {hpg} pontos de vida')

    else:
        print('Você fugiu !')
        break
if (hpm <= 0):
    print('Você venceu !')
else:
    print("Você foi derrotado!!!")

Browser other questions tagged

You are not signed in. Login or sign up in order to post.