当前位置: 首页>>代码示例>>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;未经允许,请勿转载。