本文整理汇总了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()
示例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,
#.........这里部分代码省略.........