Attribute in a Class immediately runs at the wrong time? - python

I am fairly new to Python and I am learning how classes and methods interact by creating a text-based game. The code runs great, however, my question lies in the Name(object) class. Wherever I put it in the code, (in the beginning or in the middle as shown below) I will always be prompted with an input. I suspect it may be because of the attribute name_new that prompts raw_input("> "), however, I was wondering if there was a way to prevent or change when the Class Name() is prompted.
class Character(object):
def __init__(self, name, weapon, spec_ability, health):
self.name = name
self.weapon = weapon
self.spec_ability = spec_ability
self.health = 100
def special_ability(self):
special = False
rand_num = str(random.choice(rdm))
if '1' in rand_num or '2' in rand_num or '3' in rand_num:
special = True
if special:
print "Your special ability is ready."
return True
else:
return
else:
print "Your special ability is not ready."
def Critical_Chance(self):
crit = False
rand_num = str(random.choice(rdm))
if '1' in rand_num or '2' in rand_num or '3' in rand_num:
crit = True
if crit:
print "Critical hit incoming."
return True
else:
return
class Warrior(Character):
def __init__(self, name, weapon, spec_ability, health, armor):
super(Warrior, self).__init__(name, weapon, spec_ability, health)
self.armor = armor
class Mage(Character):
def __init__(self, name, weapon, spec_ability, health, cloak):
super(Mage, self).__init__(name, weapon, spec_ability, health)
self.cloak = cloak
class Rogue(Character):
def __init__(self, name, weapon, spec_ability, health, boots):
super(Rogue, self).__init__(name, weapon, spec_ability, health)
class Name(object):
name_new = raw_input("> ")
def start():
print """\nWelcome to the world of _______. \n
You are a young hero or heroine
in search of the gold that hides within the Mountains of _____.
In order to obtain the gold and treasure that lies within the
mountains, you must battle great monsters and face dangerous
perils."""
print "Will you pursue such adventure? Or will you refuse?"
choice = raw_input("> ")
if "Yes" in choice or "yes" in choice:
Introduction()
elif "No" in choice or "no" in choice:
sys.exit()
else:
print "You typed: %s. \nPlease try again." % choice
start()
def Introduction():
print "Thank you for joining in on this adventure..."
print "Well, welcome to your adventure for the gold."
print "You will encounter dangerouse quests and enemies, but you will be\
rewarded in the end."
print "Now, what class will you be?\n\
Warrior Mage Rogue"
char_choice = raw_input("> ")
verynew_name = Name()
name_choice = Name.name_new
if "Warrior" in char_choice or "warrior" in char_choice:
name_choice = Warrior(name_choice, None, None, None, None)
print name_choice.name
print name_choice.weapon
print name_choice.spec_ability
print name_choice.health
print name_choice.armor
elif "Mage" in char_choice or "mage" in char_choice:
name_choice = Mage(name_choice, None, None, None, None)
print name_choice.name
print name_choice.weapon
print name_choice.spec_ability
print name_choice.health
print name_choice.cloak
elif "Rogue" in char_choice or "rogue" in char_choice:
name_choice = Rogue(name_choice, None, None, None, None)
print name_choice.name
print name_choice.weapon
print name_choice.spec_ability
print name_choice.health
print name_choice.boots
else:
print "You must pick a class."
start()
print "Our story starts in the town of Larton."
#super(Larton, self).enter()
start()

