#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2005
    Location
    canada, B.C.
    Posts
    12
    Rep Power
    0

    help me with rpg


    i have only been using python since my second semester(grade 9, info tech class) this is my first post, and not my last. i dont know how to make my variables work that need to be global, because i have to add and subtract them alot. there is probably a lot of better ways to do everything i have here but dont laugh! i tried hard, if you have any sugestions please tell me, i need to make the global variables subtract and add. i dont know how to make this post fancy and have the scroll bar thing where u can put ur programs so ill just put it here.

    it might all be newbie code, but plz help lol

    here is the main part with globals please help, by the way, this program is much longer lol... things might be on the next line because it is too big, i dont know how to format these posts to make it another part with a scroll bar. yet.. please look at this, dont even have to see it all, just tell me how to fix globals and subtract em

    Code:
     global potions
    global strength
    global defence
    global hp
    global health
    global level
    global accuracy
    global gc
    global exp
    # global variables?how do i use them, and add and subtract them?
    potions = 1
    strength = 2
    defence = 2
    hp = 15
    health = 15
    level = 1
    accuaracy = 2
    gc = 100
    exp = 0
    
    .... a bit later on
    
    # all the enemies to be called from other areas
    def goblin(): 
        battle(goblin, 4, 8, 6, 7, 2, 4, 1, 4, 2, 5, 100, 150, 250)
        
    def develkin():
        battle(develkin, 5, 10, 5, 6, 2, 5, 1, 4, 1, 4, 150, 250, 300)
    
    def evilCrow():
        battle(evil_crow, 7, 12, 4, 5, 3, 6, 1, 3, 0, 3, 200, 300, 400)
    
    def darkendShaman():
        battle(darkend_shaman, 8, 12, 4, 5, 3, 7, 1, 3, 0, 3, 250, 300, 450)
    
    def zombie():
        battle(zombie, 8, 13, 4, 5, 3, 6, 1, 4, -1, 3, 250, 300, 450)
    
    # the battle plan itself, many variables!
    # enemy,hp(a-b),enemy chance to hit(c-d),hit on you(e-f),potions find(g-h),change to hit enemy(i,j),gc find(x,y),exp(z).
    def battle(enemy, a, b, c, d, e, f, g, h, i, j, x, y, z,):
        enemyHp = randrange(a,b)
        print "a", enemy , "attacks"
        while enemyHp > 0:
            o = input("print 1 to stab, 2 to charge and 3 to use a potion, heals 10 hp,", potions, "left ")
            if o is 1:
                print health, "/", hp, "hp,", potions, "potions remaining"
                youHit = randrange(i,j) # your chance to hit + accuracy example, (2-4) + 2 over 5 is hit
                youHit = youHit + accuracy
                if youHit > 5:
                    attack = attack + level + strength + randrange(0,3) 
                    enemyHp = enemyHp - attack
                    print "You hit the", enemy, "for", attack, "hp,", enemyHp, "(remaining)"
                if youHit <= 5:
                    print "You missed the", enemy
                                
            elif o is 2:
                print health, "/", hp, "hp,", potions, "potions remaining"
                youHit = randrange(i,j) # your chance to hit + accuracy example, (2-4) + 2 over 5 is hit
                youHit = youHit + accuracy
                if youHit > 5: 
                    attack = attack + level + strength + randrange(3,5)
                    health = health - 1
                    enemyHp = enemyHp - attack
                    print "You hit the", enemy, "for", attack, "hp,", enemyHp, "(remaining)"
                    print "You hurt yourself for 1 hp"
                    print health, "hp,", potions, "potions remaining"
                if youHit <= 5:
                    print "You missed the", enemy
    
            elif o is 3:
                print health, "/", hp, "hp,", potions, "potions remaining"
                if potions < 1:
                    print "you dont have any potions remaining"
                else:
                    potions = potions - 1
                    health = health + 10
                while health > hp:
                    health = health - 1 # health is not the (denominator) of you health, hp is, example (20=health/30=hp), so health can not exceed it
                print "you have", potions, "potions left and", health, "/", hp, "hp left"
            elif o > 3:
                print "you have missed your turn for entering invalid numbers"
    
     
            chanceToHit = randrange(c,d) # (c-d) - defence, if over 5, they hit example, 6-8 - defence
            chanceToHit = chanceToHit - defence
            if chanceToHit > 5:
                enemyAttack = randrange(e,f) # how much damage if they hit, exampele, 3-7 damage
                print "The", enemy, "hit you for", enemyAttack, "hp"
                health = health - enemyAttack
                print "You have", health, "/", hp, "hp left"
            elif chanceToHit <= 5:
                print "The", enemy, "missed you"
            if enemyAttack is 1:
                print "The", enemy, "hit you for 1 hp"
                health = health - 1
                print "You have", health, "/", hp, "hp left"
        
        find = randrange(x,y)
        potionFind = randrange(g,h) # usualy (1-3), = 1/3 chance if u need to hit 1.
        if potionFind is 1:
            potions = potions + 1
        else:
            print ""
        print "you defeated the", enemy, "and earned", exp, "exp and you find", find ,"gc and", potionFind, "potions"
        exp = exp + z
        gc = gc + find
        print "you now have", exp ,"exp and", gc ,"gc"
        if exp >= 10000:  # level 6
            level()
        elif exp >= 8000: # level 5
            level()
        elif exp >= 5000: # level 4
            level()
        elif exp >= 3500: # level 3
            level()
        elif exp >= 2000: # level 2
            level()
        elif exp >= 1000: # level 1
            level()
        print "good job, now continue on"
        if level is 1:
            caveFight()
        elif level is 2:
            caveFight2()
        elif level is 3:
            if exp < 3851: # first battle out of the cave
                freshAir()
            else:
                wasteLandFight()
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2005
    Location
    canada, B.C.
    Posts
    12
    Rep Power
    0
    o, and i did import say erlier, import random from randrange by the way.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2005
    Location
    canada, B.C.
    Posts
    12
    Rep Power
    0
    wow crap, i just realized, when i coppied it, all the indents went away so everything is on the same line, even for the if and then statements, what ever, i hope its not too hard to read, please ignore missing spaces and such because i dont know how to make posts properly D:
  6. #4
  7. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    You should edit your original post and use code tags. If you cannot find the code tags button. In the front of your code (right before the first character you typed) add in, ["CODE"]. And at the end of your code (right after the last character you typed) add in ["/CODE"]. The code tags should not have quotes inside them. These are the basic html code tags. They will give you the scrollbar feature in the code you posted. Also, I would suggest giving your variables more valuable names so you can better know what you are looking at. Like, user_hp, instead of a (just as an example). Another idea other than using globals, is to use a class. And store all of your functions inside of there, this way you can use any of your variables no matter what part of the class you are in. And you won't need globals. If you don't know how to use a class instance, you can learn about them at the following link:
    http://python.org/doc/2.3.5/tut/node11.html
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2005
    Location
    canada, B.C.
    Posts
    12
    Rep Power
    0
    i dont really get classes... but i have many other functions that use those same variables, so maybe if you could teach me how to make it work with globals that would be good. if you wona see the whole thing i can put it up

IMN logo majestic logo threadwatch logo seochat tools logo