Exemplo n.º 1
0
class AppScreenG(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="Mistrz szachownicy GTK")

        self.engine = AppModel()
        self.gameFlag = False
        self.timeCounter = TIME_LIMIT

        self.set_default_size(800, 800)
        self.set_size_request(800, 800)
        self.box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=0)
        self.add(self.box)
        self._menuSetUp()
        self._boardSetUp()
        self._progressBarSetUp()
        self._controlSetUp()
        self._statsSetUp()
        self._makeItPretty()

    def _makeItPretty(self):
        css = b"""
        button {
            color: black;
            border-style: solid;
            border-color: gray;
            border-width: 1px;
        }
        
        button:disabled * {
            color:gray
        }
        
        #stats {
            color: black;
            border: 2px solid gray;
            padding: 0px;
            border-radius: 10px;
            margin: 4px;
            background: cornsilk;
            min-height: 40px;
        }
        
        #menu {
            color: black;
            margin: 4px;
            padding: 0px;
            border-style: solid;
            border-color: gray;
            border-width: 2px;
            border-radius: 10px;
            background: cornsilk;
            min-height: 40px;
        }
        
        progress {
            min-height: 20px;
        }
        
        progressbar, trough {
            min-height: 20px;
            margin: 2px;
            padding: 2px;
        }
        
        #whitesquare {
            background: linen;
        }
        
        #blacksquare {
            background: sienna;
        }
        
        #redsquare {
            background: red;
        }
        
        #greensquare {
            background: green;
        }
        
        #boxframe {
            padding: 10px;
        }
        
        """

        self.box.set_name("boxframe")

        self.scoreStatic.set_name("stats")
        self.score.set_name("stats")
        self.coordStatic.set_name("stats")
        self.coord.set_name("stats")
        self.startButton.set_name("menu")
        self.stopButton.set_name("menu")

        css_provider = Gtk.CssProvider()
        css_provider.load_from_data(css)
        context = Gtk.StyleContext()
        screen = Gdk.Screen.get_default()
        context.add_provider_for_screen(screen, css_provider,
                                        Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)

    def _menuSetUp(self):
        menuBar = Gtk.MenuBar()

        menu = Gtk.Menu()
        overview = Gtk.MenuItem("Menu")
        overview.set_submenu(menu)

        menuText = Gtk.MenuItem("Opis gry")
        menuText.connect("activate", self._overviewDisplay)
        menu.append(menuText)

        menuBar.append(overview)

        menuBox = Gtk.VBox(False, 2)
        menuBox.pack_start(menuBar, False, False, 0)
        self.box.pack_start(menuBox, True, True, 0)

    def _overviewDisplay(self, widget):
        dialog = Gtk.MessageDialog(
            self,
            0,
            Gtk.MessageType.OTHER,
            Gtk.ButtonsType.OK,
            "Opis gry!",
        )
        dialog.set_modal(False)
        dialog.format_secondary_text(
            OVERVIEW_TEXT
        )
        dialog.connect("response", self._onDialogResponse)
        dialog.show()

    def _onDialogResponse(self, widget, response_id):
        widget.destroy()

    def _progressBarSetUp(self):
        self.progressbar = Gtk.ProgressBar()
        self.box.pack_start(self.progressbar, False, False, 0)

    def _statsSetUp(self):
        self.stats = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, homogeneous=True)

        self.scoreStatic = Gtk.Label("Aktualny wynik:")
        self.score = Gtk.Label("0")
        self.coordStatic = Gtk.Label("Znajdź:")
        self.coord = Gtk.Label("")

        self.stats.add(self.scoreStatic)
        self.stats.add(self.score)
        self.stats.add(self.coordStatic)
        self.stats.add(self.coord)
        self.box.pack_start(self.stats, False, False, 0)

    def _controlSetUp(self):
        self.menu = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, homogeneous=True)

        self.startButton = Gtk.Button("Start")
        self.startButton.connect("clicked", self._onStartClick)
        self.menu.add(self.startButton)

        self.stopButton = Gtk.Button("Stop")
        self.stopButton.connect("clicked", self._onStopClick)
        self.stopButton.set_sensitive(False)
        self.menu.add(self.stopButton)

        self.box.pack_start(self.menu, False, False, 0)

    def _boardSetUp(self):
        self.gameBox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
        self.grid = Gtk.Grid()
        self.grid.set_size_request(560, 560)
        self.gameBox.pack_start(self.grid, False, False, 0)
        self.box.pack_start(self.gameBox, False, False, 0)

        for i in range(8 * 8):
            newButton = Gtk.Button()
            newButton.set_hexpand(True)
            newButton.set_vexpand(True)
            newButton.connect("pressed", self._onSquareClick)
            newButton.connect("released", self._onSquareRelease)
            x = getX(i)
            y = getY(i)
            if (x + y) % 2 == 0:
                newButton.set_name("whitesquare")
            else:
                newButton.set_name("blacksquare")
            self.grid.attach(newButton, x, y, 1, 1)

    def _onStartClick(self, btn):
        if self.gameFlag:
            return
        self.startButton.set_sensitive(False)
        self.stopButton.set_sensitive(True)
        self.gameFlag = True
        self.timeout_id = GLib.timeout_add(TIME_INTERVAL, self._onClockChanged)
        self.timeCounter = TIME_LIMIT

        self.engine.counterReset()
        self.engine.getNextPosition()
        self.coord.set_label(self.engine.getCurrentNotation())
        self.score.set_label("0")

    def _onStopClick(self, btn):
        if not self.gameFlag:
            return
        self.stopButton.set_sensitive(False)
        self.gameFlag = False

    def _onClockChanged(self):
        if (self.timeCounter == 0) or (self.gameFlag is False):
            self.startButton.set_sensitive(True)
            self.stopButton.set_sensitive(False)
            self.gameFlag = False
            return False
        self.timeCounter -= 1
        self.progressbar.set_fraction(self.timeCounter / TIME_LIMIT)
        return True

    def _onSquareClick(self, btn):
        if not self.gameFlag:
            return
        x = self.grid.child_get_property(btn, "top-attach")
        y = self.grid.child_get_property(btn, "left-attach")

        if self.engine.getCurrentPosition() == (x, y):
            btn.set_name("greensquare")
            self.engine.counterAdd()
            self.engine.getNextPosition()
            self.coord.set_label(self.engine.getCurrentNotation())
            self.score.set_label(str(self.engine.getCounter()))
            return
        btn.set_name("redsquare")

    def _onSquareRelease(self, btn):
        x = self.grid.child_get_property(btn, "top-attach")
        y = self.grid.child_get_property(btn, "left-attach")

        if (x + y) % 2 == 0:
            btn.set_name("whitesquare")
        else:
            btn.set_name("blacksquare")
