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 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 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()