class AsciiUI: # The ASCII user interface base class which provides the drawing loop. def __init__( self, server, world ): self.server = server self.gameClock = GameClock( ( 1, 1 ) , world) self.needBarSleep = NeedBar( ( 2, 1 ), 'Sleep ', world.characters[ 0 ], 'sleep' ) self.needBarFood = NeedBar( ( 3, 1 ), 'Food ', world.characters[ 0 ], 'food' ) self.needBarWater = NeedBar( ( 4, 1 ), 'Water ', world.characters[ 0 ], 'water' ) self.needBarUrination = NeedBar( ( 2, 30 ), 'Urination ', world.characters[ 0 ], 'urination' ) self.needBarHygiene = NeedBar( ( 3, 30 ), 'Hygiene ', world.characters[ 0 ], 'hygiene' ) self.needBarFun = NeedBar( ( 4, 30 ), 'Fun ', world.characters[ 0 ], 'fun' ) self.needBarSocial = NeedBar( ( 2, 60 ), 'Social ', world.characters[ 0 ], 'social' ) self.activityBar = ActivityBar( ( 5, 1 ), world.characters[ 0 ] ) self.mapView = MapView( (6, 0), world ) def run( self, treadPool ): os.system( 'clear' ) while( self.server.stop != True ): self.gameClock.printWidget() self.needBarSleep.printWidget() self.needBarFood.printWidget() self.needBarWater.printWidget() self.needBarUrination.printWidget() self.needBarHygiene.printWidget() self.needBarFun.printWidget() self.needBarSocial.printWidget() self.activityBar.printWidget() self.mapView.printWidget() sleep( 1 )
def __init__(self,parent): super(MainWindow, self).__init__() self.graphics = Graphics() self.parent = parent self.setFixedSize(950,631) self.setWindowTitle("Balance of Power") self.setStyleSheet("QMainWindow::separator{ width: 0px; height: 0px, margin: -10px; }") self.world = World(4,False,0) self.policyWindow = PolicyWindow(self) self.mapView = MapView(self, self.world) self.statusBar = StatusBar(self) self.newGameWindow = NewGameWindow(self) self.newsWindow = NewsWindow(self) self.closeUpWindow = CloseUpWindow(self) self.scoresWindow = ScoresWindow(self) self.controlPanel = ControlPanel(self) self.lostGameWindow = LostGameWindow(self) for i in range(2,80,1): self.world.addNews(self.world.country[1], self.world.country[i], self.world.country[i], 2 + i%4, 1, 2, False) self.addDockWidget(Qt.BottomDockWidgetArea, self.controlPanel) self.addDockWidget(Qt.TopDockWidgetArea, self.statusBar) self.setCentralWidget(self.mapView) self.setWindowIcon(QIcon(self.graphics.progIcon)) self.controlPanel.connectActions() self.menu = Menu(self) self.setMenuBar(self.menu) self.loadOptions() self.mapView.resetMapView() self.loadWorld()
def __init__( self, server, world ): self.server = server self.gameClock = GameClock( ( 1, 1 ) , world) self.needBarSleep = NeedBar( ( 2, 1 ), 'Sleep ', world.characters[ 0 ], 'sleep' ) self.needBarFood = NeedBar( ( 3, 1 ), 'Food ', world.characters[ 0 ], 'food' ) self.needBarWater = NeedBar( ( 4, 1 ), 'Water ', world.characters[ 0 ], 'water' ) self.needBarUrination = NeedBar( ( 2, 30 ), 'Urination ', world.characters[ 0 ], 'urination' ) self.needBarHygiene = NeedBar( ( 3, 30 ), 'Hygiene ', world.characters[ 0 ], 'hygiene' ) self.needBarFun = NeedBar( ( 4, 30 ), 'Fun ', world.characters[ 0 ], 'fun' ) self.needBarSocial = NeedBar( ( 2, 60 ), 'Social ', world.characters[ 0 ], 'social' ) self.activityBar = ActivityBar( ( 5, 1 ), world.characters[ 0 ] ) self.mapView = MapView( (6, 0), world )
def __init__(self, file_name, index): # Action handling self.action_handler = ActionHandler() # Create mapview self.view = MapView(self.action_handler, index) # Dictionary self.dct = {-1: Border, 1: Floor, 2: partial(self.build_goal, 1), 3: partial(self.build_goal, 2), 4: partial(self.build_player, 1), 5: partial(self.build_player, 2), 6: Block, 7: Hole, 8: "mirrorDU", 9: "mirrorUD", 10: "memory", 11: "p1Wall", 12: "p2Wall" } # Parse file self.mat = add_border(parse(file_name)) self.width = TileView.nb_lines = len(self.mat) self.height = len(self.mat[0]) self.players = {} self.goals = {} self.tiles = {pos: self.dct[element](pos, self.get_id(pos)) for i, line in enumerate(self.mat) for j, element in enumerate(line) for pos in [(i,j)]} for id_player, player in self.players.items(): self.action_handler.add_player(id_player, player)
class MainWindow(QMainWindow): def __init__(self,parent): super(MainWindow, self).__init__() self.graphics = Graphics() self.parent = parent self.setFixedSize(950,631) self.setWindowTitle("Balance of Power") self.setStyleSheet("QMainWindow::separator{ width: 0px; height: 0px, margin: -10px; }") self.world = World(4,False,0) self.policyWindow = PolicyWindow(self) self.mapView = MapView(self, self.world) self.statusBar = StatusBar(self) self.newGameWindow = NewGameWindow(self) self.newsWindow = NewsWindow(self) self.closeUpWindow = CloseUpWindow(self) self.scoresWindow = ScoresWindow(self) self.controlPanel = ControlPanel(self) self.lostGameWindow = LostGameWindow(self) for i in range(2,80,1): self.world.addNews(self.world.country[1], self.world.country[i], self.world.country[i], 2 + i%4, 1, 2, False) self.addDockWidget(Qt.BottomDockWidgetArea, self.controlPanel) self.addDockWidget(Qt.TopDockWidgetArea, self.statusBar) self.setCentralWidget(self.mapView) self.setWindowIcon(QIcon(self.graphics.progIcon)) self.controlPanel.connectActions() self.menu = Menu(self) self.setMenuBar(self.menu) self.loadOptions() self.mapView.resetMapView() self.loadWorld() def loadOptions(self): try: with open("options.dat", "r") as f: options = json.load(f) except: options = DEFAULT_OPTS for i,j in zip(options[:3], self.menu.options[:3]): j.setChecked(i) self.newGameWindow.levelOpt[options[4]].setChecked(True) self.newGameWindow.modeOpt[options[5]].setChecked(True) self.newGameWindow.sideOpt[options[6]].setChecked(True) self.menu.languages[options[3]].setChecked(True) def saveOptions(self): options = [i.isChecked() for i in self.menu.options[:3]] for i in self.menu.languages.values(): if i.isChecked(): options += [i.text().lower() + ".json"] break options += [i.checkedId() for i in self.newGameWindow.buttonGroups] try: with open("options.dat", "w") as f: json.dump(options, f) except: pass def updateLevel(self): for i in INAVL_MAP_MODES[self.world.level - 1]: self.controlPanel.mapModeAction[i].setEnabled(False) self.controlPanel.mapModeButton[i].setEnabled(False) def setStatus(self, id): """Set status label in the top part of the window""" # if -1, set currently moving player if id == -1: if self.world.twoPFlag: self.statusBar.setLabel(Local.strings[Local.MAIN_PANEL][15] \ + Local.strings[Local.DATA_COUNTRIES + self.world.human][Local.CTRY_NAME]) else: self.statusBar.setLabel(Local.strings[Local.MAIN_PANEL][14]) else: self.statusBar.setLabel(Local.strings[Local.MAIN_PANEL][id]) time.sleep(.1) QApplication.processEvents() def endGame(self): """Show effects of the game ending, depends on the active flag""" status = 0 if self.world.winFlag: self.scoresWindow.setVisible(True) status = 27 elif self.world.ANWFlag or self.world.NWFlag: self.lostGameWindow.showUp(self.world.ANWFlag) status = 28 self.setStatus(status) def saveWorld(self, manual=False): """Save the self.world property into the save.dat file using the pickle protocol""" try: pickle.dump(self.world, open("save.dat", "wb")) if manual: QMessageBox.information(self, Local.strings[Local.MENU][61], Local.strings[Local.MENU][70]) except Exception as e: if manual: QMessageBox.critical(self, Local.strings[Local.MENU][61], Local.strings[Local.MENU][71]) def loadWorld(self): """Load the pickled save.dat file into the self.world property""" try: self.setWorld(pickle.load(open("save.dat", "rb"))) w = self.world self.setStatus(-1) self.controlPanel.drawScores() self.controlPanel.yearLabel.setText(str(w.year)) self.controlPanel.switchPlayerButton.setEnabled(w.twoPFlag) self.controlPanel.nextTurnButton.setEnabled(not any((w.winFlag, w.ANWFlag, w.NWFlag, w.beingQuestioned))) if w.winFlag: self.setStatus(27) elif w.ANWFlag or w.NWFlag: self.setStatus(28) else: self.setStatus(-1) self.mapView.scene().mapPainter.recalculateMapBuffer() self.mapView.resetMapView() except: pass def setWorld(self, newWorld): """Set the self.world property""" # Backup old graphics polygons items polys = [c.mapPolyObject for c in self.world.country] # Make sure all the data are cleared try: del self.world except: pass # Create the new world self.world = newWorld self.mapView.scene().mapPainter.setWorld(self.world) self.updateLevel() # Move old graphics polygons onto the new world for poly,cntry in zip(polys, self.world.country): cntry.mapPolyObject = poly # Reset the news window self.newsWindow.setLocked(any((newWorld.winFlag, newWorld.ANWFlag, newWorld.NWFlag))) self.newsWindow.question.setEnabled(False) self.newsWindow.backDown.setEnabled(False) self.newsWindow.filters[2 * newWorld.cmptr].setChecked(True) def closeEvent(self, event): if self.menu.options[0].isChecked(): self.saveWorld() event.accept()
class Map: def __init__(self, file_name, index): # Action handling self.action_handler = ActionHandler() # Create mapview self.view = MapView(self.action_handler, index) # Dictionary self.dct = {-1: Border, 1: Floor, 2: partial(self.build_goal, 1), 3: partial(self.build_goal, 2), 4: partial(self.build_player, 1), 5: partial(self.build_player, 2), 6: Block, 7: Hole, 8: "mirrorDU", 9: "mirrorUD", 10: "memory", 11: "p1Wall", 12: "p2Wall" } # Parse file self.mat = add_border(parse(file_name)) self.width = TileView.nb_lines = len(self.mat) self.height = len(self.mat[0]) self.players = {} self.goals = {} self.tiles = {pos: self.dct[element](pos, self.get_id(pos)) for i, line in enumerate(self.mat) for j, element in enumerate(line) for pos in [(i,j)]} for id_player, player in self.players.items(): self.action_handler.add_player(id_player, player) def build_player(self, player_id, pos, pid): floor = Floor(pos, pid) self.players[player_id] = Player(player_id, pos, self, floor) return floor def build_goal(self, goal_id, pos, pid): self.goals[goal_id] = Goal(goal_id, pos, pid) return self.goals[goal_id] def get_id(self, pos): pos = XY(*pos) return pos.x * self.width + pos.y def projection(self, player_id): # Init variables result = [] player = self.players[player_id] current_pos = player.pos other_pos = next(p.pos for i,p in self.players.items() if i!=player_id) # Loop over valid positions stop = False while not stop: # Append and update current position result.append(current_pos) current_pos += player.dir # Test new position next_tile = self.tiles[current_pos] stop = isinstance(next_tile, (Block, Border, Hole)) stop = stop or current_pos == other_pos # Black hole case if isinstance(next_tile, Hole): result.append(current_pos) # Return result return result def get_success(self): # Inint result result = [] # Get results for i in (1,2): goal = self.goals[i] player = self.players[i] success = (goal.pos == player.pos) goal.set_active(success) result.append(success) # Return the result return tuple(result) def win(self): self.view.win() def lose(self, nb_tiles): self.view.lose(nb_tiles)