Reputation: 6944
I am creating a text-based adventure game (as mentioned in this post), and I am stuck on how to use Classes and their attributes, arguments related to classes, and the pre-defined keyword self.
. I have looked around for how to use them, but to no avail. I will give an example of each, with a question attached to them:
class Player:
def __init__(self, name):
self.name = name
self.maxhealth = 100
self.health = self.maxhealth
self.attack = 15
self.money = 0
def display(self, name):
print("Name:",self.name,"\nHealth:",self.health,"/",self.maxhealth,"\nAttack Damage:",self.attack,"\nMoney:",self.money)
Someone from the answers suggested changing self.name = name
to self.name = None
, but they didn't explain why or how it would help. If anyone could tell me how to define self.name
in the correct or most efficient way, please do. I would also like to know how to call it in later functions.
I am confused on how class attributes work (related to previous question), and how you know if they are enabled, and how to call them.
I am also unsure of the arguments that would go into class Player(#here):
, and how they could be used. I'm not sure if there are any uses to it, but I have seen people do (object)
and (Edum #or something like that)
and I don't know what they add, how to call them etc.
import time
import random
import sys
import pickle
class Zombie:
def __init__(self, name):
self.name = name
self.maxhealth = 50
self.health = self.maxhealth
self.attack = 5
self.golddrop = random.randint(5,10)
class Skeleton:
def __init__(self, name):
self.name = name
self.maxhealth = 75
self.health = self.maxhealth
self.attack = random.randint(5,10)
self.golddrop = random.randint(5,10)
class Friend:
def __init__(self, name):
self.name = name
self.maxhealth = 100
self.health = self.maxhealth
self.attack = random.randint(7,10)
class DungeonBoss:
def __init__(self, name):
self.name = name
self.maxhealth = 150
self.health = self.maxhealth
self.attack = random.randint(15, 25)
Those are the classes I want in my game. I am wondering whether I can make a class just called "enemies" and then maybe make sub-sets/classes that can then be called from that class (once again, any help with how to call attributes from classes will be much appreciated), so it makes the code a lot cleaner.
Please leave a comment if you are unsure about anything I just said, or if you need some more clarification with the questions. Thank you in advance.
Upvotes: 0
Views: 485
Reputation: 224
Ok so quick lesson in Python Classes. First, lets cover self. Self refers to the Object you create for that class. So lets say:
zombie1 = Zombie("bob")
self in this instance refers to the object "zombie1"
Now, to use attributes of methods. Think of a class as something that encapsulates some generic data. You have that part down it seems already. Now the attributes are those generic data types that the class contains. So for instance, your zombies name, attack, etc. To access them, we simply do as such:
zombie1.name
'bob'
zombie1.attack
5
So on and so forth. This will give you the data that is attached to those attributes.
Now to make sub classes, you could do something like this.
class Enemies(object):
def __init__(self):
self.maxhealth = 100
class Zombies(Enemies):
def __init__(self, name):
super().__init__()
self.name = name
self.health = self.maxhealth
self.attack = 5
self.golddrop = random.randint(5, 10)
You would assign an object to it like so:
zombie2 = Zombie("Mike")
The output such as this, will give you the data from the Zombie class and the Enemies Class:
zombie2.maxhealth
100
zombie.name
'Mike'
zombie2.attack
5
So on and so forth. If you look carefully, you will see that zombie2 has the attribute maxhealth, even though that is initialized and defined under Enemies. Hope this helps some!
Ill leave the enemies class up to you as far as how you wish to create generic enemy data to be used by individual monsters. I just wanted to use maxhealth as an example to show it can pull from the parent class in a sub class.
Response to comment question:
To get a players name, you can go one of two ways, create a method, or ask for it when programming it out. I will show you both ways.
class Player(object):
def __init__(self):
self.name = None
self.gold = None
def getName(self):
self.name = input("Please enter your name: ")
print(self.name)
player1 = Player()
player1.getName()
When you call player1.getName() it will ask for their name and store it to the name variable. You can then call there name via:
player1.name
'Peter'
You could also do:
class Player(object):
def __init__(self, name):
self.name = name
self.gold = None
This way you can call the players name on the object creation such as:
player1 = Player(input("Please enter your name here: "))
player1.name
'Peter'
This way allows you to specifically code a name, or ask the user for one.
Last edit for the gold bit here:
class Player(object):
def __init__(self, name):
self.name = name
self.gold = None
def giveGold(self):
print("Since you are new, 100 gold dubloons have been given to you!")
self.gold = 100
So this allows you to call Player() how ever you wish. You can hardcode a name, or ask for user input. Then run the method giveGold() on the Player() object to grant them 100 gold Dubloons.
player1 = Player("Peter")
player1.giveGold()
Since you are new, 100 gold dubloons have been given to you!
player1.gold
100
Hope that makes sense. I think I have helped get you started quite well. I will leave the rest of the game making to you :)
Upvotes: 1