| | |
Python Game: Norbert Quest
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
It's not working (the thing that shows your weapons and armor when you enter the shop). I need some help because I don't know how to correctly select the right weapon you have out of the string of weapons / armors. Here it is so far:
Also the Government Ranking thing is gonna have it's own shop (I forgot whether I said this in my previous Government related post). I need some [not code involved] help coming up with the amount of percentages of xp / money increase you'll get with each of the (I forgot exactly how many) ranks in the Government (and actually it should be Millitary but Government sounds better.)
Python Syntax (Toggle Plain Text)
import random class NorbertQuest(object): def __init__(self): self.weapon = 0 self.armor = 0 self.level = 1 self.m = random.randrange(50)+1+self.level*5 self.cost = 0 self.xp = 0 self.health = 150 self.ehealth = 100 self.money = 100 self.tnls = [100+self.level*50] self.wepORshield = 0 self.bankbalance = 0 self.withdraw = 0 self.deposit = 0 self.atk = 0 self.shield = 0 self.monster = 0 self.fight = 0 def Shop(self): self.weapons[1:"Steel Knuckles",2:"Knife",3:"Sword",4:"Gun",5:"Rocket Launcher"] self.armors[1:"Leather Jacket",2:"Padded Sweater",3:"Iron Arm-shield",4:"Bullet-Proof Jacket",5:"Bomber Squad Armor Plated Suit"] print \ """ What'll it be? Weapons: 1 - Steel Knuckles 50$ 2 - Knife 75$ 3 - Sword 300$ 4 - Gun 800$ 5 - Rocket Launcher 2500$ Armor: 1 - Leather Jacket 50$ 2 - Padded Sweater 75$ 3 - Iron Arm-shield 300$ 4 - Bullet-Proof Jacket 800$ 5 - Bomber Squad Armor Plated Suit Type exit to quit. """ self.wepORshield = raw_input("Do you want weapons or armor?") if self.wepORshield == "weapons": self.atk = input("Which weapon do you want?") if self.atk == 1: self.cost = 50 if self.money >= self.cost: print"You have bought",self.weapons[self.atk] self.money = self.money - self.cost self.weapon = 1 self.mode = "restart" elif self.money < self.cost: print"You don't have enough gold to buy this." raw_input("Press [enter] to exit.") self.mode = "restart" elif self.atk == 2: self.cost = 75 if self.money >= self.cost: print"You have bought",self.weapons[self.atk] self.money = self.money - self.cost self.weapon = 2 self.mode = "restart" elif self.money < self.cost: print"You don't have enough gold to buy this." raw_input("Press [enter] to exit.") self.mode = "restart" elif self.atk == 3: self.cost = 300 if self.money >= self.cost: print"You have bought",self.weapons[self.atk] self.money = self.money - self.cost self.weapon = 3 self.mode = "restart" elif self.money < self.cost: print"You don't have enough gold to buy this." raw_input("Press [enter] to exit.") self.mode = "restart" elif self.atk == 4: self.cost = 800 if self.money >= self.cost: print"You have bought",self.weapons[self.atk] self.money = self.money - self.cost self.weapon = 4 self.mode = "restart" elif self.money < self.cost: print"You don't have enough gold to buy this." raw_input("Press [enter] to exit.") self.mode = "restart" if self.atk == 5: self.cost = 2500 if self.money >= self.cost: print"You have bought",self.weapons[self.atk] self.money = self.money - self.cost self.weapon = 5 self.mode = "restart" elif self.money < self.cost: print"You don't have enough gold to buy this." raw_input("Press [enter] to exit.") self.mode = "restart" elif wepORshield == "shield": if self.shield == 1: self.cost = 50 if self.money >= self.cost: print"You have bought",self.armors[self.shield] self.money = self.money - self.cost self.armor = 1 self.mode = "restart" elif self.money < self.cost: print"You don't have enough gold to buy this." raw_input("Press [enter] to exit.") self.mode = "restart" elif self.shield == 2: self.cost = 75 if self.money >= self.cost: print"You have bought",self.armors[self.shield] self.money = self.money - self.cost self.armor = 2 self.mode = "restart" elif self.money < self.cost: print"You don't have enough gold to buy this." raw_input("Press [enter] to exit.") self.mode = "restart" elif self.shield == 3: self.cost = 300 if self.money >= self.cost: print"You have bought",self.armors[self.shield] self.money = self.money - self.cost self.armor = 3 self.mode = "restart" elif self.money < self.cost: print"You don't have enough gold to buy this." raw_input("Press [enter] to exit.") self.mode = "restart" elif self.shield == 4: self.cost = 800 if self.money >= self.cost: print"You have bought",self.armors[self.shield] self.money = self.money - self.cost self.armor = 4 self.mode = "restart" elif self.money < self.cost: print"You don't have enough gold to buy this." raw_input("Press [enter] to exit.") self.mode = "restart" if self.shield == 5: self.cost = 2500 if self.money >= self.cost: print"You have bought",self.armors[self.shield] self.money = self.money - self.cost self.armor = 5 self.mode = "restart" elif self.money < self.cost: print"You don't have enough gold to buy this." raw_input("Press [enter] to exit.") self.mode = "restart" elif self.atk == "exit": self.mode = "restart" def eAttack(self): self.attacke = random.randrange(12)+self.level*12 return self.attacke def HP(self): self.e = self.eAttack() self.d = self.nDefense() self.health = self.health - (self.e - self.d) print "Enemy hits for",self.e print "You block for",self.d print "Your HP is",self.health def nAttack(self): self.attack = random.randrange(10)+self.level*10 if self.weapon == 1: self.attack = random.randrange(10)+35+self.level*10 elif self.weapon == 2: self.attack = random.randrange(10)+55+self.level*10 elif self.weapon == 3: self.attack = random.randrange(10)+90+self.level*10 elif self.weapon == 4: self.attack = random.randrange(10)+150+self.level*10 elif self.weapon == 5: self.attack = random.randrange(10)+340+self.level*10 return self.attack def nDefense(self): self.armor = random.randrange(5)+self.level*5 if self.armor == 1: self.defense = random.randrange(5)+10+self.level*5 if self.armor == 2: self.defense = random.randrange(5)+25+self.level*5 if self.armor == 3: self.defense = random.randrange(5)+45+self.level*5 if self.armor == 4: self.defense = random.randrange(5)+70+self.level*5 if self.armor == 5: self.defense = random.randrange(5)+125+self.level*5 return self.defense def LVL(self): if 1 <= self.level <= len(self.tnls): self.tnl = self.tnls[self.level-1] if self.tnl <= self.xp: self.level += 1 if self.level > len(self.tnls): self.level = 100 self.xp = 0 print("Your new level is %d! Congratulations!" % self.level) if self.level == 100: print("Congratulations, you beat the Demo!") def EnemyHP(self): self.n = self.nAttack() self.ehealth = self.ehealth - self.n print "You hit for",self.n print "Enemy's HP is",self.ehealth def Bank(self): print"Welcome to the National Bank of NorbertQuest! What will you like to do?" self.bankchoice = raw_input("Do you want to withdraw or deposit? ") if self.bankchoice == "withdraw": print"Your current balance is",self.bankbalance,"dollars." self.withdraw = input("How much would you like to withdraw? ") if self.withdraw > self.bankbalance: print"You don't have that much money in the bank!" self.mode = "restart" elif self.withdraw <= self.bankbalance: self.money = self.money + self.withdraw self.bankbalance = self.bankbalance - self.withdraw print"Your new bank Balance is",self.bankbalance,"dollars. Thank you." self.mode = "restart" elif self.bankchoice == "deposit": print"Your current balance is",self.bankbalance,"dollars." self.deposit = input("How much would you like to deposit? ") if self.deposit > self.money: print"You don't have that much money on you!" self.mode = "restart" elif self.deposit <= self.money: self.bankbalance = self.money + self.deposit self.money = self.money - self.deposit print"Your new bank Balance is",self.bankbalance,"dollars. Thank you." self.mode = "restart" def Battle(self): self.fight = random.randrange(10)+1 if self.fight == 1: self.monster = "Dragon!" elif self.fight == 2: self.monster = "Rogue Knight!" elif self.fight == 3: self.monster == "Jaguar!" elif self.fight == 4: self.monster = "Orc Lord!" elif self.fight == 5: self.monster = "Goblin!" elif self.fight == 6: self.monster = "Crazy Robot!" elif self.fight == 7: self.monster = "Werewolf!" elif self.fight == 8: self.monster = "Vampire!" elif self.fight == 9: self.monster = "Giant Spider!" elif self.fight == 10: self.monster = "Pirate King!" print "You have encountered a", self.monster, "What will you do?" self.health = 150 + self.level * 100 self.ehealth = 100 + self.level * 125 while self.health > 0 and self.ehealth > 0: self.turn = input("Do you want to attack[1], defend[2] or cast a spell[coming soon]?") if self.turn == 1: print "You're attacking!" self.EnemyHP() self.HP() elif self.turn == 2: print "You're defending!" print "Monster's HP is",self.ehealth print "Your HP is",self.health else: print"The thing you typed in is either a feature that's coming soon, or is not a valid choice." if self.health <= 0: print "You are dead! You lose 50% of the money you have on you!" self.money = self.money/2 elif self.ehealth <= 0: print "You win the battle! You earn",self.m,"$!" self.money = self.money + self.m self.m = random.randrange(50)+1+self.level*10 self.xpGain = random.randrange(25)+1+self.level*25 self.xp = self.xpGain + self.xp print"You gain",self.xpGain,"Experience Points!" self.LVL() def main(self): while True: print \ """ Welcome to NorbertQuest! Type: - [battle] to hunt monsters - [shop] to buy weapons - [bank] to visit the bank - [badge] to visit the Government Building - [exit] to quit the game """ self.mode = raw_input("What do you want to do?: ") if self.mode == "shop": print"Your current weapon is",self.weapon print"Your current armor is",self.armor print"You have",money,"dollars on you." self.Shop() elif self.mode == "battle": self.Battle() elif self.mode == "badge": if self.level >= 10: print"Your current Government Rank is",self.govrank self.Badge() elif self.level < 10: print"Come back when you're stronger!" self.mode = "restart" elif self.mode == "bank": self.Bank() elif self.mode == "exit": print "If you exit now, all progress will be lost." self.exit = raw_input("Are you sure you want to quit? Yes or no: ") if "yes" in self.exit: break if "no" in self.exit: drlf.mode = "restart" if __name__ == "__main__": n = NorbertQuest() n.main() raw_input("Press [enter] to exit.")
Also the Government Ranking thing is gonna have it's own shop (I forgot whether I said this in my previous Government related post). I need some [not code involved] help coming up with the amount of percentages of xp / money increase you'll get with each of the (I forgot exactly how many) ranks in the Government (and actually it should be Millitary but Government sounds better.)
•
•
Join Date: May 2008
Posts: 538
Reputation:
Solved Threads: 86
The lines:
don't work for me. It looks like you intended to create a list, but instead you're trying to use a really strange index.
I think you intended one of the following:
python Syntax (Toggle Plain Text)
self.weapons[1:"Steel Knuckles",2:"Knife",3:"Sword",4:"Gun",5:"Rocket Launcher"] self.armors[1:"Leather Jacket",2:"Padded Sweater",3:"Iron Arm-shield",4:"Bullet-Proof Jacket",5:"Bomber Squad Armor Plated Suit"]
don't work for me. It looks like you intended to create a list, but instead you're trying to use a really strange index.
I think you intended one of the following:
python Syntax (Toggle Plain Text)
# dictionary implementation # best matches the data as you provide it self.weapons = {1:"Steel Knuckles",2:"Knife",3:"Sword",4:"Gun",5:"Rocket Launcher"} self.armors = {1:"Leather Jacket",2:"Padded Sweater",3:"Iron Arm-shield",4:"Bullet-Proof Jacket",5:"Bomber Squad Armor Plated Suit"} # list implementation # is this more what you really want? self.weapons = ["Hands", "Steel Knuckles", "Knife", "Sword", "Gun", "Rocket Launcher"] self.armors = ["Skin", "Leather Jacket", "Padded Sweater", "Iron Arm-shield", "Bullet-Proof Jacket", "Bomber Squad Armor Plated Suit"] # in either case, I would tend to format the code like this: # because I think it is more readable self.weapons = [ "Hands", "Steel Knuckles", "Knife", "Sword", "Gun", "Rocket Launcher", ]
•
•
Join Date: May 2008
Posts: 538
Reputation:
Solved Threads: 86
Ok, I'm going to post this...I've started it several times and then you post a significant rewrite of your code and to help you I have to toss this stuff out.
You can use the idea and expand on it, or ignore it, it's up to you, but I think that this would make your code easier to work on and more maintainable.
I think you should be doing more with classes, but I surely did not mean for you to turn your whole game into one large class. You are also using class variables in many instances where they are not needed. If you only use a variable within a single function, it does not need to be a member of a class.
Here's a start at classes to support stores and items:
And I think it makes sense to gather all of the player specific information together so I started a class for this too. (this is also a start of defining what needs to be saved / restored to save game progress).
So if we implement the above, the game class changes and shopping implementation is a lot smaller:
In fact the actual shopping is almost generic enough to have a single function support any store, given the correct parameters.
(The referencing of the current item and setting the purchased item being the parts that might be complex in a generic shop. Maybe those parts could stay in the specific shop and the generic shop would just display the list and do validation of the user's selection.)
You can use the idea and expand on it, or ignore it, it's up to you, but I think that this would make your code easier to work on and more maintainable.
I think you should be doing more with classes, but I surely did not mean for you to turn your whole game into one large class. You are also using class variables in many instances where they are not needed. If you only use a variable within a single function, it does not need to be a member of a class.
Here's a start at classes to support stores and items:
python Syntax (Toggle Plain Text)
class NQShopItem(object): """ This class holds the information common to all shop items """ def __init__(self, name, price, reqLevel, reqRank): self.name = name self.price = price self.reqLevel = reqLevel self.reqRank = reqRank def shopline(self, idx): return " %2d - %-32s $%5d" % (idx, self.name, self.price) class NQWeapon(NQShopItem): """ This is a class that represents weapons in NorbertQuest """ def __init__(self, name, price, reqLevel, reqRank, damage): #NQShopItem.__init__(self, name, price, reqLevel, reqRank) super(NQWeapon, self).__init__(name, price, reqLevel, reqRank) self.damage = damage NQWeaponList = [ # Name Cost Lvl rank Dam NQWeapon("Hands", 0, 1, 0, 0), NQWeapon("Steel Knuckles", 50, 1, 0, 35), NQWeapon("Knife", 75, 2, 0, 55), NQWeapon("Sword", 300, 3, 0, 90), NQWeapon("Gun", 800, 4, 0, 150), NQWeapon("Rocket Launcher", 2500, 5, 0, 340), ] class NQArmor(NQShopItem): """ This class represents armors in NorbertQuest """ def __init__(self, name, price, reqLevel, reqRank, defense): #NQShopItem.__init__(self, name, price, reqLevel, reqRank) super(NQArmor, self).__init__(name, price, reqLevel, reqRank) self.defense = defense NQArmorList = [ # Name Cost Lvl Rank def NQArmor("Skin", 0, 1, 0, 0), NQArmor("Leather Jacket", 50, 1, 0, 10), NQArmor("Padded Sweater", 75, 2, 0, 25), NQArmor("Iron Arm-shield", 300, 3, 0, 45), NQArmor("Bullet-Proof Jacket", 800, 4, 0, 70), NQArmor("Bomber Squad Armor Plated Suit", 2500, 5, 0, 125), ]
And I think it makes sense to gather all of the player specific information together so I started a class for this too. (this is also a start of defining what needs to be saved / restored to save game progress).
python Syntax (Toggle Plain Text)
class NQPlayer(object): """ This class holds the player specific data in NorbertQuest """ def __init__(self): self.name = "Player" self.weapon = 0 self.armor = 0 self.level = 1 self.xp = 0 self.rank = 0 self.money = 100 self.bankBalance = 0 def tnl(self): return 100 + self.level * 50 def setName(self, name): # todo file management related to name change (if any) self.name = name def deposit(self, amount): if amount < 0: return "Invalid deposit: amount was negative" if amount > self.money: return "Invalid deposit: amount exceeded money available" self.money -= amount self.bankBalance += amount return "" def withdraw(self, amount): if amount < 0: return "Invalid withdraw: amount was negative" if amount > self.bankBalance: return "Invalid withdraw: amount exceeded bank balance" self.money += amount self.bankBalance -= amount return "" def calcAttack(self): weapon_mod = NQWeaponList[self.weapon].damage return random.randrange(10) + self.level * 10 + weapon_mod def calcDefend(self): armor_mod = NQArmorList[self.armor].defense return random.randrange(5) + self.level * 5 + armor_mod
So if we implement the above, the game class changes and shopping implementation is a lot smaller:
python Syntax (Toggle Plain Text)
class NorbertQuest(object): def __init__(self): self.player = NQPlayer() def ShopWeapons(self): while True: print "Current Weapon:", NQWeaponList[self.player.weapon].name print "You have $", self.player.money, "to spend" print "Available Weapons:" for si in range(1, len(NQWeaponList)): print NQWeaponList[si].shopline(si) item = raw_input("Select Weapon to buy: (Press enter to exit): ") if item == "": return if not item.isdigit: print "Enter the number of the weapon you wish to purchase" else: ii = int(item) if not ii in range(1,len(NQWeaponList)): print "Please select an item from the list" else: tweap = NQWeaponList[ii] if self.player.level < tweap.reqLevel: print "You're not a high enough level to purchase a", tweap.name elif self.player.rank < tweap.reqRank: print "You don't have a high enough rank to purchase a", tweap.name elif self.player.money < tweap.price: print "You don't have enough money to purchase a", tweap.name else: print "You bought a", tweap.name self.player.weapon = ii break def ShopArmors(self): while True: print "Current Armor:", NQArmorList[self.player.armor].name print "You have $", self.player.money, "to spend" print "Available Armors:" for si in range(1, len(NQArmorList)): print NQArmorList[si].shopline(si) # TODO: finish this function break def Shop(self): while True: print "Available Stores:" print " Weapons" print " Armors" store = raw_input("Select store: (Press enter to exit): ").upper() if store == "": break if store.startswith("W"): self.ShopWeapons() if store.startswith("A"): self.ShopArmors() # the game class continues, but I haven't finished converting the style yet
In fact the actual shopping is almost generic enough to have a single function support any store, given the correct parameters.
(The referencing of the current item and setting the purchased item being the parts that might be complex in a generic shop. Maybe those parts could stay in the specific shop and the generic shop would just display the list and do validation of the user's selection.)
![]() |
Other Threads in the Python Forum
- Previous Thread: Ejecting trays of CDs
- Next Thread: Looping through and removing parts of a list
| Thread Tools | Search this Thread |
address aliased anydbm bash beginner bits calling casino changecolor class clear conversion convert corners count cturtle cursor curves definedlines dictionary digital dynamic dynamically events examples excel external file float format frange function gui handling hints homework i/o iframe import info input java line linux list lists loan loop matching mouse multiple number numbers output parsing path port prime programming projects py py2exe pygame python random rational raw_input recursion recursive scrolledtext searchingfile shebang signal singleton string strings subprocess table tails terminal text thread threading time tkinter tlapse tooltip tuple tutorial type ubuntu unicode urllib urllib2 valueerror variable web-scrape whileloop word wxpython





