File: snake.py

package info (click to toggle)
libcaca 0.99.beta14-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 6,720 kB
  • ctags: 2,334
  • sloc: ansic: 18,652; cs: 1,171; objc: 835; cpp: 741; makefile: 446; sh: 289; python: 215; ruby: 190; asm: 93
file content (167 lines) | stat: -rwxr-xr-x 4,557 bytes parent folder | download
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
#!/usr/bin/env python
#
# snake.py
# Playing with ctypes and libcaca
# http://mornie.org/blog/2007/03/25/Playng-with-ctypes-and-libcaca/
#
# Copyright (C) 2007  Daniele Tricoli aka Eriol <eriol@mornie.org>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
# -- Changelog
# * 23/03/07
#   Initial release
# * 20/10/07
#   Applied patch by Sam Hocevar: check for caca_get_event's return value
#                                 and added caca_event's missing first member
# * 25/10/07
#   Updated for newer libcaca API (Sam Hocevar)

import ctypes as C
import random
import sys
import time

CANVAS_WIDTH = 80
CANVAS_HEIGHT = 40

CENTER_X = CANVAS_WIDTH / 2
CENTER_Y = CANVAS_HEIGHT / 2

UP = 273
DOWN = 274
LEFT = 275
RIGHT = 276

class ev(C.Structure):
    _fields_ = [('opaque_structure', C.c_char_p * 32)]

class Snake(object):

    def __init__(self, center_point, length):

        self.head = center_point
        self.body = []

        for y in xrange(self.head[1] + 1, self.head[1] + length + 1):
            self.body.append((self.head[0], y))

    def move(self, direction):

        phead = tuple(self.head)

        if direction == 'UP':
            self.head[1] -=1
        elif direction == 'DOWN':
            self.head[1] +=1
        elif direction == 'LEFT':
            self.head[0] -=1
        elif direction == 'RIGHT':
            self.head[0] +=1

        self.body = [phead] + self.body[:-1]

    def grow(self):
        self.body += [tuple(self.head)] * 2

    def draw(self):
        global cv
        lcaca.cucul_set_color_ansi(cv, 0x05, 0x00)

        for p in self.body:
            lcaca.cucul_put_char(cv, p[0], p[1], ord('o'))
        lcaca.cucul_set_color_ansi(cv, 0x02, 0x00)
        lcaca.cucul_put_char(cv, self.head[0], self.head[1], ord('@'))
        lcaca.caca_refresh_display(dp)

class Target(object):

    def __init__(self):
        self.total = 0

    def random(self, width, height):
        self.x = int(random.uniform(1, width))
        self.y = int(random.uniform(1, height))
        self.value = random.choice(range(1,10))

    def sum(self):
        self.total += self.value

    def draw(self):
        global cv
        lcaca.cucul_set_color_ansi(cv, 0x03, 0x00)
        lcaca.cucul_put_char(cv, self.x, self.y, ord(str(self.value)))
        lcaca.caca_refresh_display(dp)

def draw_border():
    lcaca.cucul_set_color_ansi(cv, 0x04, 0x00)
    lcaca.cucul_draw_box(cv,
                         0,
                         0,
                         CANVAS_WIDTH - 1,
                         CANVAS_HEIGHT - 1,
                         ord('#'))

event = ev()
lcaca = C.cdll.LoadLibrary('libcaca.so.0')
cv = lcaca.cucul_create_canvas(CANVAS_WIDTH, CANVAS_HEIGHT)
dp = lcaca.caca_create_display(cv)
lcaca.caca_set_display_title(dp, "snake.py - playing with ctypes and libcaca")

s = Snake([CENTER_X, CENTER_Y], 5)
t = Target()
t.random(CANVAS_WIDTH - 2, CANVAS_HEIGHT - 2)

draw_border()
s.draw()
t.draw()

lcaca.caca_get_event(dp, 0x0001, C.byref(event), -1)

while True:
    while lcaca.caca_get_event(dp, 0x0001, C.byref(event), 0):
        ch = lcaca.caca_get_event_key_ch(C.byref(event))
        if ch == 113: # 'q' pressed
            sys.exit()
        elif ch == UP:
            d = 'UP'
        elif ch == DOWN:
            d = 'DOWN'
        elif ch == LEFT:
            d = 'LEFT'
        elif ch == RIGHT:
            d = 'RIGHT'

    try:
        s.move(d)
    except NameError:
        pass

    if (tuple(s.head) in s.body[1:] or
        not 0 < s.head[0] < CANVAS_WIDTH - 1 or
        not 0 < s.head[1] < CANVAS_HEIGHT - 1):
        print 'Game Over!'
        print 'Total score:', t.total
        sys.exit()
    elif tuple(s.head) == (t.x, t.y):
        t.sum()
        t.random(CANVAS_WIDTH - 2, CANVAS_HEIGHT - 2)
        s.grow()

    lcaca.cucul_clear_canvas(cv)
    draw_border()
    s.draw()
    t.draw()
    time.sleep(0.1)