Changeset 47:130f81975a65 in misc


Ignore:
Timestamp:
11/11/09 02:51:04 (3 years ago)
Author:
Eriol
Branch:
misc
Convert:
svn:e60e002c-0983-44b9-b2ae-8842d539f768/misc@50
Message:

Updated for newer libcaca API (Sam Hocevar)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • snake.py

    r46 r47  
    11#!/usr/bin/env python 
    2 # -*- coding: utf-8 -*- 
    32# 
    43# snake.py 
    54# Playing with ctypes and libcaca 
    6 # http://mornie.org/blog/2007/03/25/Playing-with-ctypes-and-libcaca/ 
     5# http://mornie.org/blog/2007/03/25/Playng-with-ctypes-and-libcaca/ 
    76# 
    87# Copyright (C) 2007  Daniele Tricoli aka Eriol <eriol@mornie.org> 
     
    2827#   Applied patch by Sam Hocevar: check for caca_get_event's return value 
    2928#                                 and added caca_event's missing first member 
     29# * 25/10/07 
     30#   Updated for newer libcaca API (Sam Hocevar) 
    3031 
    3132import ctypes as C 
     
    4546RIGHT = 276 
    4647 
    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)] 
     48class ev(C.Structure): 
     49    _fields_ = [('opaque_structure', C.c_char_p * 32)] 
    6650 
    6751class Snake(object): 
     
    9579    def draw(self): 
    9680        global cv 
    97         lcaca.cucul_set_color_ansi(cv, 0x05, 0x00) 
     81        lcaca.caca_set_color_ansi(cv, 0x05, 0x00) 
    9882 
    9983        for p in self.body: 
    100             lcaca.cucul_put_char(cv, p[0], p[1], ord('o')) 
    101         lcaca.cucul_set_color_ansi(cv, 0x02, 0x00) 
    102         lcaca.cucul_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('@')) 
    10387        lcaca.caca_refresh_display(dp) 
    10488 
     
    118102    def draw(self): 
    119103        global cv 
    120         lcaca.cucul_set_color_ansi(cv, 0x03, 0x00) 
    121         lcaca.cucul_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))) 
    122106        lcaca.caca_refresh_display(dp) 
    123107 
    124108def draw_border(): 
    125     lcaca.cucul_set_color_ansi(cv, 0x04, 0x00) 
    126     lcaca.cucul_draw_box(cv, 
     109    lcaca.caca_set_color_ansi(cv, 0x04, 0x00) 
     110    lcaca.caca_draw_box(cv, 
    127111                         0, 
    128112                         0, 
     
    133117event = ev() 
    134118lcaca = C.cdll.LoadLibrary('libcaca.so.0') 
    135 cv = lcaca.cucul_create_canvas(CANVAS_WIDTH, CANVAS_HEIGHT) 
     119cv = lcaca.caca_create_canvas(CANVAS_WIDTH, CANVAS_HEIGHT) 
    136120dp = lcaca.caca_create_display(cv) 
    137121lcaca.caca_set_display_title(dp, "snake.py - playing with ctypes and libcaca") 
     
    149133while True: 
    150134    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 
    152137            sys.exit() 
    153         elif event.key.utf32 == UP: 
     138        elif ch == UP: 
    154139            d = 'UP' 
    155         elif event.key.utf32 == DOWN: 
     140        elif ch == DOWN: 
    156141            d = 'DOWN' 
    157         elif event.key.utf32 == LEFT: 
     142        elif ch == LEFT: 
    158143            d = 'LEFT' 
    159         elif event.key.utf32 == RIGHT: 
     144        elif ch == RIGHT: 
    160145            d = 'RIGHT' 
    161146 
     
    176161        s.grow() 
    177162 
    178     lcaca.cucul_clear_canvas(cv) 
     163    lcaca.caca_clear_canvas(cv) 
    179164    draw_border() 
    180165    s.draw() 
Note: See TracChangeset for help on using the changeset viewer.