Skip to content

Instantly share code, notes, and snippets.

@pkulev
Created Jul 8, 2014
Embed
What would you like to do?
import sys
import curses
from curses import KEY_ENTER
import time
from collections import namedtuple
KEY = "KEY"
K_A = ord("a")
K_D = ord("d")
class Point:
def __init__(self, x, y):
self._x = x
self._y = y
@property
def x(self):
return self._x
@x.setter
def x(self, val):
self._x = val
@property
def y(self):
return self._y
@y.setter
def y(self, val):
self._y = val
Event = namedtuple("Event", ["type", "val"])
class Spaceship(object):
def __init__(self, border):
self._image = "<i>"
self._dx = 1
self._last_dx = 0
self.border = border
self._pos = Point(self.border.x // 2, self.border.y - 1)
def events(self, event):
if event.type == KEY:
if event.val == K_A:
self._dx = -1
if event.val == K_D:
self._dx = 1
def update(self):
if self._pos.x == self.border.x - len(self._image) - 1:
self._pos.x = 0
elif self._pos.x == 1 and self._last_dx == -1:
self._pos.x = self.border.x - len(self._image) + 1
print(self._pos.x, self._pos.y)
self._pos.x += self._dx
self._last_dx = self._dx
self._dx = 0
def draw(self, screen):
screen.addstr(self._pos.y, self._pos.x, self._image, curses.A_BOLD)
class App(object):
def __init__(self):
self.screen = curses.initscr()
#self.screen.nodelay(True)
self.screen.keypad(True)
curses.noecho()
curses.cbreak()
curses.curs_set(False)
self.border = namedtuple("border", ["y", "x"])(*self.screen.getmaxyx())
self.spaceship = Spaceship(self.border)
self._objects = []
self._objects.append(self.spaceship)
def deinit(self):
#self.screen.nodelay(False)
self.screen.keypad(False)
curses.nocbreak()
curses.echo()
curses.curs_set(True)
curses.endwin()
def events(self):
c = self.screen.getch()
if c == KEY_ENTER or c == 10:
#self.deinit()
sys.exit(1)
else:
for o in self._objects:
o.events(Event(type="KEY", val=c))
def update(self):
for o in self._objects:
o.update()
def render(self):
self.screen.clear()
for o in self._objects:
o.draw(self.screen)
self.screen.refresh()
time.sleep(0.2)
def loop(self):
while True:
self.events()
self.update()
self.render()
def main():
app = App()
app.loop()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment