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


\begin{DoxyCodeInclude}

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

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

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

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

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

         \textcolor{keyword}{public} game\_loop(count) \{
            count++;

            \textcolor{comment}{/* get field geometry */}
            \textcolor{keyword}{new} fx, fy, fw, fh;
            get\_geometry(PART:\textcolor{stringliteral}{"bg"}, fx, fy, fw, fh);

            \textcolor{comment}{/* get mouse coords */}
            \textcolor{keyword}{new} mx, my;
            get\_mouse(mx, my);

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

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

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

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

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

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

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

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

            \textcolor{comment}{/* show debug info (if debug enabled) */}
\textcolor{preprocessor}{#if DEBUG\_ENABLE}
\textcolor{preprocessor}{}            \textcolor{keyword}{new} \_buf[128];
            snprintf(\_buf, \textcolor{keyword}{sizeof}(\_buf),
                     \textcolor{stringliteral}{"loop:%d  [speed %f  %f] [mouse: %d %d] [ball: %f %f]"},
                     count, speedx, speedy, mx, my, bx, by);
            set\_text(PART:\textcolor{stringliteral}{"dbg"}, \_buf);
\textcolor{preprocessor}{#endif}
\textcolor{preprocessor}{}
            \textcolor{comment}{/* recall the loop in n seconds */}
            timer(1.0 / FPS, \textcolor{stringliteral}{"game\_loop"}, count);
         \}
      \}
      parts \{
         part \{ name: \textcolor{stringliteral}{"bg"};
            type: RECT;
            description \{ state: \textcolor{stringliteral}{"default"} 0.0;
               color: COLOR\_BG;
            \}
         \}
         part \{ name: \textcolor{stringliteral}{"net"};
            type: RECT;
            description \{ state: \textcolor{stringliteral}{"default"} 0.0;
               color: COLOR\_FG;
               max: 10 9999;
            \}
         \}
         part \{ name: \textcolor{stringliteral}{"score1"};
            type: TEXT;
            description \{ state: \textcolor{stringliteral}{"default"} 0.0;
               color: COLOR\_FG;
               rel2.relative: 0.5 0.5;
               rel2.offset: -20 0;
               text \{
                  text: \textcolor{stringliteral}{"0"};
                  font: \textcolor{stringliteral}{"Sans"};
                  size: 50;
                  align: 1.0 0.0;
               \} 
            \}
         \}
         part \{ name: \textcolor{stringliteral}{"score2"};
            type: TEXT;
            description \{ state: \textcolor{stringliteral}{"default"} 0.0;
               color: COLOR\_FG;
               rel1.relative: 0.5 0.0;
               rel1.offset: 20 0;
               text \{
                  text: \textcolor{stringliteral}{"0"};
                  font: \textcolor{stringliteral}{"Sans"};
                  size: 50;
                  align: 0.0 0.0;
               \} 
            \}
         \}
         part \{ name: \textcolor{stringliteral}{"pad1"};
            type: RECT;
            description \{ state: \textcolor{stringliteral}{"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: \textcolor{stringliteral}{"pad2"};
            type: RECT;
            description \{ state: \textcolor{stringliteral}{"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: \textcolor{stringliteral}{"ball"};
            type: RECT;
            description \{ state: \textcolor{stringliteral}{"default"} 0.0;
               color: COLOR\_FG;
               max: BALL\_SIZE BALL\_SIZE;
               fixed: 1 1;
               align: 0.0 0.0;
               rel1.offset: 100 100;
            \}
         \}
\textcolor{preprocessor}{         #if DEBUG\_ENABLE}
\textcolor{preprocessor}{}         part \{ name: \textcolor{stringliteral}{"dbg"};
            type: TEXT;
            description \{ state: \textcolor{stringliteral}{"default"} 0.0;
               color: 255 255 255 200;
               text \{
                  font: \textcolor{stringliteral}{"Sans"};
                  size: 12;
                  align: 1.0 1.0;
               \}
            \}
         \}
\textcolor{preprocessor}{         #endif}
\textcolor{preprocessor}{}      \}
      programs \{
         \textcolor{comment}{/* on load: reset the game and start the game loop */}
         program \{
            signal: \textcolor{stringliteral}{"load"};
            source: \textcolor{stringliteral}{""};
            script \{
               game\_reset();
               timer(0.1 , \textcolor{stringliteral}{"game\_loop"}, 0);
            \}
         \}
         \textcolor{comment}{/* mouse left click: start the game, if not yet started */}
         program \{
            signal: \textcolor{stringliteral}{"mouse,down,1"};
            source: \textcolor{stringliteral}{"bg"};
            script \{
               \textcolor{keywordflow}{if} (get\_float(ballspeedx) == 0.0)
                  start\_game();
            \}
         \}
         \textcolor{comment}{/* mouse right click: restart the game*/}
         program \{
            signal: \textcolor{stringliteral}{"mouse,down,3"};
            source: \textcolor{stringliteral}{"bg"};
            script \{
               game\_reset();
            \}
         \}
      \}
   \}
\}
\end{DoxyCodeInclude}
 