Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Question with while loop placement
#1
I'm trying to make a game that has a "fight" loop that it runs when the user encounters a demon. I pretty much have a basic version done but was wondering how to format a while loop so that the user keeps attacking until they or the enemy runs out of health. When I run the program I don't get any errors, it successfully randomizes critical attacks, regular attacks, and other stuff. If someone could help me understand how to make a while loop that keeps the attacks going. Any help would be appreciated, thanks!
import random

DemonOne = 0
DemonTwo = 1
DemonThree = 2
#DemonOneStats
DemonOneHealth = 25
DemonOnePower = 25
DemonOneLuck = 250
DemonOneLuckDisplayed = DemonOneLuck/10
DemonOneArmour = 10
DemonOneAttack = DemonOnePower/4
DemonOneAttackCalc = (DemonOneLuck/25) - 1
#DemonTwoStats
DemonTwoHealth = 50
DemonTwoPower = 50
DemonTwoLuck = 100
DemonTwoLuckDisplayed = DemonTwoLuck/2
DemonTwoArmour = 25
#DemonThreeStats
DemonThreeHealth = 100
DemonThreePower = 100
DemonThreeLuck = 50
DemonThreeLuckDisplayed = DemonThreeLuck * 2
DemonThreeArmour = 100
#UserStats
BaseHealth = 100
BaseLuck = 100
BaseLuckDisplayed = BaseLuck/2
BasePower = 100
Armour = 0
BaseAttack = BasePower/4
#FirstAttack
FirstAttackCalc = (BaseLuck/25) - 1
def stats():
    print("Health: " + str(BaseHealth), "Luck: " + str(BaseLuckDisplayed), "Power: " + str(BasePower), "Armour: " + str(Armour), "Weapon(s) equipped: " )
def DemonOneStats():
    print("The demon's stats are: Health- "+str(DemonOneHealth),"Luck- "+str(DemonOneLuckDisplayed), "Power- "+str(DemonOnePower), "Armour- " +str(DemonOneArmour))
def DemonTwoStats():
    print("The demon's stats are: Health- "+str(DemonTwoHealth),"Luck- "+str(DemonTwoLuckDisplayed), "Power- "+str(DemonTwoPower), "Armour- " +str(DemonTwoArmour))
def DemonThreeStats():
    print("The demon's stats are: Health- "+str(DemonThreeHealth),"Luck- "+str(DemonThreeLuckDisplayed), "Power- "+str(DemonThreePower), "Armour- " +str(DemonThreeArmour))

DemonChooser = random.randint(0,2)
FirstAttack = random.randint(0,FirstAttackCalc)
DemonOneCritical = random.randint(0,DemonOneAttackCalc)


#DemonOneFightLoop
if DemonChooser == 0:
    DemonOneStats()
    #while DemonOneHealth <= 0 or BaseHealth <= 0 is True:
    if FirstAttack != 0:
            DemonOneHealth = DemonOneHealth - BaseAttack
            print("You attack the demon first:")
            DemonOneStats()
            if FirstAttack == 2:
                DemonOneHealth = DemonOneHealth - BaseAttack
                print("You caught the enemy off guard! Critical hit!")
                DemonOneStats()
            elif FirstAttack == 0:
                BaseHealth = BaseHealth - DemonOneAttack
                print("You were unlucky and the demon attacked you first:")
                stats()
            if DemonOneCritical == 0:
                BaseHealth = BaseHealth - DemonOneAttack
                print("You were caught off guard by the demon! Critical hit!")
                stats()
elif DemonChooser == 1:
    DemonTwoStats()
elif DemonChooser == 2:
    DemonThreeStats()
Quote
#2
You could make it a bit easier by using a dictionary for demons like:
demons = {
    0: {
        'name': 'DemonOne',
        'health': 25,
        'power': 25,
        'luck': 250,
        'luck_display': 0,
        'armour': 10,
        'attack': 0,
        'attack_calc': 0
    },
    1: {
        'name': 'DemonTwo',
        'health': 25,
        'power': 25,
        'luck': 250,
        'luck_display': 0,
        'armour': 10,
        'attack': 0,
        'attack_calc': 0
    },
    2: {
        'name': 'DemonThree',
        'health': 25,
        'power': 25,
        'luck': 250,
        'luck_display': 0,
        'armour': 10,
        'attack': 0,
        'attack_calc': 0
    }
}

def initialize_demons():
    demons[0]['luck_display'] = demons[0]['luck'] / 10
    demons[0]['attack'] = demons[0]['power'] / 4
    demons[0]['attack_calc'] =  (demons[0]['luck'] / 25) - 1
    demons[1]['luck_display'] = demons[1]['luck'] / 10
    demons[1]['attack'] = demons[1]['power'] / 4
    demons[1]['attack_calc'] =  (demons[1]['luck'] / 25) - 1
    demons[2]['luck_display'] = demons[2]['luck'] / 10
    demons[2]['attack'] = demons[2]['power'] / 4
    demons[2]['attack_calc'] =  (demons[2]['luck'] / 25) - 1

initialize_demons()

# Example usage:
# To get power:
def get_power(demon_no):
    return demons[demon_no]['power']

def get_name(demon_no):
    return demons[demon_no]['name']

def get_attack(demon_no):
    return demons[demon_no]['attack']

def display_some_values(demon_no):
    print('{}: power: {}, attack: {}'.format(get_name(demon_no), get_power(demon_no), get_attack(demon_no)))

# for DemonTwo
display_some_values(1)
When run, results:
Output:
DemonTwo: power: 25, attack: 6.25
You could take this a step further by saving the initial dictionary values as a json file
Quote
#3
Thank you for this advice, I really appreciate it. As someone who is new to Python and these forums, tips, such as the one you gave me, are really helpful and make me a better programmer. In addition to the dictionary, do you have any suggestions on how to format a while loop so that the attacks keep on happening until someone gets 0 health? Any help with this while loop issue would be greatly appreciated. Thanks
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  question about for loop Than999 5 292 Jun-09-2020, 02:16 PM
Last Post: Emekadavid
  Data placement nboogerz 1 196 May-16-2020, 11:20 AM
Last Post: snippsat
  Automated Bet placement redmercury 2 652 Dec-04-2019, 10:53 AM
Last Post: redmercury
  Question about for loop not creating an infinite loop. FWendeburg 1 743 Feb-03-2019, 08:45 PM
Last Post: ichabod801
  Loop Condition Question malonn 6 946 Aug-01-2018, 01:56 PM
Last Post: malonn
  Beginner Loop question BigDisAok 5 1,160 Jul-24-2018, 02:04 PM
Last Post: BigDisAok
  Scoping Question: If else in for loop not evaluating i as expected datasundae 3 978 May-25-2018, 06:43 PM
Last Post: datasundae
  For loop question(skipping) jure98 1 832 Mar-31-2018, 01:54 PM
Last Post: Larz60+
  For loop question peejj 1 989 Mar-18-2018, 10:36 PM
Last Post: stranac
  Question about an infinite loop Robo_Pi 6 1,383 Mar-05-2018, 05:46 AM
Last Post: wavic

Forum Jump:


Users browsing this thread: 1 Guest(s)