-1

im new in Python and i'm self learner. Actually im trying to create easy word game. Currently im in stage when im adding monster/player damage and I have three problems which i can't figure out.

  1. I want every room to have a chance for a random monster to appear from "Monster" dictionary. However, after meeting a monster for the first time, the value of his life after the fight is overwritten in the dictionary. This causes the monster to have a negative life when you meet it again. How to fix it?

  2. When i'll find monster in any room, I have set two options "run" and "fight". When someone will type any other command, the loop returns to the draw whether a monster or a chest will appear in the room. I want the return to occur until after the event is randomly selected

  3. I have problem with placing command

if playerLife <= 0:
      print("You died, your body will lay in chambers forever")
      break

I want to end game immidiatly after players dead.

import random

from enum import Enum

playerDamage = 4
playerLife = 100


def find_aprox_value(value):
    lowestValue = 0.9 * value
    highestValue = 1.1 * value
    return random.randint(lowestValue, highestValue)


def weapon_hit(chance):
    if_hit = random.uniform(1, 100)
    if if_hit < chance:
        monsterLife[drawnMonster] = monsterLife[drawnMonster] - playerDamage
        print("You hit a monster and you dealt 4 damage. It has", monsterLife[drawnMonster], " life")
    else:
        print("You missed")


def monster_hit():
    global playerLife
    monsterHit = monsterDamage[drawnMonster]
    print("Monster did", monsterDamage[drawnMonster], "damage")
    playerLife = playerLife - monsterHit
    print("You have ", playerLife, "life")


Event = Enum('Event', ['Chest', 'Monster'])
Chest = Enum('Chest', {'greenChest': 'zieloną skrzynię',
                       'blueChest': 'niebieską skrzynię',
                       'violetChest': 'fioletową skrzynię',
                       'orangeChest': 'pomarańczową skrzynię'
                       })
Monster = Enum('Monster', {'Rat': 'Szczura',
                           'Bat': 'Nietoperza',
                           'GiantSpider': 'Ogromnego Pająka',
                           'Wolf': 'Wilka',
                           })

Color = Enum('Color', ['greenChest', 'blueChest', 'violetChest', 'orangeChest'])
MonsterKind = Enum('MonsterKind', ['Rat', 'Bat', 'GiantSpider', 'Wolf'])

eventDictionary = {

    Event.Chest: 0.4,
    Event.Monster: 0.6
}

eventList = list(eventDictionary.keys())
eventProbability = list(eventDictionary.values())

chestDictionary = {
    Chest.greenChest: 0.5,
    Chest.blueChest: 0.3,
    Chest.violetChest: 0.15,
    Chest.orangeChest: 0.05
}
PremiumChestDictionary = {
    Chest.blueChest: 0.5,
    Chest.violetChest: 0.35,
    Chest.orangeChest: 0.15
}

MonsterDictionary = {
    Monster.Rat: 0.5,
    Monster.Bat: 0.3,
    Monster.GiantSpider: 0.15,
    Monster.Wolf: 0.05
}

chestList = list(chestDictionary.keys())
chestProbability = list(chestDictionary.values())

MonsterList = list(MonsterDictionary.keys())
MonsterProbability = list(MonsterDictionary.values())

PremiumChestList = list(PremiumChestDictionary.keys())
PremiumChestProbability = list(PremiumChestDictionary.values())

colorValue = {
    Chest.greenChest: 1000,
    Chest.blueChest: 4000,
    Chest.violetChest: 9000,
    Chest.orangeChest: 16000
}
monsterLife = {
    Monster.Rat: 5,
    Monster.Bat: 10,
    Monster.GiantSpider: 15,
    Monster.Wolf: 30
}
monsterDamage = {
    Monster.Rat: 3,
    Monster.Bat: 5,
    Monster.GiantSpider: 8,
    Monster.Wolf: 12
}

gameLength = 10

Gold = 0

while gameLength > 0:

    gameAnswer = input("Do you want to move forward? \n")

    if gameAnswer == "yes":
        print("Great, lets see what is inside")
        drawnEvent = random.choices(eventList, eventProbability)[0]

        if drawnEvent == Event.Chest:
            drawnChest = random.choices(chestList, chestProbability)[0]
            goldAcquire = find_aprox_value(colorValue[drawnChest])
            print("You have find ", drawnChest.value, "inside was", goldAcquire, "gold")
            Gold = Gold + goldAcquire
            gameLength = gameLength - 1
        elif drawnEvent == Event.Monster:
            drawnMonster = random.choices(MonsterList, MonsterProbability)[0]
            print("Oh no, you have find", drawnMonster.value, "which has", monsterLife[drawnMonster],
                  "life .If you will defeat him, you will find great treasure.")
            eventAnswer = input(" What is your choice?(fight, run)")
            if eventAnswer == "fight":
                while monsterLife[drawnMonster] > 0:
                    weapon_hit(70)
                    if monsterLife[drawnMonster] > 0:
                        monster_hit()
                        if playerLife <= 0:
                            print("You died, your body will lay in chambers forever")
                            break
                drawnPremiumChest = random.choices(PremiumChestList, PremiumChestProbability)[0]
                goldAcquire = find_aprox_value(colorValue[drawnPremiumChest])
                print("Congratulations, you have defeat a monster, and you found", drawnPremiumChest.value,
                      ", inside was", goldAcquire, " gold")
                Gold = Gold + goldAcquire
                gameLength = gameLength - 1
            elif eventAnswer == "run":
                gameLength = gameLength - 1
                print("you have successfully run")
            else:
                print("Only options is run or fight")

                continue

    else:
        print("Your only options is move forward")
        continue

print("Congratulations you have acquired", Gold, "gold")

As I mentioned at the beginning, I am just starting to learn python and this is my first post on this forum, so please be understanding and thank you for your help in advance.

  • Welcome to Stack Overflow. Yours is not a question, but many! Please, focus on a single issue at a time, there's no problem in asking multiple times. Check [the tour](https://stackoverflow.com/tour) to get familiar with the site! – Ignatius Reilly Aug 31 '22 at 16:41
  • And check [this one](https://stackoverflow.com/questions/189645/how-can-i-break-out-of-multiple-loops) for how to end the program when the player dies. – Ignatius Reilly Aug 31 '22 at 16:42
  • Sure I will, is this thread can stay in this format, or should I create three seperate topics( now two cos you gave me answer for one, thanks !) – Rafał Wierzbicki Aug 31 '22 at 16:46
  • BTW, if you ask again, try to use a [mre]! – Ignatius Reilly Aug 31 '22 at 17:04

1 Answers1

0

There are many solutions to your problem (at least five). Here are the simplest:

  1. The simplest: Instead of break, you can end the program with sys.exit (0) (with code 0 because it's not an error).

  2. More elegant:

# Outer loop:
while gameLength > 0 and playerLife > 0:
    # Inner loop:
    while monsterLife[drawnMonster] > 0 and playerLife > 0:
        # Statement 'if playerLife <= 0' becomes redundant

# Outside the loop:
if playerLife > 0:
    print("Congratulations ...")
else:
    print("You died, ...")

or:

# Outer loop:
while gameLength > 0 and playerLife > 0:
    # Inner loop:
    while monsterLife[drawnMonster] > 0:
        if playerLife <= 0:
            break

# Outside the loop:
if playerLife > 0:
    print("Congratulations ...")
else:
    print("You died, ...")
  1. More advanced: instead of using break statement, an exception can be thrown. The exception must be handled by the try ... catch statement.

BTW: both continue statements are completely unnecessary - these are the last statements of the loop, so they don't change anything about the execution of the loop.

Have fun with Python.