Referencing Class properties with variables in Python 2.7 - python

I'm working on a text based game, but I haven't found a very efficient solution to my fighting system yet. I currently have my statements set up sort of like this:
class Weapon1:
damage = 4
price = 5
class Weapon2:
damage = 4
price = 5
class Enemy1:
damage = 2
health = 5
class enemy2:
damage = 3
health = 6
def fight():
Weapon = raw_input("What weapon would you like to use?")
if Weapon == "Weapon 1":
if enemy = "Enemy1":
Enemy1.health -= Weapon1.damage
else:
Enemy2.health -= Weapon1.damage
else:
if enemy = "Enemy1":
pass
else:
pass
I have a lot more enemies and classes than that, so I would like to have a single statement that can reference classes' properties using variables. I decided not to upload the actual function due to it being over 100 lines long.
The pseudocode for what I'm after would look something like this:
class pistol:
damage = 4
price = 5
class cannon:
damage = 4
price = 5
class Enemy1:
damage = 2
health = 5
class enemy2:
damage = 3
health = 6
def fight():
Weapon = raw_input("What weapon would you like to use?")
enemy.health -= weapon.damage

You can use a simple dict, here with namedtuple to make the code easier to read:
from collections import namedtuple
Weapon = namedtuple('Weapon', ['damage', 'price'])
weapons = {
'pistol': Weapon(4, 5),
'cannon': Weapon(4, 5),
# ...
}
def fight():
weapon_name = raw_input("What weapon would you like to use?")
enemy.health -= weapons[weapon_name].damage

make the weapon,enemy class and initiate before doing fight()
class weapon:
def __init__(self,d,p):
self.damage = d
self.price = p
def initiate():
pistol = weapon(4,5)
#....

Related

Python text based game - TypeError: bool oject not callable

I am creating my first python program and have been struggling with a "TypeError: 'bool' object is not callable" error. I have only been studying for a few weeks so any assistance would be great! Also, if there are any tips on how to reduce some of the code that'd be great! Thanks!
import random
class Spacecraft:
def __init__(self, type, health):
self.type = type
self.health = health
self.ship_destroyed = False
def ship_destroyed(self):
self.ship_destroyed = True
if self.health != 0:
self.health = 0
print("Your spacecraft has been destroyed!")
def ship_destroyed_def(self):
self.ship_destroyed = True
if self.health != 0:
self.health = 0
print("Your enemy spacecraft has been destroyed! YOU WON!!")
def lose_health(self, amount):
self.health -= amount
if self.health <= 0:
self.ship_destroyed()
else:
print("Your spacecraft now has {health} hit points remaining.".format(health=self.health))
def lose_health_def(self, amount):
self.health -= amount
if self.health <= 0:
self.health = 0
self.ship_destroyed()
else:
print("The enemy spacecraft now has {health} hit points remaining".format(health=self.health))
print()
def attack(self, enemy_ship):
while True:
damage_fighter = random.randrange(2, 14)
damage_defender = random.randrange(4, 10)
if self.type == "fighter":
print(
'Your {type} spacecraft attacked the enemy ship for {damage} damage and the enemy {enemy_ship} spacecraft attacked your ship for {damage2} damage.'.format(
type=self.type,
damage=damage_fighter,
enemy_ship=enemy_ship.type,
damage2=damage_defender))
self.lose_health(damage_defender)
enemy_ship.lose_health_def(damage_fighter)
elif self.type == "defender":
print(
'Your {type} spacecraft attacked the enemy ship for {damage} damage and the enemy {enemy_ship} spacecraft attacked your ship for {damage2} damage.'.format(
type=self.type,
damage=damage_defender,
enemy_ship=enemy_ship.type,
damage2=damage_fighter))
self.lose_health(damage_fighter)
enemy_ship.lose_health_def(damage_defender)
class Player:
def __init__(self, type):
self.type = type
self.current_type = 0
def attack_enemy_ship(self, enemy_ship):
my_ship = self.type[self.current_type]
their_ship = enemy_ship.type[enemy_ship.current_type]
my_ship.attack(their_ship)
a = Spacecraft("fighter", 40)
b = Spacecraft("defender", 50)
print()
player_name = input('Welcome to Space Warriors! Please enter your name and hit enter. ')
player_style = input('''
Welcome ''' + player_name + '''! Space Warriors is a game that allows you to chose between two classes of spacecraft. If you select a
fighter spacecraft when you will fight again a defender spacecraft. If you choose a defender space craft
then you will fight a fighter spacecraft. Please select "Fighter" or "Defender" and press enter. ''').lower()
player_selected = []
computer_selected = []
if player_style == 'fighter':
player_selected.append(a)
computer_selected.append(b)
else:
player_selected.append(b)
computer_selected.append(a)
live_player = Player(player_selected)
computer_player = Player(computer_selected)
print()
print("Let's get ready to fight! Both ships are launched!")
print()
live_player.attack_enemy_ship(computer_player)
Once one of the two ships reaches zero hit points I am attempting to call ship_destroyed or ship_destroyed_def, to end the game; however the program stops once one of the ships hits "0". This is when I receive the error.
def ship_destroyed(self):
self.ship_destroyed = True
You're trying to make a function and an attribute that are both named ship_destroyed. You can't do that. Pick a different name for one of them.

