def __init__(self, screen, maze): super(PMAN, self).__init__() self.screen = screen.get_rect() self.image = ImageRect(screen, "pac_left_2", PMAN.PAC_MAN_SIZE, PMAN.PAC_MAN_SIZE) self.rect = self.image.rect self.rect.x = self.screen.centerx - 10 self.rect.y = self.screen.centery + 110 self.maze = maze self.direction = "l" self.move = "l" self.speed = 5 self.tick_speed = 10 self.timer = pygame.time.get_ticks() # images self.index = 1 self.dead_index = 1 self.sound_index = 1 self.direction_l = False self.direction_r = False self.direction_u = False self.direction_d = False self.lives = 3 self.score = 0
def update(self, maze, screen): if gf.ball_stop(self, maze) is False and pygame.time.get_ticks( ) - self.timer >= self.tick_speed: self.timer = pygame.time.get_ticks() if self.move == "l": self.image.rect.x -= self.speed file = "pac_left_" + str(math.floor(self.index)) elif self.move == "r": self.image.rect.x += self.speed file = "pac_right_" + str(math.floor(self.index)) elif self.move == "d": self.image.rect.y += self.speed file = "pac_down_" + str(math.floor(self.index)) elif self.move == "u": self.image.rect.y -= self.speed file = "pac_up_" + str(math.floor(self.index)) else: file = "p_up_1" self.image = ImageRect(screen, file, PMAN.PAC_MAN_SIZE, PMAN.PAC_MAN_SIZE) self.image.rect = self.rect if self.index >= 4: self.index = 1 else: self.index += .3
def __init__(self, screen): super(Ghost, self).__init__() self.screen = screen self.image = ImageRect(screen, 'clearGhost', 128, 128) self.rect = self.image.rect self.x, self.y = 300, 360 # Type of ghost this is self.type = 0
def __init__(self, screen, mazefile): self.screen = screen self.filename = mazefile # load the maze txt file with open('maze.txt', 'r') as f: self.rows = f.readlines() # create arrays and upload images for block, ball, line(walls), and power up balls self.bricks = [] self.brick = ImageRect(screen, "wall", Maze.STANDARD_SIZE, Maze.STANDARD_SIZE) self.balls = [] self.ball = ImageRect(screen, "ball", Maze.STANDARD_SIZE, Maze.STANDARD_SIZE) self.lines = [] self.line = ImageRect(screen, "line", Maze.STANDARD_SIZE, Maze.STANDARD_SIZE) self.power_up_balls = [] self.power_up_ball = ImageRect(screen, "powerUp", 12, 12) self.build()
def __init__(self, screen): super(Point, self).__init__() self.screen = screen self.screen_rect = screen.get_rect() self.image = ImageRect(screen, 'point', 4, 4) self.rect = self.image.rect self.x = float(self.rect.x) self.y = float(self.rect.y)
def __init__(self, screen, maze): super(OrangeGhost, self).__init__() self.screen = screen.get_rect() self.image = ImageRect(screen, "orange_left_1", 30, 30) self.rect = self.image.rect self.rect.x = 350 self.rect.y = 330 self.maze = maze self.direction = "u" self.move = "u" self.speed = 5 self.tick_dead = 30 self.tick_alive = 10 self.tick_speed = self.tick_alive self.index = 1 self.dead_index = 1 self.dead_timer = 1 self.directions_remain = [] self.alive = True self.eat = False self.timer = pygame.time.get_ticks()
def ghost_collision(self, redghost, blueghost, pinkghost, orangeghost, stats, screen): if pygame.sprite.collide_rect(self, redghost) and redghost.alive is True: stats.game_pause = True die = pygame.mixer.Sound('sounds/death.wav') die.play() elif pygame.sprite.collide_rect(self, blueghost) and blueghost.alive is True: stats.game_pause = True die = pygame.mixer.Sound('sounds/death.wav') die.play() elif pygame.sprite.collide_rect(self, pinkghost) and pinkghost.alive is True: stats.game_pause = True die = pygame.mixer.Sound('sounds/death.wav') die.play() elif pygame.sprite.collide_rect( self, orangeghost) and orangeghost.alive is True: stats.game_pause = True die = pygame.mixer.Sound('sounds/death.wav') die.play() if stats.game_pause and redghost.alive is True: file = "pac_dead_" + str(math.floor(self.dead_index)) self.image = ImageRect(screen, file, PMAN.PAC_MAN_SIZE, PMAN.PAC_MAN_SIZE) self.image.rect = self.rect if self.dead_index >= 6: self.dead_index = 1 stats.game_pause = False self.lives -= 1 gf.reset(self, redghost, blueghost, pinkghost, orangeghost, stats) else: self.dead_index += .2 if self.lives == 0: stats.game_over = True
def __init__(self, screen): super(Pacman, self).__init__() self.screen = screen self.screen_rect = screen.get_rect() self.pacimg = ImageRect(screen, 'pacman', 32, 32) self.rect = self.pacimg.rect # Movement flag self.moving_up = False self.moving_down = False self.moving_left = False self.moving_right = False self.movement_switches = 0 self.can_warp = True self.pacimg.rect.centerx = self.screen_rect.centerx - 4 #self.pacimg.rect.bottom = self.screen_rect.bottom - 124 self.pacimg.rect.top = self.screen_rect.top + 128
def __init__(self, screen, mazefile, brickfile, orangeportalfile, blueportalfile, shieldfile, pointfile): self.screen = screen self.filename = mazefile with open(self.filename, 'r') as f: self.rows = f.readlines() self.bricks = [] self.shields = [] self.portals = [] self.points = Group() sz = Maze.BRICK_SIZE self.brick = ImageRect(screen, brickfile, sz, sz) self.shield = ImageRect(screen, shieldfile, sz, sz) self.blueportal = ImageRect(screen, blueportalfile, 10 * sz, 20 * sz) self.orangeportal = ImageRect(screen, orangeportalfile, 20 * sz, 10 * sz) self.deltax = self.deltay = Maze.BRICK_SIZE self.build(self.points, self.screen)
class PMAN(Sprite): PAC_MAN_SIZE = 30 def __init__(self, screen, maze): super(PMAN, self).__init__() self.screen = screen.get_rect() self.image = ImageRect(screen, "pac_left_2", PMAN.PAC_MAN_SIZE, PMAN.PAC_MAN_SIZE) self.rect = self.image.rect self.rect.x = self.screen.centerx - 10 self.rect.y = self.screen.centery + 110 self.maze = maze self.direction = "l" self.move = "l" self.speed = 5 self.tick_speed = 10 self.timer = pygame.time.get_ticks() # images self.index = 1 self.dead_index = 1 self.sound_index = 1 self.direction_l = False self.direction_r = False self.direction_u = False self.direction_d = False self.lives = 3 self.score = 0 def update(self, maze, screen): if gf.ball_stop(self, maze) is False and pygame.time.get_ticks( ) - self.timer >= self.tick_speed: self.timer = pygame.time.get_ticks() if self.move == "l": self.image.rect.x -= self.speed file = "pac_left_" + str(math.floor(self.index)) elif self.move == "r": self.image.rect.x += self.speed file = "pac_right_" + str(math.floor(self.index)) elif self.move == "d": self.image.rect.y += self.speed file = "pac_down_" + str(math.floor(self.index)) elif self.move == "u": self.image.rect.y -= self.speed file = "pac_up_" + str(math.floor(self.index)) else: file = "p_up_1" self.image = ImageRect(screen, file, PMAN.PAC_MAN_SIZE, PMAN.PAC_MAN_SIZE) self.image.rect = self.rect if self.index >= 4: self.index = 1 else: self.index += .3 def ghost_collision(self, redghost, blueghost, pinkghost, orangeghost, stats, screen): if pygame.sprite.collide_rect(self, redghost) and redghost.alive is True: stats.game_pause = True die = pygame.mixer.Sound('sounds/death.wav') die.play() elif pygame.sprite.collide_rect(self, blueghost) and blueghost.alive is True: stats.game_pause = True die = pygame.mixer.Sound('sounds/death.wav') die.play() elif pygame.sprite.collide_rect(self, pinkghost) and pinkghost.alive is True: stats.game_pause = True die = pygame.mixer.Sound('sounds/death.wav') die.play() elif pygame.sprite.collide_rect( self, orangeghost) and orangeghost.alive is True: stats.game_pause = True die = pygame.mixer.Sound('sounds/death.wav') die.play() if stats.game_pause and redghost.alive is True: file = "pac_dead_" + str(math.floor(self.dead_index)) self.image = ImageRect(screen, file, PMAN.PAC_MAN_SIZE, PMAN.PAC_MAN_SIZE) self.image.rect = self.rect if self.dead_index >= 6: self.dead_index = 1 stats.game_pause = False self.lives -= 1 gf.reset(self, redghost, blueghost, pinkghost, orangeghost, stats) else: self.dead_index += .2 if self.lives == 0: stats.game_over = True def blitme(self): self.image.blitme()
class Maze: RED = (255, 0, 0) BRICK_SIZE = 3 def __init__(self, screen, mazefile, brickfile, orangeportalfile, blueportalfile, shieldfile, pointfile): self.screen = screen self.filename = mazefile with open(self.filename, 'r') as f: self.rows = f.readlines() self.bricks = [] self.shields = [] self.portals = [] self.points = Group() sz = Maze.BRICK_SIZE self.brick = ImageRect(screen, brickfile, sz, sz) self.shield = ImageRect(screen, shieldfile, sz, sz) self.blueportal = ImageRect(screen, blueportalfile, 10 * sz, 20 * sz) self.orangeportal = ImageRect(screen, orangeportalfile, 20 * sz, 10 * sz) self.deltax = self.deltay = Maze.BRICK_SIZE self.build(self.points, self.screen) def __str__(self): return 'maze(' + self.filename + ')' def build(self, points, screen): r = self.brick.rect rshield = self.shield.rect rblue = self.blueportal.rect rorange = self.orangeportal.rect w, h = r.width, r.height dx, dy = self.deltax, self.deltay index = 0 for nrow in range(len(self.rows)): row = self.rows[nrow] for ncol in range(len(row)): col = row[ncol] if col == 'X': self.bricks.append(pygame.Rect(ncol * dx, nrow * dy, w, h)) elif col == 's': self.shields.append(pygame.Rect(ncol * dx, nrow * dy, rshield.width, rshield.height)) elif col == 'o': self.orangeportal.rect = pygame.Rect(dx + 12, (nrow - 6) * dy, rorange.width, rorange.height) elif col == 'b': self.blueportal.rect = pygame.Rect((ncol - 12) * dx, (nrow - 6) * dy, rblue.width, rblue.height) elif col == 'p': point = Point(screen) point.x = ncol * dx point.y = nrow * dy point.rect.x = point.x point.rect.y = point.y points.add(point) def blitme(self): for rect in self.bricks: self.screen.blit(self.brick.image, rect) for rect in self.shields: self.screen.blit(self.shield.image, rect) self.orangeportal.blit() self.blueportal.blit() for point in self.points: point.blit()
class RedGhost(Sprite): def __init__(self, screen, maze): super(RedGhost, self).__init__() self.screen = screen.get_rect() self.image = ImageRect(screen, "red_left_1", 30, 30) self.rect = self.image.rect self.rect.x = 300 self.rect.y = 230 self.maze = maze self.direction = "u" self.move = "u" self.speed = 5 self.tick_dead = 30 self.tick_alive = 10 self.tick_speed = self.tick_alive self.index = 1 self.dead_index = 1 self.dead_timer = 1 self.directions_remain = [] self.alive = True self.eat = False self.timer = pygame.time.get_ticks() def update(self, maze, screen, pm): if gf.ball_stop(self, maze) is False and pygame.time.get_ticks() - self.timer >= self.tick_speed: self.timer = pygame.time.get_ticks() if self.move == "l": self.image.rect.x -= self.speed if self.eat is False: file = "red_left_" + str(math.floor(self.index)) else: file = "eyes_left" elif self.move == "r": self.image.rect.x += self.speed if self.eat is False: file = "red_right_" + str(math.floor(self.index)) else: file = "eyes_right" elif self.move == "d": self.image.rect.y += self.speed if self.eat is False: file = "red_down_" + str(math.floor(self.index)) else: file = "eyes_down" elif self.move == "u": self.image.rect.y -= self.speed if self.eat is False: file = "red_up_" + str(math.floor(self.index)) else: file = "eyes_up" else: file = "red_left_1" self.image = ImageRect(screen, file, 30, 30) self.image.rect = self.rect if self.index > 2.5: self.index = 1 else: self.index += .1 if self.alive is False and self.eat is False: file = "blink_" + str(math.floor(self.dead_index)) self.image = ImageRect(screen, file, 30, 30) self.image.rect = self.rect if self.dead_timer > 20: self.alive = True else: self.dead_timer += .05 if self.dead_timer < 15: if self.dead_index > 2.5: self.dead_index = 1 else: self.dead_index += .1 else: if self.dead_index > 3.5: self.dead_index = 2 else: self.dead_index += .1 if self.alive is True: self.tick_speed = self.tick_alive elif self.alive is False: self.tick_speed = self.tick_dead self.self_movement(maze) self.dead_collide(pm, maze) def self_movement(self, maze): if self.move == "u" and self.rect == (300, 230, 30, 30): self.move = "l" elif 295 < self.rect.x < 305 and 325 < self.rect.y < 335: self.move = "u" if gf.ball_stop(self, maze) is True and self.eat is False: self.check_directions(maze) if len(self.directions_remain) == 0: self.move = "l" self.move = "r" self.move = "u" self.move = "d" else: rand = random.choice(self.directions_remain) self.move = rand if self.eat is True: self.check_directions(maze) if self.rect.x < 190 and self.rect.y is not 230: if "r" in self.directions_remain: self.move = "r" elif gf.ball_stop(self, maze): rand = random.choice(self.directions_remain) self.move = rand elif self.rect.x > 410 and self.rect.y is not 230: if "l" in self.directions_remain: self.move = "l" elif gf.ball_stop(self, maze): rand = random.choice(self.directions_remain) self.move = rand elif self.rect.y < 230 and self.rect.x is not 300: if "d" in self.directions_remain: self.move = "d" elif gf.ball_stop(self, maze): rand = random.choice(self.directions_remain) self.move = rand elif self.rect.y > 230 and self.rect.x is not 300: if "u" in self.directions_remain: self.move = "u" elif gf.ball_stop(self, maze): rand = random.choice(self.directions_remain) self.move = rand elif self.rect.y is 230 and 190 < self.rect.x < 410: if 295 < self.rect.x < 305: self.move = "d" elif self.rect.x < 300: self.move = "r" elif self.rect.x > 300: self.move = "l" else: rand = random.choice(self.directions_remain) self.move = rand def check_directions(self, maze): self.directions_remain.clear() self.direction = "u" if gf.brick_collision(self, maze) is False: if self.move is not "d": self.directions_remain.append("u") self.direction = "d" if gf.brick_collision(self, maze) is False: if self.move is not "u": self.directions_remain.append("d") self.direction = "l" if gf.brick_collision(self, maze) is False: if self.move is not "r": self.directions_remain.append("l") self.direction = "r" if gf.brick_collision(self, maze) is False: if self.move is not "l": self.directions_remain.append("r") def blitme(self): self.image.blitme() def dead_collide(self, pm, maze): if pygame.sprite.collide_rect(self, pm) and self.alive is False: if self.alive is False and self.eat is False: pm.score += 200 eat = pygame.mixer.Sound('sounds/eatghost.wav') eat.play() self.eat = True if self.eat is True: for lines in maze.lines: if lines.colliderect(self): self.eat = False self.alive = True
def update(self, maze, screen, pm): if gf.ball_stop(self, maze) is False and pygame.time.get_ticks() - self.timer >= self.tick_speed: self.timer = pygame.time.get_ticks() if self.move == "l": self.image.rect.x -= self.speed if self.eat is False: file = "orange_left_" + str(math.floor(self.index)) else: file = "eyes_left" elif self.move == "r": self.image.rect.x += self.speed if self.eat is False: file = "orange_right_" + str(math.floor(self.index)) else: file = "eyes_right" elif self.move == "d": self.image.rect.y += self.speed if self.eat is False: file = "orange_down_" + str(math.floor(self.index)) else: file = "eyes_down" elif self.move == "u": self.image.rect.y -= self.speed if self.eat is False: file = "orange_up_" + str(math.floor(self.index)) else: file = "eyes_up" else: file = "orange_left_1" self.image = ImageRect(screen, file, 30, 30) self.image.rect = self.rect if self.index > 2.5: self.index = 1 else: self.index += .1 if self.alive is False and self.eat is False: file = "blink_" + str(math.floor(self.dead_index)) self.image = ImageRect(screen, file, 30, 30) self.image.rect = self.rect if self.dead_timer > 20: self.alive = True else: self.dead_timer += .05 if self.dead_timer < 15: if self.dead_index > 2.5: self.dead_index = 1 else: self.dead_index += .1 else: if self.dead_index > 3.5: self.dead_index = 2 else: self.dead_index += .1 if self.alive is True: self.tick_speed = self.tick_alive elif self.alive is False: self.tick_speed = self.tick_dead self.self_movement(maze) self.dead_collide(pm, maze)