Exemplo n.º 2
0
class AppScreenQ(QMainWindow):
    def __init__(self):
        super(AppScreenQ, self).__init__()
        self.setWindowTitle("Mistrz szachownicy QT")

        self.timeCounter = TIME_LIMIT
        self.gameFlag = False
        self.bar = None
        self.engine = AppModel()

        self.mainLayout = QVBoxLayout()
        self.setMinimumSize(800, 800)
        self._menuSetUp()
        self._boardSetUp()
        self._progressSetUp()
        self._controlSetUp()
        self._statsSetUp()

        widget = QWidget()
        widget.setLayout(self.mainLayout)
        self.setCentralWidget(widget)
        self.show()

    def _menuSetUp(self):
        fileMenu = self.menuBar().addMenu("&Menu")
        newAct = QAction('Opis gry', self)
        newAct.triggered.connect(self._overviewDisplay)
        fileMenu.addAction(newAct)

    def _overviewDisplay(self):
        dlg = QDialog(self)
        dlg.setWindowTitle("Opis gry!")
        overviewLayout = QVBoxLayout()
        overview = QLabel(OVERVIEW_TEXT)
        overview.setWordWrap(True)
        overviewLayout.addWidget(overview)

        buttonBox = QDialogButtonBox(QDialogButtonBox.Ok)
        buttonBox.setCenterButtons(True)
        buttonBox.accepted.connect(dlg.accept)
        overviewLayout.addWidget(buttonBox)

        dlg.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred)
        dlg.setLayout(overviewLayout)

        dlg.show()

    def _statsSetUp(self):
        style = """ 
        border: 2px solid gray;
        border-radius: 10px;
        padding: 8px;
        selection-background-color: darkgray;"""
        self.stats = QHBoxLayout()

        self.scoreStatic = QLabel("Aktualny wynik:")
        self.scoreStatic.setMinimumHeight(40)
        self.scoreStatic.setMaximumHeight(40)
        self.scoreStatic.setStyleSheet(style)
        self.scoreStatic.setAlignment(QtCore.Qt.AlignCenter)

        self.score = QLabel("0")
        self.score.setMinimumHeight(40)
        self.score.setMaximumHeight(40)
        self.score.setStyleSheet(style)
        self.score.setAlignment(QtCore.Qt.AlignCenter)

        self.coordStatic = QLabel("Znajdź:")
        self.coordStatic.setMinimumHeight(40)
        self.coordStatic.setMaximumHeight(40)
        self.coordStatic.setStyleSheet(style)
        self.coordStatic.setAlignment(QtCore.Qt.AlignCenter)

        self.coord = QLabel("")
        self.coord.setMinimumHeight(40)
        self.coord.setMaximumHeight(40)
        self.coord.setStyleSheet(style)
        self.coord.setAlignment(QtCore.Qt.AlignCenter)

        self.stats.addWidget(self.scoreStatic)
        self.stats.addWidget(self.score)
        self.stats.addWidget(self.coordStatic)
        self.stats.addWidget(self.coord)
        self.mainLayout.addLayout(self.stats)

    def _controlSetUp(self):
        style = """ 
        border-style: solid;
        border-color: gray;
        border-width: 2px;
        border-radius: 10px;"""

        self.menu = QHBoxLayout()

        self.startButton = QPushButton('Start', self)
        self.startButton.setStyleSheet(style)
        self.startButton.setMinimumHeight(40)
        self.startButton.clicked.connect(self._onButtonStart)

        self.stopButton = QPushButton('Stop', self)
        self.stopButton.setStyleSheet(style)
        self.stopButton.setMinimumHeight(40)
        self.stopButton.clicked.connect(self._onButtonStop)
        self.stopButton.setEnabled(False)

        self.menu.addWidget(self.startButton)
        self.menu.addWidget(self.stopButton)

        self.mainLayout.addLayout(self.menu)

    def _progressSetUp(self):
        self.progress = QProgressBar(self)
        self.progress.setTextVisible(False)
        self.progress.setStyleSheet("border: 2px solid grey;"
                                    "border-radius: 5px;"
                                    "text-align: center;"
                                    )
        self.mainLayout.addWidget(self.progress)
        self.progress.setMaximum(TIME_LIMIT)

    def _boardSetUp(self):
        frameWidget = QWidget()
        frameLayout = QHBoxLayout()
        frameLayout.addStretch()
        frameWidget.setFixedSize(560, 560)
        frameLayout.addWidget(frameWidget)
        frameLayout.addStretch()
        self.mainLayout.addLayout(frameLayout)
        squareLayout = QGridLayout(frameWidget)
        squareLayout.setSpacing(0)
        self.squares = QButtonGroup()
        for i in range(8 * 8):
            newButton = QPushButton()
            policy = QSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred)
            newButton.setSizePolicy(policy)
            x = getX(i)
            y = getY(i)
            if (x + y) % 2 == 0:
                newButton.setStyleSheet("background-color: linen")
            else:
                newButton.setStyleSheet("background-color: sienna")
            self.squares.addButton(newButton, i)
            squareLayout.addWidget(newButton, x, y)
        self.squares.setExclusive(True)
        self.squares.buttonPressed.connect(self._onSquareClick)
        self.squares.buttonReleased.connect(self._onSquareRelease)
        self.mainLayout.addLayout(squareLayout)

    def _onButtonStart(self):
        if self.gameFlag:
            return
        self.startButton.setEnabled(False)
        self.stopButton.setEnabled(True)
        self.gameFlag = True

        self.timeCounter = TIME_LIMIT
        self.bar = QTimer()
        self.bar.timeout.connect(self._onClockChanged)
        self.bar.start(TIME_INTERVAL)

        self.engine.counterReset()
        self.engine.getNextPosition()
        self.coord.setText(self.engine.getCurrentNotation())
        self.score.setText("0")

    def _onButtonStop(self):
        if not self.gameFlag:
            return
        self.bar.stop()
        self.stopButton.setEnabled(False)
        self.startButton.setEnabled(True)
        self.gameFlag = False

    def _onClockChanged(self):
        if (self.timeCounter == 0) or (self.gameFlag is False):
            self.startButton.setEnabled(True)
            self.stopButton.setEnabled(False)
            self.gameFlag = False
        self.timeCounter -= 1
        self.progress.setValue(self.timeCounter)

    def _onSquareClick(self, btn):
        if not self.gameFlag:
            return
        i = self.squares.id(btn)
        x = getX(i)
        y = getY(i)

        if self.engine.getCurrentPosition() == (x, y):
            btn.setStyleSheet("background-color: green")
            self.engine.counterAdd()
            self.engine.getNextPosition()
            self.coord.setText(self.engine.getCurrentNotation())
            self.score.setText(str(self.engine.getCounter()))
            return
        btn.setStyleSheet("background-color: red")

    def _onSquareRelease(self, btn):
        i = self.squares.id(btn)
        x = getX(i)
        y = getY(i)
        if (x + y) % 2 == 0:
            btn.setStyleSheet("background-color: linen")
        else:
            btn.setStyleSheet("background-color: sienna")