def __init__(self): pygame.init() self.screen = pygame.display.set_mode((680, 740)) self.ai_settings = Settings() pygame.display.set_caption("Pacman Portal") self.maze = Maze(self.screen, mazefile='map.txt', brickfile='square', portalfile='square', shieldfile='shield', pointfile='point', dotfile='dot', powerpillfile='powerpill') self.pacman = Pacman(self.ai_settings, self.screen, self.maze) self.ghosts = Ghosts(self.ai_settings, self.screen, self.maze, self.pacman) self.displayname = Button(self.screen, "PacmanPortal") self.play_button = Button(self.screen, "PLAY GAME") self.highscore_button = Button(self.screen, "HIGH SCORES") self.blinkyname = Button(self.screen, "BLiNKY") self.pinkyname = Button(self.screen, "PiNKY") self.clydename = Button(self.screen, "CLYDE") self.inkeyname = Button(self.screen, "iNKEY") self.back = Button(self.screen, "Back") self.sb = Scoreboard(self.ai_settings, self.screen, self.maze, self.pacman) self.score = Button(self.screen, "hello") self.blues = 0
def start_game(self): self.nodes = NodeGroup("Mazes/maze1.txt") self.pellets = PelletGroup("Mazes/maze1.txt") self.pacman = Pacman(self.nodes, self.sheet) self.ghosts = Ghosts(self.nodes, self.sheet) self.paused = True self.lifeIcons = Lives(self.sheet) self.maze = Maze(self.sheet) self.maze.get_maze("Mazes/maze1") self.maze.stitch_maze(self.background) self.pelletsEaten = 0 self.switch_song(1)
def __init__(self): pygame.init() self.screen = pygame.display.set_mode((1200, 800)) pygame.display.set_caption("Pacman Portal") # self.expandfile = ExpandFile('images/pacman_maze.txt', expandBy=3) self.maze = Maze(self.screen, mazefile='images/pacman_maze_expanded_pts.txt', brickfile='brick', pointfile='point') self.pacman = Pacman(self.screen) self.pac_group = Group() self.ghosts = Ghosts(self.screen) self.play_button = Button(self.screen, "Play") self.stats = Stats()
def test_ghost_and_walls_collision(self): test_ghost = Ghosts(1, 180, 190) test_ghost_direction = "right" new_direction = self.ghosts.ghost_move(self, test_ghost, test_ghost_direction, self.game.walls) collision = self.wall.collision(self, test_ghost, self.game.walls) self.assertEqual(collision, True) self.assertNotEqual(new_direction, test_ghost_direction)
def test_ghost_move_up(self): test_ghost = Ghosts(2, 100, 460) test_ghost_direction = "up" x = 100 y = 459 self.ghosts.ghost_move(self, test_ghost, test_ghost_direction, self.game.walls) self.assertEqual(test_ghost.rect.x, x) self.assertEqual(test_ghost.rect.y, y)
def __init__(self): pygame.init() self.screen = pygame.display.set_mode((680, 740)) pygame.display.set_caption("Pacman Portal") self.maze = Maze(self.screen, mazefile='maze.txt', brickfile='square', portalfile='close_portal', shieldfile='shield', powerfile='powerpil', pointsfile='points', foodfile='cherry') self.menu = Startup(self.screen, 'title.png', 'playbutton.png') self.pacman = Pacman(self.screen) self.ghosts = Ghosts(self.screen) self.eloop = EventLoop(finished=False)
def __init__(self): """class makes pacman move and draws the screen""" self.move_down = False self.move_up = False self.move_right = False self.move_left = False self._pacman_start = True self.first_game_screen = True self.game_over_screen = False self.game_win_screen = False self.game_over = False self.game_win = False self.play_again = None self.width = 890 self.height = 660 self.x = 100 self.y = 100 self._new_x = 0 self._new_y = 0 self._collision = False self.screen = pygame.display.set_mode((self.width, self.height)) pygame.display.set_caption("Pac-Man") self.walls = pygame.sprite.Group() self.pacman_group = pygame.sprite.GroupSingle() self.points = pygame.sprite.Group() self.ghosts_list = pygame.sprite.Group() self.pacman = Pacman(self.x, self.y, self.move_left, self.move_down, self.move_up) self.ghosts = Ghosts(1, 500, 210) self.ghost_1 = Ghosts(1, 775, 40) self.ghost_2 = Ghosts(2, 500, 210) self.ghost_3 = Ghosts(3, 100, 460) self._ghost_collision = False self.ghosts_list.add(self.ghost_1, self.ghost_2, self.ghost_3) self.ghosts_list.add(self.ghost_2) self.ghost_1_direction = "left" self.ghost_2_direction = "right" self.ghost_3_direction = "down" self.wall = Wall(self.x, self.y, self.width, self.height) self.wall.draw_walls(self.screen, self.walls) self.point = Point(self.x, self.y) self.points = self.point.draw_points(self.points) self.point_count = 0 pygame.init() # pylint: disable=(no-member)
def make_enemies(self): counter = 0 for ghost_type, position in enumerate(self.ghosts_positions): self.ghosts.append( Ghosts(game=self, player=self.player, settings=self.settings, position_x=self.ghosts_positions[counter][0], position_y=self.ghosts_positions[counter][1], ghost_type=ghost_type)) counter += 1
class Game: BLACK = (0, 0, 0) def __init__(self): pygame.init() self.screen = pygame.display.set_mode((680, 740)) pygame.display.set_caption("Pacman Portal") self.maze = Maze(self.screen, mazefile='maze.txt', brickfile='square', portalfile='close_portal', shieldfile='shield', powerfile='powerpil', pointsfile='points', foodfile='cherry') self.menu = Startup(self.screen, 'title.png', 'playbutton.png') self.pacman = Pacman(self.screen) self.ghosts = Ghosts(self.screen) self.eloop = EventLoop(finished=False) def __str__(self): return 'Game(Pacman Portal),maze=' def play(self): while not self.eloop.finished: self.eloop.check_events(self.pacman) self.pacman.update() # if self.game_active: self.update_screen() def update_screen(self): self.screen.fill(Game.BLACK) self.maze.blitme() self.pacman.blit_pac() self.ghosts.blit_ghost() # if not self.eloop.game_active: # self.menu.draw_menu() pygame.display.flip()
class Game: BLACK = (0, 0, 0) def __init__(self): pygame.init() self.screen = pygame.display.set_mode((1200, 800)) pygame.display.set_caption("Pacman Portal") # self.expandfile = ExpandFile('images/pacman_maze.txt', expandBy=3) self.maze = Maze(self.screen, mazefile='images/pacman_maze_expanded_pts.txt', brickfile='brick', pointfile='point') self.pacman = Pacman(self.screen) self.pac_group = Group() self.ghosts = Ghosts(self.screen) self.play_button = Button(self.screen, "Play") self.stats = Stats() def __str__(self): return 'Game(Pacman Portal), maze=' + str(self.maze) + ')' def play(self): eloop = EventLoop(finished=False) while not eloop.finished: while not self.stats.game_active: self.play_button.draw_button() eloop.check_events(self.pacman, self.play_button, self.stats) eloop.check_events(self.pacman, self.play_button, self.stats) self.pacman.update(self.maze, self.pac_group) self.update_screen() def update_screen(self): self.screen.fill(Game.BLACK) self.maze.blitme() self.pacman.blitme() self.ghosts.blitme() pygame.display.flip()
def __init__(self, ai_settings, screen, maze): """Initialize the ship and set its starting position.""" super(Pacman, self).__init__() self.screen = screen self.ai_settings = ai_settings self.maze = maze self.size = 30 self.ghosts = Ghosts(self.ai_settings, self.screen, self.maze, self) # Get ship from sprite sheet and load its image and rect. self.ss = spritesheet.Spritesheet('images/pacman.png') self.s1 = spritesheet.Spritesheet('images/pacmandeath.png') self.image = self.ss.image_at((0, 0, 16, 16), -1) self.image = pygame.transform.scale(self.image, (self.size, self.size)) self.rect = self.image.get_rect() # Initilize all pacman assets self.image4 = self.s1.image_at((0, 0, 16, 16), -1) self.image4 = pygame.transform.scale(self.image4, (self.size, self.size)) self.image5 = self.s1.image_at((16, 0, 16, 16), -1) self.image5 = pygame.transform.scale(self.image5, (self.size, self.size)) self.imageright1 = self.ss.image_at((0, 0, 16, 16), -1) self.imageright1 = pygame.transform.scale(self.imageright1, (self.size, self.size)) self.imageright2 = self.ss.image_at((16, 0, 16, 16), -1) self.imageright2 = pygame.transform.scale(self.imageright2, (self.size, self.size)) self.imageleft1 = self.ss.image_at((0, 16, 16, 16), -1) self.imageleft1 = pygame.transform.scale(self.imageleft1, (self.size, self.size)) self.imageleft2 = self.ss.image_at((16, 16, 16, 16), -1) self.imageleft2 = pygame.transform.scale(self.imageleft2, (self.size, self.size)) self.imageup1 = self.ss.image_at((0, 32, 16, 16), -1) self.imageup1 = pygame.transform.scale(self.imageup1, (self.size, self.size)) self.imageup2 = self.ss.image_at((16, 32, 16, 16), -1) self.imageup2 = pygame.transform.scale(self.imageup2, (self.size, self.size)) self.imagedown1 = self.ss.image_at((0, 48, 16, 16), -1) self.imagedown1 = pygame.transform.scale(self.imagedown1, (self.size, self.size)) self.imagedown2 = self.ss.image_at((16, 48, 16, 16), -1) self.imagedown2 = pygame.transform.scale(self.imagedown2, (self.size, self.size)) self.image3 = self.ss.image_at((32, 0, 16, 16), -1) self.image3 = pygame.transform.scale(self.image3, (self.size, self.size)) self.screen = screen self.screen_rect = screen.get_rect() self.rect.center = self.screen_rect.center self.waka = pygame.mixer.Sound('sounds/waka.wav') self.intromus = pygame.mixer.Sound('sounds/intro.wav') self.dsound = pygame.mixer.Sound('sounds/death.wav') self.esound = pygame.mixer.Sound('sounds/eatghost.wav') self.dead = 0 # Load the ship image and get its rect. # self.image = pygame.image.load('images/ship.bmp') # self.rect = self.image.get_rect() # Start each new ship at the bottom center of the screen. # self.rect.centerx = self.screen_rect.centerx # self.rect.bottom = self.screen_rect.bottom # Store a decimal value for the ship's center self.centerx = float(self.rect.centerx) self.centery = float(self.rect.centery) # Movement flags self.moving_right = False self.moving_left = False self.moving_up = False self.moving_down = False self.tick = 0 self.i = 0
class GameController(object): def __init__(self): pygame.init() self.screen = pygame.display.set_mode(SCREENSIZE, 0, 32) self.background = None self.set_background() self.clock = pygame.time.Clock() self.score = 0 self.font = pygame.font.SysFont("arial", 20) self.lives = 5 self.fruit = None self.pelletsEaten = 0 self.sheet = SpriteSheet() self.cur_song = 2 self.data_driver = MongoDriver("1") self.level = 1 def switch_song(self, delta): self.cur_song += delta if self.cur_song == 5: self.cur_song = 1 if self.cur_song == 0: self.cur_song = 4 if self.cur_song == 1: pygame.mixer.music.load("Music/song1.mp3") elif self.cur_song == 2: pygame.mixer.music.load("Music/song2.mp3") elif self.cur_song == 3: pygame.mixer.music.load("Music/song3.mp3") elif self.cur_song == 4: pygame.mixer.music.load("Music/song4.mp3") pygame.mixer.music.play(999) pygame.event.wait() def set_background(self): self.background = pygame.Surface(SCREENSIZE).convert() self.background.fill(BLACK) def start_game(self): self.nodes = NodeGroup("Mazes/maze1.txt") self.pellets = PelletGroup("Mazes/maze1.txt") self.pacman = Pacman(self.nodes, self.sheet) self.ghosts = Ghosts(self.nodes, self.sheet) self.paused = True self.lifeIcons = Lives(self.sheet) self.maze = Maze(self.sheet) self.maze.get_maze("Mazes/maze1") self.maze.stitch_maze(self.background) self.pelletsEaten = 0 self.switch_song(1) def update(self): dt = self.clock.tick(30) / 1000.0 if not self.paused: self.pacman.update(dt) self.ghosts.update(dt, self.pacman) if self.fruit is not None: self.fruit.update(dt) else: if not self.pacman.alive: self.pacman.update(dt) if self.pacman.deathSequenceFinished: if self.lives == 0: self.start_game() else: self.restart_level() self.check_events() self.render() def save_game(self): self.data_driver.clean() self.data_driver.save(self.pellets.get_snapshot(), self.score, self.lives, self.level) def load_game(self): self.start_game() self.pellets.create_pellet_list_snap(self.data_driver.get_pellets()) self.score = self.data_driver.get_score() self.lives = self.data_driver.get_lives() self.level = self.data_driver.get_level() def check_events(self): for event in pygame.event.get(): if event.type == QUIT: self.save_game() exit() if event.type == KEYDOWN: if event.key == K_SPACE: self.paused = not self.paused if event.key == K_PERIOD: self.switch_song(1) if event.key == K_COMMA: self.switch_song(-1) if event.key == K_F8: self.save_game() if event.key == K_F7: self.load_game() if not self.paused: self.check_pellet_events() self.check_ghost_events() self.check_fruit_events() def check_pellet_events(self): pellet = self.pacman.eat_pellets(self.pellets.pelletList) if pellet: self.pelletsEaten += 1 self.score += pellet.points if self.pelletsEaten == 70 or self.pelletsEaten == 140: if self.fruit is None: self.fruit = Fruit(self.nodes, self.sheet) self.pellets.pelletList.remove(pellet) if pellet.name == "powerpellet": self.ghosts.freight_mode() if self.pellets.is_empty(): self.level += 1 self.start_game() def restart_level(self): self.pacman = Pacman(self.nodes, self.sheet) self.ghosts = Ghosts(self.nodes, self.sheet) self.fruit = None self.paused = True def check_ghost_events(self): self.ghosts.release(self.pelletsEaten) ghost = self.pacman.eat_ghost(self.ghosts) # if self.pacman.eatGhost(self.ghost): if ghost is not None: if ghost.mode.name == "FREIGHT": self.score += ghost.points ghost.spawn_mode() elif ghost.mode.name != "SPAWN": self.lives -= 1 self.paused = True self.pacman.alive = False self.pacman.animate.set_animation("death", 0) if (self.lives < 1): self.level = 1 self.lives = 5 self.score = 0 self.start_game() def check_fruit_events(self): if self.fruit is not None: if self.pacman.eat_fruit(self.fruit) or self.fruit.its_time_to_die: if not self.fruit.its_time_to_die: self.score += self.fruit.points self.fruit = None # TODO: original font score def render_score(self): text = self.font.render("Score: " + str(self.score), False, WHITE) self.screen.blit(text, (10, 10, 100, 100)) def render_level(self): text = self.font.render("Level: " + str(self.level), False, WHITE) self.screen.blit(text, (200, 10, 300, 100)) def render(self): self.screen.blit(self.background, (0, 0)) self.nodes.render(self.screen) self.pellets.render(self.screen) self.pacman.render(self.screen) self.ghosts.render(self.screen) self.render_score() self.render_level() if self.fruit is not None: self.fruit.render(self.screen) y = TILE_HEIGHT * N_ROWS text = self.font.render("Lives: ", False, WHITE) self.screen.blit(text, (10, 60, 100, 100)) self.lifeIcons.render(self.screen, self.lives - 1) pygame.display.update()
def restart_level(self): self.pacman = Pacman(self.nodes, self.sheet) self.ghosts = Ghosts(self.nodes, self.sheet) self.fruit = None self.paused = True
def Game(): pygame.init() gamesettings = Settings() screen = pygame.display.set_mode( (gamesettings.screen_width, gamesettings.screen_height)) pygame.display.set_caption("Pacman Portal") # Start screen showgamestats = GameStats(screen, gamesettings) startScreen = StartScreen(screen, gamesettings, showgamestats) # Grouping blocks and pellets and ghosts blocks = Group() powerpills = Group() shield = Group() portals = Group() ghosts = Group() intersections = Group() fruit = Fruit(screen) thepacman = Pacman(screen, gamesettings) # Making the ghosts redghost = Ghosts(screen, "red") cyanghost = Ghosts(screen, "cyan") orangeghost = Ghosts(screen, "orange") pinkghost = Ghosts(screen, "pink") ghosts.add(redghost) ghosts.add(cyanghost) ghosts.add(orangeghost) ghosts.add(pinkghost) # Making the two portals orange = Portal(screen, "orange") blue = Portal(screen, "blue") portals.add(orange) portals.add(blue) startScreen.makeScreen(screen, gamesettings) fruit.fruitReset() gf.readFile(screen, blocks, shield, powerpills, intersections) frames = 0 # for the victory fanfare and death animation # play intro chime playIntro = True screen.fill(BLACK) while True: if (gamesettings.game_active): pygame.time.Clock().tick(120) #120 fps lock screen.fill(BLACK) showgamestats.blitstats() gf.check_events(thepacman, powerpills, gamesettings, orange, blue) gf.check_collision(thepacman, blocks, powerpills, shield, ghosts, intersections, showgamestats, gamesettings, fruit, orange, blue) for block in blocks: block.blitblocks() for theshield in shield: theshield.blitshield() for pill in powerpills: pill.blitpowerpills() for portal in portals: portal.blitportal() for ghost in ghosts: ghost.blitghosts() ghost.update() if (ghost.DEAD): ghost.playRetreatSound() elif (ghost.afraid): ghost.playAfraidSound( ) # if ghosts are afraid, loop their sound for intersection in intersections: intersection.blit() fruit.blitfruit() thepacman.blitpacman() thepacman.update() if (len(powerpills) == 0): gamesettings.game_active = False gamesettings.victory_fanfare = True if (playIntro and pygame.time.get_ticks() % 200 <= 50): mixer.Channel(2).play( pygame.mixer.Sound('sounds/pacman_beginning.wav')) pygame.time.wait(4500) playIntro = False elif (gamesettings.victory_fanfare): if (frames <= 120): for block in blocks: block.color = ((255, 255, 255)) block.blitblocks() elif (frames <= 240): for block in blocks: block.color = ((0, 0, 255)) block.blitblocks() elif (frames <= 360): for block in blocks: block.color = ((255, 255, 255)) block.blitblocks() elif (frames <= 480): for block in blocks: block.color = ((0, 0, 255)) block.blitblocks() else: gamesettings.game_active = True gamesettings.victory_fanfare = False thepacman.resetPosition() for ghost in ghosts: ghost.resetPosition() ghost.speed += 1 showgamestats.level += 1 fruit.fruitReset() gf.readFile(screen, blocks, shield, powerpills, intersections) frames = 0 pygame.time.wait(1000) frames += 1 elif (thepacman.DEAD): thepacman.deathAnimation(frames) frames += 1 if (frames > 600): gamesettings.game_active = True thepacman.DEAD = False thepacman.resetPosition() for ghost in ghosts: ghost.resetPosition() frames = 0 pygame.time.wait(1000) if (showgamestats.num_lives < 0): #reset game and save score screen.fill(BLACK) pygame.time.wait(2000) gamesettings.game_active = False thepacman.resetPosition() for ghost in ghosts: ghost.resetPosition() ghost.speed = 1 showgamestats.num_lives = 3 showgamestats.save_hs_to_file() showgamestats.score = 0 showgamestats.level = 1 fruit.fruitReset() playIntro = True # reset the chime gf.readFile(screen, blocks, shield, powerpills, intersections) startScreen.makeScreen(screen, gamesettings) pygame.display.flip()
def Game(): pygame.init() settings = Settings() screen = pygame.display.set_mode((settings.screen_width, settings.screen_height)) pygame.display.set_caption("Pacman Portal") startScreen = StartScreen(screen, settings) showgamestats = GameStats(screen, settings) bricks = Group() powerpellets = Group() pellets = Group() shield = Group() fruits = Group() portal = Group() thepacman = Pacman(screen, settings) blinky = Ghosts(screen, "red") inky = Ghosts(screen, "cyan") clyde = Ghosts(screen, "orange") pinky = Ghosts(screen, "pink") startScreen.makeScreen(screen) gf.readFile(screen, bricks, shield, pellets, powerpellets) screen.fill(BLACK) while True: screen.fill(BLACK) showgamestats.blitstats() gf.check_events(thepacman) gf.check_collision(thepacman, bricks, pellets, powerpellets, shield, fruits) thepacman.update() for brick in bricks: brick.blitbricks() for theshield in shield: theshield.blitshield() for power in powerpellets: power.blitpowerpills() for pellet in pellets: pellet.blitpellet() thepacman.blitpacman() blinky.blitghosts() inky.blitghosts() clyde.blitghosts() pinky.blitghosts() pygame.display.flip()
def Game(): pygame.init() gamesettings = Settings() screen = pygame.display.set_mode( (gamesettings.screen_width, gamesettings.screen_height)) pygame.display.set_caption("Pacman Portal") # Start screen startScreen = StartScreen(screen, gamesettings) showgamestats = GameStats(screen, gamesettings) # Grouping blocks and pellets blocks = Group() powerpills = Group() shield = Group() portal = Group() thepacman = Pacman(screen, gamesettings) # Making the ghosts redghost = Ghosts(screen, "red") cyanghost = Ghosts(screen, "cyan") orangeghost = Ghosts(screen, "orange") pinkghost = Ghosts(screen, "pink") startScreen.makeScreen(screen) gf.readFile(screen, blocks, shield, powerpills, portal) screen.fill(BLACK) while True: screen.fill(BLACK) showgamestats.blitstats() gf.check_events(thepacman) gf.check_collision(thepacman, blocks, powerpills, shield) thepacman.update() for block in blocks: block.blitblocks() for theshield in shield: theshield.blitshield() for pill in powerpills: pill.blitpowerpills() for theportal in portal: theportal.blitportal() thepacman.blitpacman() redghost.blitghosts() cyanghost.blitghosts() orangeghost.blitghosts() pinkghost.blitghosts() pygame.display.flip()
class Game: BLACK = (0, 0, 0) def __init__(self): pygame.init() self.screen = pygame.display.set_mode((680, 740)) self.ai_settings = Settings() pygame.display.set_caption("Pacman Portal") self.maze = Maze(self.screen, mazefile='map.txt', brickfile='square', portalfile='square', shieldfile='shield', pointfile='point', dotfile='dot', powerpillfile='powerpill') self.pacman = Pacman(self.ai_settings, self.screen, self.maze) self.ghosts = Ghosts(self.ai_settings, self.screen, self.maze, self.pacman) self.displayname = Button(self.screen, "PacmanPortal") self.play_button = Button(self.screen, "PLAY GAME") self.highscore_button = Button(self.screen, "HIGH SCORES") self.blinkyname = Button(self.screen, "BLiNKY") self.pinkyname = Button(self.screen, "PiNKY") self.clydename = Button(self.screen, "CLYDE") self.inkeyname = Button(self.screen, "iNKEY") self.back = Button(self.screen, "Back") self.sb = Scoreboard(self.ai_settings, self.screen, self.maze, self.pacman) self.score = Button(self.screen, "hello") self.blues = 0 def __str__(self): return 'Game(Pacman Portal), maze=' + str(self.maze) + ')' def play(self): # self.displayname = Button(self.screen, "Portal Pacman") self.displayname.rect.centery -= 300 self.displayname.msg_image_rect.center = self.displayname.rect.center # self.highscore_button = Button(self.screen, "Portal Pacman") self.highscore_button.rect.centery += 330 self.highscore_button.msg_image_rect.center = self.highscore_button.rect.center # self.play_button = Button(self.screen, "Portal Pacman") self.play_button.rect.centery += 280 self.play_button.msg_image_rect.center = self.play_button.rect.center hs_file = open("score.txt", "r") self.ai_settings.hs = int(hs_file.read()) hs_file.close() msgg = 'The high score is ' + str(self.ai_settings.hs) self.score = Button(self.screen, msgg) self.score.rect.centery -= 150 self.score.msg_image_rect.center = self.score.rect.center self.ghosts.intro() self.pacman.intro() self.blues = pygame.mixer.Sound('sounds/blueghosts.wav') eloop = EventLoop(finished=False) while not eloop.finished: eloop.check_events(self.ai_settings, self.pacman, self.maze, self.play_button, self.highscore_button, self.back, self.ghosts, self.blues, self.sb) if self.ai_settings.score > self.ai_settings.hs and self.ai_settings.game_active: self.ai_settings.hs = self.ai_settings.score msgg = 'The high score is ' + str(self.ai_settings.hs) score = Button(self.screen, msgg) score.rect.centery += 150 score.msg_image_rect.center = score.rect.center if len(self.maze.dots) == 1: self.maze = Maze(self.screen, mazefile='map.txt', brickfile='square', portalfile='square', shieldfile='shield', pointfile='point', dotfile='dot', powerpillfile='powerpill') self.pacman.reset() self.ghosts.reset() self.ai_settings.level += 1 if not self.ai_settings.game_active: pygame.mouse.set_visible(True) self.update_screen() def update_screen(self): self.pacman.update() self.ghosts.update() self.screen.fill(Game.BLACK) self.maze.blitme() self.pacman.blitme() self.ghosts.blitme() self.sb.prep_score() self.sb.prep_high_score() # self.sb.prep_pacmans() self.sb.show_score() if not self.ai_settings.game_active: self.screen.fill(self.ai_settings.bg_color) self.displayname.draw_button() self.highscore_button.draw_button() self.play_button.draw_button() self.ghosts.introupdate() self.ghosts.blitme() self.pacman.introupdate() self.pacman.blitme() if self.ai_settings.shs: self.screen.fill(self.ai_settings.bg_color) self.back.draw_button() self.score.draw_button() pygame.display.flip()
class Game: def __init__(self): """class makes pacman move and draws the screen""" self.move_down = False self.move_up = False self.move_right = False self.move_left = False self._pacman_start = True self.first_game_screen = True self.game_over_screen = False self.game_win_screen = False self.game_over = False self.game_win = False self.play_again = None self.width = 890 self.height = 660 self.x = 100 self.y = 100 self._new_x = 0 self._new_y = 0 self._collision = False self.screen = pygame.display.set_mode((self.width, self.height)) pygame.display.set_caption("Pac-Man") self.walls = pygame.sprite.Group() self.pacman_group = pygame.sprite.GroupSingle() self.points = pygame.sprite.Group() self.ghosts_list = pygame.sprite.Group() self.pacman = Pacman(self.x, self.y, self.move_left, self.move_down, self.move_up) self.ghosts = Ghosts(1, 500, 210) self.ghost_1 = Ghosts(1, 775, 40) self.ghost_2 = Ghosts(2, 500, 210) self.ghost_3 = Ghosts(3, 100, 460) self._ghost_collision = False self.ghosts_list.add(self.ghost_1, self.ghost_2, self.ghost_3) self.ghosts_list.add(self.ghost_2) self.ghost_1_direction = "left" self.ghost_2_direction = "right" self.ghost_3_direction = "down" self.wall = Wall(self.x, self.y, self.width, self.height) self.wall.draw_walls(self.screen, self.walls) self.point = Point(self.x, self.y) self.points = self.point.draw_points(self.points) self.point_count = 0 pygame.init() # pylint: disable=(no-member) def draw_screen(self): """draws the screen with points, walls and updated score and place for pacman and ghosts""" self.screen.fill((0, 0, 0)) self._get_points() font = pygame.font.SysFont("Arial", 24) text = font.render("Score: " + str(self.point_count), True, (18, 247, 110)) self.screen.blit(text, (775, 20)) self.points.draw(self.screen) self.pacman_group.draw(self.screen) self.walls.draw(self.screen) self.ghosts_list.draw(self.screen) def _get_points(self): """Uses point_collect method from Point class to test if pacman collides with points. Saves the score if pacman collects points and sets the self.game_win True.""" hit = self.point.collect_points(self.pacman, self.points) if hit == 1: self.point_count += 1 if hit == 2: self.point_count += 2 if self.point_count == 87: self.game_win = True def update_place(self): """updates place for pacman and ghosts""" if self._pacman_start: self.pacman_start_screen() if self._pacman_start is False: self.pacman_move() self._ghost_collision = self.ghosts.ghost_collision( self.pacman, self.ghosts_list) if self._ghost_collision is True: self.game_over = True self.ghost_1_direction = self.ghosts.ghost_move( self.ghost_1, self.ghost_1_direction, self.walls) self.ghost_2_direction = self.ghosts.ghost_move( self.ghost_2, self.ghost_2_direction, self.walls) self.ghost_3_direction = self.ghosts.ghost_move( self.ghost_3, self.ghost_3_direction, self.walls) self.pacman_group.add(self.pacman) def pacman_start_screen(self): """creates a new start view. Different views for first game, game over and winning.""" if self.first_game_screen: self.pacman = Pacman(self.x, self.y, self.move_left, self.move_down, self.move_up) self.pacman_group.add(self.pacman) start = pygame.image.load("src/pictures/start.png") self.screen.blit(start, (205, 170)) if self.game_over_screen: game_over = pygame.image.load("src/pictures/Game_over.png") self.screen.blit(game_over, (205, 170)) self.screen.blit(self.game_over_score, (457, 273)) if self.game_win_screen: win = pygame.image.load("src/pictures/you_won.png") self.screen.blit(win, (205, 170)) def save_game_over_score(self, score): """saves the score for new start view""" font = pygame.font.SysFont("Jellee-Roman", 48) self.game_over_score = font.render(str(score), True, (18, 247, 95)) def pacman_move(self): """updates new coordinates for pacman and uses collision method from Wall class to check for collision Args: new_x (adds new coordinates) new_y (adds new coordinates) """ self.x += self._new_x self.y += self._new_y self.pacman = Pacman(self.x, self.y, False, False, True) self._collision = self.wall.collision(self.pacman, self.walls) if self._collision: self.x -= self._new_x self.y -= self._new_y self.pacman = Pacman(self.x, self.y, self.move_left, self.move_down, self.move_up) self.pacman = Pacman(self.x, self.y, self.move_left, self.move_down, self.move_up) self.pacman_group.add(self.pacman) def move(self): """Functions for different keys. Uses different move methods depending on which key is pressed""" for event in pygame.event.get(): if event.type == pygame.KEYDOWN: # pylint: disable=(no-member) if event.key == pygame.K_LEFT: # pylint: disable=(no-member) self._pacman_set_direction_left() if event.key == pygame.K_RIGHT: # pylint: disable=(no-member) self._pacman_set_direction_right() # pylint: disable=(no-member) if event.key == pygame.K_UP: # pylint: disable=(no-member) self._pacman_set_direction_up() if event.key == pygame.K_DOWN: # pylint: disable=(no-member) self._pacman_set_direction_down() if event.key == pygame.K_RETURN: # pylint: disable=(no-member) self._pacman_start = False if event.type == pygame.QUIT: # pylint: disable=(no-member) exit() def _pacman_set_direction_right(self): """Sets the moving direction. The current direction is True, others are False""" self.move_right = True if self.move_right: self.move_left = False self.move_up = False self.move_down = False self._new_x = 1 self._new_y = 0 def _pacman_set_direction_left(self): """Sets the moving direction. The current direction is True, others are False""" self.move_left = True if self.move_left: self.move_right = False self.move_up = False self.move_down = False self._new_x = -1 self._new_y = 0 def _pacman_set_direction_up(self): """Sets the moving direction. The current direction is True, others are False""" self.move_up = True if self.move_up: self.move_left = False self.move_right = False self.move_down = False self._new_x = 0 self._new_y = -1 def _pacman_set_direction_down(self): """Sets the moving direction. The current direction is True, others are False""" self.move_down = True if self.move_down: self.move_left = False self.move_right = False self.move_up = False self._new_x = 0 self._new_y = 1
def makeScreen(self, screen, gamesettings): # create second set of characters for the title screen titlepacman = Pacman(screen, gamesettings) titlepacman.rect.x, titlepacman.rect.y = 0, 280 titleredghost = Ghosts(screen, "red") titleredghost.rect.x = -70 titlecyanghost = Ghosts(screen, "cyan") titlecyanghost.rect.x = -105 titleorangeghost = Ghosts(screen, "orange") titleorangeghost.rect.x = -140 titlepinkghost = Ghosts(screen, "pink") titlepinkghost.rect.x = -175 titlecyanghost.speed, titleorangeghost.speed, titlepinkghost.speed, titleredghost.speed = 2, 2, 2, 2 pygame.init() pygame.display.set_caption("PACMAN") background = pygame.Surface(screen.get_size()) background = background.convert() background.fill(BLACK) # Display P in front of the PACMAN pic font = pygame.font.Font(None, 144) text1 = font.render("PA", 2, WHITE) textpos1 = text1.get_rect() textpos1 = ((self.settings.screen_width / 2) - 300, (self.settings.screen_height / 8) - 75) font = pygame.font.Font(None, 144) # Display "MAN" after the PACMAN pic text2 = font.render("MAN", 2, WHITE) textpos2 = text2.get_rect() textpos2 = ((self.settings.screen_width / 2), (self.settings.screen_height / 8) - 75) # PACMAN position pacman_pos = ((self.settings.screen_width / 2) - 150, (self.settings.screen_height / 8) - 55) # Ghosts position and text # Cyan Ghost font = pygame.font.Font(None, 44) text3 = font.render(" INKY", 2, (0, 255, 255)) textpos3 = ((self.settings.screen_width / 2) - 250, (self.settings.screen_height / 4) + 20) background.blit(self.image, (180, 220)) # Orange Ghost text4 = font.render(" CLYDE", 2, (255, 165, 0)) textpos4 = ((self.settings.screen_width / 2) - 150, (self.settings.screen_height / 4) + 20) background.blit(self.image2, (290, 220)) # Pink Ghost text5 = font.render(" PINKY", 2, (255, 20, 147)) textpos5 = ((self.settings.screen_width / 2) - 25, (self.settings.screen_height / 4) + 20) background.blit(self.image3, (410, 220)) # Red Ghost text6 = font.render(" BLINKY", 2, (250, 0, 0)) textpos6 = ((self.settings.screen_width / 2) + 100, (self.settings.screen_height / 4) + 20) background.blit(self.image4, (550, 220)) # Draw onto screen background.blit(text1, textpos1) background.blit(self.pacmanimage, pacman_pos) background.blit(text2, textpos2) background.blit(text3, textpos3) background.blit(text4, textpos4) background.blit(text5, textpos5) background.blit(text6, textpos6) # Blit everything to screen screen.blit( self.image, (self.settings.screen_width / 2, self.settings.screen_height / 3)) screen.blit(background, (200, 200)) #Play button play_button = Button(screen, "Play") # High Scores button hs_button = Button(screen, "High Scores") show_hs_menu = False # animation titlepacman.moving_right = True titlecyanghost.moving_right = True titleorangeghost.moving_right = True titlepinkghost.moving_right = True titleredghost.moving_right = True pygame.display.flip() while True: pygame.time.Clock().tick(120) # 120 fps lock for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() elif event.type == pygame.MOUSEBUTTONDOWN: show_hs_menu = False mouse_x, mouse_y = pygame.mouse.get_pos() play_clicked = play_button.rect.collidepoint( mouse_x, mouse_y) hs_clicked = hs_button.rect.collidepoint(mouse_x, mouse_y) if (play_clicked): self.settings.game_active = True return elif (hs_clicked): show_hs_menu = True if (show_hs_menu): screen.fill(BLACK) pygame.font.init() myfont = pygame.font.SysFont(None, 40) score1 = myfont.render( '1. ' + str(self.showgamestats.high_scores_all[0]), False, (255, 255, 255)) score2 = myfont.render( '2. ' + str(self.showgamestats.high_scores_all[1]), False, (255, 255, 255)) score3 = myfont.render( '3. ' + str(self.showgamestats.high_scores_all[2]), False, (255, 255, 255)) notify = myfont.render( 'Click anywhere on the screen to go back', False, (255, 255, 255)) screen.blit(score1, (150, 58)) screen.blit(score2, (150, 188)) screen.blit(score3, (150, 318)) screen.blit(notify, (50, 400)) else: if (titlepinkghost.rect.x > 850): # move left titlepacman.moving_right, titlepacman.moving_left = False, True titlecyanghost.moving_right, titlecyanghost.moving_left, titlecyanghost.afraid = False, True, True titleorangeghost.moving_right, titleorangeghost.moving_left, titleorangeghost.afraid = False, True, True titlepinkghost.moving_right, titlepinkghost.moving_left, titlepinkghost.afraid = False, True, True titleredghost.moving_right, titleredghost.moving_left, titleredghost.afraid = False, True, True elif (titlepacman.rect.x < -50): # move right titlepacman.moving_right, titlepacman.moving_left = True, False titlecyanghost.moving_right, titlecyanghost.moving_left, titlecyanghost.afraid = True, False, False titleorangeghost.moving_right, titleorangeghost.moving_left, titleorangeghost.afraid = True, False, False titlepinkghost.moving_right, titlepinkghost.moving_left, titlepinkghost.afraid = True, False, False titleredghost.moving_right, titleredghost.moving_left, titleredghost.afraid = True, False, False screen.blit(background, (0, 0)) play_button.draw_button() hs_button.draw_button() titlepacman.blitpacman() titlepacman.update() titlecyanghost.blitghosts() titleorangeghost.blitghosts() titleredghost.blitghosts() titlepinkghost.blitghosts() titlecyanghost.update() titleorangeghost.update() titleredghost.update() titlepinkghost.update() pygame.display.flip()
class Pacman(Sprite): def __init__(self, ai_settings, screen, maze): """Initialize the ship and set its starting position.""" super(Pacman, self).__init__() self.screen = screen self.ai_settings = ai_settings self.maze = maze self.size = 30 self.ghosts = Ghosts(self.ai_settings, self.screen, self.maze, self) # Get ship from sprite sheet and load its image and rect. self.ss = spritesheet.Spritesheet('images/pacman.png') self.s1 = spritesheet.Spritesheet('images/pacmandeath.png') self.image = self.ss.image_at((0, 0, 16, 16), -1) self.image = pygame.transform.scale(self.image, (self.size, self.size)) self.rect = self.image.get_rect() # Initilize all pacman assets self.image4 = self.s1.image_at((0, 0, 16, 16), -1) self.image4 = pygame.transform.scale(self.image4, (self.size, self.size)) self.image5 = self.s1.image_at((16, 0, 16, 16), -1) self.image5 = pygame.transform.scale(self.image5, (self.size, self.size)) self.imageright1 = self.ss.image_at((0, 0, 16, 16), -1) self.imageright1 = pygame.transform.scale(self.imageright1, (self.size, self.size)) self.imageright2 = self.ss.image_at((16, 0, 16, 16), -1) self.imageright2 = pygame.transform.scale(self.imageright2, (self.size, self.size)) self.imageleft1 = self.ss.image_at((0, 16, 16, 16), -1) self.imageleft1 = pygame.transform.scale(self.imageleft1, (self.size, self.size)) self.imageleft2 = self.ss.image_at((16, 16, 16, 16), -1) self.imageleft2 = pygame.transform.scale(self.imageleft2, (self.size, self.size)) self.imageup1 = self.ss.image_at((0, 32, 16, 16), -1) self.imageup1 = pygame.transform.scale(self.imageup1, (self.size, self.size)) self.imageup2 = self.ss.image_at((16, 32, 16, 16), -1) self.imageup2 = pygame.transform.scale(self.imageup2, (self.size, self.size)) self.imagedown1 = self.ss.image_at((0, 48, 16, 16), -1) self.imagedown1 = pygame.transform.scale(self.imagedown1, (self.size, self.size)) self.imagedown2 = self.ss.image_at((16, 48, 16, 16), -1) self.imagedown2 = pygame.transform.scale(self.imagedown2, (self.size, self.size)) self.image3 = self.ss.image_at((32, 0, 16, 16), -1) self.image3 = pygame.transform.scale(self.image3, (self.size, self.size)) self.screen = screen self.screen_rect = screen.get_rect() self.rect.center = self.screen_rect.center self.waka = pygame.mixer.Sound('sounds/waka.wav') self.intromus = pygame.mixer.Sound('sounds/intro.wav') self.dsound = pygame.mixer.Sound('sounds/death.wav') self.esound = pygame.mixer.Sound('sounds/eatghost.wav') self.dead = 0 # Load the ship image and get its rect. # self.image = pygame.image.load('images/ship.bmp') # self.rect = self.image.get_rect() # Start each new ship at the bottom center of the screen. # self.rect.centerx = self.screen_rect.centerx # self.rect.bottom = self.screen_rect.bottom # Store a decimal value for the ship's center self.centerx = float(self.rect.centerx) self.centery = float(self.rect.centery) # Movement flags self.moving_right = False self.moving_left = False self.moving_up = False self.moving_down = False self.tick = 0 self.i = 0 def update(self): """Update the ship's posisiton based on the movement flags.""" # Update the ship's center value, not the rect. # Update rect object from self.center. if self.dead: self.dsound.play() self.image = self.image3 self.screen.fill(self.ai_settings.bg_color) self.blitme() self.maze.blitme() pygame.display.flip() sleep(.1) self.image = self.imageright2 self.screen.fill(self.ai_settings.bg_color) self.blitme() self.maze.blitme() pygame.display.flip() sleep(.1) self.image = self.image3 self.screen.fill(self.ai_settings.bg_color) self.blitme() self.maze.blitme() pygame.display.flip() sleep(.1) self.image = self.image4 self.screen.fill(self.ai_settings.bg_color) self.blitme() self.maze.blitme() pygame.display.flip() sleep(.1) self.image = self.image5 self.screen.fill(self.ai_settings.bg_color) self.blitme() self.maze.blitme() pygame.display.flip() sleep(.1) self.dead = 0 self.reset() self.ghosts.reset() else: if self.moving_right: if self.tick == 0: self.image = self.imageright1 if self.tick == 20: self.image = self.imageright2 if self.tick == 40: self.image = self.image3 self.tick += 1 if self.tick == 60: self.tick = 0 self.centerx += self.ai_settings.pac_speed_factor # self.mr() if self.moving_left: if self.tick == 0: self.image = self.imageleft1 if self.tick == 20: self.image = self.imageleft2 if self.tick == 40: self.image = self.image3 self.tick += 1 if self.tick == 60: self.tick = 0 self.centerx -= self.ai_settings.pac_speed_factor if self.moving_up: if self.tick == 0: self.image = self.imageup1 if self.tick == 20: self.image = self.imageup2 if self.tick == 40: self.image = self.image3 self.tick += 1 if self.tick == 60: self.tick = 0 self.centery -= self.ai_settings.pac_speed_factor if self.moving_down: if self.tick == 0: self.image = self.imagedown1 if self.tick == 20: self.image = self.imagedown2 if self.tick == 40: self.image = self.image3 self.tick += 1 if self.tick == 60: self.tick = 0 self.centery += self.ai_settings.pac_speed_factor self.rect.centerx = self.centerx self.rect.centery = self.centery def reset(self): self.rect.center = self.screen_rect.center self.centerx = float(self.rect.centerx) self.centery = float(self.rect.centery) def intro(self): self.centerx = 40 self.centery = 312 # self.rect.left = self.screen_rect.right def introupdate(self): if self.ai_settings.intro: if self.ai_settings.powerpill: self.moving_right = True self.centerx += 0.5 if pygame.Rect.colliderect(self.rect, self.ghosts.powerpill_rect) or self.rect.centerx > 680: self.ghosts.powerpill_rect.centerx = -100 self.ai_settings.powerpill = False if not self.ai_settings.powerpill: self.moving_left = True self.moving_right = False self.centerx -= 0.5 if self.rect.centerx < 0: self.ai_settings.introduction = 1 def blitme(self): """Draw the ship at its current location.""" self.screen.blit(self.image, self.rect) def mr(self): self.centerx += self.ai_settings.pac_speed_factor def ml(self): self.centerx -= self.ai_settings.pac_speed_factor def mu(self): self.centery -= self.ai_settings.pac_speed_factor def md(self): self.centery += self.ai_settings.pac_speed_factor def die(self): pass