Created
May 30, 2011 19:53
-
-
Save ncweinhold/999382 to your computer and use it in GitHub Desktop.
curses snake WIP
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
import curses | |
import time | |
class Direction(object): | |
NORTH=0 | |
EAST=1 | |
SOUTH=2 | |
WEST=3 | |
class Position(object): | |
def __init__(self, x, y): | |
self.x = x | |
self.y = y | |
def get_x(self): | |
return self.x | |
def get_y(self): | |
return self.y | |
def set_x(self, x): | |
self.x = x | |
def set_y(self, y): | |
self.y = y | |
class Snake(object): | |
def __init__(self, direction=Direction.EAST): | |
self.direction = direction | |
self.body = [Position(9,20), | |
Position(8,20), | |
Position(7,20), | |
Position(6,20), | |
Position(5,20)] | |
self.has_eaten = False | |
def get_direction(self): | |
return self.direction | |
def set_direction(self, direction): | |
# should really check whether direction is an instance of Direction | |
self.direction = direction | |
def get_body(self): | |
return self.body | |
def grow(self): | |
self.has_eaten = True | |
def move(self): | |
body_index = len(self.body) - 1 | |
if self.has_eaten: | |
extended_segment_pos = Position(self.body[body_index].get_x(), | |
self.body[body_index].get_y()) | |
self.body.append(extended_segment_pos) | |
self.has_eaten = False | |
while body_index != 0: | |
self.body[body_index] = Position(self.body[body_index - 1].get_x(), | |
self.body[body_index - 1].get_y()) | |
body_index = body_index - 1 | |
if self.direction == Direction.NORTH: | |
self.body[0] = Position(self.body[0].get_x(), self.body[0].get_y() - 1) | |
if self.direction == Direction.EAST: | |
self.body[0] = Position(self.body[0].get_x()+1, self.body[0].get_y()) | |
if self.direction == Direction.SOUTH: | |
self.body[0] = Position(self.body[0].get_x(), self.body[0].get_y() + 1) | |
if self.direction == Direction.WEST: | |
self.body[0] = Position(self.body[0].get_x()-1, self.body[0].get_y()) | |
def print_body(self): | |
for pos in self.body: | |
print "x: ", pos.get_x() | |
print "y: ", pos.get_y() | |
class SnakeGame(object): | |
def __init__(self): | |
self.stdscr = curses.initscr() | |
curses.noecho() | |
curses.cbreak() | |
curses.curs_set(0) | |
self.stdscr.keypad(1) | |
self.stdscr.nodelay(1) | |
self.snake = Snake() | |
self.score = 0 | |
self.running = True | |
self.paused = False | |
def game_loop(self): | |
while self.running: | |
if not self.paused: | |
self.update_game() | |
self.render_objects() | |
self.handle_events() | |
time.sleep(0.2) | |
def handle_events(self): | |
c = self.stdscr.getch() | |
if c == ord('q'): | |
self.running = False | |
elif c == ord('p'): | |
self.paused = not self.paused | |
elif c == curses.KEY_LEFT: | |
direction = self.snake.get_direction() | |
if (direction == Direction.NORTH) or (direction == Direction.SOUTH): | |
self.snake.set_direction(Direction.WEST) | |
elif c == curses.KEY_RIGHT: | |
direction = self.snake.get_direction() | |
if (direction == Direction.NORTH) or (direction == Direction.SOUTH): | |
self.snake.set_direction(Direction.EAST) | |
elif c == curses.KEY_DOWN: | |
direction = self.snake.get_direction() | |
if (direction == Direction.EAST) or (direction == Direction.WEST): | |
self.snake.set_direction(Direction.SOUTH) | |
elif c == curses.KEY_UP: | |
direction = self.snake.get_direction() | |
if (direction == Direction.EAST) or (direction == Direction.WEST): | |
self.snake.set_direction(Direction.NORTH) | |
elif c == ord('g'): | |
self.snake.grow() | |
def update_game(self): | |
self.snake.move() | |
def render_objects(self): | |
self.render_snake() | |
def cleanup(self): | |
curses.nocbreak() | |
self.stdscr.keypad(0) | |
self.stdscr.nodelay(0) | |
curses.curs_set(2) | |
curses.echo() | |
curses.endwin() | |
def render_snake(self): | |
self.stdscr.clear() | |
body = self.snake.get_body() | |
for pos in body: | |
self.stdscr.addch(pos.get_y(), pos.get_x(), 35) | |
self.stdscr.refresh() | |
if __name__ == "__main__": | |
print "Snake Game started" | |
game = SnakeGame() | |
game.game_loop() | |
game.cleanup() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment