|
|
|
|
|
""" |
|
|
Example application for the 'blessed' Terminal library for python. |
|
|
|
|
|
It is also an experiment in functional programming. |
|
|
""" |
|
|
|
|
|
from __future__ import division, print_function |
|
|
|
|
|
|
|
|
from random import randrange |
|
|
from functools import partial |
|
|
from collections import namedtuple |
|
|
|
|
|
|
|
|
from blessed import Terminal |
|
|
|
|
|
|
|
|
|
|
|
try: |
|
|
|
|
|
|
|
|
echo = partial(print, end='', flush=True) |
|
|
echo(u'') |
|
|
except TypeError: |
|
|
|
|
|
import sys |
|
|
|
|
|
def echo(text): |
|
|
"""Python 2 version of print(end='', flush=True).""" |
|
|
sys.stdout.write(u'{0}'.format(text)) |
|
|
sys.stdout.flush() |
|
|
|
|
|
|
|
|
Location = namedtuple('Point', ('y', 'x',)) |
|
|
|
|
|
|
|
|
Nibble = namedtuple('Nibble', ('location', 'value')) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Direction = namedtuple('Direction', ('y', 'x',)) |
|
|
|
|
|
|
|
|
|
|
|
LEFT = (0, -1) |
|
|
RIGHT = (0, 1) |
|
|
UP = (-1, 0) |
|
|
DOWN = (1, 0) |
|
|
|
|
|
|
|
|
def left_of(segment, term): |
|
|
"""Return Location left-of given segment.""" |
|
|
|
|
|
|
|
|
return Location(y=segment.y, |
|
|
x=max(0, segment.x - 1)) |
|
|
|
|
|
|
|
|
def right_of(segment, term): |
|
|
"""Return Location right-of given segment.""" |
|
|
return Location(y=segment.y, |
|
|
x=min(term.width - 1, segment.x + 1)) |
|
|
|
|
|
|
|
|
def above(segment, term): |
|
|
"""Return Location above given segment.""" |
|
|
|
|
|
|
|
|
return Location( |
|
|
y=max(0, segment.y - 1), |
|
|
x=segment.x) |
|
|
|
|
|
|
|
|
def below(segment, term): |
|
|
"""Return Location below given segment.""" |
|
|
return Location( |
|
|
y=min(term.height - 1, segment.y + 1), |
|
|
x=segment.x) |
|
|
|
|
|
|
|
|
def next_bearing(term, inp_code, bearing): |
|
|
""" |
|
|
Return direction function for new bearing by inp_code. |
|
|
|
|
|
If no inp_code matches a bearing direction, return a function for the current bearing. |
|
|
""" |
|
|
return { |
|
|
term.KEY_LEFT: left_of, |
|
|
term.KEY_RIGHT: right_of, |
|
|
term.KEY_UP: above, |
|
|
term.KEY_DOWN: below, |
|
|
}.get(inp_code, |
|
|
|
|
|
{LEFT: left_of, |
|
|
RIGHT: right_of, |
|
|
UP: above, |
|
|
DOWN: below}[(bearing.y, bearing.x)]) |
|
|
|
|
|
|
|
|
def change_bearing(f_mov, segment, term): |
|
|
"""Return new bearing given the movement f(x).""" |
|
|
return Direction( |
|
|
f_mov(segment, term).y - segment.y, |
|
|
f_mov(segment, term).x - segment.x) |
|
|
|
|
|
|
|
|
def bearing_flipped(dir1, dir2): |
|
|
""" |
|
|
Direction-flipped check. |
|
|
|
|
|
Return true if dir2 travels in opposite direction of dir1. |
|
|
""" |
|
|
return (0, 0) == (dir1.y + dir2.y, dir1.x + dir2.x) |
|
|
|
|
|
|
|
|
def hit_any(loc, segments): |
|
|
"""Return True if `loc' matches any (y, x) coordinates within segments.""" |
|
|
|
|
|
return loc in segments |
|
|
|
|
|
|
|
|
def hit_vany(locations, segments): |
|
|
"""Return True if any locations are found within any segments.""" |
|
|
return any(hit_any(loc, segments) |
|
|
for loc in locations) |
|
|
|
|
|
|
|
|
def hit(src, dst): |
|
|
"""Return True if segments are same position (hit detection).""" |
|
|
return src.x == dst.x and src.y == dst.y |
|
|
|
|
|
|
|
|
def next_wormlength(nibble, head, worm_length): |
|
|
"""Return new worm_length if current nibble is hit.""" |
|
|
if hit(head, nibble.location): |
|
|
return worm_length + nibble.value |
|
|
return worm_length |
|
|
|
|
|
|
|
|
def next_speed(nibble, head, speed, modifier): |
|
|
"""Return new speed if current nibble is hit.""" |
|
|
return speed * modifier if hit(head, nibble.location) else speed |
|
|
|
|
|
|
|
|
def head_glyph(direction): |
|
|
"""Return character for worm head depending on horiz/vert orientation.""" |
|
|
return u':' if direction in (left_of, right_of) else u'"' |
|
|
|
|
|
|
|
|
def next_nibble(term, nibble, head, worm): |
|
|
""" |
|
|
Provide the next nibble. |
|
|
|
|
|
continuously generate a random new nibble so long as the current nibble hits any location of the |
|
|
worm. Otherwise, return a nibble of the same location and value as provided. |
|
|
""" |
|
|
loc, val = nibble.location, nibble.value |
|
|
while hit_vany([head] + worm, nibble_locations(loc, val)): |
|
|
loc = Location(x=randrange(1, term.width - 1), |
|
|
y=randrange(1, term.height - 1)) |
|
|
val = nibble.value + 1 |
|
|
return Nibble(loc, val) |
|
|
|
|
|
|
|
|
def nibble_locations(nibble_location, nibble_value): |
|
|
"""Return array of locations for the current "nibble".""" |
|
|
|
|
|
|
|
|
return [ |
|
|
Location(x=nibble_location.x + offset, y=nibble_location.y) |
|
|
for offset in range(1 + len('{}'.format(nibble_value)) - 1) |
|
|
] |
|
|
|
|
|
|
|
|
def main(): |
|
|
"""Program entry point.""" |
|
|
|
|
|
|
|
|
term = Terminal() |
|
|
worm = [Location(x=term.width // 2, y=term.height // 2)] |
|
|
worm_length = 2 |
|
|
bearing = Direction(*LEFT) |
|
|
direction = left_of |
|
|
nibble = Nibble(location=worm[0], value=0) |
|
|
color_nibble = term.black_on_green |
|
|
color_worm = term.yellow_reverse |
|
|
color_head = term.red_reverse |
|
|
color_bg = term.on_blue |
|
|
echo(term.move_yx(1, 1)) |
|
|
echo(color_bg(term.clear)) |
|
|
|
|
|
|
|
|
speed = 0.1 |
|
|
modifier = 0.93 |
|
|
inp = None |
|
|
|
|
|
echo(term.move_yx(term.height, 0)) |
|
|
with term.hidden_cursor(), term.cbreak(), term.location(): |
|
|
while inp not in (u'q', u'Q'): |
|
|
|
|
|
|
|
|
if len(worm) > worm_length: |
|
|
echo(term.move_yx(*worm.pop(0))) |
|
|
echo(color_bg(u' ')) |
|
|
|
|
|
|
|
|
head = worm.pop() |
|
|
|
|
|
|
|
|
|
|
|
if hit_any(head, worm): |
|
|
break |
|
|
|
|
|
|
|
|
|
|
|
n_nibble = next_nibble(term, nibble, head, worm) |
|
|
|
|
|
|
|
|
worm_length = next_wormlength(nibble, head, worm_length) |
|
|
speed = next_speed(nibble, head, speed, modifier) |
|
|
|
|
|
if n_nibble != nibble: |
|
|
|
|
|
|
|
|
for (yloc, xloc) in nibble_locations(*nibble): |
|
|
echo(u''.join(( |
|
|
term.move_yx(yloc, xloc), |
|
|
(color_worm if (yloc, xloc) == head |
|
|
else color_bg)(u' '), |
|
|
term.normal))) |
|
|
|
|
|
echo(term.move_yx(*n_nibble.location) + ( |
|
|
color_nibble('{}'.format(n_nibble.value)))) |
|
|
|
|
|
|
|
|
echo(term.move_yx(*head) + color_head(head_glyph(direction))) |
|
|
|
|
|
|
|
|
if worm: |
|
|
echo(term.move_yx(*(worm[-1]))) |
|
|
echo(color_worm(u' ')) |
|
|
echo(term.move_yx(*head)) |
|
|
|
|
|
|
|
|
|
|
|
inp = term.inkey(timeout=speed) |
|
|
|
|
|
|
|
|
nxt_direction = next_bearing(term, inp.code, bearing) |
|
|
|
|
|
|
|
|
nxt_bearing = change_bearing(nxt_direction, head, term) |
|
|
|
|
|
|
|
|
|
|
|
if not bearing_flipped(bearing, nxt_bearing): |
|
|
direction = nxt_direction |
|
|
bearing = nxt_bearing |
|
|
|
|
|
|
|
|
|
|
|
worm.extend([head, direction(head, term)]) |
|
|
|
|
|
|
|
|
nibble = n_nibble |
|
|
|
|
|
echo(term.normal) |
|
|
score = (worm_length - 1) * 100 |
|
|
echo(u''.join((term.move_yx(term.height - 1, 1), term.normal))) |
|
|
echo(u''.join((u'\r\n', u'score: {}'.format(score), u'\r\n'))) |
|
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
main() |
|
|
|