class ExitGameException(BaseException):
pass
+class RestartGameException(BaseException):
+ pass
+
class TileContents(Enum):
Empty = "Empty"
Wall = "Wall"
self.pause()
if ch == ord('q'):
raise ExitGameException()
+ if ch == ord('r'):
+ raise RestartGameException()
def random_empty_tile(self):
return self.random.choice([
break
if ch == ord('q'):
raise ExitGameException()
+ if ch == ord('r'):
+ raise RestartGameException()
self.win.nodelay(True)
self.paint()
ch = self.win.getch()
if ch == 27 or ch == ord('q'):
break
+ if ch == ord('r'):
+ raise RestartGameException()
raise ExitGameException()
def update(self):
return win
if __name__ == '__main__':
- win = setup()
- gamearea = GameArea(win=win, highscore_file=os.path.expanduser('~/.local/snake/highscore'))
- try:
- while True:
- gamearea.paint()
- if not gamearea.turbo:
- sleep(0.1)
- else:
- sleep(0.05)
- gamearea.getch()
- gamearea.update()
- except ExitGameException:
- pass
- finally:
- curses.endwin()
+ while True:
+ win = setup()
+ gamearea = GameArea(win=win, highscore_file=os.path.expanduser('~/.local/snake/highscore'))
+ try:
+ while True:
+ gamearea.paint()
+ if not gamearea.turbo:
+ sleep(0.1)
+ else:
+ sleep(0.05)
+ gamearea.getch()
+ gamearea.update()
+ except ExitGameException:
+ break
+ except RestartGameException:
+ continue
+ finally:
+ curses.endwin()