def main(stdscr): global frame_no, speed, position, score c = Canvas() bar_width = 16 bars = [Bar(bar_width)] stdscr.refresh() while True: frame_no += 1 for bar in bars: if check_collision(position, bar): return while not keys.empty(): if keys.get() == 113: return speed = 32.0 c.set(0,0) c.set(width, height) if frame_no % 50 == 0: bars.append(Bar(bar_width)) for x,y in bird: c.set(x,y+position) for bar_index, bar in enumerate(bars): if bar.x < 1: bars.pop(bar_index) score += 1 else: bars[bar_index].x -= 1 for x,y in bar.draw(): c.set(x,y) f = c.frame()+'\n' stdscr.addstr(0, 0, f) stdscr.addstr(height/4+1, 0, 'score: {0}'.format(score)) stdscr.refresh() c.clear() speed -= 2 position -= speed/10 if position < 0: position = 0 speed = 0.0 elif position > height-13: position = height-13 speed = 0.0 sleep(1.0/fps)
def image2term(image, threshold=128, ratio=None, invert=False): if image.startswith('http://') or image.startswith('https://'): i = Image.open(StringIO(urllib2.urlopen(image).read())).convert('L') else: i = Image.open(open(image)).convert('L') w, h = i.size if ratio: w = int(w * ratio) h = int(h * ratio) i = i.resize((w, h), Image.ANTIALIAS) else: tw = getTerminalSize()[0] tw *= 2 if tw < w: ratio = tw / float(w) w = tw h = int(h * ratio) i = i.resize((w, h), Image.ANTIALIAS) can = Canvas() x = y = 0 try: i_converted = i.tobytes() except AttributeError: i_converted = i.tostring() for pix in i_converted: if invert: if ord(pix) > threshold: can.set(x, y) else: if ord(pix) < threshold: can.set(x, y) x += 1 if x >= w: y += 1 x = 0 return can.frame(0, 0)
else: url = 'http://xkcd.com/%s/' % argv[1] c = urllib2.urlopen(url).read() img_url = re.findall('http:\/\/imgs.xkcd.com\/comics\/[^"\']+', c)[0] i = Image.open(StringIO(urllib2.urlopen(img_url).read())).convert('L') w, h = i.size tw, th = get_terminal_size() tw *= 2 th *= 2 if tw < w: ratio = tw / float(w) w = tw h = int(h * ratio) i = i.resize((w, h), Image.ANTIALIAS) can = Canvas() x = y = 0 try: i_converted = i.tobytes() except AttributeError: i_converted = i.tostring() for pix in i_converted: if ord(pix) < 128: can.set(x, y) x += 1 if x >= w: y += 1 x = 0 print(can.frame())
import sys import os sys.path.append(os.path.abspath("..")) from termux2d import Canvas from timeit import timeit c = Canvas() frames = 1000 * 10 sizes = ((0, 0), (10, 10), (20, 20), (20, 40), (40, 20), (40, 40), (100, 100)) for x, y in sizes: c.set(0, 0) for i in range(y): c.set(x, i) r = timeit(c.frame, number=frames) print('{0}x{1}\t{2}'.format(x, y, r)) c.clear()
from __future__ import print_function import sys import os sys.path.append(os.path.abspath("..")) from termux2d import Canvas import math s = Canvas() for x in range(1800): s.set(x/10, math.sin(math.radians(x)) * 10) print(s.frame()) s.clear() for x in range(0, 1800, 10): s.set(x/10, 10 + math.sin(math.radians(x)) * 10) s.set(x/10, 10 + math.cos(math.radians(x)) * 10) print(s.frame()) s.clear() for x in range(0, 3600, 20): s.set(x/20, 4 + math.sin(math.radians(x)) * 4) print(s.frame())