Updating properties of an object in Python OOP - python

I am trying to create a simple game in Python using the OOP style.
The parent class is set up and there are two sub-classes, one for the hero and one for the ork.
Basically, when the hero attacks the ork (or vice versa) I want the health to be updated based on the damage done (damage is the amount of power the attacking character has). Currently, every time it loops it resets the health values back to the original of 100.
What is the best way of doing this using OOP? I can figure out how to do it in my own procedural and messy way, but I would like to see how it should be done.
class Character:
'''Blueprint for game characters'''
def __init__(self):
#default values
self.character = ""
self.speed = 0
self.power = 0
self.health = 100
def attack(self, attackObj):
self.attacker = self.character
self.attackerPower = self.power
self.victim = attackObj.character
self.victimHealth = attackObj.health
self.newHealth = self.victimHealth - self.attackerPower
print(self.character, "you have chosen to attack", self.victim)
print(self.victim, "you have suffered", self.attackerPower, "damage and your health is now", self.newHealth)
class Hero(Character):
'''Inherits from character to create hero'''
def __init__(self):
Character.__init__(self)
self.character = "Hero"
self.speed = 8
self.power = 9
print(self.character, "you have",self.speed, "speed,", self.power, "power and", self.health, "health.")
class Ork(Character):
'''Inherits from character to create ork'''
def __init__(self):
Character.__init__(self)
self.character = "Ork"
self.speed = 2
self.power = 8
print(self.character, "you have",self.speed, "speed,", self.power, "power and", self.health, "health.")
def main():
charclass = Character()
hero = Hero()
ork = Ork()
endLoop = False
while endLoop == False:
print("Please choose your character by typing the corresponding key: ")
print("H for hero")
print("O for ork")
charChoice = input()
if charChoice in ("H", "h", "hero", "Hero"):
charChoice = hero
enemy = ork
hero = Hero()
elif charChoice in ("O", "o", "ork", "Ork"):
charChoice = ork
enemy = hero
print("Please choose an action by typing the corresponding key: ")
print("A to attack")
actionChoice = input()
if actionChoice in ("A", "a"):
charChoice.attack(enemy)
else:
print("Nothing chosen!")
finishedYN = input("Have you finished? Y/N ")
if finishedYN in ("Y", "y", "Yes", "yes", "YES"):
print("You have chosen to end the game...")
endloop = True
break
else:
pass
if __name__ == "__main__":
main()

A quick fix to your code. Remove all these unecessary attributes, e.g. self.attacker (that's just self), self.attackPower (that's just self.power). self.victim = attackObj.character just gives your object a new attribute that is the same string as whatever attackObj.character is. Similarly, this:self.newHealth = self.victimHealth - self.attackerPower just creates a new attribute each time the method is called that will always be 100 - self.attack
def attack(self, attackObj):
attackObj.health -= self.power
print(self.character, "you have chosen to attack", attackObj.character)
print(attackObj.character, "you have suffered", self.power, "damage and your health is now", attackObj.health)
Really, an even better way is to add methods that mutate your object, that will be used as an interface with how your object interacts with other objects. So, for example:
class Character:
'''Blueprint for game characters'''
def __init__(self):
#default values
self.character = ""
self.speed = 0
self.power = 0
self.health = 100
def attack(self, victim):
victim.receive_damage(self.power)
def receive_damage(raw_damage):
self.health -= raw_damage
This improves the extensibility of your code. You could more easily implement a "buffs" system, or add an "armor" element, that affects how you recieve damage, without ever having to change the code in attack. You can override these methods in subclasses. So, perhaps you want all orks to have "thickskin", and in Ork:
def receive_damage(raw_damage):
self.health -= raw_damage*self.thickskin_modifier
Where self.thickskin_modifier was set in the Ork class __init__. It could be something like 0.9.

Related

How to take away health points when using a class?