When you run your program, the module-level code is executed. This includes your Name class.
class Name(object):
name_new = raw_input("> ")
This creates the class Name and also executes the code within the class. Which is why you're prompted to enter a name.
If you want to be able to prompt the user for a name like this
verynew_name = Name()
you have to change your Name class so that raw_input is executed when you instantiate the class (because that's what Name() does, it creates an instance of Name):
class Name(object):
def __init__(self):
self.name_new = raw_input("> ")
(Note that instead of name_choice = Name.name_new you'll have to use name_choice = verynew_name.name_new.)

define constructors for your Character class and subclasses that don't take a name as a paramter:
e.g.
class Warrior(Character):
def __init__(self, weapon, spec_ability, health, armor):
super(Warrior, self).__init__(weapon, spec_ability, health)
self.armor = armor
and in character class, you can define self.name to be None by default
You can always assign the name later on in the logic, by using something like
character.name = Name.name_new
As long as you do that before you actually read the value of name, you will be fine

Related

In python, steal a class B method AND use it as an instance of class A from within a class A method

I've been successfully able to "spellsiphon" (steal) a method from one class (darkMage) to give to the player class (Player).
However, the "stolen" method still seems to belong to the darkMage class--in other words, when the Player casts the stolen spell, it still reads (in all ways) as the darkMage casting the spell.
Can you help? I've done this:
Added the stolen darkMage() spell to the Player's spellbook (a list)
Successfully paired the "cast" command with items in the spellbook
I want to:
- Have the Player() casting the spell rather than the darkMage (when the stolen method is called, it's run as the darkMage rather than the Player)
class Player(livingThing):
def __init__(self,name="The Stranger", HP=10, MP=5, strength=1, intellect=1, spirit=1, luck=5, gil=6):
self.name = name
self.HP = HP
self.MP = MP
self.gil = gil
self.strength = strength
self.intellect = intellect
self.spirit = spirit
self.luck = luck
self.spellbook = []
def act(self, enemy):
actions = {
"a" : self.attack,
"heal" : self.heal,
"flee" : self.flee,
"cast" : self.cast,
"siphon" : self.spellsiphon
}
#Takes input from the player
self.safe = False
while ((self.HP > 0) and (enemy.HP > 0)) and (self.safe != True):
decision = input("What would you like to do? ")
#Sets the user's input as lower-case and checks for it within the dictionary
if decision.lower() in actions:
actions[decision.lower()](enemy)
if self.safe != True:
enemy.agreact(self)
self.printHP(enemy)
else:
print("That didn't workkkkkk! Try again.")
# Prints both player and enemy HP
def printHP(self, enemy):
print("{0}'s' HP: {1} \n{2}'s HP: {3}".format(self.name, self.HP, enemy.name, enemy.HP))
# Allows the player to attack an enemy (currently functional)
def attack(self, enemy):
enemy.HP -= self.strength
print("You strike {0} for {1} damage!".format(enemy.name, self.strength))
#player.printHP(enemy)
# Allows the player to heal a certain amount of health based on its "spirit" stat (currently functional)
def heal(self, enemy):
healed = randint(0, self.spirit)
self.HP += healed
print("You've healed for {0}!".format(healed))
#player.printHP(enemy)
#Allows the player to attempt to run away
def flee(self, enemy):
randluck = randint(0, self.luck)
if randluck > 3:
print("You successfully escaped!")
self.safe = True
else:
print("You weren't able to escape!")
def cast(self, enemy):
if len(self.spellbook) != 0:
spellchoice = randint(0, len(self.spellbook)-1)
self.spellbook[spellchoice](self)
else:
print("You don't have any spells to cast!")
### SPELLSIPHON IS CURRENTLY BROKEN; IT -DOES- SIPHON THE SPELL BUT ONLY ALLOWS THE DARK MAGE TO CONTINUE CASTING ###
def spellsiphon(self, enemy):
if len(enemy.spellbook) != 0:
randspell = randint(0, len(enemy.spellbook)-1)
stolenspell = darkMage().spellbook[randspell]
#(type(enemy).__name__).__init__(self, stolenspell)
self.spellbook.append(stolenspell)
print("You've successfully stolen {0} from {1}!".format("stolenspell", enemy.name))
else:
print("You can't steal a spell from {0}!".format(enemy.name))
# Anything that can act with/against the player
class Actor(livingThing):
def __init__(self, name="Unknown Entity", HP=10, MP=2, strength=1, intellect=1, spirit=3, gil=3):
self. name = name
self.HP = HP
self.MP = MP
self.gil = gil
self.strength = strength
self.intellect = intellect
self.spirit = spirit
self.abilities = [self.strike, self.heal]
def printabilities():
print(self.abilities)
# Chooses how your opponent will respond to your attack
def agreact(self, player):
choice = randint(0, len(self.abilities)-1)
self.abilities[choice](player)
# A basic "hit back" reaction from your opponent--everyone can do this
def strike(self, player):
player.HP -= self.strength
print("{0} hit {1} for {2}!".format(self.name, player.name, self.strength))
def heal(self, enemy):
healed = randint(0, self.spirit)
self.HP += healed
print("{0} healed for {1}!".format(self.name, healed))
### CURRENT VERSION SUPPORTS THE ADDITION OF NEW ABILITIES IN CHILD CLASSES VIA THE "super().__init__()" method! ###
class Enemy(Actor):
def __init__(self):
super().__init__()
self.abilities.append(self.cast)
self.name = "Unknown Enemy"
self.HP = 600
self.spellbook = []
def cast(self, opponent):
if len(self.spellbook) != 0:
spellchoice = randint(0, len(self.spellbook)-1)
self.spellbook[spellchoice](opponent)
class darkMage(Enemy):
def __init__(self):
super().__init__()
self.player = Player()
self.name = "Dark Mage"
self.spellbook.extend((self.fireball, self.icenova))
def fireball(cls, opponent):
choice = randint(cls.intellect*1, cls.intellect*2)
spellname = "Fireball"
opponent.HP -= choice
print("{0} casted fireball for {1} damage!".format(cls.name, choice))
def icenova(cls, opponent):
opponent.HP -= cls.intellect
choice = randint(0,1)
name = "Ice Nova"
if choice == 1:
frozen = True
else:
frozen = False
print("{0} casted ice nova for {1} damage!".format(cls.name, cls.intellect))
if frozen == True:
print("{0} has been frozen solid!".format(opponent.name))
It looks like you want the spells to be detached from the enemies/players. You can do this by making them static methods instead of instance methods (you're using def fireball(cls,..) but not using the #classmethod decorator, so they are really instance methods). Static methods don't automatically get passed the class/instance they are attached to like class/instance methods, so you can pass they can be 'stolen' and 'cast' (called) without remembering the original owner.
Here is a simple example:
class DarkMage:
def __init__(self, name):
self.name = name
self.spellbook = [self.fireball]
#staticmethod
def fireball(caster, target):
print("{} casted fireball on {} for 10 damage".format(caster.name, target.name))
class Player:
def __init__(self, name):
self.name = name
self.spellbook = []
def spellsiphon(self, enemy):
if enemy.spellbook:
spellchoice = randint(0, len(enemy.spellbook)-1)
self.spellbook.append(enemy.spellbook[spellchoice])
def cast(self, enemy):
if self.spellbook:
spellchoice = randint(0, len(self.spellbook)-1)
self.spellbook[spellchoice](self, enemy)
>>> dm = DarkMage('Bad Guy')
>>> p = Player('Do-Gooder')
>>> p.spellsiphon(dm)
>>> p.cast(e)
Do-Gooder casted fireball on Evildoer for 10 damage
>>> p.cast(dm)
Do-Gooder casted fireball on Bad Guy for 10 damage
You can also define the static methods outside the classes, which would let you share them between classes a bit easier, but I think the ideal way to do this would be to make spells their own python classes so that you can attach names and other attributes to them:
class Fireball:
name = 'fireball'
damage = 10
#classmethod
def cast(cls, caster, target):
print(
'{} cast {} on {} for {} damage!'.format(
caster.name,
cls.name,
target.name,
cls.damage,
)
)
class SuperFireball(Fireball):
name = 'super fireball'
damage = 50
class Player:
def __init__(self):
self.spellbook = [Fireball]
def list_spells(self):
for spell in self.spellbook:
print(spell.name)
def cast(self, enemy):
if self.spellbook:
spellchoice = randint(0, len(self.spellbook)-1)
self.spellbook[spellchoice].cast(self, enemy)

Python iterate through a list of class objects and extract a value

Hello I am having some trouble with trying to iterate through a list of classes in order to print out a particular value that thet instance of that class holds.My program compiles but it does not print any thing out, I have also tried having "player.player in playerlist" in the for loop but I also got the same result.Any help would be greatly appreciated.
My main function
class main:
if __name__ == '__main__':
while True:
print("Please Select An Option Below:")
print("1 - Create Player:")
print("2 - List Players:")
print("3 - Create Team:")
print("4 - Sell Player:")
print("5 - Buy Player:")
x = raw_input("Please select a option: \n")
playerList = []
if(x == '1'):
import Player
p1 = Player.player()
pname = raw_input("Please Enter A Player Name \n")
page = raw_input("Please Enter A Player Age \n")
pwage = raw_input("Please Enter A Player Wage \n")
pteam = raw_input("Please Enter A Player Team \n")
p1.pname = pname
p1.page = page
p1.pwage = pwage
p1.pteam = pteam
playerList.append(p1)
continue
if(x == '2'):
for p1 in playerList:
print(p1.pname)
continue
My player class
class player(object):
#property
def pname(self):
return self.pname
#pname.setter
def pname (self, value):
pass
#property
def page(self):
return self.page
#page.setter
def page (self, value):
pass
#property
def pwage(self):
return self.pwage
#pwage.setter
def pwage(self, value):
pass
#property
def pteam(self):
return self.pteam
#pteam.setter
def pteam(self, value):
pass
----update-----
class main:
if __name__ == '__main__':
while True:
print("Please Select An Option Below:")
print("1 - Create Player:")
print("2 - List Players:")
print("3 - Create Team:")
print("4 - Sell Player:")
print("5 - Buy Player:")
x = raw_input("Please select a option: \n")
playerList = []
if(x == '1'):
import Player
nplayer = Player.player()
nplayer.set_name(raw_input("test\n"))
playerList.append(nplayer)
continue
if(x == '2'):
for player in playerList:
print(player.get_name)
continue
class player(object):
__name = ""
#property
def get_name(self):
return self.__name
def set_name(self, name):
self.__name = name
When you write p1.pname = pnamethe setter is called. However, you don't do anything in the setter. So when the getter is called later, it doesn't have anything to return.
For getters and setters to work in python, you need to do something like this:
class player(object):
#property
def pname(self):
return self._pname
#pname.setter
def pname (self, value):
self._pname = value
Many thanks to those who helped it my problem was caused by not setting up the setter/getter properly as told above and me having my array in the display menu option causing it to send the instance of the class to another list.
class main:
if __name__ == '__main__':
playerList = []
while True:
print("Please Select An Option Below:")
print("1 - Create Player:")
print("2 - List Players:")
print("3 - Create Team:")
print("4 - Sell Player:")
print("5 - Buy Player:")
x = raw_input("Please select a option: \n")
if(x == '1'):
import Player
nplayer = Player.player()
nplayer.set_name(raw_input("Please Enter A Name\n"))
playerList.append(nplayer)
continue
if(x == '2'):
for nplayer in playerList:
print(nplayer.get_name)
continue
class player(object):
__name = ""
#property
def get_name(self):
return self.__name
def set_name(self, name):
self.__name = name

ERROR: EmptyRoom(), NameError: name 'EmptyRoom' is not defined

ERROR: Line 29, in Map 'empty_room' :
I have tried rewriting my classes and other small things and have not been able to come up with any solutions. My indentations are correct(or so they seem) on notepad++ they just didn't transfer over very well to SOF. Any troubleshooting is appreciated:) THANK YOU!!
P.S. I am learning on my own with the book 'Learn Python the Hard Way' and I am doing an exercise to make a game similar to zork. Hope this helps.
from sys import exit
class Scene(object):
def enter(self):
print "This scene is not configured"
exit(1)
class Engine(object):
##calling Engine(x) x is the mapScene
def __init__(self, mapScene):
self.mapScene = mapScene
def play(self):
currentScene = self.mapScene.openingScene()
while True:
print "\n-------------------"
nextSceneName = currentScene.enter()
currentScene = self.mapScene.nextScene(nextSceneName)
class Map(object):
scenes = {
'empty_room': EmptyRoom(),
'living_room': LivingRoom(),
'office': Office(),
'hallway': Hallway(),
'kitchen': Kitchen(),
'master_bedroom': MasterBedroom(),
'kids_bedroom': KidsBedroom(),
'attic': Attic()
}
##when calling Map(x) x is the startscene
def __init__(self, startScene):
self.startScene = startScene
##When calling nextScene(x) x is the sceneName
def nextScene(self, sceneName):
return Map.scenes.get(sceneName)
##????
def openingScene(self):
return self.nextScene(self.startScene)
class EmptyRoom(Scene):
def enter(self):
print ""
action = raw_input("> ")
if action == "open door":
return 'living_room'
class LivingRoom(Scene):
def enter(self):
print ""
action = raw_input("> ")
if action == "kitchen":
return 'kitchen'
elif action == "stairs" or "go upstairs":
print "The steps creek as you ascend to the unknown..."
print "Are you sure you want to go up here?"
action = raw_input("> ")
if action == "yes" or "kinda":
return 'hallway'
else:
return 'living_room'
elif action == "empty room":
return 'empty_room'
else:
return 'living_room'
class Kitchen(Scene):
def enter(self):
print ""
action = raw_input("> ")
if action == "office" or "go right":
return 'office'
elif action == "living room":
return 'living_room'
else:
return 'kitchen'
class Office(Scene):
def enter(self):
print ""
action = raw_input("> ")
if action == "kitchen":
return 'kitchen'
class MasterBedroom(Scene):
def enter(self):
print ""
action = raw_input("> ")
if action == "hallway":
return 'hallway'
class KidsBedroom(Scene):
def enter(self):
print ""
action = raw_input("> ")
if action == "hallway":
return 'hallway'
class Hallway(Scene):
def enter(self):
print ""
action = raw_input("> ")
if action == "downstairs" or "stairs" or "living room":
return 'living_room'
elif action == "bedroom" or "master bedroom" or "left":
return 'master_bedroom'
elif action == "kids room" or "kids bedroom" or "right":
return 'kids_bedroom'
elif action == "pull string":
print"You have just opened the attic staircase would you like to go up?"
action = raw_input("> ")
if action == "yes":
return 'attic'
elif action == "no" or "nope":
return 'hallway'
else:
print "I wouldn't have went either\n"
print "SMASH, the attic door springs shut\n"
return 'hallway'
else:
return 'hallway'
class Attic(Scene):
def enter(self):
print ""
action = raw_input("> ")
if action == "downstairs" or "hallway":
return 'hallway'
aMap = Map('empty_room')
aGame = Engine(aMap)
aGame.play()
The class definition should come first before it is used.
class EmptyRoom(Scene):
def enter(self):
print ""
action = raw_input("> ")
if action == "open door":
return 'living_room'
class Map(object):
scenes = {
'empty_room': EmptyRoom(),
...
}
Same for LivingRoom, Office, Hallway, Kitchen, MasterBedroom, KidsBedroom, Attic.
The definition of scenes inside Map is run when the class is being built, not when you later call the class to instantiate it. At that time, EmptyRoom doesn't exist yet - Python works from the top of your code down, so EmptyRoom only exists after it has reached the end of the indented block underneath class EmptyRoom:. So for this to work, Map needs to go after all of your other classes rather than before.

Simple Python Game (stuck on classes)

I'm really stuck on this simple user input game I created. It's an exercise from learn python the hard way. I've been trying for over a week to figure it out on my own and finally caved in to asking for help. I think the problem point in my code is the Engine() class. Anyway here's the code:
from sys import exit
from random import randint
class Island(object):
def enter(self):
pass
class Engine(object):
def __init__(self, island_map):
self.island_map = island_map
def play(self):
current_island = island_map.opening_island()
last_island = self.island_map.next_island('Tropical')
while current_island != last_island:
next_island_name = current_island.enter()
current_island = self.island_map.next_island(next_island_name)
current_island.enter()
class Loser(Island):
snippets = ["Welcome to loser Island",
"Can't win them all",
"There's always next time"]
def enter(self):
print "Game over"
print Loser.snippets[randint(0,len(self.snippets)-1)]
exit(1)
class Jungle(Island):
def enter(self):
print "Welcome to the Jungle!"
print "You must fight the monkey to pass!"
print "Choose your weapon..."
weapons = ['stick', 'fly swatter', 'banana']
print weapons
fighter_weapon = raw_input("Choose from a a weapon above...")
if fighter_weapon == 'stick':
print "Stick can't beat a monkey!"
print "Monkey wins"
return 'Loser'
elif fighter_weapon == 'fly swatter':
print "The monkey steals your fly swatter"
print "Monkey wins"
return 'Loser'
elif fighter_weapon == 'banana':
print "The monkey is hypnotized by the banana"
print "You continue to the next island..."
return 'Frozen'
else:
print "What? Doesn't make sense"
return 'Jungle'
class Frozen(Island):
#add green, blue circle, and black diamond
def enter(self):
print "This is snow land"
print "Here's your snowboard, choose your path"
print "([[[[[[[[[)"
runs = ["green", "blue", "black"]
print runs
run_choice = raw_input(" >")
if run_choice == 'green':
print "Easy way out?"
print "No good"
print "Wrong"
return 'Loser'
elif run_choice == 'blue':
print "okay, at least not the easiest way"
print "You have some guts, I'll let you choose one more time"
return 'Frozen'
elif run_choice == 'black':
print "I like your style"
print "Going for the hard choice shows courage"
print "Continue to the next island"
return 'Tropical'
else:
print "Say whaaat?!"
return 'Frozen'
class Tropical(Island):
def enter(self):
print "You made it to the final Island!"
print "Look here's the treasure chest!"
print " All that's left to do is guess the code on the chest"
print "Be smart, you only get five guesses..."
t_key = "1234"
chest_guess = raw_input("What do you think it is?")
guesses = 0
while chest_guess != t_key and guesses < 4:
print "Guess again"
guesses += 1
chest_guess = raw_input(" ?")
if chest_guess == t_key:
print "You guessed right!"
print "You won"
else:
print "Sorry"
return 'Loser'
class Map(object):
islands = {
'Loser': Loser(),
'Jungle': Jungle(),
'Frozen': Frozen(),
'Tropical': Tropical(),
}
def __init__(self, start_island):
self.start_island = start_island
def next_island(self, island):
val = Map.islands.get(island)
return val
def opening_island(self):
return self.next_island(self.start_island)
mj_map = Map('Jungle')
mj_game = Engine(mj_map)
mj_game.play()
Right now I'm stuck on an error that states "island_map" is not defined in my Engine class. Which I don't understand because to me it looks like it is defined. Any input would be appreciated.
def play(self):
current_island = island_map.opening_island()
needs to be changed to
def play(self):
current_island = self.island_map.opening_island()
as it is apart of that instance

Why does my python class method behave differently by itself than when I run the module?

I am making a small text game, and one of my methods is behaving unexpectedly, and I can't figure out why. If I import the module and run the method in python, it works fine. Some parts of the game aren't complete, but that parts that I need run fine.
With the module pasted below, choose method is changing a list into a string, but only when the module is run. I'm running this on 2.7.3, on a mac.
Edit: I figured it out. The program execution had continued, and I was confused by the choose call in the match method. The options parameter sent in match was a string, instead of the list it should have been, but that was simple to figure out why.
import fighters
import weapons
from random import randint
from sys import exit
class Hero(object):
weapons = ["spikeglove", "shortsword"]
hp = {"actual":50, "base":50}
score = 0
def attack(self, weapon, method):
return None
class Scene(object):
# Returns one or more items from the provided choices
def choose(self, options, max_choices, prompt):
chosen = []
while len(chosen) < max_choices and len(options) > 0:
print
print prompt
print options, chosen
for o in options:
print "- {}".format(o)
print type(options)
choice = raw_input("> ")
if choice in options:
chosen.append(choice)
options.remove(choice)
print options, chosen
else:
print "That is not one of the options!"
if len(chosen) == 1:
return chosen[0]
else:
return chosen
class Death(Scene):
def enter(self):
print "You are now dead"
return "menu"
class Exit(Scene):
def enter(self):
print "Thanks for playing!"
return exit(1)
class Menu(Scene):
def enter(self):
menu_options = ["play", "train", "exit"]
choice = self.choose(menu_options, 1, "Welcome to the Arena!")
return choice
class Level(Scene):
def __init__(self):
self.prologue = "Welcome to {}".format(self.__class__.__name__)
next_scene = "level1"
# A match is the fight between the Hero and a particular Enemy
# A Hero must defeat each Enemy via a match to win the tournament/level
def match(self, hp, weapons, enemy):
hero_hp = hp
enemy_hp = enemy.stats["hp"]
last_attack = ""
while hero_hp and enemy_hp:
weapon_choice = self.choose(weapons, 1, "> ")
attack_choice = self.choose(weapon_choice.available_attacks(last_attack), 1, "> ")
last_attack = attack_choice
hero_attack = hero.attack(attack_choice)
enemy_attack = enemy.attack()
print "Hero: {} {}: {}".format(hero_hp, enemy, enemy_hp)
if hero_hp:
return "victory", hero_hp
else:
return "defeat"
# The game cycles through the enemies that our hero has to face
# passing the setup to the "match" function for the fighting
def enter(self):
print
print
print self.prologue
start_hp = hero.hp['actual']
weapons = self.choose(hero.weapons, 2, "Choose a weapon")
while self.enemies:
enemy = fighters.roster[self.enemies.pop()]
match_outcome, finish_hp = self.match(start_hp, weapons, enemy)
if match_outcome == "defeat":
hero.hp['actual'] = hero.hp['base']
return "death"
# The Hero enters the next match with the current HP obtained from the previous match
start_hp = finish_hp
# Add our Hero's victory points, and get them some medical attention
# before heading back to the Barracks
points_award = 5
hero.points += points_award
hero.hp['actual'] = hero.hp['base']
new_weapon = "shortsword"
hero.add_weapon(new_weapon)
epilogue = """Congratulations!
You have gained {} points for a total score of {}.\n
You have unlocked the \"{}\" """.format(
points_award, hero.points, new_weapon)
return next_scene
class Train(Scene):
pass
class Level1(Level):
enemies = ["boxer", "boxer"]
next_scene = "level2"
class Level2(Level):
pass
class Level3(Level):
pass
class Level4(Level):
pass
class Level5(Level):
pass
class Engine(object):
def __init__(self, start_scene):
self.start_scene = start_scene
scenes = {
"menu": Menu(),
"death": Death(),
"exit": Exit(),
"play": Level1(),
"level2": Level2(),
"level3": Level3(),
"level4": Level4(),
"level5": Level5(),
}
def next_scene(self, scene_name):
return self.scenes[scene_name]
def play(self):
current_scene = self.scenes[self.start_scene]
while True:
next_scene = current_scene.enter()
current_scene = self.scenes[next_scene]
if __name__ == "__main__":
hero = Hero()
game = Engine("menu")
game.play()
Terminal output:
$ python arena.py
Welcome to the Arena!
['play', 'train', 'exit'] []
- play
- train
- exit
<type 'list'>
> play
['train', 'exit'] ['play']
Welcome to Level1
Choose a weapon
['spikeglove'] []
- spikeglove
<type 'list'>
> spikeglove
[] ['spikeglove']
>
spikeglove []
- s
- p
- i
- k
- e
- g
- l
- o
- v
- e
<type 'str'>
Yet when I run in python:
>>> import arena
>>> s = arena.Level1()
>>> s.choose(['shortsword', 'spikeglove'], 2, "Choose a weapon")
Choose a weapon
['shortsword', 'spikeglove'] []
- shortsword
- spikeglove
<type 'list'>
> shortsword
['spikeglove'] ['shortsword']
Choose a weapon
['spikeglove'] ['shortsword']
- spikeglove
<type 'list'>
> spikeglove
[] ['shortsword', 'spikeglove']
['shortsword', 'spikeglove']
The if __name__ == "__main__": means that the portion of code indented after that if-statement will be executed only when the script is executed, not when it is imported.

Categories