I cannot import from another file

I am making a soccer game. I am new to Python. I am trying to make soccerplayers on a file, then import it to my main game.
Here is soccerplayer.py
class soccerp:
def __init__(self,overall,name,speed,shoot,ballc,defence):
self.overall = overall
self.name = name
self.speed = speed
self.shoot = shoot
self.ballc = ballc
self.defence = defence
Here is soccerkeeper.py
class soccerk:
def __init__(self,overall,name,dive,reactspeed,reach,jump):
self.overall = overall
self.name = name
self.dive = dive
self.reactspeed = reactspeed
self.reach = reach
self.jump = jump
Here is soccerplayerlist.py
from soccerplayer import soccerp
from soccerkeeper import soccerk
#name overall speed shootingpower ballcontrol defence
david = soccerp("david",114,181,179,183,148)
john = soccerp("john",119,179,185,187,151)
soccerplayers = [david,john]
And here is my game.py
import time
from soccerplayerlist import soccerplayers
#name overall speed shootingpower ballcontrol defence
ovr = [120,124,158,132,109] #will edit as the players overalls
teamovr = round(sum(ovr) / len(ovr))
def start():
print("Please pick your teams name : ")
team = input("> ")
print("")
time.sleep(1)
return team
def menu(team):
print("teams name : " + team)
print("team overall : " + str(teamovr))
def game():
team = start()
#while True:
teamovr = round(sum(ovr) / len(ovr))
menu(team)
print(david.name) #checking if the players were imported from soccerplayerlist.py
game()
when I run the code, It says
NameError: name 'david' is not defined
I think that it didnt import the players, I may be wrong, what am I doing wrong here?
The issue is that you're importing the list and not the values of the list.
If you do
print(soccerplayers[0].name)
You should get the desired result.
The name david is not the one you import it is the list of soccerplayers
what you can do is instead of this line.
print(david.name)
do this:
for player in soccerplayers:
if player.name == "david":
david = player
I personaly recommend on pygame it is a library in python which allows to make high level games.

Error when using classes and then calling on the class

I made a class as you can see here.
class Player():
def __init__(self, name, maxhealth, base_attack, gold, weapon, curweapon, healing_potions):
player = Player('player', 100, 100, 5, 30, 'Normal Sword', 'Normal Sword', 0 )
self.name = name
self.maxhealth = maxhealth
self.health = self.maxhealth
self.base_attack = base_attack
self.gold = gold
self.weap = weapon
self.curweapon = curweapon
self.healing_potions = healing_potions
But then when I try and call on the healing_potions part like so
if question == '2':
player_in_diningroom = True
print("You enter the dining room")
print("")
print("You find a Potion Of healing on the table!")
print("")
healing_potions += 1
player_in_diningroom = False
Then it gives me this error
Traceback (most recent call last):
File "c:/Users/Isaiah/Desktop/All of my programs/Role playing game.py", line 179, in
healing_potions += 1
NameError: name 'healing_potions' is not defined
PS C:\Users\Isaiah\Desktop\All of my programs>
I don't quite understand why you initialize a player object inside your player class. This causes infinite recurison where you constantly create a player instance infinitely.
You most likely need to create it outside of your class. I've added a method to the class so we can increase the health potions using a method belonging to the instance. This is generally the recommended practice.
#player class
class Player():
def __init__(self, name, maxhealth, base_attack, gold, weapon, curweapon, healing_potions):
self.name = name
self.maxhealth = maxhealth
self.health = self.maxhealth
self.base_attack = base_attack
self.gold = gold
self.weap = weapon
self.curweapon = curweapon
self.healing_potions = healing_potions
def increase_health_potions(self):
self.healing_potions +=1
Then we initialize a player instance/object. I noticed you had one extra parameter in the instance you created, so I removed one to make it work
#create an instance called player
player = Player('player', 100, 100, 5, 'Normal Sword', 'Normal Sword', 0 )
question = '2'
if question == '2':
player_in_diningroom = True
print("You enter the dining room")
print("")
print("You find a Potion Of healing on the table!")
print("")
player.healing_potions += 1 #accesing and increasing variable belonging to instance
player.increase_health_potions() #call method belonging to instance that increases the variable
player_in_diningroom = False
print(player.healing_potions)
Take a notice at
player.healing_potions += 1
You have to reference the player, that you want to increase the health potions at.

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)

