class Fairy(Sprite): def __init__(self, posx, posy): Sprite.__init__(self) self.move = Movement(self, thrust_strength = 15000, accelx = 3800, maxspeedx = 2000, maxspeedy = 2500, posx=posx, posy=posy) self.brain = None self.fairy_wingup = media.load_image('fairy_wingup.png') self.fairy_wingmid = media.load_image('fairy_wingmid.png') self.fairy_wingdown = media.load_image('fairy_wingdown.png') self.image = self.fairy_wingup self.rect = self.image.get_rect() self.currentframe = 1 def update(self, tick): if self.currentframe == 1: self.image = self.fairy_wingup elif self.currentframe == 2: self.image = self.fairy_wingmid elif self.currentframe == 3: self.image = self.fairy_wingdown self.currentframe += 1 if self.currentframe == 4: self.currentframe = 1 self.brain.update(tick) self.move.calculate_movement(tick) self.rect.x = self.move.posx self.rect.y = self.move.posy
class Ghost(Sprite): def __init__(self, posx, posy): Sprite.__init__(self) self.move = Movement(self, thrust_strength = 1000, accelx = 1000, accely = 1000, maxspeedx = 153, maxspeedy = 153, gravity = 0, posx = posx, posy = posy) if random.randint(0, 2) == 1: self.image = media.load_image('Cone.png') self.rect = self.image.get_rect() self.image = pygame.transform.scale(self.image, (int(self.rect.width*1.5), int(self.rect.height*1.5))) else: self.image = media.load_image('Trou.png') self.rect = self.image.get_rect() self.image = pygame.transform.scale(self.image, (self.rect.width*2, self.rect.height*2)) self.rect = self.image.get_rect() def update(self, tick): self.move.moveleft(tick) self.move.calculate_movement(tick) self.rect.x = self.move.posx self.rect.y = self.move.posy
class Ghost(Sprite): def __init__(self, posx, posy): Sprite.__init__(self) self.move = Movement(self, thrust_strength = 1000, accelx = 1000, accely = 1000, maxspeedx = 60, maxspeedy = 60, gravity = 0, posx = posx, posy = posy) self.ghost1 = media.load_image('ghost1.png') self.ghost2 = media.load_image('ghost2.png') self.image = self.ghost1 self.rect = self.image.get_rect() self.currentframe = 1 def update(self, tick): if self.currentframe >= 1 and self.currentframe <= 4: self.image = self.ghost2 elif self.currentframe >= 5 and self.currentframe <= 8: self.image = self.ghost1 self.currentframe += 1 if self.currentframe == 9: self.currentframe = 1 self.brain.update(tick) self.move.calculate_movement(tick) self.rect.x = self.move.posx self.rect.y = self.move.posy
class MainChar(Sprite): def __init__(self): self.move = Movement(self, thrust_strength = 100000, accelx = 100000, accely = 100000, maxspeedx = 120, maxspeedy = 120, gravity = 0, posx=200, posy=200) self.hunter = media.load_image('hunter.png').convert_alpha() self.firstupdate = False self.image = self.hunter self.rect = self.image.get_rect() self.imgflip = False self.dir = 1 self.fuel = 10000 self.lives = 3 self.score = 0 self.caught_fairies = Group() self.fairies_to_catch = Group() self.out_of_fuel_event = None self.no_more_life_event = None self.all_fairies_caught_event = None self.moveright(1000) def set_init_pos(self): self.move.posx = 200 self.move.posy = 200 self.move.speedx = 0 self.move.speedy = 0 self.dir = 1 self.flip() def flip(self): if not self.imgflip and self.dir == -1: self.image = pygame.transform.flip(self.image, True, False) self.imgflip = True elif self.imgflip and self.dir == 1: self.image = pygame.transform.flip(self.image, True, False) self.imgflip = False def moveright(self, tick): self.dir = 1 self.flip() self.move.moveright(tick) def moveup(self, tick): self.move.thrust(tick) def movedown(self, tick): self.move.movedown(tick) def update(self, tick): if not self.firstupdate: self.image = self.hunter self.imgflip = False self.flip() self.firstupdate = False self.move.calculate_movement(tick) self.rect.x = self.move.posx self.rect.y = self.move.posy def raise_no_more_life_event(self): self.no_more_life_event() def remove_life(self): self.lives -= 1 if self.lives == 0: self.raise_no_more_life_event()
class MainChar(Sprite): def __init__(self): self.move = Movement(self, thrust_strength = 1800, accelx = 900, maxspeedx = 1000, maxspeedy = 1000, posx=50, posy=50) self.hunter = media.load_image('hunter.png').convert_alpha() self.hunter_boost = media.load_image('hunter_boost.png').convert_alpha() self.firstupdate = False self.image = self.hunter self.rect = self.image.get_rect() self.imgflip = False self.dir = 1 self.fuel = 10000 self.lives = 3 self.caught_fairies = Group() self.fairies_to_catch = Group() self.out_of_fuel_event = None self.no_more_life_event = None self.all_fairies_caught_event = None def set_init_pos(self): self.move.posx = 33 self.move.posy = 50 self.move.speedx = 0 self.move.speedy = 0 self.dir = 1 self.flip() def flip(self): if not self.imgflip and self.dir == -1: self.image = pygame.transform.flip(self.image, True, False) self.imgflip = True elif self.imgflip and self.dir == 1: self.image = pygame.transform.flip(self.image, True, False) self.imgflip = False def moveleft(self, tick): self.dir = -1 self.flip() if self.move.speedy == 0: self.move.moveleft(tick) def moveright(self, tick): self.dir = 1 self.flip() if self.move.speedy == 0: self.move.moveright(tick) def thrust(self, tick): if self.fuel > 0: self.image = self.hunter_boost self.imgflip = False self.flip() self.firstupdate = True if self.imgflip: self.move.moveleft(tick / 2) else: self.move.moveright(tick / 2) self.move.thrust(tick) self.fuel -= tick / 8 else: self.fuel = 0 self.out_of_fuel_event() def update(self, tick): if not self.firstupdate: self.image = self.hunter self.imgflip = False self.flip() self.firstupdate = False self.move.calculate_movement(tick) if self.move.speedx > 0: self.move.speedx -= 1 elif self.move.speedx < 0: self.move.speedx += 1 self.rect.x = self.move.posx self.rect.y = self.move.posy self.catch_fairies() def raise_out_of_fuel_event(self): self.out_of_fuel_event() def raise_no_more_life_event(self): self.no_more_life_event() def raise_all_fairies_caught_event(self): self.all_fairies_caught_event() def remove_life(self): self.lives -= 1 if self.lives == 0: self.raise_no_more_life_event() def catch_fairies(self): for f in self.fairies_to_catch: dist = math.sqrt((f.rect.centerx - self.rect.centerx)**2 + (f.rect.centery - self.rect.centery)**2) if dist < 20: f.kill() self.caught_fairies.add(f) if len(self.fairies_to_catch) == 0: self.raise_all_fairies_caught_event()