| 1 | #!/usr/bin/env python |
|---|
| 2 | # -*- coding: utf-8 -*- |
|---|
| 3 | # |
|---|
| 4 | # snake.py |
|---|
| 5 | # Playing with ctypes and libcaca |
|---|
| 6 | # http://mornie.org/blog/2007/03/25/Playing-with-ctypes-and-libcaca/ |
|---|
| 7 | # |
|---|
| 8 | # Copyright (C) 2007 Daniele Tricoli aka Eriol <eriol@mornie.org> |
|---|
| 9 | # |
|---|
| 10 | # This program is free software; you can redistribute it and/or |
|---|
| 11 | # modify it under the terms of the GNU General Public License |
|---|
| 12 | # as published by the Free Software Foundation; either version 2 |
|---|
| 13 | # of the License, or (at your option) any later version. |
|---|
| 14 | # |
|---|
| 15 | # This program is distributed in the hope that it will be useful, |
|---|
| 16 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 18 | # GNU General Public License for more details. |
|---|
| 19 | # |
|---|
| 20 | # You should have received a copy of the GNU General Public License |
|---|
| 21 | # along with this program; if not, write to the Free Software |
|---|
| 22 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|---|
| 23 | # |
|---|
| 24 | # -- Changelog |
|---|
| 25 | # * 23/03/07 |
|---|
| 26 | # Initial release |
|---|
| 27 | # * 20/10/07 |
|---|
| 28 | # Applied patch by Sam Hocevar: check for caca_get_event's return value |
|---|
| 29 | # and added caca_event's missing first member |
|---|
| 30 | # * 25/10/07 |
|---|
| 31 | # Updated for newer libcaca API (Sam Hocevar) |
|---|
| 32 | |
|---|
| 33 | import ctypes as C |
|---|
| 34 | import random |
|---|
| 35 | import sys |
|---|
| 36 | import time |
|---|
| 37 | |
|---|
| 38 | CANVAS_WIDTH = 80 |
|---|
| 39 | CANVAS_HEIGHT = 40 |
|---|
| 40 | |
|---|
| 41 | CENTER_X = CANVAS_WIDTH / 2 |
|---|
| 42 | CENTER_Y = CANVAS_HEIGHT / 2 |
|---|
| 43 | |
|---|
| 44 | UP = 273 |
|---|
| 45 | DOWN = 274 |
|---|
| 46 | LEFT = 275 |
|---|
| 47 | RIGHT = 276 |
|---|
| 48 | |
|---|
| 49 | class ev(C.Structure): |
|---|
| 50 | _fields_ = [('opaque_structure', C.c_char_p * 32)] |
|---|
| 51 | |
|---|
| 52 | class Snake(object): |
|---|
| 53 | |
|---|
| 54 | def __init__(self, center_point, length): |
|---|
| 55 | |
|---|
| 56 | self.head = center_point |
|---|
| 57 | self.body = [] |
|---|
| 58 | |
|---|
| 59 | for y in xrange(self.head[1] + 1, self.head[1] + length + 1): |
|---|
| 60 | self.body.append((self.head[0], y)) |
|---|
| 61 | |
|---|
| 62 | def move(self, direction): |
|---|
| 63 | |
|---|
| 64 | phead = tuple(self.head) |
|---|
| 65 | |
|---|
| 66 | if direction == 'UP': |
|---|
| 67 | self.head[1] -=1 |
|---|
| 68 | elif direction == 'DOWN': |
|---|
| 69 | self.head[1] +=1 |
|---|
| 70 | elif direction == 'LEFT': |
|---|
| 71 | self.head[0] -=1 |
|---|
| 72 | elif direction == 'RIGHT': |
|---|
| 73 | self.head[0] +=1 |
|---|
| 74 | |
|---|
| 75 | self.body = [phead] + self.body[:-1] |
|---|
| 76 | |
|---|
| 77 | def grow(self): |
|---|
| 78 | self.body += [tuple(self.head)] * 2 |
|---|
| 79 | |
|---|
| 80 | def draw(self): |
|---|
| 81 | global cv |
|---|
| 82 | lcaca.caca_set_color_ansi(cv, 0x05, 0x00) |
|---|
| 83 | |
|---|
| 84 | for p in self.body: |
|---|
| 85 | lcaca.caca_put_char(cv, p[0], p[1], ord('o')) |
|---|
| 86 | lcaca.caca_set_color_ansi(cv, 0x02, 0x00) |
|---|
| 87 | lcaca.caca_put_char(cv, self.head[0], self.head[1], ord('@')) |
|---|
| 88 | lcaca.caca_refresh_display(dp) |
|---|
| 89 | |
|---|
| 90 | class Target(object): |
|---|
| 91 | |
|---|
| 92 | def __init__(self): |
|---|
| 93 | self.total = 0 |
|---|
| 94 | |
|---|
| 95 | def random(self, width, height): |
|---|
| 96 | self.x = int(random.uniform(1, width)) |
|---|
| 97 | self.y = int(random.uniform(1, height)) |
|---|
| 98 | self.value = random.choice(range(1,10)) |
|---|
| 99 | |
|---|
| 100 | def sum(self): |
|---|
| 101 | self.total += self.value |
|---|
| 102 | |
|---|
| 103 | def draw(self): |
|---|
| 104 | global cv |
|---|
| 105 | lcaca.caca_set_color_ansi(cv, 0x03, 0x00) |
|---|
| 106 | lcaca.caca_put_char(cv, self.x, self.y, ord(str(self.value))) |
|---|
| 107 | lcaca.caca_refresh_display(dp) |
|---|
| 108 | |
|---|
| 109 | def draw_border(): |
|---|
| 110 | lcaca.caca_set_color_ansi(cv, 0x04, 0x00) |
|---|
| 111 | lcaca.caca_draw_box(cv, |
|---|
| 112 | 0, |
|---|
| 113 | 0, |
|---|
| 114 | CANVAS_WIDTH - 1, |
|---|
| 115 | CANVAS_HEIGHT - 1, |
|---|
| 116 | ord('#')) |
|---|
| 117 | |
|---|
| 118 | event = ev() |
|---|
| 119 | lcaca = C.cdll.LoadLibrary('libcaca.so.0') |
|---|
| 120 | cv = lcaca.caca_create_canvas(CANVAS_WIDTH, CANVAS_HEIGHT) |
|---|
| 121 | dp = lcaca.caca_create_display(cv) |
|---|
| 122 | lcaca.caca_set_display_title(dp, "snake.py - playing with ctypes and libcaca") |
|---|
| 123 | |
|---|
| 124 | s = Snake([CENTER_X, CENTER_Y], 5) |
|---|
| 125 | t = Target() |
|---|
| 126 | t.random(CANVAS_WIDTH - 2, CANVAS_HEIGHT - 2) |
|---|
| 127 | |
|---|
| 128 | draw_border() |
|---|
| 129 | s.draw() |
|---|
| 130 | t.draw() |
|---|
| 131 | |
|---|
| 132 | lcaca.caca_get_event(dp, 0x0001, C.byref(event), -1) |
|---|
| 133 | |
|---|
| 134 | while True: |
|---|
| 135 | while lcaca.caca_get_event(dp, 0x0001, C.byref(event), 0): |
|---|
| 136 | ch = lcaca.caca_get_event_key_ch(C.byref(event)) |
|---|
| 137 | if ch == 113: # 'q' pressed |
|---|
| 138 | sys.exit() |
|---|
| 139 | elif ch == UP: |
|---|
| 140 | d = 'UP' |
|---|
| 141 | elif ch == DOWN: |
|---|
| 142 | d = 'DOWN' |
|---|
| 143 | elif ch == LEFT: |
|---|
| 144 | d = 'LEFT' |
|---|
| 145 | elif ch == RIGHT: |
|---|
| 146 | d = 'RIGHT' |
|---|
| 147 | |
|---|
| 148 | try: |
|---|
| 149 | s.move(d) |
|---|
| 150 | except NameError: |
|---|
| 151 | pass |
|---|
| 152 | |
|---|
| 153 | if (tuple(s.head) in s.body[1:] or |
|---|
| 154 | not 0 < s.head[0] < CANVAS_WIDTH - 1 or |
|---|
| 155 | not 0 < s.head[1] < CANVAS_HEIGHT - 1): |
|---|
| 156 | print 'Game Over!' |
|---|
| 157 | print 'Total score:', t.total |
|---|
| 158 | sys.exit() |
|---|
| 159 | elif tuple(s.head) == (t.x, t.y): |
|---|
| 160 | t.sum() |
|---|
| 161 | t.random(CANVAS_WIDTH - 2, CANVAS_HEIGHT - 2) |
|---|
| 162 | s.grow() |
|---|
| 163 | |
|---|
| 164 | lcaca.caca_clear_canvas(cv) |
|---|
| 165 | draw_border() |
|---|
| 166 | s.draw() |
|---|
| 167 | t.draw() |
|---|
| 168 | time.sleep(0.1) |
|---|