source: libcaca/trunk/python/test1.py @ 4093

Last change on this file since 4093 was 521, checked in by Jean-Yves Lamoureux, 14 years ago
  • Added two examples for python bindings (a subset of cacademo, and bitmap example)
  • Property svn:executable set to *
File size: 3.5 KB
Line 
1#!/usr/bin/python2.4
2
3import caca
4import math
5from random import Random
6from math import *
7
8
9ret = caca.init()
10print "caca.init() returned ", ret
11print "Window size is ",caca.get_window_width(),"x",caca.get_window_height()
12print "Buffer size is ",caca.get_width(),"x",caca.get_height()
13
14caca.set_size(80,25)
15print "Buffer size is now",caca.get_width(),"x",caca.get_height()
16
17
18print "Feature name for CACA_DITHERING_FSTEIN is "+caca.get_feature_name(caca.CACA_DITHERING_FSTEIN)
19
20
21sprite = int(caca.load_sprite("./caca.txt"))
22
23
24rand = Random()
25
26i = 0
27while caca.get_event(caca.CACA_EVENT_KEY_PRESS) != caca.CACA_EVENT_KEY_PRESS|caca.CACA_KEY_ESCAPE:
28
29
30    i=i+1;
31   
32    caca.clear();
33   
34    caca.set_color(caca.CACA_COLOR_YELLOW, caca.CACA_COLOR_BLACK);
35    xo = caca.get_width() / 4;
36    yo = caca.get_height() / 4 + 5 * sin(0.03*i);
37   
38    for j in range(0,16):
39        xa = xo - (30 + sin(0.03*i) * 8) * sin(0.03*i + 3.1415*j/8);
40        ya = yo + (15 + sin(0.03*i) * 4) * cos(0.03*i + 3.1415*j/8);
41        caca.draw_thin_line(int(xo), int(yo), int(xa), int(ya));
42
43    j = 15 + sin(0.03*i) * 8;
44    caca.set_color(caca.CACA_COLOR_WHITE, caca.CACA_COLOR_BLACK);
45    caca.fill_ellipse(int(xo), int(yo), int(j), int(j / 2), '#');
46    caca.set_color(caca.CACA_COLOR_YELLOW, caca.CACA_COLOR_BLACK);
47    caca.draw_ellipse(xo, yo, j, j / 2, '0');
48
49    xo = caca.get_width() * 5 / 8;
50    yo = 2;
51
52    xa = caca.get_width() / 8 + sin(0.03*i) * 5;
53    ya = caca.get_height() / 2 + cos(0.03*i) * 5;
54
55    xb = caca.get_width() - 10 - cos(0.02*i) * 10;
56    yb = caca.get_height() * 3 / 4 - 5 + sin(0.02*i) * 5;
57
58    xc = caca.get_width() / 4 - sin(0.02*i) * 5;
59    yc = caca.get_height() * 3 / 4 + cos(0.02*i) * 5;
60
61    caca.set_color(caca.CACA_COLOR_GREEN, caca.CACA_COLOR_BLACK);
62    caca.fill_triangle(xo, yo, xb, yb, xa, ya, '%');
63    caca.set_color(caca.CACA_COLOR_YELLOW, caca.CACA_COLOR_BLACK);
64    caca.draw_thin_triangle(xo, yo, xb, yb, xa, ya);
65
66    caca.set_color(caca.CACA_COLOR_RED, caca.CACA_COLOR_BLACK);
67    caca.fill_triangle(xa, ya, xb, yb, xc, yc, ' ');
68    caca.set_color(caca.CACA_COLOR_YELLOW, caca.CACA_COLOR_BLACK);
69    caca.draw_thin_triangle(xa, ya, xb, yb, xc, yc);
70
71    caca.set_color(caca.CACA_COLOR_BLUE, caca.CACA_COLOR_BLACK);
72    caca.fill_triangle(xo, yo, xb, yb, xc, yc, '%');
73    caca.set_color(caca.CACA_COLOR_YELLOW, caca.CACA_COLOR_BLACK);
74    caca.draw_thin_triangle(xo, yo, xb, yb, xc, yc);
75
76    xa = 2;
77    ya = 2;
78
79    xb = caca.get_width() - 3;
80    yb = caca.get_height() / 2;
81
82    xc = caca.get_width() / 3;
83    yc = caca.get_height() - 3;
84
85    caca.set_color(caca.CACA_COLOR_CYAN, caca.CACA_COLOR_BLACK);
86    caca.draw_thin_triangle(xa, ya, xb, yb, xc, yc);
87
88    xo = caca.get_width() / 2 + cos(0.027*i) * caca.get_width() / 3;
89    yo = caca.get_height() / 2 - sin(0.027*i) * caca.get_height() / 2;
90
91    caca.draw_thin_line(xa, ya, xo, yo);
92    caca.draw_thin_line(xb, yb, xo, yo);
93    caca.draw_thin_line(xc, yc, xo, yo);
94
95    caca.draw_sprite(xo, yo, sprite, 0);
96
97    for j in range(i - 60, i):
98        delta = (rand.random()*10)-5;
99        caca.set_color(rand.random()*15, rand.random()*15);
100        caca.putchar(caca.get_width() / 2
101                    + cos(0.02*j) * (delta + caca.get_width() / 4),
102                   caca.get_height() / 2
103                    + sin(0.02*j) * (delta + caca.get_height() / 3),
104                   ' ');
105    caca.draw_sprite(caca.get_width() / 2 + cos(0.02*i) * caca.get_width() / 4,
106                   caca.get_height() / 2 + sin(0.02*i) * caca.get_height() / 3,
107                   int(sprite), int(0));
108
109
110   
111    caca.refresh();
112
113
114caca.end()
Note: See TracBrowser for help on using the repository browser.