~ubuntu-branches/ubuntu/raring/libcaca/raring

« back to all changes in this revision

Viewing changes to python/snake.py

  • Committer: Bazaar Package Importer
  • Author(s): Sam Hocevar (Debian packages)
  • Date: 2007-11-25 19:08:40 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20071125190840-3r4k9nkxsyo7m3ck
Tags: 0.99.beta13b-1
* New upstream release.

* debian/control:
  + Do not build-depend on the whole texlive suite, but on more fine-grained
    packages. Thanks to Norbert Preining for the hints (latex -recorder, then
    inspect the .fls file).
* Ship libcaca++, libcucul++ and their development files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
32
import ctypes as C
 
33
import random
 
34
import sys
 
35
import time
 
36
 
 
37
CANVAS_WIDTH = 80
 
38
CANVAS_HEIGHT = 40
 
39
 
 
40
CENTER_X = CANVAS_WIDTH / 2
 
41
CENTER_Y = CANVAS_HEIGHT / 2
 
42
 
 
43
UP = 273
 
44
DOWN = 274
 
45
LEFT = 275
 
46
RIGHT = 276
 
47
 
 
48
class ev(C.Structure):
 
49
    _fields_ = [('opaque_structure', C.c_char_p * 32)]
 
50
 
 
51
class 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.cucul_set_color_ansi(cv, 0x05, 0x00)
 
82
 
 
83
        for p in self.body:
 
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)
 
88
 
 
89
class 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.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)
 
107
 
 
108
def draw_border():
 
109
    lcaca.cucul_set_color_ansi(cv, 0x04, 0x00)
 
110
    lcaca.cucul_draw_box(cv,
 
111
                         0,
 
112
                         0,
 
113
                         CANVAS_WIDTH - 1,
 
114
                         CANVAS_HEIGHT - 1,
 
115
                         ord('#'))
 
116
 
 
117
event = ev()
 
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")
 
122
 
 
123
s = Snake([CENTER_X, CENTER_Y], 5)
 
124
t = Target()
 
125
t.random(CANVAS_WIDTH - 2, CANVAS_HEIGHT - 2)
 
126
 
 
127
draw_border()
 
128
s.draw()
 
129
t.draw()
 
130
 
 
131
lcaca.caca_get_event(dp, 0x0001, C.byref(event), -1)
 
132
 
 
133
while 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.cucul_clear_canvas(cv)
 
164
    draw_border()
 
165
    s.draw()
 
166
    t.draw()
 
167
    time.sleep(0.1)