4
# Playing with ctypes and libcaca
5
# http://mornie.org/blog/2007/03/25/Playng-with-ctypes-and-libcaca/
7
# Copyright (C) 2007 Daniele Tricoli aka Eriol <eriol@mornie.org>
9
# This program is free software; you can redistribute it and/or
10
# modify it under the terms of the GNU General Public License
11
# as published by the Free Software Foundation; either version 2
12
# of the License, or (at your option) any later version.
14
# This program is distributed in the hope that it will be useful,
15
# but WITHOUT ANY WARRANTY; without even the implied warranty of
16
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
# GNU General Public License for more details.
19
# You should have received a copy of the GNU General Public License
20
# along with this program; if not, write to the Free Software
21
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27
# Applied patch by Sam Hocevar: check for caca_get_event's return value
28
# and added caca_event's missing first member
30
# Updated for newer libcaca API (Sam Hocevar)
40
CENTER_X = CANVAS_WIDTH / 2
41
CENTER_Y = CANVAS_HEIGHT / 2
48
class ev(C.Structure):
49
_fields_ = [('opaque_structure', C.c_char_p * 32)]
53
def __init__(self, center_point, length):
55
self.head = center_point
58
for y in xrange(self.head[1] + 1, self.head[1] + length + 1):
59
self.body.append((self.head[0], y))
61
def move(self, direction):
63
phead = tuple(self.head)
67
elif direction == 'DOWN':
69
elif direction == 'LEFT':
71
elif direction == 'RIGHT':
74
self.body = [phead] + self.body[:-1]
77
self.body += [tuple(self.head)] * 2
81
lcaca.cucul_set_color_ansi(cv, 0x05, 0x00)
84
lcaca.cucul_put_char(cv, p[0], p[1], ord('o'))
85
lcaca.cucul_set_color_ansi(cv, 0x02, 0x00)
86
lcaca.cucul_put_char(cv, self.head[0], self.head[1], ord('@'))
87
lcaca.caca_refresh_display(dp)
94
def random(self, width, height):
95
self.x = int(random.uniform(1, width))
96
self.y = int(random.uniform(1, height))
97
self.value = random.choice(range(1,10))
100
self.total += self.value
104
lcaca.cucul_set_color_ansi(cv, 0x03, 0x00)
105
lcaca.cucul_put_char(cv, self.x, self.y, ord(str(self.value)))
106
lcaca.caca_refresh_display(dp)
109
lcaca.cucul_set_color_ansi(cv, 0x04, 0x00)
110
lcaca.cucul_draw_box(cv,
118
lcaca = C.cdll.LoadLibrary('libcaca.so.0')
119
cv = lcaca.cucul_create_canvas(CANVAS_WIDTH, CANVAS_HEIGHT)
120
dp = lcaca.caca_create_display(cv)
121
lcaca.caca_set_display_title(dp, "snake.py - playing with ctypes and libcaca")
123
s = Snake([CENTER_X, CENTER_Y], 5)
125
t.random(CANVAS_WIDTH - 2, CANVAS_HEIGHT - 2)
131
lcaca.caca_get_event(dp, 0x0001, C.byref(event), -1)
134
while lcaca.caca_get_event(dp, 0x0001, C.byref(event), 0):
135
ch = lcaca.caca_get_event_key_ch(C.byref(event))
136
if ch == 113: # 'q' pressed
152
if (tuple(s.head) in s.body[1:] or
153
not 0 < s.head[0] < CANVAS_WIDTH - 1 or
154
not 0 < s.head[1] < CANVAS_HEIGHT - 1):
156
print 'Total score:', t.total
158
elif tuple(s.head) == (t.x, t.y):
160
t.random(CANVAS_WIDTH - 2, CANVAS_HEIGHT - 2)
163
lcaca.cucul_clear_canvas(cv)