9.7 객체지향으로 개발하기 4
class Explosion(Actor):
def __init__(self, img_names, pos):
super().__init__(img_names[0], pos)
self.images = img_names
self.fps = 8
self.duration = 15
def update(self):
self.animate()
self.duration -= 1
class Tank(Actor, CheckOutOfScreen):
def __init__(self, img_name, pos, angle, walls, screen):
Actor.__init__(self, img_name, pos)
CheckOutOfScreen.__init__(self, screen)
self._original_pos = None
self.angle = angle
self.walls = walls
self.bullets = []
self.explosions = []
... 생략
def draw(self):
super().draw()
for bullet in self.bullets:
bullet.draw()
for explosion in self.explosions:
explosion.draw()
def collidelist_bullets(self, tanks):
tank_index = -1
for bullet in self.bullets:
bullet.move()
# 화면경계 확인
if bullet.is_out_of_screen():
self.bullets.remove(bullet)
# 벽 더미 확인
wall_index = bullet.collidelist(self.walls)
if wall_index != -1:
del self.walls[wall_index]
self.bullets.remove(bullet)
# 상대 탱크 확인
tank_index = bullet.collidelist(tanks)
if tank_index != -1:
self.bullets.remove(bullet)
explosion = Explosion(["explosion3", "explosion4"], \
tanks[tank_index].pos)
self.explosions.append(explosion)
# 폭발 에니메이션
for explosion in self.explosions:
explosion.update()
if explosion.duration == 0:
self.explosions.remove(explosion)
return tank_indexLast updated