So I made a combat system, and for the life of me i cannot figure out how to make it start automatically. It does run, but it ignores the function at the beginning and doesn't run it. There is a similar text adventure I looked at as an example for my own project, and it runs just fine, and it is fundamentally the same as mine in the beginning. Is there a way to call functions outside of a function not in IDE?

Recommended Answers

All 6 Replies

What do you mean by call functions outside of a function not in an IDE? And can you post your code?

Oh my god..... Wow... I just realized my mistake. I was trying to put a call in front of the defined function, so it didn't know where to go. None of my code was activated prior to a function call, so scratch all of what I just said. Sorry for the inconvenience. XD I feel dumb. I'll still post the code since I probably have plenty of other bugs I haven't worked out.

import random
import time
import os
#hp globals
global playerhp
global monsterhp
#loot globals
global gold
global goldgain
#exp globals
global experience
global experiencegain
global level
#monster globals
global monstertype
#misc globals
global turn
global heal
heal = random.randrange(5)+2
turn = 0
monstertype = random.randrange(3)+1
gold = 0
goldgain = random.randrange(15)+5
level = 1
experience = 0
experiencegain = random.randrange(10)+5
playerhp = 20
monsterhp = random.randrange(6) +12


def menu():
    print "This is the menu"
    print "================="
    print "1. Combat"
    print "2. Exit"
    print "================="
    menu_prompt()

def menu_prompt():
    global turn
    turn = 0

    m_i = input("Select a number: ")
    if m_i == 1:
        start()
    elif m_i == 2:
        end()
    else:
        print "Invalid command!"
        menu()
        

def start():
    global turn
    global monsterhp
    global monstertype
    turn = 0
    monstertype = random.randrange(3)+1
    monsterhp = random.randrange(6) + 10 + monstertype * 2
    print "Welcome to Simon's RPG Combat Protocol Test!"
    print "You level is ",level,"."
    print "You have ",experience," xp out of 30."
    print "You have ",playerhp," health left."
    print "Please select an option by number:"
    print "============================================"
    print "1. Start"
    print "2. Exit"
    print "3. Menu"
    start_prompt()

def start_prompt():
    sp_i = input("Input Required: ")
    if sp_i == 2:
        print "Goodbye"
        exit
    elif sp_i == 1:
        phase1()
    elif sp_i == "exit":
        exit
    elif sp_i == 3:
        menu()
    else:
        print "Invalid Option"
        start()
        
def phase1():
    os.system("cls")
    global monstertype
    print "A monster comes out of the bushes..."
    time.sleep(1)
    print "The monster is..."
    time.sleep(1)
    if monstertype == 1:
        print "A goblin!"
    elif monstertype == 2:
        print "A troll!"
    elif monstertype == 3:
        print "An Ogre!"
    else:
        print "Error! Monster Type Value Invalid! Restarting in 3 seconds."
        time.sleep(3)
        start()
    phase2()

def phase2():
    os.system("cls")
    global playerhp
    global monsterhp
    global turn

    if monstertype == 1:
        mtype = "goblin"
    elif monstertype == 2:
        mtype = "troll"
    elif monstertype == 3:
        mtype = "ogre"
    else:
        print "ERROR! Monster type variable invalid!"


    if turn == 0:
        print "Player action phase! You face a fearsome",mtype
        print "Your health: ",playerhp
        print "Enemy health: ",monsterhp
        print "1. Attack"
        print "2. Heal"
        print "3. Runaway/exit"
        turn = turn + 1
        playeract()

    if turn == 1:
        monsteract()

def playeract():
    p_act = input("Choose an action: ")
    if p_act == 1:
        player_attack()
    elif p_act == 2:
        player_heal()
    elif p_act == 3:
        exit()
    else:
        print "That command isn't valid!"
        playeract()

def player_heal():
    os.system("cls")
    global heal
    global playerhp
    
    heal = random.randrange(8)+4 
    
    print "You skip your attack phase to take time to heal up!"
    time.sleep(1)
    print "You gain",heal,"health!"
    time.sleep(1)
    playerhp = playerhp + heal
    turn = 1
    phase2()
    
def player_attack():
    os.system("cls")
    global level
    global monstertype
    global monsterhp
    pdamage = random.randrange(6)+3+(2*level)
    if monstertype == 1:
        mtype = "goblin"
    elif monstertype == 2:
        mtype = "troll"
    elif monstertype == 3:
        mtype = "ogre"
    else:
        print "ERROR! Monster type variable invalid!"

    print "You attack!"
    time.sleep(1)
    print "You deal", pdamage, "damage to the", mtype, "!"
    monsterhp = monsterhp - pdamage
    time.sleep(1)
    monsterdeadcheck()

def monsteract():
    os.system("cls")
    global turn
    global monstertype
    global playerhp
    monsterdamage = random.randrange(2*monstertype)+ monstertype + 1
    
    if monstertype == 1:
        mtype = "goblin"
    elif monstertype == 2:
        mtype = "troll"
    elif monstertype == 3:
        mtype = "ogre"
    else:
        print "ERROR! Monster type variable invalid!"

    print "Monster attack phase!"
    time.sleep(1)
    print "The fearsome ",mtype," inflicts ",monsterdamage," damage!"
    time.sleep(1)
    print "A grevious wound indeed, but the battle rages on!"
    playerhp = playerhp - monsterdamage
    time.sleep(1)
    turn = 0
    playerdeadcheck()


def monsterdeadcheck():
    global monsterhp
    if monsterhp <= 0:
        playervictory()
    else:
        phase2()

def playerdeadcheck():
    global playerhp
    if playerhp <= 0:
        playerdefeat()
    else:
        phase2()

def playervictory():
    os.system("cls")
    global monstertype
    global gold
    global goldgain
    global experiencegain
    global experience
    global experienceleft

    experiencegain = random.randrange(15)+5 *monstertype
    goldgain = random.randrange(15)+5*monstertype

    if monstertype == 1:
        mtype = "goblin"
    elif monstertype == 2:
        mtype = "troll"
    elif monstertype == 3:
        mtype = "ogre"
    else:
        print "ERROR! Monster type variable invalid!"

    print "Congratulations! You have slain the foul",mtype;"!"
    time.sleep(1)
    print "You have found a hard earned",goldgain,"gold!"
    time.sleep(1)
    print "You have also learned well from your battle,"
    print "and have earned",experiencegain,"experience points!"
    experience = experience + experiencegain
    gold=gold+goldgain
    turn = 0
    time.sleep(5)
    levelupcheck()

def playerdefeat():
    os.system("cls")
    print "A great travesty indeed...."
    time.sleep(3)
    print "Another noble hero eats the dirt..."
    time.sleep(2)
    print "GAME OVER"
    time.sleep(1)
    exit

def levelupcheck():
    os.system("cls")
    global experience
    global level

    if experience >= 30:
        level = level + 1
        playerhp = 20
        experience = 0
        print "You have gained a level! Congratulations!"
        print "You feel new strength flowing through you..."
        time.sleep(1)
        print "Damage increase!"
        time.sleep(1)
        print "Full heal!"
        time.sleep(1)
        start()
    else:
        start()

def end():
    print "Goodbye!"
    exit

menu()

It works. Just a hint, you do not need to repeat the statements of the global variables in each function.

I enjoyed playing your RPG.

I also enjoyed this and I'm making my own as well! Thanks for the idea!

Oh....Well then....

Thanks! Should make my code a bit shorter.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.