summaryrefslogtreecommitdiff
path: root/legacy/edje/doc/examples/embryo_pong.edc
diff options
context:
space:
mode:
authorDavide Andreoli <dave@gurumeditation.it>2010-10-11 21:43:19 +0000
committerDavide Andreoli <dave@gurumeditation.it>2010-10-11 21:43:19 +0000
commit9908fe0298cc824948c3d0cc367102c1478bbeb2 (patch)
tree9921df9940d8ad58d8f5dc288f9f4fd3178c2108 /legacy/edje/doc/examples/embryo_pong.edc
parentdd5b79eff6172255c34af9894f4bf622489cf36f (diff)
And finally one funny (at least to code) edje example:
a super-simple (but full featured) pong game, fully written in embryo, in the style of the original game. To run it, like all the other examples, just go in the doc/examples folder and do: edje_cc embryo_pong.edc && edje_player embryo_pong.edj Have fun ...beating the ""AI"" is really difficult ;) SVN revision: 53278
Diffstat (limited to '')
-rw-r--r--legacy/edje/doc/examples/embryo_pong.edc273
1 files changed, 273 insertions, 0 deletions
diff --git a/legacy/edje/doc/examples/embryo_pong.edc b/legacy/edje/doc/examples/embryo_pong.edc
new file mode 100644
index 0000000000..863b5648a9
--- /dev/null
+++ b/legacy/edje/doc/examples/embryo_pong.edc
@@ -0,0 +1,273 @@
1
2#define DEBUG_ENABLE 0
3#define FPS 30
4#define COLOR_BG 50 50 50 255
5#define COLOR_FG 222 222 222 255
6#define PAD_SIZE 60
7#define PAD_DISTANCE 20
8#define BALL_SPEED 5.0
9#define BALL_SPEEDUP 0.1
10#define BALL_SIZE 10
11
12#if DEBUG_ENABLE
13#define DBG(...) { \
14 new _buf[128]; \
15 snprintf(_buf, sizeof(_buf), __VA_ARGS__); \
16 set_text(PART:"dbg", _buf); }
17#else
18#define DBG(...)
19#endif
20
21collections {
22 group { name: "main";
23 min: 500 300;
24 script {
25 public ballx, bally;
26 public ballspeedx, ballspeedy;
27 public player_score, ai_score;
28
29 public game_reset() {
30 set_int(ai_score, 0);
31 set_int(player_score, 0);
32 set_text(PART:"score1", "0");
33 set_text(PART:"score2", "0");
34 game_init();
35 }
36
37 public game_init() {
38 set_float(ballspeedx, 0.0);
39 set_float(ballspeedy, 0.0);
40 set_float(ballx, 100);
41 set_float(bally, 100);
42 }
43
44 public start_game() {
45 set_float(ballspeedx, BALL_SPEED);
46 set_float(ballspeedy, BALL_SPEED * randf());
47 }
48
49 public player_wins() {
50 new buf[16]
51 set_int(player_score, get_int(player_score) + 1);
52 snprintf(buf, sizeof(buf), "%d", get_int(player_score));
53 set_text(PART:"score1", buf);
54 game_init();
55 }
56
57 public ai_wins() {
58 new buf[16]
59 set_int(ai_score, get_int(ai_score) + 1);
60 snprintf(buf, sizeof(buf), "%d", get_int(ai_score));
61 set_text(PART:"score2", buf);
62 game_init();
63 }
64
65 public game_loop(count) {
66 count++;
67
68 /* get field geometry */
69 new fx, fy, fw, fh;
70 get_geometry(PART:"bg", fx, fy, fw, fh);
71
72 /* get mouse coords */
73 new mx, my;
74 get_mouse(mx, my);
75
76 /* get the ball info */
77 new Float:bx = get_float(ballx);
78 new Float:by = get_float(bally);
79 new Float:speedx = get_float(ballspeedx);
80 new Float:speedy = get_float(ballspeedy);
81
82 /* move the player pad */
83 new pady = my - PAD_SIZE / 2;
84 if (pady < 0) pady = 0;
85 else if (pady + PAD_SIZE > fh) pady = fh - PAD_SIZE;
86 custom_state(PART:"pad1", "default", 0.0);
87 set_state_val(PART:"pad1", STATE_REL1_OFFSET, 20, pady);
88 set_state(PART:"pad1", "custom", 0.0);
89
90 /* move the AI pad */
91 new pad2y = round(by) - PAD_SIZE / 2 - BALL_SIZE / 2;
92 if (pad2y < 0) pad2y = 0;
93 else if (pad2y + PAD_SIZE > fh) pad2y = fh - PAD_SIZE;
94 custom_state(PART:"pad2", "default", 0.0);
95 set_state_val(PART:"pad2", STATE_REL1_OFFSET, 20, pad2y);
96 set_state(PART:"pad2", "custom", 0.0);
97
98 /* calc new ball position */
99 bx += speedx;
100 by += speedy;
101
102 /* check walls collision */
103 if (by < 0)
104 {
105 speedy = -speedy;
106 by = 0;
107 }
108 else if (by + BALL_SIZE > fh)
109 {
110 speedy = -speedy;
111 by = fh - BALL_SIZE - 1;
112 }
113
114 /* check player pad collision */
115 if ((speedx < 0) &&
116 (bx < PAD_DISTANCE + 10) && (bx > 0) &&
117 (by + BALL_SIZE > pady) && (by < pady + PAD_SIZE))
118 {
119 new Float:dy = by - pady - PAD_SIZE / 2;
120 speedy += dy / 10;
121 speedx = -speedx + BALL_SPEEDUP;
122 }
123
124 /* check AI pad collision */
125 else if ((bx + BALL_SIZE > fw - PAD_DISTANCE - 10) &&
126 (bx + BALL_SIZE < fw) &&
127 (by + BALL_SIZE > pad2y) && (by < pad2y + PAD_SIZE))
128 {
129 new Float:dy = by - pad2y - PAD_SIZE / 2;
130 speedy += dy / 10;
131 speedx = -speedx - BALL_SPEEDUP;
132 }
133
134 /* apply the new ball position */
135 custom_state(PART:"ball", "default", 0.0);
136 set_state_val(PART:"ball", STATE_REL1_OFFSET, round(bx), round(by));
137 set_state(PART:"ball", "custom", 0.0);
138
139 /* update global vars */
140 set_float(ballx, bx);
141 set_float(bally, by);
142 set_float(ballspeedx, speedx);
143 set_float(ballspeedy, speedy);
144
145 /* AI score a point */
146 if (bx < 0) ai_wins();
147 /* player score a point */
148 if (bx + BALL_SIZE > fw) player_wins();
149
150 /* show debug info (if debug enabled) */
151 DBG("loop:%d [speed %f %f] [mouse: %d %d] [ball: %f %f]",
152 count, speedx, speedy, mx, my, bx, by)
153
154 /* recall the loop in n seconds */
155 timer(1.0 / FPS, "game_loop", count);
156 }
157 }
158 parts {
159 part { name: "bg";
160 type: RECT;
161 description { state: "default" 0.0;
162 color: COLOR_BG;
163 }
164 }
165 part { name: "net";
166 type: RECT;
167 description { state: "default" 0.0;
168 color: COLOR_FG;
169 max: 10 9999;
170 }
171 }
172 part { name: "score1";
173 type: TEXT;
174 description { state: "default" 0.0;
175 color: COLOR_FG;
176 rel2.relative: 0.5 0.5;
177 rel2.offset: -20 0;
178 text {
179 text: "0";
180 font: "Sans";
181 size: 50;
182 align: 1.0 0.0;
183 }
184 }
185 }
186 part { name: "score2";
187 type: TEXT;
188 description { state: "default" 0.0;
189 color: COLOR_FG;
190 rel1.relative: 0.5 0.0;
191 rel1.offset: 20 0;
192 text {
193 text: "0";
194 font: "Sans";
195 size: 50;
196 align: 0.0 0.0;
197 }
198 }
199 }
200 part { name: "pad1";
201 type: RECT;
202 description { state: "default" 0.0;
203 color: COLOR_FG;
204 max: 10 PAD_SIZE;
205 fixed: 1 1;
206 align: 0.0 0.0;
207 rel1.offset: PAD_DISTANCE 0;
208 }
209 }
210 part { name: "pad2";
211 type: RECT;
212 description { state: "default" 0.0;
213 color: COLOR_FG;
214 max: 10 PAD_SIZE;
215 fixed: 1 1;
216 align: 1.0 0.0;
217 rel2.offset: -PAD_DISTANCE 0;
218 }
219 }
220 part { name: "ball";
221 type: RECT;
222 description { state: "default" 0.0;
223 color: COLOR_FG;
224 max: BALL_SIZE BALL_SIZE;
225 fixed: 1 1;
226 align: 0.0 0.0;
227 rel1.offset: 100 100;
228 }
229 }
230 #if DEBUG_ENABLE
231 part { name: "dbg";
232 type: TEXT;
233 description { state: "default" 0.0;
234 color: 255 255 255 200;
235 text {
236 font: "Sans";
237 size: 12;
238 align: 1.0 1.0;
239 }
240 }
241 }
242 #endif
243 }
244 programs {
245 /* on load: reset the game and start the game loop */
246 program {
247 signal: "load";
248 source: "";
249 script {
250 game_reset();
251 timer(0.1 , "game_loop", 0);
252 }
253 }
254 /* mouse left click: start the game, if not yet started */
255 program {
256 signal: "mouse,down,1";
257 source: "bg";
258 script {
259 if (get_float(ballspeedx) == 0.0)
260 start_game();
261 }
262 }
263 /* mouse right click: restart the game*/
264 program {
265 signal: "mouse,down,3";
266 source: "bg";
267 script {
268 game_reset();
269 }
270 }
271 }
272 }
273}