\hypertarget{embryo_pong_8edc-example}{}\section{embryo\+\_\+pong.\+edc}
Super-\/simple Pong implementation in pure embryo.


\begin{DoxyCodeInclude}

#define DEBUG\_ENABLE 0
#define FPS 30
#define COLOR\_BG 50 50 50 255
#define COLOR\_FG 222 222 222 255
#define PAD\_SIZE 60
#define PAD\_DISTANCE 20
#define BALL\_SPEED 5.0
#define BALL\_SPEEDUP 0.1
#define BALL\_SIZE 10


collections \{
   group \{ name: "main";
      min: 500 300;
      script \{
         public ballx, bally;
         public ballspeedx, ballspeedy;
         public player\_score, ai\_score;

         public game\_reset() \{
            set\_int(ai\_score, 0);
            set\_int(player\_score, 0);
            set\_text(PART:"score1", "0");
            set\_text(PART:"score2", "0");
            game\_init();
         \}
         
         public game\_init() \{
            set\_float(ballspeedx, 0.0);
            set\_float(ballspeedy, 0.0);
            set\_float(ballx, 100);
            set\_float(bally, 100);
         \}
         
         public start\_game() \{
            set\_float(ballspeedx, BALL\_SPEED);
            set\_float(ballspeedy, BALL\_SPEED * randf());
         \}

         public player\_wins() \{
            new buf[16]
            set\_int(player\_score, get\_int(player\_score) + 1);
            snprintf(buf, sizeof(buf), "%d", get\_int(player\_score));
            set\_text(PART:"score1", buf);
            game\_init();
         \}

         public ai\_wins() \{
            new buf[16]
            set\_int(ai\_score, get\_int(ai\_score) + 1);
            snprintf(buf, sizeof(buf), "%d", get\_int(ai\_score));
            set\_text(PART:"score2", buf);
            game\_init();
         \}

         public game\_loop(count) \{
            count++;

            /* get field geometry */
            new fx, fy, fw, fh;
            get\_geometry(PART:"bg", fx, fy, fw, fh);

            /* get mouse coords */
            new mx, my;
            get\_mouse(mx, my);

            /* get the ball info */
            new Float:bx = get\_float(ballx);
            new Float:by = get\_float(bally);
            new Float:speedx = get\_float(ballspeedx);
            new Float:speedy = get\_float(ballspeedy);

            /* move the player pad */
            new pady = my - PAD\_SIZE / 2;
            if (pady < 0) pady = 0;
            else if (pady + PAD\_SIZE > fh) pady = fh - PAD\_SIZE;
            custom\_state(PART:"pad1", "default", 0.0);
            set\_state\_val(PART:"pad1", STATE\_REL1\_OFFSET, 20, pady);
            set\_state(PART:"pad1", "custom", 0.0);

            /* move the AI pad */
            new pad2y = round(by) - PAD\_SIZE / 2 - BALL\_SIZE / 2;
            if (pad2y < 0) pad2y = 0;
            else if (pad2y + PAD\_SIZE > fh) pad2y = fh - PAD\_SIZE;
            custom\_state(PART:"pad2", "default", 0.0);
            set\_state\_val(PART:"pad2", STATE\_REL1\_OFFSET, 20, pad2y);
            set\_state(PART:"pad2", "custom", 0.0);
         
            /* calc new ball position */
            bx += speedx;
            by += speedy;

            /* check walls collision */
            if (by < 0)
            \{
               speedy = -speedy;
               by = 0;
            \}
            else if (by + BALL\_SIZE > fh)
            \{
               speedy = -speedy;
               by = fh - BALL\_SIZE - 1;
            \}

            /* check player pad collision */
            if ((speedx < 0) &&
                (bx < PAD\_DISTANCE + 10) && (bx > 0) &&
                (by + BALL\_SIZE > pady) && (by < pady + PAD\_SIZE))
            \{
               new Float:dy = by - pady - PAD\_SIZE / 2;
               speedy += dy / 10;
               speedx = -speedx + BALL\_SPEEDUP;
            \}

            /* check AI pad collision */
            else if ((bx + BALL\_SIZE > fw - PAD\_DISTANCE - 10) &&
                     (bx + BALL\_SIZE < fw) &&
                     (by + BALL\_SIZE > pad2y) && (by < pad2y + PAD\_SIZE))
            \{
               new Float:dy = by - pad2y - PAD\_SIZE / 2;
               speedy += dy / 10;
               speedx = -speedx - BALL\_SPEEDUP;
            \}
            
            /* apply the new ball position */
            custom\_state(PART:"ball", "default", 0.0);
            set\_state\_val(PART:"ball", STATE\_REL1\_OFFSET, round(bx), round(by));
            set\_state(PART:"ball", "custom", 0.0);

            /* update global vars */
            set\_float(ballx, bx);
            set\_float(bally, by);
            set\_float(ballspeedx, speedx);
            set\_float(ballspeedy, speedy);

            /* AI score a point */
            if (bx < 0) ai\_wins();
            /* player score a point */
            if (bx + BALL\_SIZE > fw) player\_wins();

            /* show debug info (if debug enabled) */
#if DEBUG\_ENABLE
            new \_buf[128];
            snprintf(\_buf, sizeof(\_buf),
                     "loop:%d  [speed %f  %f] [mouse: %d %d] [ball: %f %f]",
                     count, speedx, speedy, mx, my, bx, by);
            set\_text(PART:"dbg", \_buf);
#endif

            /* recall the loop in n seconds */
            timer(1.0 / FPS, "game\_loop", count);
         \}
      \}
      parts \{
         part \{ name: "bg";
            type: RECT;
            description \{ state: "default" 0.0;
               color: COLOR\_BG;
            \}
         \}
         part \{ name: "net";
            type: RECT;
            description \{ state: "default" 0.0;
               color: COLOR\_FG;
               max: 10 9999;
            \}
         \}
         part \{ name: "score1";
            type: TEXT;
            description \{ state: "default" 0.0;
               color: COLOR\_FG;
               rel2.relative: 0.5 0.5;
               rel2.offset: -20 0;
               text \{
                  text: "0";
                  font: "Sans";
                  size: 50;
                  align: 1.0 0.0;
               \} 
            \}
         \}
         part \{ name: "score2";
            type: TEXT;
            description \{ state: "default" 0.0;
               color: COLOR\_FG;
               rel1.relative: 0.5 0.0;
               rel1.offset: 20 0;
               text \{
                  text: "0";
                  font: "Sans";
                  size: 50;
                  align: 0.0 0.0;
               \} 
            \}
         \}
         part \{ name: "pad1";
            type: RECT;
            description \{ state: "default" 0.0;
               color: COLOR\_FG;
               max: 10 PAD\_SIZE;
               fixed: 1 1;
               align: 0.0 0.0;
               rel1.offset: PAD\_DISTANCE 0;
            \}
         \}
         part \{ name: "pad2";
            type: RECT;
            description \{ state: "default" 0.0;
               color: COLOR\_FG;
               max: 10 PAD\_SIZE;
               fixed: 1 1;
               align: 1.0 0.0;
               rel2.offset: -PAD\_DISTANCE 0;
            \}
         \}
         part \{ name: "ball";
            type: RECT;
            description \{ state: "default" 0.0;
               color: COLOR\_FG;
               max: BALL\_SIZE BALL\_SIZE;
               fixed: 1 1;
               align: 0.0 0.0;
               rel1.offset: 100 100;
            \}
         \}
         #if DEBUG\_ENABLE
         part \{ name: "dbg";
            type: TEXT;
            description \{ state: "default" 0.0;
               color: 255 255 255 200;
               text \{
                  font: "Sans";
                  size: 12;
                  align: 1.0 1.0;
               \}
            \}
         \}
         #endif
      \}
      programs \{
         /* on load: reset the game and start the game loop */
         program \{
            signal: "load";
            source: "";
            script \{
               game\_reset();
               timer(0.1 , "game\_loop", 0);
            \}
         \}
         /* mouse left click: start the game, if not yet started */
         program \{
            signal: "mouse,down,1";
            source: "bg";
            script \{
               if (get\_float(ballspeedx) == 0.0)
                  start\_game();
            \}
         \}
         /* mouse right click: restart the game*/
         program \{
            signal: "mouse,down,3";
            source: "bg";
            script \{
               game\_reset();
            \}
         \}
      \}
   \}
\}
\end{DoxyCodeInclude}
 