Changeset 47:130f81975a65 in misc
- Timestamp:
- 11/11/09 02:51:04 (2 years ago)
- Branch:
- misc
- Convert:
- svn:e60e002c-0983-44b9-b2ae-8842d539f768/misc@50
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
snake.py
r46 r47 1 1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*-3 2 # 4 3 # snake.py 5 4 # Playing with ctypes and libcaca 6 # http://mornie.org/blog/2007/03/25/Play ing-with-ctypes-and-libcaca/5 # http://mornie.org/blog/2007/03/25/Playng-with-ctypes-and-libcaca/ 7 6 # 8 7 # Copyright (C) 2007 Daniele Tricoli aka Eriol <eriol@mornie.org> … … 28 27 # Applied patch by Sam Hocevar: check for caca_get_event's return value 29 28 # and added caca_event's missing first member 29 # * 25/10/07 30 # Updated for newer libcaca API (Sam Hocevar) 30 31 31 32 import ctypes as C … … 45 46 RIGHT = 276 46 47 47 class MOUSE(C.Structure): 48 _fields_ = [('x', C.c_uint), 49 ('y', C.c_uint), 50 ('button', C.c_uint)] 51 52 class RESIZE(C.Structure): 53 _fields_ = [('w', C.c_uint), 54 ('h', C.c_uint)] 55 56 class KEY(C.Structure): 57 _fields_ = [('ch', C.c_uint), 58 ('utf32', C.c_ulong), 59 ('utf8', C.c_char_p * 8)] 60 61 class ev(C.Union): 62 _fields_ = [('type', C.c_uint), 63 ('mouse', MOUSE), 64 ('resize', RESIZE), 65 ('key', KEY)] 48 class ev(C.Structure): 49 _fields_ = [('opaque_structure', C.c_char_p * 32)] 66 50 67 51 class Snake(object): … … 95 79 def draw(self): 96 80 global cv 97 lcaca.c ucul_set_color_ansi(cv, 0x05, 0x00)81 lcaca.caca_set_color_ansi(cv, 0x05, 0x00) 98 82 99 83 for p in self.body: 100 lcaca.c ucul_put_char(cv, p[0], p[1], ord('o'))101 lcaca.c ucul_set_color_ansi(cv, 0x02, 0x00)102 lcaca.c ucul_put_char(cv, self.head[0], self.head[1], ord('@'))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('@')) 103 87 lcaca.caca_refresh_display(dp) 104 88 … … 118 102 def draw(self): 119 103 global cv 120 lcaca.c ucul_set_color_ansi(cv, 0x03, 0x00)121 lcaca.c ucul_put_char(cv, self.x, self.y, ord(str(self.value)))104 lcaca.caca_set_color_ansi(cv, 0x03, 0x00) 105 lcaca.caca_put_char(cv, self.x, self.y, ord(str(self.value))) 122 106 lcaca.caca_refresh_display(dp) 123 107 124 108 def draw_border(): 125 lcaca.c ucul_set_color_ansi(cv, 0x04, 0x00)126 lcaca.c ucul_draw_box(cv,109 lcaca.caca_set_color_ansi(cv, 0x04, 0x00) 110 lcaca.caca_draw_box(cv, 127 111 0, 128 112 0, … … 133 117 event = ev() 134 118 lcaca = C.cdll.LoadLibrary('libcaca.so.0') 135 cv = lcaca.c ucul_create_canvas(CANVAS_WIDTH, CANVAS_HEIGHT)119 cv = lcaca.caca_create_canvas(CANVAS_WIDTH, CANVAS_HEIGHT) 136 120 dp = lcaca.caca_create_display(cv) 137 121 lcaca.caca_set_display_title(dp, "snake.py - playing with ctypes and libcaca") … … 149 133 while True: 150 134 while lcaca.caca_get_event(dp, 0x0001, C.byref(event), 0): 151 if event.key.utf32 == 113: # 'q' pressed 135 ch = lcaca.caca_get_event_key_ch(C.byref(event)) 136 if ch == 113: # 'q' pressed 152 137 sys.exit() 153 elif event.key.utf32== UP:138 elif ch == UP: 154 139 d = 'UP' 155 elif event.key.utf32== DOWN:140 elif ch == DOWN: 156 141 d = 'DOWN' 157 elif event.key.utf32== LEFT:142 elif ch == LEFT: 158 143 d = 'LEFT' 159 elif event.key.utf32== RIGHT:144 elif ch == RIGHT: 160 145 d = 'RIGHT' 161 146 … … 176 161 s.grow() 177 162 178 lcaca.c ucul_clear_canvas(cv)163 lcaca.caca_clear_canvas(cv) 179 164 draw_border() 180 165 s.draw()
Note: See TracChangeset
for help on using the changeset viewer.
