-
Notifications
You must be signed in to change notification settings - Fork 0
/
classes.py
80 lines (73 loc) · 2.66 KB
/
classes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
from importlib import import_module
import csv
class person:
all = []
def __init__(self, name, age, weight, height, type = "base"):
self.__name = name
self.age = age
self.weight = weight
self.height = height
self.type = type
if self.type == None:
self.type = "base"
person.all.append(self)
@property
def name(self):
print("you are trying to receive the name")
return self.__name
@name.setter
def name(self, value):
print("you are trying to change the name")
self.__name = value
def __repr__(self):
return(f"{self.__class__.__name__}[{self.name},{self.age},{self.weight},{self.height},{self.type}]")
def display_stats(self):
print(f"""Name: {self.name} \nAge: {self.age} \nName: {self.weight} kg \nHeight: {self.height} cm \nType:{self.type}""")
#we use @ as a decorator to declare a class method
@classmethod
def importFromCSV(cls):
with open('persons.csv', 'r') as f:
reader = csv.DictReader(f, delimiter = ",")
items = list(reader)
for item in items:
person(
name = item.get('name'),
age = float(item.get('age')),
weight = float(item.get('weight')),
height = float(item.get('height')),
type = item.get('type'),
)
#staticMethod - does not pass class method as an argument
#treat static method like a normal, isolated function
@staticmethod
def is_integer(num):
if(isinstance(num, float)):
return num.is_integer()
elif isinstance(num, int):
return True
else:
return False
#inheritence
class fighter(person):
all = []
def __init__(self, name, age, weight, height, type = "fighter"):
super().__init__(name, age, weight, height,type)
self.health = 100
self.speed = 15
self.attackSpeed = 35
self.base_att = 15
self.base_def = 35
fighter.all.append(self)
def takeDamage(self, damageGiven):
damageGiven *= (1 - self.base_def/1000)
self.health -= damageGiven
def dealDamage(self):
return self.base_att
def displayStats(self):
return super().display_stats()
# return super().display_stats() + f"Health: {self.health} \nSpeed: {self.speed} \nAttack speed: {self.attackSpeed} \nAttack: {self.base_att} \nDefence: {self.base_def} "
person.importFromCSV()
# fighterHiro = fighter("Alex", 19, 190, 176)
# print(fighterHiro.health)
# fighterHiro.takeDamage(fighterHiro.dealDamage())
# print(fighterHiro.displayStats())