I'm working on a game in python and I can't figure out how to take away health once attack function has taken place. I can run the program and the attack function works fine, it shows a random integer between 1 and 50 but doesn't actually take away health from castlehealth = 100
Underneath print("You attacked for " + str(self.attack)) I left the next line blank because I don't know what to type in, I tried a lot of different things and just can't get the attack to take away from castlehealth.
Here is my code:
import os
import time
from random import randint
class GameActions:
def __init__(self):
castlehealth = 100
self.castlehealth = castlehealth
def health(self):
print("Castle health is: " + str(self.castlehealth))
print()
def attack(self):
attack = randint(0, 50)
self.attack = attack
print("You attacked for " + str(self.attack))
def game():
while True:
game_actions = GameActions()
print("What do you want to do?")
print()
print("attack, view hp")
ans = input()
if ans == "hp":
game_actions.health()
if ans == "attack":
game_actions.attack()
You want the following:
self.castlehealth -= attack
Try something like self.castlehealth -= attack. I also fixed some potential indentation issues for you.
Your full code sample could look this this:
import os
import time
from random import randint
class GameActions:
def __init__(self):
castlehealth = 100
self.castlehealth = castlehealth
def health(self):
print("Castle health is: " + str(self.castlehealth))
print()
def attack(self):
attack = randint(0, 50)
self.attack = attack
print("You attacked for " + str(self.attack))
self.castlehealth -= attack
def game():
while True:
game_actions = GameActions()
print("What do you want to do?")
print()
print("attack, view hp")
ans = input()
if ans == "hp":
game_actions.health()
if ans == "attack":
game_actions.attack()
Explanation: self.castlehealth is an instance variable of your GameActions class. The function GameActions.attack() creates a new attack variable as a random integer and then subtracts that value from the instance variable self.castlehealth of the GameActions class. Now self.castlehealth will be the updated value. Consider also tracking the various attacks and resulting healths in a data structure, since every time you have a new attack self.castlehealth and self.attack will change values and you will lose the ability to access the previous values.

how to make a pokemon battle with def

