Last active
March 14, 2018 03:17
-
-
Save ncfavier/1c9a9f18adc0d2bd8bad0fd3660eca47 to your computer and use it in GitHub Desktop.
Python curses snake game
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import curses | |
import time | |
import random | |
SNAKE_COLOR, FOOD_COLOR = 1, 2 | |
UP, RIGHT, DOWN, LEFT = (0, -1), (1, 0), (0, 1), (-1, 0) | |
INITIAL_LENGTH = 10 | |
INITIAL_SPEED = 15 | |
SPEED_INCREMENT = 0.5 | |
class App: | |
def __init__(self): | |
self.snake = [] | |
self.direction = None | |
self.speed = 0 | |
self.food = None | |
self.score = 0 | |
self.highScore = 0 | |
self.paused = False | |
def main(screen): | |
curses.curs_set(False) | |
curses.use_default_colors() | |
curses.init_pair(SNAKE_COLOR, curses.COLOR_WHITE, curses.COLOR_BLUE) | |
curses.init_pair(FOOD_COLOR, curses.COLOR_WHITE, curses.COLOR_MAGENTA) | |
self.screen = screen | |
self.screen.nodelay(True) | |
self.start() | |
try: | |
while self.handleEvents(): | |
if not self.paused: | |
self.update() | |
self.draw() | |
time.sleep(1/self.speed) | |
except KeyboardInterrupt: | |
pass | |
curses.wrapper(main) | |
print('High score: %d' % self.highScore) | |
def start(self): | |
self.snake = [(curses.COLS//2, curses.LINES//2)] * INITIAL_LENGTH | |
self.direction = RIGHT | |
self.speed = INITIAL_SPEED | |
self.spawnFood() | |
self.score = 0 | |
def spawnFood(self): | |
self.food = (random.randint(0, curses.COLS-1), random.randint(0, curses.LINES-2)) | |
def draw(self): | |
self.screen.erase() | |
for cell in self.snake: | |
self.screen.addstr(cell[1], cell[0], ' ', curses.color_pair(SNAKE_COLOR)) | |
self.screen.addstr(self.food[1], self.food[0], ' ', curses.color_pair(FOOD_COLOR)) | |
info = 'Score: %d - High: %d' % (self.score, self.highScore) | |
self.screen.insstr(curses.LINES-1, 0, info.center(curses.COLS)) | |
self.screen.refresh() | |
def handleEvents(self): | |
try: | |
c = self.screen.getkey() | |
if c == 'q': | |
return False | |
elif c == ' ': | |
self.paused = not self.paused | |
elif c == 'KEY_UP' and self.direction != DOWN: | |
self.direction = UP | |
elif c == 'KEY_RIGHT' and self.direction != LEFT: | |
self.direction = RIGHT | |
elif c == 'KEY_DOWN' and self.direction != UP: | |
self.direction = DOWN | |
elif c == 'KEY_LEFT' and self.direction != RIGHT: | |
self.direction = LEFT | |
except curses.error: | |
pass | |
return True | |
def update(self): | |
self.snake.append(((self.snake[-1][0] + self.direction[0]) % curses.COLS, (self.snake[-1][1] + self.direction[1]) % (curses.LINES-1))) | |
if self.snake[-1] == self.food: | |
self.score += 1 | |
self.highScore = max(self.score, self.highScore) | |
self.speed += SPEED_INCREMENT | |
self.spawnFood() | |
else: | |
self.snake.pop(0) | |
for cell in self.snake[:-1]: | |
if self.snake[-1] == cell: | |
self.start() | |
return | |
if __name__ == '__main__': | |
app = App() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment