source: libcaca/trunk/python/snake.py @ 3984

Last change on this file since 3984 was 2824, checked in by Sam Hocevar, 12 years ago

Continuing the libcucul-libcaca merge.

  • Property svn:executable set to *
File size: 4.4 KB
Line 
1#!/usr/bin/env python
2#
3# snake.py
4# Playing with ctypes and libcaca
5# http://mornie.org/blog/2007/03/25/Playng-with-ctypes-and-libcaca/
6#
7# Copyright (C) 2007  Daniele Tricoli aka Eriol <eriol@mornie.org>
8#
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.
13#
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.
18#
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.
22#
23# -- Changelog
24# * 23/03/07
25#   Initial release
26# * 20/10/07
27#   Applied patch by Sam Hocevar: check for caca_get_event's return value
28#                                 and added caca_event's missing first member
29# * 25/10/07
30#   Updated for newer libcaca API (Sam Hocevar)
31
32import ctypes as C
33import random
34import sys
35import time
36
37CANVAS_WIDTH = 80
38CANVAS_HEIGHT = 40
39
40CENTER_X = CANVAS_WIDTH / 2
41CENTER_Y = CANVAS_HEIGHT / 2
42
43UP = 273
44DOWN = 274
45LEFT = 275
46RIGHT = 276
47
48class ev(C.Structure):
49    _fields_ = [('opaque_structure', C.c_char_p * 32)]
50
51class Snake(object):
52
53    def __init__(self, center_point, length):
54
55        self.head = center_point
56        self.body = []
57
58        for y in xrange(self.head[1] + 1, self.head[1] + length + 1):
59            self.body.append((self.head[0], y))
60
61    def move(self, direction):
62
63        phead = tuple(self.head)
64
65        if direction == 'UP':
66            self.head[1] -=1
67        elif direction == 'DOWN':
68            self.head[1] +=1
69        elif direction == 'LEFT':
70            self.head[0] -=1
71        elif direction == 'RIGHT':
72            self.head[0] +=1
73
74        self.body = [phead] + self.body[:-1]
75
76    def grow(self):
77        self.body += [tuple(self.head)] * 2
78
79    def draw(self):
80        global cv
81        lcaca.caca_set_color_ansi(cv, 0x05, 0x00)
82
83        for p in self.body:
84            lcaca.caca_put_char(cv, p[0], p[1], ord('o'))
85        lcaca.caca_set_color_ansi(cv, 0x02, 0x00)
86        lcaca.caca_put_char(cv, self.head[0], self.head[1], ord('@'))
87        lcaca.caca_refresh_display(dp)
88
89class Target(object):
90
91    def __init__(self):
92        self.total = 0
93
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))
98
99    def sum(self):
100        self.total += self.value
101
102    def draw(self):
103        global cv
104        lcaca.caca_set_color_ansi(cv, 0x03, 0x00)
105        lcaca.caca_put_char(cv, self.x, self.y, ord(str(self.value)))
106        lcaca.caca_refresh_display(dp)
107
108def draw_border():
109    lcaca.caca_set_color_ansi(cv, 0x04, 0x00)
110    lcaca.caca_draw_box(cv,
111                         0,
112                         0,
113                         CANVAS_WIDTH - 1,
114                         CANVAS_HEIGHT - 1,
115                         ord('#'))
116
117event = ev()
118lcaca = C.cdll.LoadLibrary('libcaca.so.0')
119cv = lcaca.caca_create_canvas(CANVAS_WIDTH, CANVAS_HEIGHT)
120dp = lcaca.caca_create_display(cv)
121lcaca.caca_set_display_title(dp, "snake.py - playing with ctypes and libcaca")
122
123s = Snake([CENTER_X, CENTER_Y], 5)
124t = Target()
125t.random(CANVAS_WIDTH - 2, CANVAS_HEIGHT - 2)
126
127draw_border()
128s.draw()
129t.draw()
130
131lcaca.caca_get_event(dp, 0x0001, C.byref(event), -1)
132
133while True:
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
137            sys.exit()
138        elif ch == UP:
139            d = 'UP'
140        elif ch == DOWN:
141            d = 'DOWN'
142        elif ch == LEFT:
143            d = 'LEFT'
144        elif ch == RIGHT:
145            d = 'RIGHT'
146
147    try:
148        s.move(d)
149    except NameError:
150        pass
151
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):
155        print 'Game Over!'
156        print 'Total score:', t.total
157        sys.exit()
158    elif tuple(s.head) == (t.x, t.y):
159        t.sum()
160        t.random(CANVAS_WIDTH - 2, CANVAS_HEIGHT - 2)
161        s.grow()
162
163    lcaca.caca_clear_canvas(cv)
164    draw_border()
165    s.draw()
166    t.draw()
167    time.sleep(0.1)
Note: See TracBrowser for help on using the repository browser.