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


Python Snake.turn方法代码示例

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


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

示例1: __init__

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

    def __init__(self):
	cols = 50
	rows = 50
	side = 10

	self.snake = Snake()

	self.game = Game(cols, rows)
	self.game.add_snake(self.snake)
	self.game.start()

	self.w = Tkinter.Tk()
	self.c = Tkinter.Canvas(self.w,
				width=side*cols,
				height=side*rows,
				background='black')
	self.c.pack()

	self.view = SnakeTkinterView(self.c, side, self.game)
	self.view.add_action_listener(self)

	for key in ["<Left>", "<Right>", "<Up>", "<Down>"]:
	    self.w.bind(key, self.view.got_key)

    def turn_action(self, direction):
	self.snake.turn(direction)

    def tick(self):
	self.game.tick()
	self.view.draw()
	self.c.after(100, self.tick) # keep going

    def run(self):
	self.view.draw()
	self.c.after(100, self.tick)
	self.c.mainloop()
开发者ID:martinjos,项目名称:PySnake,代码行数:40,代码来源:snake_tkinter_game.py

示例2: World

# 需要导入模块: from snake import Snake [as 别名]
# 或者: from snake.Snake import turn [as 别名]
class World(ShowBase):
    def __init__(self):
        super().__init__()

        self.disable_mouse()
        self._background = load_object(
            "background",
            pos=Point2(0, 0),
            scale=9000,
            depth=200,
            transparency=False
        )
        self._gameboard = load_object(
            "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,
#.........这里部分代码省略.........
开发者ID:montreal91,项目名称:cobra,代码行数:103,代码来源:main.py


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