當前位置: 首頁>>代碼示例>>Python>>正文


Python Snake.check_loss方法代碼示例

本文整理匯總了Python中snake.Snake.check_loss方法的典型用法代碼示例。如果您正苦於以下問題:Python Snake.check_loss方法的具體用法?Python Snake.check_loss怎麽用?Python Snake.check_loss使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在snake.Snake的用法示例。


在下文中一共展示了Snake.check_loss方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: Game

# 需要導入模塊: from snake import Snake [as 別名]
# 或者: from snake.Snake import check_loss [as 別名]
class Game():
    star_cords = (0, 0)

    def __init__(self, screen):
        self.screen = screen
        self.RUNNING = True
        self.speed = 1.0
        self.place_star()

    def speed_up(self):
        self.speed /= 2.0

    def speed_down(self):
        self.speed *= 2.0

    def calc_start_loc(self):
        y = curses.LINES // 2
        x = curses.COLS // 2

        return (y, x)

    def start(self):
        self.snek = Snake(
            self.screen,
            6,
            self.calc_start_loc()
        )
        self.snek.render()
        while self.RUNNING:
            try:
                time.sleep(self.speed)
                self.tick()
            except KeyboardInterrupt:
                self.RUNNING = False

    def place_star(self):
        y = random.randint(0, curses.LINES - 1)
        x = random.randint(0, curses.COLS - 1)

        self.star_cords = (y, x)

    def check_star_get(self):
        if self.star_cords in [
            seg.coordinates
            for seg in self.snek.body
        ]:
            self.snek.add_segment()
            self.place_star()

    def tick(self):
        self.screen.clear()
        direction = self.screen.getch()
        if 258 <= direction <= 261:
            if direction == self.snek.direction:
                self.speed_up()
            elif (direction <= 259 and self.snek.direction <= 259
                  or direction >= 260 and self.snek.direction >= 260):
                self.speed_down()
            elif direction < 260:
                self.speed_down()
                self.snek.direction = direction
            else:
                self.speed_up()
                self.snek.direction = direction
        self.snek.move()
        self.snek.check_loss()
        self.check_star_get()
        self.screen.addstr(
            self.star_cords[0],
            self.star_cords[1],
            '*'
        )
        self.snek.render()
        self.screen.refresh()
開發者ID:LegNBass,項目名稱:PySnake,代碼行數:76,代碼來源:main.py


注:本文中的snake.Snake.check_loss方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。