def __init__(self, pacman_agent): self.pacman_agent = pacman_agent self.grid = grid.Grid() self.pacman = pacman.Pacman(grid.PACMAN_STARTPOS, 0, 0) self.blinky = ghost.Blinky(grid.GHOST_STARTPOS, 0, 0) self.inky = ghost.Inky(grid.GHOST_STARTPOS, 0, 0) self.pinky = ghost.Pinky(grid.GHOST_STARTPOS, 0, 0) self.clyde = ghost.Clyde(grid.GHOST_STARTPOS, 0, 0) self.ghosts = pygame.sprite.Group() self.ghost_house = pygame.sprite.Group() self.ghosts.add(self.blinky) self.ghost_house.add(self.inky) self.ghost_house.add(self.pinky) self.ghost_house.add(self.clyde) pygame.time.set_timer(CHANGESTATEEVENT, int(self.ghosts_state * 1000)) pygame.time.set_timer(GHOSTHOUSEEVENT, int(GHOSTHOUSETIME * 1000)) self._font = pygame.font.Font(globals.FONT, globals.FONT_SIZE)
def init_settings(self): self.index = 1 self.indexInc = 1 self.speed = 5 self.timer = 1 self.timer2 = 0.5 self.timer3 = 1.5 self.delta_t = 0 self.pacSpeed = 3 self.ghostSpeed = 3 self.collisioncounter = 0 self.pill = powerpill.LargePowerPill( self.image_library, self.screen, self.settings, 1000, self.settings.get_screen_height() / 2 + 20) self.pacman = pac.PacMan( [300, self.settings.get_screen_height() / 2], self.image_library, self.screen, self.settings, sound_lib=None) self.blinky = ghost.Blinky(self.clock, self.image_library, self.screen, self.settings, self.sounds, 200, self.settings.get_screen_height() / 2) self.inky = ghost.Inky(self.clock, self.image_library, self.screen, self.settings, self.sounds, 155, self.settings.get_screen_height() / 2) self.clyde = ghost.Clyde(self.clock, self.image_library, self.screen, self.settings, self.sounds, 110, self.settings.get_screen_height() / 2) self.pinky = ghost.Pinky(self.clock, self.image_library, self.screen, self.settings, self.sounds, 65, self.settings.get_screen_height() / 2) self.blinky.change_direction("Right") self.pinky.change_direction("Right") self.inky.change_direction("Right") self.clyde.change_direction("Right") self.ghosts = [self.blinky, self.pinky, self.inky, self.clyde] self.fakeGhosts = self.ghosts.copy() self.loop = True
def create_ghosts(self): self.ghost_blinky = ghost.Blinky(self.sprites["ghosts"]["blinky-red"], self.square_size, map.map_1, self.player.rect, self.current_level) self.ghost_clyde = ghost.Clyde(self.sprites["ghosts"]["clyde-orange"], self.square_size, map.map_1, self.player.rect, self.current_level) self.ghost_pinky = ghost.Pinky(self.sprites["ghosts"]["pinky-pink"], self.square_size, map.map_1, self.player.rect, self.player.direction, self.current_level) self.ghost_inky = ghost.Inky(self.sprites["ghosts"]["inky-turquoise"], self.square_size, map.map_1, self.player.rect, self.player.direction, self.ghost_blinky.pos, self.current_level) self.all_ghosts = [ self.ghost_blinky, self.ghost_clyde, self.ghost_pinky, self.ghost_inky ] self.ghost_moving_sound_time = 0.5 self.ghost_moving_timer = 0 self.start_screen_timer = time.time() + self.start_screen_time self.sounds["beginning"].play()
def __init__(self, clock, pac_images, image_library, screen, settings): self.clock = clock self.image_library = image_library self.screen = screen self.settings = settings self.pac_images = pac_images self.regularPacImages = pac_images.copy() for index, image in enumerate(self.pac_images): self.pac_images[index] = pygame.transform.rotozoom(image, 0, 3) self.sounds = None # Text Settings self.font = pygame.font.Font('Fonts/PFont.ttf', 150) self.ghostFont = pygame.font.Font('Fonts/PFont.ttf', 30) self.playFontOne = pygame.font.Font('Fonts/PFont.ttf', 50) self.playFontTwo = pygame.font.Font('Fonts/PFont.ttf', 75) self.twoHunImg = self.ghostFont.render("200", True, self.settings.whiteFont, self.settings.get_bg_color()) self.fourHunImg = self.ghostFont.render("400", True, self.settings.whiteFont, self.settings.get_bg_color()) self.eightHunImg = self.ghostFont.render("800", True, self.settings.whiteFont, self.settings.get_bg_color()) self.sixteenHunImg = self.ghostFont.render( "1600", True, self.settings.whiteFont, self.settings.get_bg_color()) self.twoRect = self.twoHunImg.get_rect() self.fourRect = self.fourHunImg.get_rect() self.eightRect = self.eightHunImg.get_rect() self.sixteenRect = self.sixteenHunImg.get_rect() self.scoreList = [ self.twoHunImg, self.fourHunImg, self.eightHunImg, self.sixteenHunImg ] self.rectList = [ self.twoRect, self.fourRect, self.eightRect, self.sixteenRect ] self.clydeImg = self.ghostFont.render("\"CLYDE\"", True, (255, 136, 0), self.settings.get_bg_color()) self.inkyImg = self.ghostFont.render("\"INKY\"", True, (0, 255, 255), self.settings.get_bg_color()) self.pinkyImg = self.ghostFont.render("\"PINKY\"", True, (222, 129, 145), self.settings.get_bg_color()) self.blinkyImg = self.ghostFont.render("\"BLINKY\"", True, (255, 0, 0), self.settings.get_bg_color()) self.clydeRect = self.clydeImg.get_rect() self.inkyRect = self.inkyImg.get_rect() self.pinkyRect = self.pinkyImg.get_rect() self.blinkyRect = self.blinkyImg.get_rect() self.titleMessageOne = self.font.render("PA", True, self.settings.whiteFont, self.settings.get_bg_color()) self.titleMessageTwo = self.font.render("MAN", True, self.settings.whiteFont, self.settings.get_bg_color()) self.oneRect = self.titleMessageOne.get_rect() self.oneRect.top = 100 self.oneRect.centerx = self.settings.get_screen_width() / 2 - 150 self.twoRect = self.titleMessageTwo.get_rect() self.twoRect.top = self.oneRect.top self.twoRect.x = self.oneRect.right + 100 self.playImgOne = self.playFontOne.render("PLAY GAME", True, self.settings.whiteFont, self.settings.get_bg_color()) self.playImgTwo = self.playFontTwo.render("PLAY GAME", True, self.settings.whiteFont, self.settings.get_bg_color()) self.playRectOne = self.playImgTwo.get_rect() self.playRectOne.centerx = self.settings.get_screen_width() / 2 + 100 self.playRectOne.y = 1000 self.playRectTwo = self.playImgTwo.get_rect() self.playRectTwo.centerx = self.settings.get_screen_width() / 2 + 50 self.playRectTwo.y = 1000 self.hsImgOne = self.playFontOne.render("HIGH SCORES", True, self.settings.whiteFont, self.settings.get_bg_color()) self.hsImgTwo = self.playFontTwo.render("HIGH SCORES", True, self.settings.whiteFont, self.settings.get_bg_color()) self.hsRectOne = self.hsImgTwo.get_rect() self.hsRectOne.centerx = self.settings.get_screen_width() / 2 + 100 self.hsRectOne.y = 1100 self.hsRectTwo = self.hsImgTwo.get_rect() self.hsRectTwo.centerx = self.settings.get_screen_width() / 2 + 50 self.hsRectTwo.y = 1100 self.pRect = self.pac_images[0].get_rect() self.pRect.top = self.oneRect.top self.index = 1 self.indexInc = 1 self.speed = 5 self.timer = 1 self.timer2 = 0.5 self.timer3 = 1.5 self.delta_t = 0 self.pacSpeed = 3 self.ghostSpeed = 3 self.collisioncounter = 0 self.pill = powerpill.LargePowerPill( image_library, screen, settings, 1000, self.settings.get_screen_height() / 2 + 20) self.pacman = pac.PacMan( [300, self.settings.get_screen_height() / 2], image_library, screen, settings, sound_lib=None) self.blinky = ghost.Blinky(self.clock, image_library, screen, settings, self.sounds, 200, self.settings.get_screen_height() / 2) self.inky = ghost.Inky(self.clock, image_library, screen, settings, self.sounds, 155, self.settings.get_screen_height() / 2) self.clyde = ghost.Clyde(self.clock, image_library, screen, settings, self.sounds, 110, self.settings.get_screen_height() / 2) self.pinky = ghost.Pinky(self.clock, image_library, screen, settings, self.sounds, 65, self.settings.get_screen_height() / 2) self.ghosts = [self.blinky, self.pinky, self.inky, self.clyde] self.fakeGhosts = self.ghosts.copy() self.loop = True self.title_loop()
def run_game(): # Provides consistent window positioning. These settings center Pygame window for my computer. os.environ['SDL_VIDEO_WINDOW_POS'] = '60, 35' pygame.init() # Initial Set-up settings = s.Settings() pygame.display.set_caption(settings.get_game_title()) screen = pygame.display.set_mode( (settings.get_screen_width(), settings.get_screen_height())) screen.fill(settings.get_bg_color()) clock = pygame.time.Clock() # Generate Game Objects image_lib = gF.import_image_library() sound_lib = gF.import_sound_library() maze = m.Maze(image_lib, screen, settings) pacman = p.PacMan(maze.pacmanCoordinates, image_lib, screen, settings, sound_lib) pills = maze.pills.copy() large_pills = maze.largePills.copy() blinky = g.Blinky(clock, image_lib, screen, settings, sound_lib, maze.blinkyCoordinates[0], maze.blinkyCoordinates[1], maze.nodes) clyde = g.Clyde(clock, image_lib, screen, settings, sound_lib, maze.clydeCoordinates[0], maze.clydeCoordinates[1], maze.nodes) inky = g.Inky(clock, image_lib, screen, settings, sound_lib, maze.inkyCoordinates[0], maze.inkyCoordinates[1], maze.nodes) pinky = g.Pinky(clock, image_lib, screen, settings, sound_lib, maze.pinkyCoordinates[0], maze.pinkyCoordinates[1], maze.nodes) ghosts = [blinky, clyde, inky, pinky] title_sequence = tS.TitleScreen(clock, pacman.images.copy(), image_lib, screen, settings) title_sequence.refresh_screen() pacman.set_map(maze.get_map(), maze.rowIndex, maze.columnIndex) maze.draw_part_maze() scoreboard = sb.ScoreBoard(maze, screen, settings) timer = 1 # pacTimer timer2 = 0.5 # pillTimer timer3 = 1.5 # ghostTimer delta_t = 0 # Delta to subtract from time reset_flag = False print(reset_flag) gF.start_game(ghosts, large_pills, pacman, pills, sound_lib[0]) pygame.mixer.Sound.play(sound_lib[1], -1) while True: gF.check_events(pacman) pacman.update() reset_flag = gF.check_collisions(ghosts, large_pills, maze, pacman, pills, scoreboard, sound_lib[3]) delta_t, timer, timer2, timer3 = gF.check_time(clock, delta_t, ghosts, large_pills, timer, timer2, timer3, pacman) for pill in large_pills: pill.blit() for ghost in ghosts: # Comment ghost.update out for game to run without ghosts updating. ghost.update() ghost.blit() pacman.blit() pygame.display.flip() if reset_flag: screen.fill(settings.get_bg_color()) pills = maze.pills.copy() large_pills = maze.largePills.copy() maze.draw_part_maze() gF.start_game(ghosts, large_pills, pacman, pills, sound_lib[0]) timer = 1 timer2 = 0.5 timer3 = 1.5 delta_t = 0 reset_flag = False print(reset_flag)