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


Python Snake.get_pos方法代码示例

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


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

示例1: __init__

# 需要导入模块: from snake import Snake [as 别名]
# 或者: from snake.Snake import get_pos [as 别名]
class Game:
    """
    The main game class that holds the gameloop.
    """
    def __init__(self, mlhost, mlport):
        """
        Create a screen and define some game specific things.
        """
        pymlgame.init()
        self.screen = pymlgame.Screen(mlhost, mlport, 40, 16)
        self.clock = pymlgame.Clock(15)

        part = (int(self.screen.width / 2), int(self.screen.height / 2))
        self.snake = Snake([(part[0] - 2, part[1]), (part[0] - 1, part[1]),
                            part], RIGHT, (self.screen.width, self.screen.height))
        self.gameover = False
        self.apple = self.generate_apple()
        self.apple_surface = pymlgame.Surface(1, 1)
        self.apple_surface.draw_dot((0, 0), GREEN)
        self.oldapple = self.apple
        self.score = 0
        self.highscore = self.get_highscore()

    def update(self):
        """
        Update the screens contents in every loop.
        """
        if not self.snake.move():
                    self.gameover = True

        if self.snake.get_pos() == self.apple:
                self.score += 1
                self.oldapple = self.apple
                self.apple = self.generate_apple()

    def render(self):
        """
        Send the current screen content to Mate Light.
        """
        self.screen.reset()

        # draw snake
        surface = pymlgame.Surface(self.screen.width, self.screen.height)
        for part in self.snake.parts:
            surface.draw_dot(part, pymlgame.locals.RED)
        self.screen.blit(surface)

        # draw apple
        self.screen.blit(self.apple_surface, self.apple)

        if self.snake.parts[0] == self.oldapple:
            self.snake.grow = True
            self.oldapple = None

        self.screen.update()

        # TODO: accelerate every 5 points by 1 fps
        self.clock.tick()

    def handle_events(self):
        """
        Loop through all events.
        """
        for event in pymlgame.get_events():
            if event.type == E_NEWCTLR:
                print('new ctlr with id:', event.id)
            elif event.type == E_KEYDOWN:
                if event.button == CTLR_UP:
                    if self.snake.direction != DOWN:
                        self.snake.direction = UP
                elif event.button == CTLR_DOWN:
                    if self.snake.direction != UP:
                        self.snake.direction = DOWN
                elif event.button == CTLR_LEFT:
                    if self.snake.direction != RIGHT:
                        self.snake.direction = LEFT
                elif event.button == CTLR_RIGHT:
                    if self.snake.direction != LEFT:
                        self.snake.direction = RIGHT
            elif event.type == E_PING:
                #print('ping from', event.id)
                pass

    def gameloop(self):
        """
        A game loop that circles through the methods.
        """
        try:
            while not self.gameover:
                self.handle_events()
                self.update()
                self.render()
            print('game over - score:', self.score)
            print('current highscore:', self.highscore)
            if self.score > int(self.highscore):
                self.write_highscore()

            end = time.time() + 5
            while time.time() < end:
                self.screen.reset()
#.........这里部分代码省略.........
开发者ID:PyMLGame,项目名称:pymlsnake,代码行数:103,代码来源:game.py


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