當前位置: 首頁>>代碼示例>>Python>>正文


Python Snake.die方法代碼示例

本文整理匯總了Python中snake.Snake.die方法的典型用法代碼示例。如果您正苦於以下問題:Python Snake.die方法的具體用法?Python Snake.die怎麽用?Python Snake.die使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在snake.Snake的用法示例。


在下文中一共展示了Snake.die方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: SnakeWidget

# 需要導入模塊: from snake import Snake [as 別名]
# 或者: from snake.Snake import die [as 別名]
class SnakeWidget(QWidget):
    def __init__(self, parent):
        super(SnakeWidget, self).__init__(parent)
        self.snake = Snake()
        self.newGame()
        self.colors = [QtGui.QColor(255, 0, 0, 255),
                       QtGui.QColor(255, 255, 0, 255),
                       QtGui.QColor(0, 0, 255, 255),
                       QtGui.QColor(0, 255, 0, 255)]

    def newGame(self):
        self.is_paused = False
        self.show()
        self.timer = QtCore.QTimer(self)
        self.timer.timeout.connect(self.move)
        self.timer.start(75)

    def resizeEvent(self, event):
        self._cell_height = self.size().height() // self.snake.field_height
        self._cell_width = self.size().width() // self.snake.field_width

    def paintEvent(self, event):
        canvas = QtGui.QPainter()
        canvas.begin(self)
        self.drawFood(canvas)
        self.drawSnake(canvas)
        canvas.end()

    def keyPressEvent(self, event):
        directions = {QtCore.Qt.Key_Down: "down",
                      QtCore.Qt.Key_Up: "up",
                      QtCore.Qt.Key_Right: "right",
                      QtCore.Qt.Key_Left: "left"}
        if self.snake.dead and event.key() == QtCore.Qt.Key_N:
            self.newGame()

        if event.key() == QtCore.Qt.Key_Escape:
            self.die()
            self.hide()

        if event.key() == QtCore.Qt.Key_Space:
            self.pause()

        if not self.is_paused and event.key() in directions.keys():
            self.snake.key_press(directions[event.key()])

    def pause(self):
        if not self.snake.dead:
            if self.is_paused:
                self.pause_label.setText("")
                self.is_paused = False
                self.timer.start()
            else:

                self.is_paused = True
                self.pause_label.setText("Press Space or arrows to continue")
                self.timer.stop()
                print(self.snake.score)

    def drawSnake(self, canvas):
        canvas.setPen(QtCore.Qt.NoPen)
        for segment in self.snake.snake_body:
            canvas.setBrush(QtGui.QColor(255, 80, 0, 255))
            canvas.drawRect(segment[0] * self._cell_width,
                            segment[1] * self._cell_height,
                            self._cell_width, self._cell_height)

    def drawFood(self, canvas):
        canvas.setPen(QtCore.Qt.NoPen)
        canvas.setBrush(QtGui.QColor(0, 80, 255, 255))
        canvas.drawEllipse(
            self.snake.food_coordinates[0] * self._cell_width,
            self.snake.food_coordinates[1] * self._cell_height,
            self._cell_width, self._cell_height
            )

    @QtCore.pyqtSlot()
    def move(self):
        self.snake.move()
        self.repaint()

    def die(self):
        self.timer.stop()
        self.dead = True
        self.snake.die()
開發者ID:bitterfly,項目名稱:tamagotchi,代碼行數:87,代碼來源:snake_widget.py


注:本文中的snake.Snake.die方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。