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

    Join Date
    Feb 2013
    Posts
    1
    Rep Power
    0

    Problem with changing class variable.. text based rpg


    import random
    import time

    class p:
    name = "default"
    hp = 40
    maxhp = 20
    att = 2
    dfn = 0
    lvl = 1
    curexp = 0
    maxexp = 5
    gold = 0
    room = 0

    def lvlup(self):
    xp = 0

    if self.curexp >= self.maxexp:
    xp = self.curexp - self.maxexp
    self.curexp = xp
    self.maxexp = self.maxexp + (self.maxexp/20)
    self.maxhp = self.maxhp + 2
    self.att = self.att + 1
    self.dfn = self.dfn + 1
    # fix player hp to reset after lvl
    print("You have gained a level!")

    def rest(self):
    if self.hp < self.maxhp:
    print("You are resting")
    print("5")
    time.sleep(1)
    print("4")
    time.sleep(1)
    print("3")
    time.sleep(1)
    print("2")
    time.sleep(1)
    print("1")
    time.sleep(1)
    print("POOF! You're at max health!")
    self.hp = self.maxhp
    else:
    print("Silly, " + name + " you're at maximum health!")

    class m:
    name = "default"
    hp = 10
    att = 2
    dfn = 1
    gold = 1
    exp = 5

    def reward(self):
    gold = 0
    exp = 0
    player.gold = + self.gold
    gold = self.gold
    player.curexp = + self.exp
    exp = + self.gold


    class bs:
    turn = random.randint(0,1)

    def battle(self):
    while player.hp and monster.hp > 0:
    gold = str(monster.gold)
    exp = str(monster.exp)
    def playerattack():
    monster.hp = monster.hp - (player.att - monster.dfn)
    mhp = str(monster.hp)
    print("You attack! Enemy has " + mhp + "HP remaining")
    time.sleep(2)
    bs.turn = 1
    def monsterattack():
    player.hp = player.hp - (monster.att - player.dfn)
    php = str(player.hp)
    print("You have been hit.. You have " + php + "HP remaining")
    bs.turn = 0

    if self.turn == 0:
    print("It is your turn what will you do?")
    print ("1) Attack!")
    if input() == "1":
    playerattack()
    else:
    print("That is not a valid option..")
    time.sleep(2)

    if self.turn == 1:
    print("It is your opponent's turn..")
    monsterattack()
    input()

    if monster.hp < 1:
    print("You won!")
    monster.reward()
    print("You gained " + exp + " EXP and " + gold + " GOLD.!")
    player.lvlup()

    if player.hp < 1:
    print("You are dead..")
    time.sleep(10000)

    import time

    print()
    print()
    print()
    print("************************************************************************")
    print(" TextLand")
    print("************************************************************************")
    print()
    print()
    print()
    print("What is your name?")
    name = input()

    print("Hello, " + name)
    print("While playing a new game your soul has been sucked into the digital world.!")
    time.sleep(2)
    print("Now you must find a way to escape or forever be a part of this program.")
    print('''

    You see nothing but darkness. You are afraid and confused..''')
    time.sleep(5)
    print("You hear a voice echo.. It is calling your name. It seems to come from no where yet everywhere.")
    print('''Voice: I am the programmer. I have summoned you to this plane to bug test
    my program''')
    print("You can not leave until you have completed this task.")
    time.sleep(10)
    print('''

    CHAPTER 1''')
    time.sleep(5)
    print('''You wake up in a terribly pixelated room. The walls are gray as are the
    floor and ceiling. You examine the room and see a table with 3 legs. Even though
    one of the legs is visibly shorter the table remains even. On the table is a key.

    You then notice a door across the room.''')
    time.sleep(5)

    choice = 0

    while choice != 1 or choice == 2:
    print("What will you do?")
    print('''1) Take Key
    2) Open Door''')
    choice = input()
    choice = str(choice)

    if choice == str(1):
    print('''You go to snatch up the key but instead you are startled
    by a squeeky voice.''')
    time.sleep(5)
    print('''Voice: \"I am the Prince of the Rats and if you want my
    key then you'll have to.. challenge me! But now not. Pipsqueek. You are unworthy.
    For now. Let's see if you can deal with my minions..\"
    ''')
    time.sleep(10)
    break
    if choice == str(2):
    print("Door locked")

    player = p()
    player.room = 1
    while player.room == 1:
    print("You are in Room 1")
    print("1) Fight Mouse")
    print("2) Fight Rat Prince")
    print("3) Rest")
    while input() == "1":
    print("You engage the Mouse")
    time.sleep(2)
    monster = m()
    monster.name = "Mouse"
    monster.hp = 10
    monster.att = 2
    monster.dfn = 1
    monster.gold = 1
    monster.exp = 5

    battle = bs()
    battle.battle()
    break

    while input() == "2":
    print("You engage the Rat Prince")
    time.sleep(2)
    monster = m()
    monster.name = "Rat"
    monster.hp = 20
    monster.att = 5
    monster.dfn = 2
    monster.gold = 5
    monster.exp = 15

    battle = bs()
    battle.battle()
    break

    while input() == "3":
    player.rest()
    break

    I want to make it so when you use rest() function it changes your player.curexp to player.maxexp. And the battle system seems to work.. but one problem is when the monster gets to 0 it still attacks for one turn. Any suggestions greatly appreciated.!

    Thanks
  2. #2
  3. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    611
    Rep Power
    65
    Please apply code tags. Use [/code] at the end of your code and [code] at the start.
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,897
    Rep Power
    481
    # If choice is 1 the loop exits.
    while choice != 1 or choice == 2:
    # therefore equivalent, but simple
    while choice != 1:


    Your code is for python3. input returns a string.
    choice = input()
    choice = str(choice) # unnecessary statement


    I don't understand why you chose to define functions within the while loop. I suppose their variables take fresh values from the enclosing scope. The usual reason to define classes or functions within a function is for testing, to pass as a callable to another function, or to return a custom-made function. In this program the structure merely confuses me. See python decorators, for example, or functions of the functools module. So there it is, I didn't answer your question because I'm confused.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo