mercredi 14 décembre 2022

How to make this code more compactful and simpler to run?

I fairly started coding no more than 2 weeks now; not even for long, and I was inspired by a thread to make a random back to forth game between a Hero and a Monster where it randomly choose a certain number to decrease the health of either characters and go back and forth until once below 0, the survivor wins. Since I am still very new, I haven't been able to use functions to my advantage to simplify and shorten my code, is there any advice or altercations you would add to the code?

#Randomly generated battle between monster and hero
import random
import time

print('A monster ambushed you hero! Fight back!')
time.sleep(1.2)

hero = 100
monster = 100

chance1 = 1
chance2 = 2
chance3 = 3

while hero and monster > 0:
    print('The hero strikes the monster!')
    number1 = random.randint(1, 3)
    if number1 == 1:
        damage = random.randint(4, 10)
    if number1 == 2:
        damage = random.randint(11, 16)
    if number1 == 3:
        damage = random.randint(17, 25)
    sum1 = monster - damage
    monster = sum1
    time.sleep(1.5)
    print('Monster HP:' + str(sum1))
    if monster <= 1:
        print('Hero Wins!')
        break
    print('The monster attacks the hero!')
    number2 = random.randint(1, 3)
    if number2 == 1:
        damage = random.randint(4, 10)
    if number2 == 2:
        damage = random.randint(11, 16)
    if number2 == 3:
        damage = random.randint(17, 25)
    sum2 = hero - damage
    hero = sum2
    time.sleep(1.5)
    print('Hero HP:' + str(sum2))
    if hero <= 1:
        print('Monster Wins!')
        break

It may seem scrambled but its my first small project and I'd like any feedback so I can expand my knowledge to fluently make similar commands, thank you.




Aucun commentaire:

Enregistrer un commentaire