1 | #!/usr/bin/env python |
---|
2 | # -*- coding: utf-8 -*- |
---|
3 | # |
---|
4 | # libcaca Colour ASCII-Art library |
---|
5 | # Python language bindings |
---|
6 | # Copyright (c) 2010 Alex Foulon <alxf@lavabit.com> |
---|
7 | # All Rights Reserved |
---|
8 | # |
---|
9 | # This library is free software. It comes without any warranty, to |
---|
10 | # the extent permitted by applicable law. You can redistribute it |
---|
11 | # and/or modify it under the terms of the Do What The Fuck You Want |
---|
12 | # To Public License, Version 2, as published by Sam Hocevar. See |
---|
13 | # http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
14 | # |
---|
15 | |
---|
16 | """ Libcaca Python bindings """ |
---|
17 | |
---|
18 | import random |
---|
19 | import sys |
---|
20 | import time |
---|
21 | |
---|
22 | import caca |
---|
23 | |
---|
24 | from caca.canvas import Canvas |
---|
25 | from caca.display import Display, Event |
---|
26 | |
---|
27 | class CellArray(object): |
---|
28 | def __init__(self, width, height): |
---|
29 | self.array = [] |
---|
30 | self.width = width |
---|
31 | self.height = height |
---|
32 | |
---|
33 | for i in xrange(0, self.height): |
---|
34 | self.array.append([]) |
---|
35 | for j in xrange(0, self.width): |
---|
36 | self.array[i].append([]) |
---|
37 | |
---|
38 | def get(self, x, y): |
---|
39 | return self.array[x][y] |
---|
40 | |
---|
41 | def set(self, x, y, value): |
---|
42 | self.array[x][y] = value |
---|
43 | |
---|
44 | def neighbors(self, x, y): |
---|
45 | n = 0 |
---|
46 | h = self.height |
---|
47 | w = self.width |
---|
48 | |
---|
49 | if self.get((x-1)%h, (y-1)%w): |
---|
50 | n += 1 |
---|
51 | if self.get((x-1)%h, y): |
---|
52 | n += 1 |
---|
53 | if self.get((x-1)%h, (y+1)%w): |
---|
54 | n += 1 |
---|
55 | if self.get(x, (y-1)%w): |
---|
56 | n += 1 |
---|
57 | if self.get(x, (y+1)%w): |
---|
58 | n += 1 |
---|
59 | if self.get((x+1)%h, (y-1)%w): |
---|
60 | n += 1 |
---|
61 | if self.get((x+1)%h, y): |
---|
62 | n += 1 |
---|
63 | if self.get((x+1)%h, (y+1)%w): |
---|
64 | n += 1 |
---|
65 | |
---|
66 | return n |
---|
67 | |
---|
68 | def population(self): |
---|
69 | n = 0 |
---|
70 | |
---|
71 | for i in xrange(0, self.height): |
---|
72 | for j in xrange(0, self.width): |
---|
73 | if self.get(i, j): |
---|
74 | n += 1 |
---|
75 | |
---|
76 | return n |
---|
77 | |
---|
78 | class CellApp(object): |
---|
79 | def __init__(self, width, height): |
---|
80 | self.cycle = 0 |
---|
81 | self.auto = False |
---|
82 | self.width = width |
---|
83 | self.height = height |
---|
84 | |
---|
85 | self.ca = CellArray(self.width, self.height) |
---|
86 | self.cbuf = CellArray(self.width, self.height) |
---|
87 | |
---|
88 | def nextCycle(self): |
---|
89 | self.cycle += 1 |
---|
90 | for x in xrange(0, self.ca.height): |
---|
91 | for y in xrange(0, self.ca.width): |
---|
92 | if self.ca.get(x, y): |
---|
93 | if self.ca.neighbors(x, y) >= 2 and self.ca.neighbors(x, y) <= 3: |
---|
94 | self.cbuf.set(x, y, 1) |
---|
95 | else: |
---|
96 | self.cbuf.set(x, y, 0) |
---|
97 | elif not self.ca.get(x, y): |
---|
98 | if self.ca.neighbors(x, y) == 3: |
---|
99 | self.cbuf.set(x, y, 1) |
---|
100 | else: |
---|
101 | self.cbuf.set(x, y, 0) |
---|
102 | else: |
---|
103 | self.cbuf.set(x, y, 0) |
---|
104 | for x in xrange(0, self.ca.height): |
---|
105 | for y in xrange(0, self.ca.width): |
---|
106 | self.ca.set(x, y, self.cbuf.get(x, y)) |
---|
107 | |
---|
108 | def resetCycle(self): |
---|
109 | self.cycle = 0 |
---|
110 | |
---|
111 | def randomCells(self): |
---|
112 | for x in xrange(0, self.ca.height): |
---|
113 | for y in xrange(0, self.ca.width): |
---|
114 | self.ca.set(x, y, random.randint(0, 1)) |
---|
115 | |
---|
116 | def renderCells(self, cv): |
---|
117 | cv.clear() |
---|
118 | cv.set_color_ansi(caca.COLOR_WHITE, caca.COLOR_BLUE) |
---|
119 | cv.put_str(0, 0, " "*cv.get_width()) |
---|
120 | cv.put_str(0, 0, "s: start, p: pause, n: next, r: random cells, z: reset all") |
---|
121 | cv.put_str(0, cv.get_height()-1, " "*cv.get_width()) |
---|
122 | cv.put_str(0, cv.get_height()-1, "generation: %d, population: %d" % (self.cycle, self.ca.population())) |
---|
123 | cv.set_color_ansi(caca.COLOR_DEFAULT, caca.COLOR_BLACK) |
---|
124 | posx = (cv.get_height() - self.height) / 2 |
---|
125 | posy = (cv.get_width() - self.width) / 2 |
---|
126 | for x in xrange(0, self.ca.height): |
---|
127 | for y in xrange(0, self.ca.width): |
---|
128 | if self.ca.get(x, y): |
---|
129 | cv.put_str(posy+y, posx+x, '@') |
---|
130 | |
---|
131 | def zeroCells(self): |
---|
132 | for x in xrange(0, self.ca.height): |
---|
133 | for y in xrange(0, self.ca.width): |
---|
134 | self.ca.set(x, y, 0) |
---|
135 | |
---|
136 | if __name__ == "__main__": |
---|
137 | cv = Canvas() |
---|
138 | dp = Display(cv) |
---|
139 | ev = Event() |
---|
140 | |
---|
141 | app = CellApp(80, 20) |
---|
142 | app.zeroCells() |
---|
143 | |
---|
144 | while True: |
---|
145 | if dp.get_event(caca.EVENT_KEY_PRESS, ev, 2): |
---|
146 | ch = ev.get_key_ch() |
---|
147 | if ch == ord('q'): |
---|
148 | break |
---|
149 | elif ch == ord('s'): |
---|
150 | app.auto = True |
---|
151 | elif ch == ord('n'): |
---|
152 | app.nextCycle() |
---|
153 | elif ch == ord('r'): |
---|
154 | app.randomCells() |
---|
155 | elif ch == ord('p'): |
---|
156 | if app.auto: |
---|
157 | app.auto = False |
---|
158 | else: |
---|
159 | app.auto = True |
---|
160 | elif ch == ord('z'): |
---|
161 | app.resetCycle() |
---|
162 | app.zeroCells() |
---|
163 | app.auto = False |
---|
164 | |
---|
165 | if app.auto: |
---|
166 | app.nextCycle() |
---|
167 | |
---|
168 | app.renderCells(cv) |
---|
169 | dp.refresh() |
---|
170 | time.sleep(0.2) |
---|
171 | |
---|