Esempio n. 1
0
    def create_grid(self, grid_width, grid_height):
        """Create a grid layout with stacked widgets.

        Parameters
        ----------
        grid_width : int
            the width of the grid
        grid_height : int
            the height of the grid
        """
        self.grid_layout = QGridLayout()
        self.setLayout(self.grid_layout)
        self.grid_layout.setSpacing(1)
        self.grid_wgs = {}
        for i in xrange(grid_height):
            for j in xrange(grid_width):
                self.grid_wgs[(i, j)] = FieldWidget()
                self.grid_layout.addWidget(self.grid_wgs[(i, j)], i, j)
Esempio n. 2
0
def ms_game_main(board_width, board_height, num_mines, port, ip_add):
    """Main function for Mine Sweeper Game.

    Parameters
    ----------
    board_width : int
        the width of the board (> 0)
    board_height : int
        the height of the board (> 0)
    num_mines : int
        the number of mines, cannot be larger than
        (board_width x board_height)
    port : int
        UDP port number, default is 5678
    ip_add : string
        the ip address for receiving the command,
        default is localhost.
    """
    ms_game = MSGame(board_width,
                     board_height,
                     num_mines,
                     port=port,
                     ip_add=ip_add)

    ms_app = QApplication([])

    ms_window = QWidget()
    ms_window.setAutoFillBackground(True)
    ms_window.setWindowTitle("Mine Sweeper")
    ms_layout = QGridLayout()
    ms_window.setLayout(ms_layout)

    fun_wg = gui.ControlWidget()
    grid_wg = gui.GameWidget(ms_game, fun_wg)
    remote_thread = gui.RemoteControlThread()

    def update_grid_remote(move_msg):
        """Update grid from remote control."""
        if grid_wg.ms_game.game_status == 2:
            grid_wg.ms_game.play_move_msg(str(move_msg))
            grid_wg.update_grid()

    remote_thread.transfer.connect(update_grid_remote)

    def reset_button_state():
        """Reset button state."""
        grid_wg.reset_game()

    fun_wg.reset_button.clicked.connect(reset_button_state)

    ms_layout.addWidget(fun_wg, 0, 0)
    ms_layout.addWidget(grid_wg, 1, 0)

    remote_thread.control_start(grid_wg.ms_game)

    ms_window.show()
    ms_app.exec_()
Esempio n. 3
0
class GameWidget(QWidget):
    """Setup Game Interface."""

    def __init__(self, ms_game, ctrl_wg):
        """Init the game."""
        super(GameWidget, self).__init__()

        self.ms_game = ms_game
        self.ctrl_wg = ctrl_wg
        self.init_ui()

    def init_ui(self):
        """Init game interface."""
        board_width = self.ms_game.board_width
        board_height = self.ms_game.board_height
        self.create_grid(board_width, board_height)
        self.time = 0
        self.timer = QtCore.QTimer()
        self.timer.timeout.connect(self.timing_game)
        self.timer.start(1000)

    def create_grid(self, grid_width, grid_height):
        """Create a grid layout with stacked widgets.

        Parameters
        ----------
        grid_width : int
            the width of the grid
        grid_height : int
            the height of the grid
        """
        self.grid_layout = QGridLayout()
        self.setLayout(self.grid_layout)
        self.grid_layout.setSpacing(1)
        self.grid_wgs = {}
        for i in xrange(grid_height):
            for j in xrange(grid_width):
                self.grid_wgs[(i, j)] = FieldWidget()
                self.grid_layout.addWidget(self.grid_wgs[(i, j)], i, j)

    def timing_game(self):
        """Timing game."""
        self.ctrl_wg.game_timer.display(self.time)
        self.time += 1

    def reset_game(self):
        """Reset game board."""
        self.ms_game.reset_game()
        self.update_grid()
        self.time = 0
        self.timer.start(1000)

    def update_grid(self):
        """Update grid according to info map."""
        info_map = self.ms_game.get_info_map()
        for i in xrange(self.ms_game.board_height):
            for j in xrange(self.ms_game.board_width):
                self.grid_wgs[(i, j)].info_label(info_map[i, j])

        self.ctrl_wg.move_counter.display(self.ms_game.num_moves)
        if self.ms_game.game_status == 2:
            self.ctrl_wg.reset_button.setIcon(QtGui.QIcon(CONTINUE_PATH))
        elif self.ms_game.game_status == 1:
            self.ctrl_wg.reset_button.setIcon(QtGui.QIcon(WIN_PATH))
            self.timer.stop()
        elif self.ms_game.game_status == 0:
            self.ctrl_wg.reset_button.setIcon(QtGui.QIcon(LOSE_PATH))
            self.timer.stop()