I am making a pvz simulator. We have a game board with 1 = peashooter and 2 = zombie:
[[(1, <__main__.Plant object at 0x0000011F0FDDA4C0>), (2, <__main__.Zombie object at 0x0000011F0F73D100>), 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]
When the peashooter object shoots a pea ten times, the zombie is defeated and disappears. If they are not in the same row or the peashooter is in front of the zombie, the pea has no effect on that particular zombie.
My code:
class Object:
def __init__(self, objtype, row, column, name=None):
self.pos = self.row, self.column = row, column
self.name = name
self.setup = setup
setup[row][column] = (objtype, self)
class Plant(Object):
def __init__(self,row, column, name=None, aoe=1, damage=1):
super().__init__(1, row, column, name)
self.aoe = aoe
self.damage = damage
def shoot(self):
location = self.setup[self.row][self.column]
curr_column = self.column
while True:
try:
if location[0] == 2: # zombie
location[1].hit(self)
return
except IndexError:
pass
try:
location = self.setup[self.row][curr_column]
except IndexError:
return
curr_column += 1
class PlantTypes:
@staticmethod
def peashooter(row, column, name=None):
return Plant(row, column, name)
class Zombie(Object):
def __init__(self, row, column, name=None, health=10):
super().__init__(2, row, column, name)
self.health = health
def hit(self, plant):
self.health -= plant.damage
if self.health <= 0:
setup[self.row][self.column] = 0
class ZombieTypes:
@staticmethod
def basezombie(row, column, name=None):
return Zombie(row, column, name)
setup = [
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]
]
p0 = PlantTypes.peashooter(0, 0)
z0 = ZombieTypes.basezombie(0, 1)
print(setup)
for i in range(10):
p0.shoot()
print(setup)
Any suggestions for readability/performance/other?