so this is my code( or just a long sample)
from easygui import*
health = 100
ai_h = 100
moves = ['scratch']
class player:
def starter():
starter = buttonbox('what do you want to see?',
choices = ['fjade', 'soarer','mudwet'])
msgbox = ('that is a great choice')
def p_attack():
global ai_h
battle = choicebox('battle, chose your move!',
choices = moves)
if battle == 'scratch':
msgbox('you used scratch')
ai_h = ai_h - 10
msgbox(ai_h)
if health < 0:
msgbox('you failed')
quit()
sys.exit()
def __init__(self):
self.health = health
class ai:
def __init__(self):
self.healtha = ai_h
def attack():
global health
msgbox('the oppenet hit you!')
msgbox('you lost 10 health')
health = health - 10
msgbox(health)
if ai_h < 0:
msgbox('you defeated ai')
class battle:
def __init__():
pass
def war(opponet, opponet_health, your_health):
msgbox('oppent want to battle!')`
that is my code and as you can see the def war(i wanted to use battle)
is not yet finished. I tried many examples but sometimes the ai's health goes to negative something and the program is still going. can anyone help?

Accessing Class variable via function variable

I'm playing with Python 3 and putting together an RPG-style system, but am struggling with OOP as I've not really used it in python before.
I'm specifically struggling with the following function:
def choosestat(statname, max, min):
# Takes the name of the stat
# as mentioned in the Player/Character class and
# a min/max value for the length. Allows the player
# to set their stats for the char.
print("Your stat choices are: " + str(stats))
choice = int(input("Please select a strength score ("+min+":"+max+")\n"))
if type(choice) == int and choice < (max+1) and choice > (min-1):
self.statname = stats[choice-1]
stats.pop(choice-1)
else:
print("Please select a valid option.\n")
This should be pulling from the below, to set playerchar.strength to the selected amount:
class Character(object):
# A Class for ALL characters - player, enemy etc.
def __init__(self, name, hp, armor, damage, strength,
intelligence, wisdom, dexterity, constitution, charisma,
inventory, profession):
self.name = name
self.hp = hp
self.armor = armor
self.damage = damage
self.strength = strength
self.intelligence = intelligence
self.wisdom = wisdom
self.dexterity = dexterity
self.constitution = constitution
self.charisma = charisma
self.inventory = inventory
self.profession = profession
class Player(Character):
# A class for the Player character only.
# We will use a few parameters to set initial variables
def __init__(self):
super().__init__(name="", hp=10, armor=10, damage=10, strength=7,
intelligence=7, wisdom=7, dexterity=7, constitution=7,
charisma=7, inventory=[], profession="")
maxhp = 10
level = 1
exp = 0
However, when I run the code, I simply get:
Traceback (most recent call last):
File "main.py", line 122, in <module>
menu()
File "main.py", line 53, in menu
gameloop()
File "main.py", line 73, in gameloop
statchoice()
File "main.py", line 108, in statchoice
choosestat(strength, 6, 1)
NameError: name 'strength' is not defined
The full code is here:
import os
import random
import time
from utilities import roll
class Character(object):
# A Class for ALL characters - player, enemy etc.
def __init__(self, name, hp, armor, damage, strength,
intelligence, wisdom, dexterity, constitution, charisma,
inventory, profession):
self.name = name
self.hp = hp
self.armor = armor
self.damage = damage
self.strength = strength
self.intelligence = intelligence
self.wisdom = wisdom
self.dexterity = dexterity
self.constitution = constitution
self.charisma = charisma
self.inventory = inventory
self.profession = profession
class Player(Character):
# A class for the Player character only.
# We will use a few parameters to set initial variables
def __init__(self):
super().__init__(name="", hp=10, armor=10, damage=10, strength=7,
intelligence=7, wisdom=7, dexterity=7, constitution=7,
charisma=7, inventory=[], profession="")
maxhp = 10
level = 1
exp = 0
class Enemy(Character):
# a class for any enemy.
def __init__(self):
super().__init__(name=enemyname, hp=10, armor=10, damage=10,
strength=7, intelligence=7, wisdom=7, dexterity=7,
constitution=7, charisma=7, inventory=[],
profession="")
playerchar = Player()
# simply call playerchar rather than calling the Class each time
def menu():
# Running the Main Menu under a single Function.
# This is the simplest method of running the menu
while True:
print("|==================|")
print("Welcome to Py RPG!")
print("Please select an option!")
print("1. Start a Game\n")
print("2. Settings\n")
print("3. Quit")
choice = input("\n>")
if choice == 1 or choice == "start":
gameloop()
elif choice == 2 or choice == "settings":
settings()
elif choice == 3 or choice == "quit":
break
else:
print("Please select an option from the menu.")
def settings():
# Settings page for all amendments
# TODO - create a 'settings' file and have this read/amend it?
print("Nothing exists here at the moment!")
menu()
def gameloop():
# the main game loop, contains all calls to relevant functions
while True:
print("This is the main game")
print("Let's begin. What is your name?")
playerchar.name = input(">")
print("Well then, " + playerchar.name + ", let us begin.")
statchoice()
def choosestat(statname, max, min):
# Takes the name of the stat
# as mentioned in the Player/Character class and
# a min/max value for the length. Allows the player
# to set their stats for the char.
print("Your stat choices are: " + str(stats))
choice = int(input("Please select a strength score ("+min+":"+max+")\n"))
if type(choice) == int and choice < (max+1) and choice > (min-1):
self.statname = stats[choice-1]
stats.pop(choice-1)
else:
print("Please select a valid option.\n")
def displaystats(entity):
# quick function to display all relevant stats in-line.
print("Are you happy with your choices?\n")
print("Strength: " + str(entity.strength))
print("Intelligence: " + str(entity.intelligence))
print("Wisdom: " + str(entity.wisdom))
print("Dexterity: " + str(entity.dexterity))
print("Constitution: " + str(entity.constitution))
print("Charisma: " + str(entity.charisma))
def statchoice():
# Using the roll function, we get 6 ability scores, append them to 'stats',
# and run the choosestat function for each to set stats.
stats = []
stats.append(roll(4, 6))
stats.append(roll(4, 6))
stats.append(roll(4, 6))
stats.append(roll(4, 6))
stats.append(roll(4, 6))
stats.append(roll(4, 6))
choosestat(strength, 6, 1)
choosestat(intelligence, 5, 1)
choosestat(wisdom, 4, 1)
choosestat(dexterity, 3, 1)
choosestat(constitution, 2, 1)
choosestat(charisma, 1, 1)
displaystats(playerchar)
reroll = input("Do you wish to re-roll?")
if reroll == "yes" or reroll == "y":
statchoice()
menu()
You can use setattr().
choosestat('strength', 6, 1)
def choosestat(statname, max, min):
print("Your stat choices are: " + str(stats))
choice = int(input("Please select a strength score ("+min+":"+max+")\n"))
if type(choice) == int and choice < (max+1) and choice > (min-1):
setattr(self, statname, stats[choice-1]
stats.pop(choice-1)
else:
print("Please select a valid option.\n")
It's important to pass a string of the field name you want to change to setattr() or in this case through choosestat().
You want your choosestat function inside your class if you are calling self.*something*. Right now it's not. If you move that inside your Character class and add the self argument to it. Then you can access members of that class. Right now in statchoice, strength is not defined. You could try passing in a string "strength" and having a dict inside of choosestat (a member of Character) that maps the strings to their member counter parts:
{"strength" : self.strength, "intelligence" : self.intelligence} and so forth. Also, I don't think that your stats array is global, so you'll either have to make it global or somehow a member of the class and rework some of your other functions, too.

Converting globals to class

I have been using global variables for a little text game in python and have come across a lot of articles saying that global variables are a no no in python. I have been trying to understand how to get what I have below (just a health variable and being able to change it and print it) working using classes but I am confused how I can converted something like this in a class. Any help, example, point in the right direction would be great.
Here is an example of me using variables.
import sys
import time
health = 100
b = 1
def intro():
print("You will die after two moves")
def exittro():
time.sleep(1)
print("Thanks for playing!")
sys.exit()
def move():
global health
global b
health -= 50
if health <= 51 and b >0:
print("almost dead")
b = b - 1
def death():
if health == 0 or health <= 0:
print("...")
time.sleep(1)
print("You died\n")
time.sleep(2)
print("Dont worry, this game sucks anyway\n")
exittro()
intro()
a = 1
while a == 1:
input("Press Enter to move")
move()
death()
Thank you
Edit: this is the kind of thing I have been trying to do...
class Test:
def __init__(self):
number = 100
def __call__(self):
return number
def reduceNum(self):
number -=10
def printNum(self):
print(number)
a = 1
while a == 1:
input("Enter")
Test.self.reduceNum()
Test.self.printNum()
I would avoid classes for this, as classes are generally slower. You could make the function return the new value for the health variable.
I would also suggest making a main controller function to take the return value and apply it to other functions. This prevents global variables outside of a function's scope.
import time
def intro():
print("You will die after two moves")
def outro():
time.sleep(1)
print("Thanks for playing!")
# sys.exit() # You can avoid this now by just stopping the program normally
def move(health):
health -= 50
if health <= 51:
print("almost dead")
return health # Return the new health to be stored in a variable
def death(health):
if health <= 0:
print("...")
time.sleep(1)
print("You died\n")
time.sleep(2)
print("Dont worry, this game sucks anyway\n")
return True # Died
return False # Didn't die
def main():
health = 100 # You start with 100 health
intro()
while not death(health):
# While the death function doesn't return `True` (i.e., you didn't die) ...
input("Press enter to move")
health = move(health) # `health` is the new health value
outro()
If you want to use classes, you need to actually instantiate the class (Make a new object from it) by doing instance = Test(). You also need to store variables as attributes of self (so self.number = number) as any local variables are different from each other.
class Test:
def __init__(self):
self.number = 100
def __call__(self):
return self.number
def reduceNum(self):
self.number -= 10
def printNum(self):
print(self.number)
a = 1
game = Test()
while a == 1:
input("Enter")
game.reduceNum()
game.printNum()
# Or:
print(game())
# As you've changed `__call__` to return the number as well.

Adding a combat system to a text adventure game; confused by the structure of the code I'm using

Through some educational materials I've been tasked to use the below structure (the classes) for a text adventure game and am required to add a simple combat system for battle between a hero and an enemy.
Currently I am able to have an enemy created in each room and move between the start room(corridor) to the bathroom and back, but at this point I'm stuck. I can't determine where I should be creating my 'hero' or how I'd communicate the changes I'd need to make to the health attributes etc.
If I could structure the code in another way, I would be able to complete the game, but as it is there is a gap in my understanding of how to enable various sections of code communicate with each other.
Thanks,
Dave
# text based adventure game
import random
import time
from sys import exit
class Game(object):
def __init__(self, room_map):
self.room_map = room_map
def play(self):
current_room = self.room_map.opening_room()
while True:
next_room_name = current_room.enter()
current_room = self.room_map.next_room(next_room_name)
class Character(object):
def __init__(self, name, health, attack):
self.name = name
self.health = health
self.attack = attack
class Hero(Character):
def __init__(self, name):
super(Hero, self).__init__(name, 10, 2)
def __str__(self):
rep = "You, " + self.name + ", have " + str(self.health) + " health and " + \
str(self.attack) + " attack."
return rep
class Enemy(Character):
ENEMIES = ["Troll", "Witch", "Ogre", "Jeremy Corbyn"]
def __init__(self):
super(Enemy, self).__init__(random.choice(self.ENEMIES),
random.randint(4, 6), random.randint(2, 4))
def __str__(self):
rep = "The " + self.name + " has " + str(self.health) + \
" health, and " + str(self.attack) + " attack."
return rep
class Room(object):
def __init__(self):
self.commands = ["yes", "no"]
self.rooms = ["\'corridor\'", "\'bathroom\'", "\'bedroom\'"]
self.enemy = Enemy()
def command_list(self):
print("Commands: ", ", ".join(self.commands))
def enter_room_question(self):
print("Which room would you like to enter?")
print("Rooms:", ", ".join(self.rooms))
def leave_room_question(self):
print("Do you want to leave this room?")
print("Commands:", ", ".join(self.commands))
class Bathroom(Room):
def enter(self):
print("You enter the bathroom. But, wait! There is an", \
self.enemy.name, "!")
print(self.enemy)
print("You are in the bathroom. Need to take a dump? ")
self.command_list()
response = input("> ")
while response not in self.commands:
print("Sorry I didn't recognise that answer")
print("You are in the bathroom. Need to take a dump?")
self.command_list()
response = input("> ")
if response == "yes":
print("Not while I'm here!")
return "death"
elif response == "no":
print("Good.")
self.leave_room_question()
response = input("> ")
if response == "yes":
return "corridor"
else:
return "death"
class Bedroom(Room):
def enter(self):
pass
class Landing(Room):
def enter(self):
pass
class Corridor(Room):
def enter(self):
print("You are standing in the corridor. There are two rooms available to enter.")
self.enter_room_question()
response = input("> ")
if response == "corridor":
print("You're already here silly.")
else:
return response
class Death(Room):
QUIPS = ["Off to the man in sky. You are dead",
"You died, no-one cried.",
"Lolz. You're dead!"]
def enter(self):
time.sleep(1)
print(random.choice(Death.QUIPS))
exit()
class Map(object):
ROOMS = {"corridor": Corridor(),
"bathroom": Bathroom(),
"death": Death(),
"landing": Landing(),
"bedroom": Bedroom()}
def __init__(self, start_room):
self.start_room = start_room
self.hero = hero
def next_room(self, room_name):
return Map.ROOMS.get(room_name)
def opening_room(self):
return self.next_room(self.start_room)
a_hero = Hero("Dave")
a_map = Map("corridor")
a_game = Game(a_map, a_hero)
a_game.play()
If I were you, I would set out a game schema. You could find out asking yourself questions like this:
What are the really important entities?
In your case, as you have done, I would consider Character, Enemy, Room and Map, inheriting when it would be appropiate, like Character-> Hero and Enemy, and several types of room from Room as Bathroom, Corridor, ...
If I were you a consider using a data structure to represent the Map. For example, if you are considering do a text game adventure, you could think in different rooms as different states in your game. If you are in the bathroom, you could be attacked by an enemy and if you are in the bedroom, you can retrieve your hitpoints (life) so these places can be thought as different states.
As an example, you would an array for group all your different rooms (states)
rooms = ["bedroom", "bathroom", "corridor", "kitchen", "living_room"]
and other rooms that you can think about.
(there is probably a better example, more efficient and so on, so this example is to help you about not giving up when you gets stuck about an problem.
According to this example, if you use an array, you can assign a value to each room (equal to each position in the array)
Moreover, you will need to know the hero's position, so you could assign a random value to it using rand(). You can read links below for more information:
random docs python
stackoverflow answer
At last, you also would find useful to compare the hero's position, which would have a random assigned value previously with every position of your array or rooms
In this cases, you could use a if... elif.. elif... to compare those values and do something according to the room where your hero would be.
I hope this answer will be useful for you.
Let me know if you have any doubt about my answer.
Cheers

Categories

Resources