def main(): # For avoiding sound delay - thanks to BigglesW at StackOverflow pygame.mixer.pre_init(44100, -16, 1, 512) # Initialization ====================== pygame.init() # frames per second setting FPS = 60 fpsClock = pygame.time.Clock() # set up the window screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT)) pygame.display.set_caption('CometZ') # Setting window icon icon = pygame.image.load("iconShip.png") pygame.display.set_icon(icon) # gameState # States available: # menu, pause, game, gameOver # Create game instance game = Game() # ===================================== # Main game loop while True: # =========================================================== # EVENTS ================================================ # =========================================================== # check all events in event list for event in pygame.event.get(): game.handle(event) # for continuously pressed keys keys = pygame.key.get_pressed() game.keys(keys) # =========================================================== # GAME LOGIC ================================================ # =========================================================== # update game game.update() # =========================================================== # DRAWING ================================================ # =========================================================== # draw to the screen game.draw(screen) pygame.display.update() # =========================================================== # OTHER ================================================ # =========================================================== fpsClock.tick(FPS)
class ServerGameWindow(UIElement): def __init__(self, rect: Rect, color: Optional[Color], sock, connection_list, receive_list, parent_conn, child_conn, send_process, nicks): mod_loader.import_mods() super().__init__(rect, color) fps_font = Font('assets/fonts/arial.ttf', 20) sub_elem = UIElement(Rect(50, 50, 50, 50), None) sub_elem.append_child(FPSCounter(Rect(50, 50, 0, 0), fps_font)) self.append_child(sub_elem) self.sock = sock self.connection_list = connection_list self.receive_list = receive_list self.child_conn = child_conn self.parent_conn = parent_conn self.send_process = send_process self.game = Game(Game.Side.SERVER, mod_loader, self.parent_conn, nicks, -1, connection_list=self.connection_list) self.minimap_elem = UIImage( Rect(0, config['screen']['size'][1] - 388, 0, 0), 'assets/sprite/minimap.png') self.minimap = Minimap(self.game) self.minimap_elem.append_child(self.minimap) self.append_child(self.minimap_elem) self.game.create_unit('warrior', (0, 0)) self.game.create_unit('fortress', (500, 0)) self.game.create_unit('fortress', (-500, 0)) self.game.create_unit('archer', (-25, -25)) self.game.create_unit('archer', (25, -25)) self.game.create_unit('archer', (-25, 25)) self.game.create_unit('archer', (25, 25)) def update(self, event): self.game.update(event) if event.type == EVENT_UPDATE: while self.receive_list: sender, command = self.receive_list.pop(0) self.game.handle_command(command[0], command[1:], sender=sender) return super().update(event) def draw(self, screen): super().draw(screen) self.game.draw(screen) def shutdown(self): print('Shutdown') self.send_process.terminate() self.parent_conn.close() self.sock.close()
class ClientGameWindow(UIElement): def __init__(self, rect: Rect, color: Optional[Color], sock, receive_list, socket_process, parent_conn, child_conn, send_process, nicks, team_id): super().__init__(rect, color) self.sock = sock self.receive_list = receive_list self.socket_process = socket_process self.parent_conn = parent_conn self.child_conn = child_conn self.send_process = send_process mod_loader.import_mods() config.reload() fps_font = Font('assets/fonts/arial.ttf', 20) sub_elem = UIElement(Rect(50, 50, 50, 50), None) sub_elem.append_child(FPSCounter(Rect(50, 50, 0, 0), fps_font)) self.append_child(sub_elem) self.game = Game(Game.Side.CLIENT, mod_loader, self.parent_conn, nicks, team_id) self.minimap = Minimap(self.game) self.minimap_elem = UIImage( Rect(0, config['screen']['size'][1] - 388, 0, 0), 'assets/sprite/minimap.png') self.minimap_elem.append_child(self.minimap) self.minimap_elem.append_child( ResourceMenu(self.game.current_player, Rect(45, 108, 0, 0), Font('assets/fonts/arial.ttf', 25))) menu_parent = UIElement(Rect(0, 0, 0, 0), None) self.append_child(menu_parent) menu_parent.append_child(self.minimap_elem) menu_parent.append_child(BuildMenu(self.relative_bounds, self.game)) menu_parent.append_child(ProduceMenu(self.relative_bounds, self.game)) def update(self, event): self.game.update(event) if event.type == EVENT_UPDATE: while self.receive_list: args = self.receive_list.pop(0) self.game.handle_command(args[0], args[1:]) return super().update(event) def draw(self, screen): super().draw(screen) self.game.draw(screen) def shutdown(self): self.sock.close() self.send_process.terminate() self.socket_process.terminate() self.parent_conn.close() self.child_conn.close() print('Closed')