def update_state_data(self, data): self.missiles = [] for _m in data['missiles']: self.missiles.append(Missile(_m)) self.opponents = [] for opponent in data['others']: self.opponents.append(Ship(opponent)) self.myself = Ship(data['you'])
def setPlayers(obs): global numPlayers, board numPlayers = len(obs['players']) playerIndex = obs['player'] allPlayerIds = obs['players'][playerIndex][2].keys() # Set friendly players on board. for id in allPlayerIds: player = obs['players'][playerIndex][2][id] tempShip = Ship(id, player[0] % 15, player[0] // 15, True) board.setShipSpace(tempShip) # Set all other players for i in range(numPlayers): # Make sure we are not looking at own players if (i != playerIndex): allPlayerIds = obs['players'][i][2].keys() # Set friendly players on board. for id in allPlayerIds: player = obs['players'][i][2][id] tempShip = Ship(id, player[0] % 15, player[0] // 15, False) board.setShipSpace(tempShip) board.show()
if __name__ == "__main__": if 'Windows' in platform.platform(): # car pb de dpi sur windows from ctypes import windll windll.shcore.SetProcessDpiAwareness(1) pg.init() clock = pg.time.Clock() SCREEN_W, SCREEN_H = 1500, 900 SCREEN = pg.display.set_mode((SCREEN_W, SCREEN_H)) SCALE = 3 images = getImgDict('../img') time = 0 player = Ship([SCREEN_W // 2, SCREEN_H // 2], images) enemies = [] bullets = [] stars = [Star(SCREEN_W, SCREEN_H, SCALE) for i in range(100)] cursor = 0 mode = 'menu' while mode: time += 1 clock.tick(30) for e in pg.event.get(): if e.type == pg.QUIT: mode = None continue
def run_game(): # Initialize Pygame pygame.mixer.pre_init(frequency=44100) pygame.init() # Initialize settings, preload assets, and create a clock settings = Settings() sounds = Sounds(settings) images = Images() clock = pygame.time.Clock() # Set up the window pygame.display.set_icon(images.icon) screen = pygame.display.set_mode( (settings.screen_width, settings.screen_height)) pygame.display.set_caption("Bullet Heck!") # Try to create a joystick object try: gamepad = Joystick(settings.gamepad_id) gamepad.init() settings.gamepad_connected = True except pygame.error: gamepad = None settings.gamepad_connected = False # Initialize the stats, HUD, and splash screen stats = Stats(settings) hud = HUD(settings, screen, stats, images) splash = SplashScreen(settings, images, screen) # Create the ship and groups for everything else ship = Ship(settings, screen, stats, images) stars = Group() bullets = Group() enemies = Group() enemy_bullets = Group() explosions = Group() pickups = Group() if not settings.mute_music: pygame.mixer.music.play(loops=-1) # Pause the music by default pygame.mixer.music.pause() # Main loop while stats.done is False: gf.check_events(settings, screen, ship, gamepad, bullets, stats, sounds, enemies, images, enemy_bullets, splash, hud) gf.update_stars(settings, screen, stars, images) gf.manage_game_level(settings, stats) if stats.game_active: ship.update(settings, images) gf.spawn_enemies(settings, screen, enemies, images, id, stats) gf.update_bullets(settings, screen, ship, bullets, enemies, sounds, enemy_bullets, images, stats, hud, explosions, pickups, splash) gf.update_enemy_stuff(settings, screen, ship, enemies, sounds, stats, explosions, images, pickups, hud, bullets, enemy_bullets, splash) # Update the explosions even if the game is paused. gf.update_explosions(explosions) gf.update_screen(settings, screen, stars, ship, bullets, enemies, explosions, pickups, hud, stats, enemy_bullets, splash) clock.tick(settings.fps_limit) if settings.show_fps: stats.fps = clock.get_fps()
def prep_ship(self): self.ship = Ship(self.settings, self.screen, self.stats, self.images) self.ship.rect.x = 5 self.ship.rect.y = self.powerbar.rect.bottom + 2
class HUD(): def __init__(self, settings, screen, stats, images): """The HUD.""" self.screen = screen self.settings = settings self.stats = stats self.images = images self.text_color = settings.white self.font = pygame.font.Font("fonts/unoestado.ttf", 32) self.font_small = pygame.font.Font("fonts/unoestado.ttf", 16) self.healthbar = Statbar(settings, screen, settings.red, stats.ship_health, settings.ship_health) self.invbar = Statbar(settings, screen, settings.blue, stats.ship_inv_timer, settings.ship_mercy_inv, self.healthbar) self.powerbar = Statbar(settings, screen, settings.green, stats.ship_level, settings.max_ship_level) self.prep_statbars() self.prep_ship() self.prep_life_amount() self.prep_score() self.prep_high_score() self.prep_fps() def prep_statbars(self): self.healthbar.rect.x = 5 self.healthbar.rect.y = 5 self.invbar.rect.center = self.healthbar.rect.center self.powerbar.rect.x = 5 self.powerbar.rect.y = self.healthbar.rect.bottom + 2 def prep_ship(self): self.ship = Ship(self.settings, self.screen, self.stats, self.images) self.ship.rect.x = 5 self.ship.rect.y = self.powerbar.rect.bottom + 2 def prep_life_amount(self): lives_left = self.stats.ship_lives lives_str = "X " + str(lives_left) self.lives_image = self.font.render(lives_str, True, self.text_color) self.lives_rect = self.lives_image.get_rect() self.lives_rect.left = self.ship.rect.right + 5 self.lives_rect.centery = self.ship.rect.centery def prep_score(self): score_str = "Score: " + "{:,}".format(self.stats.score) self.score_image = self.font.render(score_str, True, self.text_color) self.score_rect = self.score_image.get_rect() self.score_rect.left = 0 self.score_rect.bottom = self.settings.screen_height def prep_high_score(self): hs_str = "High score: " + "{:,}".format(self.stats.high_score) self.hs_image = self.font.render(hs_str, True, self.text_color) self.hs_rect = self.hs_image.get_rect() self.hs_rect.left = 0 self.hs_rect.bottom = self.score_rect.top def prep_fps(self): fps_str = str(int(self.stats.fps)) + " FPS" self.fps_image = self.font_small.render(fps_str, True, self.text_color) self.fps_rect = self.fps_image.get_rect() self.fps_rect.right = self.settings.screen_width self.fps_rect.bottom = self.settings.screen_height def update(self, stats): """Update and draw the HUD.""" self.ship.animate() self.ship.blitme() self.screen.blit(self.lives_image, self.lives_rect) self.screen.blit(self.score_image, self.score_rect) if not stats.game_active: self.screen.blit(self.hs_image, self.hs_rect) if self.settings.show_fps: self.prep_fps() self.screen.blit(self.fps_image, self.fps_rect) self.invbar.update(stats.ship_inv_timer) self.healthbar.update(stats.ship_health) self.powerbar.update(stats.ship_level)