Problema con un método de instancia
Publicado por David (4 intervenciones) el 27/05/2021 14:28:45
Hola, tengo la siguiente duda:
-Creo 3 clases diferentes
- En una de las clases, utilizo otra clase como atributo
-En dicha clase. al usar un método(atttack()),debería devolver un valor entero, pero el caso es que me da el siguiente error: TypeError: '>' not supported between instances of 'NoneType' and 'int'
- He probado a hacer un casting int al valor devuelto por dicho método, sin embargo sigue sin funcionar.
Pongo el código por si alguien puede echarme una mano:
-Creo 3 clases diferentes
- En una de las clases, utilizo otra clase como atributo
-En dicha clase. al usar un método(atttack()),debería devolver un valor entero, pero el caso es que me da el siguiente error: TypeError: '>' not supported between instances of 'NoneType' and 'int'
- He probado a hacer un casting int al valor devuelto por dicho método, sin embargo sigue sin funcionar.
Pongo el código por si alguien puede echarme una mano:
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import random
class Weapon():
def __init__(self,name,damage,scope):
self.name=name
self.damage=int(damage)
if scope=="short" or scope=="long":
self.scope=scope
class Pirate():
boat_life=200
def __init__(self,name,W):
self.name=name
self.weapon=W
def attack(self,enemigo):
if (self.weapon.scope=="short" and enemigo.position=="near") or (self.weapon.scope=="long" and enemigo.position=="far") or (self.weapon.scope=="long" and enemigo.position=="near"):
print("{} ha atacado a {}".format(self.name,enemigo.name))
return int(self.weapon.damage)
def hit(self,daño):
self.boat_life=self.boat_life-daño
class Enemy():
def __init__(self,name,health,position,min_damage,max_damage):
self.name=name
self.health=health
if position=="near"or position=="far":
self.position=position
self.min_damage=int(min_damage)
self.max_damage=int(max_damage)
def attack(self,Pirate):
daño=random.randint(0,1)
print("¡Oh no! {} ha atacado a {}".format(self.name,Pirate.name))
if daño==0:
return self.min_damage
else:
return self.max_damage
def move(self):
movimiento=random.randint(0,1)
if movimiento==0:
self.position="near"
else:
self.position="far"
def hit(self,daño):
self.health=self.health-daño
Espada=Weapon("Espada",3,"short")
Hacha=Weapon("Hacha",5,"short")
Arco=Weapon("Arco",2,"long")
Torvellino1=Enemy("Torvellino1",25,"near",2,5)
Torvellino2=Enemy("Torvellino2",25,"near",4,8)
Torvellino3=Enemy("Torvellino3",25,"far",3,5)
Torvellino4=Enemy("Torvellino4",25,"far",6,9)
Pyratilla=Pirate("pyratilla",Espada)
Pym=Pirate("pym",Arco)
Pyerce=Pirate("pyerce",Hacha)
ListaPiratas=[Pyratilla,Pym,Pyerce]
ListaEnemigos=[Torvellino1,Torvellino2,Torvellino3,Torvellino4]
indice=0
def restart_battle():
"""Se encarga de regenerar la batalla
"""
Pyratilla.boat_life=200
Pym.boat_life=200
Pyerce.boat_life=200
Torvellino1.health=25
Torvellino2.health=25
Torvellino3.health=25
Torvellino4.health=25
ListaPiratas=[Pyratilla,Pym,Pyerce]
ListaEnemigos=[Torvellino1,Torvellino2,Torvellino3,Torvellino4]
while len(ListaPiratas)>0 and len(ListaEnemigos)>0:
if len(ListaPiratas)<3:
restart_battle()
else:
indice=0
for pirata in ListaPiratas:
if ListaEnemigos[indice].health>0:
valor=pirata.attack(ListaEnemigos[indice])
if valor>0:
ListaEnemigos[indice].hit(valor)
else:
ListaEnemigos.pop(indice)
indicePiratas=random.randint(0,2)
for enemigos in ListaEnemigos:
decision=random.randint(0,1)
if decision==0:
enemigos.move()
else:
valor=enemigos.attack(ListaPiratas[indicePiratas])
ListaPiratas[indicePiratas].hit(valor)
if ListaPiratas[indicePiratas].boat_life<=0:
ListaPiratas.pop(indicePiratas)
if len(ListaEnemigos)==0:
print("¡¡¡La victoria es de los Pyrates")
Valora esta pregunta


0