def __init__(self): super(GameModel, self).__init__() # Get levels self.levels = level.get_levels() self.current_level = None self.num_wave = 0 self.num_level = 0 # Add the player self.player = Player() self.player.position = 400, 300 # Node for player bullets self.player_bullets = cocos.batch.BatchNode() # Node for enemy bullets self.enemy_bullets = cocos.batch.BatchNode() # Node for particles self.particles = cocos.cocosnode.CocosNode() # Message box that will displayed in between waves and levels w, h = director.get_window_size() self.message = cocos.text.Label( "nothing", color=(50, 50, 50, 255), font_name="Orbitron", font_size=40, anchor_x="center", anchor_y="center" ) self.message.position = w / 2, h / 2 self.message.visible = False # Register player event listeners self.player.push_handlers(self) # Paused flag self.paused = False self.pause_menu = PauseMenu(self)
def __init__(self): super(GameModel, self).__init__() # Get levels self.levels = level.get_levels() self.current_level = None self.num_wave = 0 self.num_level = 0 # Add the player self.player = Player() self.player.position = 400, 300 # Node for player bullets self.player_bullets = cocos.batch.BatchNode() # Node for enemy bullets self.enemy_bullets = cocos.batch.BatchNode() # Node for particles self.particles = cocos.cocosnode.CocosNode() # Message box that will displayed in between waves and levels w, h = director.get_window_size() self.message = cocos.text.Label('nothing', color=(50, 50, 50, 255), font_name='Orbitron', font_size=40, anchor_x='center', anchor_y='center') self.message.position = w / 2, h / 2 self.message.visible = False # Register player event listeners self.player.push_handlers(self) # Paused flag self.paused = False self.pause_menu = PauseMenu(self)
def main_loop(): pygame.init() pygame.display.set_caption(game.NAME) clock = pygame.time.Clock() background = pygame.Surface(game.screen.get_size()).convert() background.fill(pygame.Color(0, 0, 0)) input_queue = multiprocessing.Queue() while True: # Choose player mode options = ["AI Demo", "Two players", "Three players", "Four players"] selection = Menu(options).show() if selection is False: return else: game.num_players = selection + 1 # Choose level levels = level.get_levels() selection = Menu([lvl.name for lvl in levels]).show() if selection is False: continue else: game.level = levels[selection] # If single player, add an AI player if game.num_players == 1: game.num_players = 4 game.init_level() ai_engines = [] ai_processes = [] ai_engines.append(ai_classes[0]) ai_engines.append(ai_classes[1]) ai_engines.append(ai_classes[2]) ai_engines.append(ai_classes[1]) shared_apples = multiprocessing.Array(process.GameObject, list((apple.x, apple.y) for apple in game.apples)) shared_players = multiprocessing.Array(process.MovableGameObject, list(((player.x, player.y), player.direction) for player in game.players)) ai_processes = [_class(player_index=i, board=game.shared_board, players=shared_players, apples=shared_apples, player=game.players[i], args=(input_queue,)) for i, _class in enumerate(ai_engines)] # Load threaded AI if game.use_multiprocessing: map(lambda proc: proc.start(), ai_processes) game.players[1].name = 'The Spirit of AI' game.players[1].name = 'Bebe Bot' game.players[2].name = 'The Will of AI' game.players[3].name = 'Bot Choy' else: game.init_level() # Start game loop return_to_menu = False game_status = None ai_frame_count = 1 start_time = time.time() while not return_to_menu: clock.tick(game.frames_per_second) while True: # Process key presses from AI threads. # Pulls values from input queue, creates pygame Events, and # submits to event queue. try: pygame.event.post(pygame.event.Event(KEYDOWN, {'key': input_queue.get_nowait(),})) except Queue.Empty, qe: break # Process non multiprocessing AI moves if not game.use_multiprocessing: # map(lambda proc: proc.execute(), ai_processes) for proc in ai_processes: if not isinstance(proc, JasonAI): proc.execute() if ai_frame_count < 3: ai_frame_count += 1 else: for proc in ai_processes: if isinstance(proc, JasonAI): proc.execute() ai_frame_count = 1 # Get input for event in pygame.event.get(): if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE): return_to_menu = True # Shutdown all AI processes if game.use_multiprocessing: map(lambda proc: proc.shutdown(), ai_processes) break if event.type == KEYDOWN: if event.key == K_SPACE: game.players[0].grow = True elif event.key == K_RETURN and game_status == "win": game.init_level() game_status = None continue elif event.key in game.player_controls[0]: game.players[0].set_direction(game.player_controls[0].index(event.key)) elif event.key in game.player_controls[1] and game.num_players > 1: game.players[1].set_direction(game.player_controls[1].index(event.key)) elif event.key in game.player_controls[2] and game.num_players > 2: game.players[2].set_direction(game.player_controls[2].index(event.key)) elif event.key in game.player_controls[3] and game.num_players > 3: game.players[3].set_direction(game.player_controls[3].index(event.key)) # Update effects for effect in game.effects: effect.update() # Update game game.update() # Update shared board for i, v in enumerate([(apple.x, apple.y) for apple in game.apples]): shared_apples[i] = v for i, v in enumerate([((player.x, player.y), player.direction, player.get_length()) for player in game.players]): shared_players[i] = v # Draw the screen game.screen.blit(background, (0, 0)) game.draw() for effect in game.effects: effect.draw() # Draw scoreboard score_icon_size = 30 score_width = 55 score_margin = 120 all_score_widths = game.num_players * score_width + (game.num_players-1) * score_margin score_x = (game.WINDOW_WIDTH - all_score_widths)/2 score_y = game.WINDOW_HEIGHT - game.SCOREBOARD_HEIGHT + (game.SCOREBOARD_HEIGHT-score_icon_size)/2 for i, player in enumerate(game.players): icon = pygame.Rect(score_x + i*(score_width+score_margin), score_y, score_icon_size, score_icon_size) text = str(len(player.kills)) score = pygame.font.SysFont('impact', 30).render(text, 1, pygame.Color("white")) score_pos = score.get_rect(left = icon.right + 10, centery = icon.centery) game.screen.blit(score, score_pos) pygame.draw.rect(game.screen, player.color, icon) runtime = int(time.time() - start_time) runtime_min = runtime // 60 runtime_sec = runtime % 60 if runtime_min < 10: runtime_min = "0" + str(runtime_min) if runtime_sec < 10: runtime_sec = "0" + str(runtime_sec) runtime_text = "%s : %s" % (runtime_min, runtime_sec) time_text = pygame.font.SysFont('impact', 24).render(runtime_text, 1, pygame.Color("white")) time_pos = time_text.get_rect(x = game.WINDOW_WIDTH - 95, y = game.WINDOW_HEIGHT - 54) game.screen.blit(time_text, time_pos) # Check for the win condition winners = filter(lambda p: len(p.kills) >= game.level.kills_to_win, game.players) if winners: game_status = 'win' # Check for ties least_deaths = min(len(w.deaths) for w in winners) winners = filter(lambda w: len(w.deaths) == least_deaths, winners) if len(winners) > 1: title = pygame.font.SysFont("impact", 100).render("Draw!", 1, pygame.Color("white")) else: title = pygame.font.SysFont("impact", 100).render(winners[0].name + " wins!", 1, pygame.Color("white")) # Draw title title_pos = title.get_rect(centerx = game.WINDOW_WIDTH/2, centery = 200) game.screen.blit(title, title_pos) # Draw subtitle subtext = pygame.font.SysFont("verdana", 15).render("Press [ENTER] to play again, or [ESC] to return to the main menu.", 1, pygame.Color("white")) subtext_pos = subtext.get_rect(centerx = game.WINDOW_WIDTH/2, y = title_pos.bottom + 10) game.screen.blit(subtext, subtext_pos) # Draw summary header_width = 200 header_height = 30 header_margin = 0 header_x = (game.WINDOW_WIDTH - header_width * game.num_players) / 2 header_y = subtext_pos.bottom + 50 cell_margin = 20 for i, player in enumerate(game.players): # Draw header box header = pygame.Rect(header_x + (header_width+header_margin)*i, header_y, header_width, header_height) pygame.draw.rect(game.screen, player.color, header) # Draw header text text = pygame.font.SysFont("arial", 16, bold=True).render(player.name, 1, pygame.Color("white")) text_pos = text.get_rect(centerx = header.centerx, centery = header.centery) game.screen.blit(text, text_pos) # Draw death summary s = "Total deaths: " + str(len(player.deaths)) death_summary_font = pygame.font.SysFont("arial", 14, bold=True).render(s, 1, pygame.Color("white")) death_summary_pos = death_summary_font.get_rect(centerx = header.centerx, centery = header.bottom + cell_margin) game.screen.blit(death_summary_font, death_summary_pos) strings = [] strings.append(str(len(filter(lambda c: isinstance(c, game_objects.SnakePart) and c.player is not player, player.deaths))) + " by collision") strings.append(str(len(filter(lambda c: isinstance(c, game_objects.Missile) and c.player is not player, player.deaths))) + " by missile") strings.append(str(len(filter(lambda c: isinstance(c, game_objects.Wall), player.deaths))) + " by wall") strings.append(str(len(filter(lambda c: (isinstance(c, game_objects.Missile) and c.player is player) or (isinstance(c, game_objects.SnakePart) and c.player is player), player.deaths))) + " by suicide") for i, s in enumerate(strings): text = pygame.font.SysFont("arial", 13).render(s, 1, pygame.Color("white")) text_pos = text.get_rect(centerx = header.centerx, centery = death_summary_pos.bottom + (i+1)*cell_margin) game.screen.blit(text, text_pos) # Draw kill summary s = "Total kills: " + str(len(player.kills)) kill_summary_font = pygame.font.SysFont("arial", 14, bold=True).render(s, 1, pygame.Color("white")) kill_summary_pos = kill_summary_font.get_rect(centerx = header.centerx, centery = header.bottom + cell_margin + 130) game.screen.blit(kill_summary_font, kill_summary_pos) for i, opponent in enumerate(player.kills): text = pygame.font.SysFont("arial", 13).render(opponent.name, 1, opponent.color) text_pos = text.get_rect(centerx = header.centerx, centery = kill_summary_pos.bottom + (i+1)*cell_margin) game.screen.blit(text, text_pos) # Display! pygame.display.flip()