summaryrefslogtreecommitdiff
path: root/bin/worms.py
blob: be46d3dad3c4557d24b01913044ece61a7438171 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/usr/bin/env python
"""
This is an example application for the 'blessings' Terminal library for python.

It is also an experiment in functional programming.
"""

from __future__ import division, print_function
from collections import namedtuple
from functools import partial
from random import randrange

from blessings import Terminal


# python 2/3 compatibility, provide 'echo' function as an
# alias for "print without newline and flush"
try:
    echo = partial(print, end='', flush=True)
    echo('begin.')
except TypeError:
    # TypeError: 'flush' is an invalid keyword argument for this function
    import sys

    def echo(object):
        sys.stdout.write(u'{}'.format(object))
        sys.stdout.flush()

# a worm is a list of (y, x) segments Locations
Location = namedtuple('Point', ('y', 'x',))

# a nibble is a (x,y) Location and value
Nibble = namedtuple('Nibble', ('location', 'value'))

# A direction is a bearing, fe.
# y=0, x=-1 = move right
# y=1, x=0 = move down
Direction = namedtuple('Direction', ('y', 'x',))

# these functions return a new Location instance, given
# the direction indicated by their name.
LEFT = (0, -1)
left_of = lambda segment, term: Location(
    y=segment.y,
    x=max(0, segment.x - 1))

RIGHT = (0, 1)
right_of = lambda segment, term: Location(
    y=segment.y,
    x=min(term.width - 1, segment.x + 1))

UP = (-1, 0)
above = lambda segment, term: Location(
    y=max(0, segment.y - 1),
    x=segment.x)

DOWN = (1, 0)
below = lambda segment, term: Location(
    y=min(term.height - 1, segment.y + 1),
    x=segment.x)

# return a direction function that defines the new bearing for any matching
# keyboard code of inp_code; otherwise, the function for the current bearing.
next_bearing = lambda term, inp_code, bearing: {
    term.KEY_LEFT: left_of,
    term.KEY_RIGHT: right_of,
    term.KEY_UP: above,
    term.KEY_DOWN: below,
}.get(inp_code,
      # direction function given the current bearing
      {LEFT: left_of,
       RIGHT: right_of,
       UP: above,
       DOWN: below}[(bearing.y, bearing.x)])


# return new bearing given the movement f(x).
change_bearing = lambda f_mov, segment, term: Direction(
    f_mov(segment, term).y - segment.y,
    f_mov(segment, term).x - segment.x)

# direction-flipped check, reject traveling in opposite direction.
bearing_flipped = lambda dir1, dir2: (
    (0, 0) == (dir1.y + dir2.y, dir1.x + dir2.x)
)

# returns True if `loc' matches any (y, x) coordinates,
# within list `segments' -- such as a list composing a worm.
hit_any = lambda loc, segments: loc in segments

# same as above, but `locations' is also an array of (y, x) coordinates.
hit_vany = lambda locations, segments: any(
    hit_any(loc, segments) for loc in locations)

# returns True if segments are same position (hit detection)
hit = lambda src, dst: src.x == dst.x and src.y == dst.y

# returns new worm_length if current nibble is hit,
next_wormlength = lambda nibble, head, worm_length: (
    worm_length + nibble.value if hit(head, nibble.location)
    else worm_length)

# returns new speed if current nibble is hit,
next_speed = lambda nibble, head, speed, modifier: (
    speed * modifier if hit(head, nibble.location)
    else speed)

# when displaying worm head, show a different glyph for horizontal/vertical
head_glyph = lambda direction: (u':' if direction in (left_of, right_of)
                                else u'"')


# 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.
def next_nibble(term, nibble, head, worm):
    l, v = nibble.location, nibble.value
    while hit_vany([head] + worm, nibble_locations(l, v)):
        l = Location(x=randrange(1, term.width - 1),
                     y=randrange(1, term.height - 1))
        v = nibble.value + 1
    return Nibble(l, v)


# generate an array of locations for the current nibble's location -- a digit
# such as '123' may be hit at 3 different (y, x) coordinates.
def nibble_locations(nibble_location, nibble_value):
    return [Location(x=nibble_location.x + offset,
                     y=nibble_location.y)
            for offset in range(0, 1 + len('{}'.format(nibble_value)) - 1)]


def main():
    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(1, 1))
    echo(color_bg(term.clear))

    # speed is actually a measure of time; the shorter, the faster.
    speed = 0.1
    modifier = 0.93
    inp = None

    with term.hidden_cursor(), term.raw():
        while inp not in (u'q', u'Q'):

            # delete the tail of the worm at worm_length
            if len(worm) > worm_length:
                echo(term.move(*worm.pop(0)))
                echo(color_bg(u' '))

            # compute head location
            head = worm.pop()

            # check for hit against self; hitting a wall results in the (y, x)
            # location being clipped, -- and death by hitting self (not wall).
            if hit_any(head, worm):
                break

            # get the next nibble, which may be equal to ours unless this
            # nibble has been struck by any portion of our worm body.
            n_nibble = next_nibble(term, nibble, head, worm)

            # get the next worm_length and speed, unless unchanged.
            worm_length = next_wormlength(nibble, head, worm_length)
            speed = next_speed(nibble, head, speed, modifier)

            if n_nibble != nibble:
                # erase the old one, careful to redraw the nibble contents
                # with a worm color for those portions that overlay.
                for (y, x) in nibble_locations(*nibble):
                    echo(term.move(y, x) + (color_worm if (y, x) == head
                                            else color_bg)(u' '))
                    echo(term.normal)
                # and draw the new,
                echo(term.move(*n_nibble.location) + (
                    color_nibble('{}'.format(n_nibble.value))))

            # display new worm head
            echo(term.move(*head) + color_head(head_glyph(direction)))

            # and its old head (now, a body piece)
            if worm:
                echo(term.move(*(worm[-1])))
                echo(color_worm(u' '))
            echo(term.move(*head))

            # wait for keyboard input, which may indicate
            # a new direction (up/down/left/right)
            inp = term.inkey(speed)

            # discover new direction, given keyboard input and/or bearing.
            nxt_direction = next_bearing(term, inp.code, bearing)

            # discover new bearing, given new direction compared to prev
            nxt_bearing = change_bearing(nxt_direction, head, term)

            # disallow new bearing/direction when flipped (running into
            # oneself, fe. travelling left while traveling right)
            if not bearing_flipped(bearing, nxt_bearing):
                direction = nxt_direction
                bearing = nxt_bearing

            # append the prior `head' onto the worm, then
            # a new `head' for the given direction.
            worm.extend([head, direction(head, term)])

            # re-assign new nibble,
            nibble = n_nibble

    echo(term.normal)
    score = (worm_length - 1) * 100
    echo(u''.join((term.move(term.height - 1, 1), term.normal)))
    echo(u''.join((u'\r\n', u'score: {}'.format(score), u'\r\n')))

if __name__ == '__main__':
    main()