pyglet.image.ImageGrid(self, image, rows, columns, …)
- image : 이미지의 주소값
- row, col : 애니메이션으로 만들 이미지가 몇 행 몇 열인지
pyglet.image.Animation.from_image_sequence(sequence, period, loop=True)
- period: second, 주기
- sequence : ImageGrid를 통해 로드한 값
📌 def load_animation(imgage):
= Static Method도 Class method도 아니여서 instance로 call이 불가능 하다!
class Alien(Actor):
def load_animation(imgage):
seq = ImageGrid(load(imgage), 2, 1)
return Animation.from_image_sequence(seq, 0.5)
# Static member Variable Dic : (애니메이션, score)
TYPES = {
'1': (load_animation('img/alien1.png'), 40),
'2': (load_animation('img/alien2.png'), 20),
'3': (load_animation('img/alien3.png'), 10)
}
✅ def __init__(self, img, x, y, score, column=None):
self.column
: alien def __init__(self, img, x, y, score, column=None):
super(Alien, self).__init__(img, x, y)
self.score = score
self.column = column
📌 def from_type(x, y, alien_type, column):
= Alien을 만들어서 return
--> AlienColumn Class의 생성자에서 self.aliens
멤버 변수 만들 때 호출
def from_type(x, y, alien_type, column):
animation, score = Alien.TYPES[alien_type]
return Alien(animation, x, y, score, column)
📌 def on_exit(self):
= Alien이 없어질 때 call되는 함수
--> ⭐ super?
def on_exit(self):
super(Alien, self).on_exit()
if self.column:
self.column.remove(self)
enumerate(iterable, start=0)
- i : 0,1,2,3,4 ...
- alien : element 3, 3, 2, 2, 1
for i, alien in alien_types
class AlienColumn(object):
def __init__(self, x, y):
alien_types = enumerate(['3', '3', '2', '2', '1'])
self.aliens = [Alien.from_type(x, y+i*60, alien, self)
for i, alien in alien_types]
📌 def should_turn(self, d):
= 외계인들이 벽에 부딪혔을 때 방향을 바꾸어주는 함수
🔃
AlienGroup Class
- def side_reached(self): 에서 호출
def should_turn(self, d):
if len(self.aliens) == 0:
return False
alien = self.aliens[0]
x, width = alien.x, alien.parent.width
return x >= width - 50 and d == 1 or x <= 50 and d == -1
📌 def remove(self, alien):
= alien을 제거
def remove(self, alien):
self.aliens.remove(alien)
📌 def shoot(self):
= 총알 생성
def shoot(self):
# alien이 존재할 때 1/1000의 확률로 총알 생성
if random.random() < 0.001 and len(self.aliens) > 0:
pos = self.aliens[0].position
return Shoot(pos[0], pos[1] - 50)
return None
class AlienGroup(object):
def __init__(self, x, y):
self.columns = [AlienColumn(x + i * 60, y)
for i in range(10)]
self.speed = eu.Vector2(10, 0)
self.direction = 1
self.elapsed = 0.0
self.period = 1.0
📌 def update(self, elapsed):
def update(self, elapsed):
self.elapsed += elapsed
while self.elapsed >= self.period:
self.elapsed -= self.period
offset = self.direction * self.speed
if self.side_reached():
self.direction *= -1
offset = eu.Vector2(0, -10)
for alien in self:
alien.move(offset)
📌 def side_reached(self)
def side_reached(self):
return any(map(lambda c: c.should_turn(self.direction),
self.columns))