Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 2nd, 2013, 07:11 PM
noo123454321 noo123454321 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 1 noo123454321 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 m 25 sec
Reputation 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

Reply With Quote
  #2  
Old February 3rd, 2013, 11:01 AM
Dietrich's Avatar
Dietrich Dietrich is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 499 Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 4 Days 3 h 19 m 2 sec
Reputation Power: 63
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

Reply With Quote
  #3  
Old February 3rd, 2013, 03:15 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,458 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 4 Days 6 h 26 m 43 sec
Reputation Power: 403
# 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!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Problem with changing class variable.. text based rpg

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap