Simple Python gain experience system

Asked

Viewed 56 times

-2

I am having some doubts, I am creating an RPG by commands, simple thing to put in practice what I am learning. The truth is that I caught in a situation, I want to create a command in which the Player chooses the "Hunt" function, from there is generated a random number (between 1 and 100) that will define how much XP he won in the hunt, then I wanted to add this random number to the player experience, adding every time he hunts, well, I got some results and several problems too:

    import random, time

level = 1
n_level = 50

exp_loot = random.randint(1,100)
exp = 0

while exp >= n_level:
    level += 1
    n_level = round(n_level * 2)

def separador():
    print('-' * 50)

def hunt():
    exp_loot = random.randint(1,100)
    return exp_loot

def stats():
    separador()
    print('Level: {}'.format(level))
    print('EXP: {}/{}'.format(exp, n_level))
    separador()


while True:
    player = int(input(''))

    if player == 0:
        print('Voce está caçando..')
        exp += hunt()
        time.sleep(1)
        print('Você ganhou {}xp'.format(hunt()))

    elif player == 1:
        stats()

When the Player enters the number 1, it shows the status (Level and XP) When you enter the number 0 he hunts

But the sum of the random Xp is not correct, someone can help me?

1 answer

0

The problem is that when you show the amount of XP wins on the hunt you call the hunt() getting a new value. You must create a variable to store the earned value and then add the player’s total XP to the earned XP of the hunt. See how was the code below:

import random, time

level = 1
n_level = 50

exp_loot = random.randint(1,100)
exp = 0

while exp >= n_level:
    level += 1
    n_level = round(n_level * 2)

def separador():
    print('-' * 50)

def hunt():
    exp_loot = random.randint(1,100)
    return exp_loot

def stats():
    global exp
    separador()
    print('Level: {}'.format(level))
    print('EXP: {}/{}'.format(exp, n_level))
    separador()


while True:
    player = int(input(''))

    if player == 0:
        print('Você está caçando...')

        # Obtém o XP ganho da caça
        exp_hunt = hunt()

        # Soma o XP total com o da caça
        exp += exp_hunt
        time.sleep(1)

        # Mostra o XP ganho da caça que está na variável "exp_hunt"
        print('Você ganhou {}xp'.format(exp_hunt))

    elif player == 1:
        stats()
  • Man, that’s exactly what I needed, you’re an angel, thank you very much

  • Arigatou Gozaimasu <3

Browser other questions tagged

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