当前位置: 首页>>代码示例>>Python>>正文


Python Snake.gen_dot方法代码示例

本文整理汇总了Python中snake.Snake.gen_dot方法的典型用法代码示例。如果您正苦于以下问题:Python Snake.gen_dot方法的具体用法?Python Snake.gen_dot怎么用?Python Snake.gen_dot使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在snake.Snake的用法示例。


在下文中一共展示了Snake.gen_dot方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: World

# 需要导入模块: from snake import Snake [as 别名]
# 或者: from snake.Snake import gen_dot [as 别名]

#.........这里部分代码省略.........
            "background",
            pos=Point2(0, 0),
            scale=39.5,
            depth=100,
            transparency=False
        )
        gen_label_text("ESC  : Quit", 0)
        gen_label_text("SPACE: Pause", 1)
        gen_label_text("R    : Restart", 2)
        self._score = gen_label_text(
            "", 0, left=False
        )

        self._bricks = deque()
        self._dot = load_object(
            "brick", pos=Point2(0, 0)
        )
        self._restart()
        self.accept("escape", sys.exit)
        self.accept("enter", self.restart)
        self.accept("arrow_up", self._turn, [settings.POS_Y])
        self.accept("arrow_down", self._turn, [settings.NEG_Y])
        self.accept("arrow_left", self._turn, [settings.NEG_X])
        self.accept("arrow_right", self._turn, [settings.POS_X])
        self.accept("space", self._tooggle_pause)
        self.accept("r", self._restart)

        self.period = settings.PERIOD
        self.pause = True

    def _game_loop(self, task):
        dt = task.time - task.last
        if not self._snake.alive:
            return task.done
        if self.pause:
            return task.cont
        elif dt >= self.period:
            task.last = task.time
            self._snake.move_forward()
            self._snake.check_state()
            self._update_snake()
            self._update_dot()
            self._update_score()
        return task.cont

    def _draw_snake(self):
        for point in self._snake.body:
            brick = load_object("brick", pos=Point2(point.real, point.imag))
            self._bricks.append(brick)

    def _update_snake(self):
        if len(self._snake.body) > len(self._bricks):
            new_head = self._dot
            self._make_dot()
            self._bricks.appendleft(new_head)

        for i in range(len(self._snake.body)):
            point = self._snake.body[i]
            brick = self._bricks[i]
            brick.setPos(point.real, settings.SPRITE_POS, point.imag)

    def _make_dot(self):
        self._dot = load_object(
            "brick", pos=Point2(self._snake.dot.real, self._snake.dot.imag)
        )

    def _update_dot(self):
        x, y = self._dot.getX(), self._dot.getZ()
        if (x, y) != self._snake.dot:
            self._dot.setPos(
                self._snake.dot.real, settings.SPRITE_POS, self._snake.dot.imag)

    def _update_score(self):
        self._score.setText(
            "Score: %s" % self._snake.get_score()
        )

    def _tooggle_pause(self):
        self.pause = not self.pause

    def _restart(self):
        self.pause = True
        for b in self._bricks:
            b.remove_node()
        self._snake = Snake(
            body=_make_default_body(),
            vector=settings.POS_X,
            dot=complex()
        )
        self._snake.gen_dot()
        self._bricks = deque()
        self._update_dot()
        self._draw_snake()
        taskMgr.remove("GameLoop")
        self._game_task = taskMgr.add(self._game_loop, "GameLoop")
        self._game_task.last = 0

    def _turn(self, direction: complex):
        if not self.pause:
            self._snake.turn(direction)
开发者ID:montreal91,项目名称:cobra,代码行数:104,代码来源:main.py


注:本文中的snake.Snake.gen_dot方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。