def __init__(self, game): super().__init__() self.game = game self.screen = self.game.screen self.velocity = Ghost.SPEED * Vector(1, 0) self.image = pg.image.load('images/red_ghost4r.png') self.rect = self.image.get_rect() self.rect.left = self.rect.width self.rect.top = self.rect.height self.x = float(self.rect.x) self.alive = True self.rev = False self.timer = None self.timerR = None self.timerL = None self.timerU = None self.timerD = None self.timer = self.timerU self.current_direction = 'up' self.moveD = 'u' self.speed = 1 self.directions_remain = [] self.eyes_animations = { 'left': 'eyes_left', 'down': 'eyes_down', 'right': 'eyes_right', 'up': 'eyes_up' } self.timerBlueGhost = TimerDual(game=self.game, images1=['g3762', 'g3770'], images2=['g3512', 'g3520'], wait1=200, wait2=200, wait_switch_timers=6000, frameindex1=0, frameindex2=0, step1=1, step2=1, looponce=False)
def danger(self): self.images_blue = self.frames('blue-dead') self.images_white = self.frames('danger-white') self.images = self.images_blue + self.images_white self.timer = TimerDual(self.images_blue, self.images_white)
class Ghost(Sprite): SPEED = 0 def __init__(self, game): super().__init__() self.game = game self.screen = self.game.screen self.velocity = Ghost.SPEED * Vector(1, 0) self.image = pg.image.load('images/red_ghost4r.png') self.rect = self.image.get_rect() self.rect.left = self.rect.width self.rect.top = self.rect.height self.x = float(self.rect.x) self.alive = True self.rev = False self.timer = None self.timerR = None self.timerL = None self.timerU = None self.timerD = None self.current_direction = 'right' self.eyes_animations = { 'left': 'eyes_left', 'down': 'eyes_down', 'right': 'eyes_right', 'up': 'eyes_up' } self.timerBlueGhost = TimerDual(game=self.game, images1=['g3762', 'g3770'], images2=['g3512', 'g3520'], wait1=200, wait2=200, wait_switch_timers=6000, frameindex1=0, frameindex2=0, step1=1, step2=1, looponce=False) def width(self): return self.rect.width def height(self): return self.rect.height def check_edges(self): r = self.rect s_r = self.screen.get_rect() return r.right >= s_r.right or r.left <= 0 def draw(self): temp_image = self.timer.imagerect() self.screen.blit(temp_image.image, self.rect) def move(self): if self.velocity == Vector(): return self.ai() self.rect.left += self.velocity.x self.rect.top += self.velocity.y self.game.limit_on_screen(self.rect) # change = False ''' wall_hit_list = pg.sprite.spritecollide(self, self.game.walls, False) if len(wall_hit_list) > 0: self.rect.left -= self.velocity.x self.rect.top -= self.velocity.y # change direction self.ai() ''' if self.alive and not self.rev: if self.velocity.x != 0: if self.velocity.x > 0: self.current_direction = 'right' self.timer = self.timerR else: self.current_direction = 'left' self.timer = self.timerL elif self.velocity.y != 0: if self.velocity.y > 0: self.current_direction = 'down' self.timer = self.timerD else: self.current_direction = 'up' self.timer = self.timerU elif self.alive and self.rev: # call run away functionality pass else: # eyes # call eyes run to start pass def reverse_draw(self): # self.timer = self.timerBlueGhost if not self.alive: # eyes # temp_image = self.eyes_animations[self.current_direction] temp_image = (ImageRect( self.screen, self.eyes_animations[self.current_direction], height=25, width=25)) self.screen.blit(temp_image.image, self.rect) return if self.timerBlueGhost.counter >= 2: self.reanimate() temp_image = self.timer.imagerect() self.screen.blit(temp_image.image, self.rect) def reverse(self): self.rev = True self.timer = self.timerBlueGhost self.timerBlueGhost.reset() def reanimate(self): self.rev = False self.alive = True # self.reset() self.timer = self.timerD def update(self): pass # self.move() # self.draw() def reverse_update(self): self.move() self.reverse_draw() def reset(self): self.timer = self.timerD self.rev = False self.alive = True def update(self): if self.rev: self.reverse_update() return self.move() self.draw() def ai(self): pass
class Ghost(Sprite): SPEED = 0 def __init__(self, game): super().__init__() self.game = game self.screen = self.game.screen self.velocity = Ghost.SPEED * Vector(1, 0) self.image = pg.image.load('images/red_ghost4r.png') self.rect = self.image.get_rect() self.rect.left = self.rect.width self.rect.top = self.rect.height self.x = float(self.rect.x) self.alive = True self.rev = False self.timer = None self.timerR = None self.timerL = None self.timerU = None self.timerD = None self.timer = self.timerU self.current_direction = 'up' self.moveD = 'u' self.speed = 1 self.directions_remain = [] self.eyes_animations = { 'left': 'eyes_left', 'down': 'eyes_down', 'right': 'eyes_right', 'up': 'eyes_up' } self.timerBlueGhost = TimerDual(game=self.game, images1=['g3762', 'g3770'], images2=['g3512', 'g3520'], wait1=200, wait2=200, wait_switch_timers=6000, frameindex1=0, frameindex2=0, step1=1, step2=1, looponce=False) def width(self): return self.rect.width def height(self): return self.rect.height def check_edges(self): r = self.rect s_r = self.screen.get_rect() return r.right >= s_r.right or r.left <= 0 def draw(self): temp_image = self.timer.imagerect() self.screen.blit(temp_image.image, self.rect) def move(self): if self.ball_stop(self.moveD) is False: if self.alive and not self.rev: if self.moveD == 'r': self.timer = self.timerR self.rect.x += self.speed elif self.moveD == 'l': self.timer = self.timerL self.rect.x -= self.speed elif self.moveD == 'd': self.timer = self.timerD self.rect.y += self.speed else: self.timer = self.timerU self.rect.y -= self.speed self.moveD = 'u' else: if self.moveD == 'r': self.rect.x += self.speed elif self.moveD == 'l': self.rect.x -= self.speed elif self.moveD == 'd': self.rect.y += self.speed else: self.rect.y -= self.speed self.moveD = 'u' self.ai() def reverse_draw(self): # self.timer = self.timerBlueGhost if not self.alive: # eyes # temp_image = self.eyes_animations[self.current_direction] temp_image = (ImageRect( self.screen, self.eyes_animations[self.current_direction], height=25, width=25)) self.screen.blit(temp_image.image, self.rect) return if self.timerBlueGhost.counter >= 2: self.reanimate() temp_image = self.timer.imagerect() self.screen.blit(temp_image.image, self.rect) def reverse(self): self.rev = True self.timer = self.timerBlueGhost self.timerBlueGhost.reset() def reanimate(self): self.rev = False self.alive = True self.timer = self.timerU self.draw() def reverse_update(self): self.move() self.reverse_draw() def reset(self): self.timer = self.timerD self.rev = False self.alive = True def update(self): if self.rev: self.reverse_update() return self.move() self.draw() def ai(self): if self.moveD == "u" and self.rect == (286, 300, 30, 30): self.moveD = "l" elif 280 < self.rect.x < 301 and 300 < self.rect.y < 360: self.moveD = "u" if self.ball_stop(self.moveD) is True and self.alive is True: self.check_directions() if len(self.directions_remain) == 0: self.moveD = "l" self.moveD = "r" self.moveD = "u" self.moveD = "d" else: rand = random.choice(self.directions_remain) self.moveD = rand if self.alive is False: self.check_directions() if self.rect.x < 190 and self.rect.y is not 300: if "r" in self.directions_remain: self.moveD = "r" elif self.ball_stop(self.moveD): rand = random.choice(self.directions_remain) self.moveD = rand elif self.rect.x > 400 and self.rect.y is not 300: if "l" in self.directions_remain: self.moveD = "l" elif self.ball_stop(self.moveD): rand = random.choice(self.directions_remain) self.moveD = rand elif self.rect.y < 300 and self.rect.x is not 286: if "d" in self.directions_remain: self.moveD = "d" elif self.ball_stop(self.moveD): rand = random.choice(self.directions_remain) self.moveD = rand elif self.rect.y > 300 and self.rect.x is not 286: if "u" in self.directions_remain: self.moveD = "u" elif self.ball_stop(self.moveD): rand = random.choice(self.directions_remain) self.moveD = rand elif 295 < self.rect.y < 320 and 265 < self.rect.x < 305: if 265 < self.rect.x < 305: self.moveD = "d" self.reanimate() elif self.rect.x < 286: self.moveD = "r" # self.reanimate() elif self.rect.x > 286: self.moveD = "l" # self.reanimate() else: rand = random.choice(self.directions_remain) self.moveD = rand def ball_stop(self, m): if m == "l": temp = self.rect.move(-10, 0) elif m == "r": temp = self.rect.move(10, 0) elif m == "u": temp = self.rect.move(0, -10) elif m == "d": temp = self.rect.move(0, 10) else: temp = self.rect.move(0, 0) for wall in self.game.walls: if wall.rect.colliderect(temp): return True return False def check_dir(self, m): if m == "left": temp = self.rect.move(-10, 0) elif m == "right": temp = self.rect.move(10, 0) elif m == "up": temp = self.rect.move(0, -10) elif m == "down": temp = self.rect.move(0, 10) else: temp = self.rect.move(0, 0) for wall in self.game.walls: if wall.rect.colliderect(temp): return True return False def check_directions(self): self.directions_remain.clear() self.current_direction = "up" if self.check_dir(self.current_direction) is False: if self.moveD is not "d": self.directions_remain.append("u") self.current_direction = "down" if self.check_dir(self.current_direction) is False: if self.moveD is not "u": self.directions_remain.append("d") self.current_direction = "left" if self.check_dir(self.current_direction) is False: if self.moveD is not "r": self.directions_remain.append("l") self.current_direction = "right" if self.check_dir(self.current_direction) is False: if self.moveD is not "l": self.directions_remain.append("r")