Getting functions to interact with each other

The project: Write a program in python in which the virtual dealer Jake plays against the virtual players: Mike and Will. Mike and Will are free to bet on different outcomes with different payout ratios. This will allow the comparison of various strategies. You should keep track of each player's bank roll (including the dealer)
The game is played with a 7 faced die with numbers [0 - 6] numbers[1, 2, 3] are blue and numbers [4, 5, 6] are green.
Correct Parity Pays: 2/1
Correct Colour Pays: 2/1
Exact Number Pays: 5/1
Here is the first draft with the modifications #Harvey Summer suggested. Comments on how I can improve the code's structure and performance are appreciated.
from random import choice
from random import randint
class die_face():
# This class is used to define the properties linked to each outcome on the dice.
def __init__(self, num, colour, parity):
self.num = num
self.colour = colour
self.parity = parity
# Determine the properties linked to each outcome on the dice.
zero = die_face(0, 'none', 'none')
one = die_face(1, 'blue', 'odd')
two = die_face(2, 'blue', 'even')
three = die_face(3, 'blue', 'odd')
four = die_face(4, 'green', 'even')
five = die_face(5, 'green', 'odd')
six = die_face(6, 'green', 'even')
options = [zero, one, two, three, four, five, six,]
class bet():
# Define the bets
def __init__(self, bet_type, odds):
self.bet_type = bet_type
self.odds = odds
num_bet = bet('num', 5)
colour_bet = bet('colour', 2)
parity_bet = bet('parity', 2)
class broker():
# Define the properties of the broker.
def __init__(self, name, balance):
self.name = name
self.balance = balance
def __str__(self):
result = "Name: {} \n" \
"Balance: {}" .format(self.name, self.balance)
return result
def modify_balance(self, amount):
self.balance += amount
main_broker = broker('Main',1e3)
def random_strategy():
# Bet a random amount on a random game with a random guess.
guess = 'empty'
game_mode= choice([num_bet, colour_bet, parity_bet])
if game_mode == num_bet:
guess = randint(0,6)
elif game_mode == colour_bet:
guess = choice(['blue','green'])
elif game_mode == parity_bet:
guess = choice(['even','odd'])
value = randint(1,10)
return game_mode , value, guess
class player():
# This class defines each player
def __init__(self, name, strategy, bank_roll):
self.name = name
self.strategy = strategy
self.bank_roll = bank_roll
def modify_balance(self, amount):
self.bank_roll += amount
def __str__(self):
result = "Name: {} \n" \
"Bank Roll: {}" .format(self.name, self.bank_roll)
return result
def play(self):
return self.strategy()
# Add the players
Will = player("Will",random_strategy,100)
def dealer(type, bet_value, guess):
#Roll the dice
correct = choice(options)
#Return amount based on Win or Lose
if type == num_bet.bet_type:
if correct.num == guess:
return num_bet.odds * bet_value - bet_value
else:
return -bet_value
if type == colour_bet.bet_type:
if correct.colour == guess:
return colour_bet.odds * bet_value - bet_value
else:
return -bet_value
if type == parity_bet.bet_type:
if correct.parity == guess:
return parity_bet.odds * bet_value - bet_value
else:
return -bet_value
def main_play(player):
# Collect the bets from the players
bets = player.play()
# Roll and return bets
amount = dealer(bets[0].bet_type, bets[1], bets[2])
# Distribute the money
main_broker.modify_balance(amount*-1)
player.modify_balance(amount)
print(player)
print(main_broker)
I would create a bettingtable as a broker where money is put at risk and the outcome to the dealer and players are exchanged based on the play outcome, and allow players and dealer to place bet and collect winnings. Encapsulate betting logic to the players and abstracted it from a game rules class. Each player should have a risk tolerance or game play style (lame, aggressive, cheater, etc.)
If you build this right, it shouldn't matter what the game is: dice, cards, etc. should basically play the same.

Categories