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


\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{}
\DoxyCodeLine{\#define DEBUG\_ENABLE 0}
\DoxyCodeLine{\#define FPS 30}
\DoxyCodeLine{\#define COLOR\_BG 50 50 50 255}
\DoxyCodeLine{\#define COLOR\_FG 222 222 222 255}
\DoxyCodeLine{\#define PAD\_SIZE 60}
\DoxyCodeLine{\#define PAD\_DISTANCE 20}
\DoxyCodeLine{\#define BALL\_SPEED 5.0}
\DoxyCodeLine{\#define BALL\_SPEEDUP 0.1}
\DoxyCodeLine{\#define BALL\_SIZE 10}
\DoxyCodeLine{}
\DoxyCodeLine{}
\DoxyCodeLine{collections \{}
\DoxyCodeLine{   group \{ name: "main";}
\DoxyCodeLine{      min: 500 300;}
\DoxyCodeLine{      script \{}
\DoxyCodeLine{         public ballx, bally;}
\DoxyCodeLine{         public ballspeedx, ballspeedy;}
\DoxyCodeLine{         public player\_score, ai\_score;}
\DoxyCodeLine{}
\DoxyCodeLine{         public game\_reset() \{}
\DoxyCodeLine{            set\_int(ai\_score, 0);}
\DoxyCodeLine{            set\_int(player\_score, 0);}
\DoxyCodeLine{            set\_text(PART:"score1", "0");}
\DoxyCodeLine{            set\_text(PART:"score2", "0");}
\DoxyCodeLine{            game\_init();}
\DoxyCodeLine{         \}}
\DoxyCodeLine{         }
\DoxyCodeLine{         public game\_init() \{}
\DoxyCodeLine{            set\_float(ballspeedx, 0.0);}
\DoxyCodeLine{            set\_float(ballspeedy, 0.0);}
\DoxyCodeLine{            set\_float(ballx, 100);}
\DoxyCodeLine{            set\_float(bally, 100);}
\DoxyCodeLine{         \}}
\DoxyCodeLine{         }
\DoxyCodeLine{         public start\_game() \{}
\DoxyCodeLine{            set\_float(ballspeedx, BALL\_SPEED);}
\DoxyCodeLine{            set\_float(ballspeedy, BALL\_SPEED * randf());}
\DoxyCodeLine{         \}}
\DoxyCodeLine{}
\DoxyCodeLine{         public player\_wins() \{}
\DoxyCodeLine{            new buf[16]}
\DoxyCodeLine{            set\_int(player\_score, get\_int(player\_score) + 1);}
\DoxyCodeLine{            snprintf(buf, sizeof(buf), "\%d", get\_int(player\_score));}
\DoxyCodeLine{            set\_text(PART:"score1", buf);}
\DoxyCodeLine{            game\_init();}
\DoxyCodeLine{         \}}
\DoxyCodeLine{}
\DoxyCodeLine{         public ai\_wins() \{}
\DoxyCodeLine{            new buf[16]}
\DoxyCodeLine{            set\_int(ai\_score, get\_int(ai\_score) + 1);}
\DoxyCodeLine{            snprintf(buf, sizeof(buf), "\%d", get\_int(ai\_score));}
\DoxyCodeLine{            set\_text(PART:"score2", buf);}
\DoxyCodeLine{            game\_init();}
\DoxyCodeLine{         \}}
\DoxyCodeLine{}
\DoxyCodeLine{         public game\_loop(count) \{}
\DoxyCodeLine{            count++;}
\DoxyCodeLine{}
\DoxyCodeLine{            /* get field geometry */}
\DoxyCodeLine{            new fx, fy, fw, fh;}
\DoxyCodeLine{            get\_geometry(PART:"bg", fx, fy, fw, fh);}
\DoxyCodeLine{}
\DoxyCodeLine{            /* get mouse coords */}
\DoxyCodeLine{            new mx, my;}
\DoxyCodeLine{            get\_mouse(mx, my);}
\DoxyCodeLine{}
\DoxyCodeLine{            /* get the ball info */}
\DoxyCodeLine{            new Float:bx = get\_float(ballx);}
\DoxyCodeLine{            new Float:by = get\_float(bally);}
\DoxyCodeLine{            new Float:speedx = get\_float(ballspeedx);}
\DoxyCodeLine{            new Float:speedy = get\_float(ballspeedy);}
\DoxyCodeLine{}
\DoxyCodeLine{            /* move the player pad */}
\DoxyCodeLine{            new pady = my -\/ PAD\_SIZE / 2;}
\DoxyCodeLine{            if (pady < 0) pady = 0;}
\DoxyCodeLine{            else if (pady + PAD\_SIZE > fh) pady = fh -\/ PAD\_SIZE;}
\DoxyCodeLine{            custom\_state(PART:"pad1", "default", 0.0);}
\DoxyCodeLine{            set\_state\_val(PART:"pad1", STATE\_REL1\_OFFSET, 20, pady);}
\DoxyCodeLine{            set\_state(PART:"pad1", "custom", 0.0);}
\DoxyCodeLine{}
\DoxyCodeLine{            /* move the AI pad */}
\DoxyCodeLine{            new pad2y = round(by) -\/ PAD\_SIZE / 2 -\/ BALL\_SIZE / 2;}
\DoxyCodeLine{            if (pad2y < 0) pad2y = 0;}
\DoxyCodeLine{            else if (pad2y + PAD\_SIZE > fh) pad2y = fh -\/ PAD\_SIZE;}
\DoxyCodeLine{            custom\_state(PART:"pad2", "default", 0.0);}
\DoxyCodeLine{            set\_state\_val(PART:"pad2", STATE\_REL1\_OFFSET, 20, pad2y);}
\DoxyCodeLine{            set\_state(PART:"pad2", "custom", 0.0);}
\DoxyCodeLine{         }
\DoxyCodeLine{            /* calc new ball position */}
\DoxyCodeLine{            bx += speedx;}
\DoxyCodeLine{            by += speedy;}
\DoxyCodeLine{}
\DoxyCodeLine{            /* check walls collision */}
\DoxyCodeLine{            if (by < 0)}
\DoxyCodeLine{            \{}
\DoxyCodeLine{               speedy = -\/speedy;}
\DoxyCodeLine{               by = 0;}
\DoxyCodeLine{            \}}
\DoxyCodeLine{            else if (by + BALL\_SIZE > fh)}
\DoxyCodeLine{            \{}
\DoxyCodeLine{               speedy = -\/speedy;}
\DoxyCodeLine{               by = fh -\/ BALL\_SIZE -\/ 1;}
\DoxyCodeLine{            \}}
\DoxyCodeLine{}
\DoxyCodeLine{            /* check player pad collision */}
\DoxyCodeLine{            if ((speedx < 0) \&\&}
\DoxyCodeLine{                (bx < PAD\_DISTANCE + 10) \&\& (bx > 0) \&\&}
\DoxyCodeLine{                (by + BALL\_SIZE > pady) \&\& (by < pady + PAD\_SIZE))}
\DoxyCodeLine{            \{}
\DoxyCodeLine{               new Float:dy = by -\/ pady -\/ PAD\_SIZE / 2;}
\DoxyCodeLine{               speedy += dy / 10;}
\DoxyCodeLine{               speedx = -\/speedx + BALL\_SPEEDUP;}
\DoxyCodeLine{            \}}
\DoxyCodeLine{}
\DoxyCodeLine{            /* check AI pad collision */}
\DoxyCodeLine{            else if ((bx + BALL\_SIZE > fw -\/ PAD\_DISTANCE -\/ 10) \&\&}
\DoxyCodeLine{                     (bx + BALL\_SIZE < fw) \&\&}
\DoxyCodeLine{                     (by + BALL\_SIZE > pad2y) \&\& (by < pad2y + PAD\_SIZE))}
\DoxyCodeLine{            \{}
\DoxyCodeLine{               new Float:dy = by -\/ pad2y -\/ PAD\_SIZE / 2;}
\DoxyCodeLine{               speedy += dy / 10;}
\DoxyCodeLine{               speedx = -\/speedx -\/ BALL\_SPEEDUP;}
\DoxyCodeLine{            \}}
\DoxyCodeLine{            }
\DoxyCodeLine{            /* apply the new ball position */}
\DoxyCodeLine{            custom\_state(PART:"ball", "default", 0.0);}
\DoxyCodeLine{            set\_state\_val(PART:"ball", STATE\_REL1\_OFFSET, round(bx), round(by));}
\DoxyCodeLine{            set\_state(PART:"ball", "custom", 0.0);}
\DoxyCodeLine{}
\DoxyCodeLine{            /* update global vars */}
\DoxyCodeLine{            set\_float(ballx, bx);}
\DoxyCodeLine{            set\_float(bally, by);}
\DoxyCodeLine{            set\_float(ballspeedx, speedx);}
\DoxyCodeLine{            set\_float(ballspeedy, speedy);}
\DoxyCodeLine{}
\DoxyCodeLine{            /* AI score a point */}
\DoxyCodeLine{            if (bx < 0) ai\_wins();}
\DoxyCodeLine{            /* player score a point */}
\DoxyCodeLine{            if (bx + BALL\_SIZE > fw) player\_wins();}
\DoxyCodeLine{}
\DoxyCodeLine{            /* show debug info (if debug enabled) */}
\DoxyCodeLine{\#if DEBUG\_ENABLE}
\DoxyCodeLine{            new \_buf[128];}
\DoxyCodeLine{            snprintf(\_buf, sizeof(\_buf),}
\DoxyCodeLine{                     "loop:\%d  [speed \%f  \%f] [mouse: \%d \%d] [ball: \%f \%f]",}
\DoxyCodeLine{                     count, speedx, speedy, mx, my, bx, by);}
\DoxyCodeLine{            set\_text(PART:"dbg", \_buf);}
\DoxyCodeLine{\#endif}
\DoxyCodeLine{}
\DoxyCodeLine{            /* recall the loop in n seconds */}
\DoxyCodeLine{            timer(1.0 / FPS, "game\_loop", count);}
\DoxyCodeLine{         \}}
\DoxyCodeLine{      \}}
\DoxyCodeLine{      parts \{}
\DoxyCodeLine{         part \{ name: "bg";}
\DoxyCodeLine{            type: RECT;}
\DoxyCodeLine{            description \{ state: "default" 0.0;}
\DoxyCodeLine{               color: COLOR\_BG;}
\DoxyCodeLine{            \}}
\DoxyCodeLine{         \}}
\DoxyCodeLine{         part \{ name: "net";}
\DoxyCodeLine{            type: RECT;}
\DoxyCodeLine{            description \{ state: "default" 0.0;}
\DoxyCodeLine{               color: COLOR\_FG;}
\DoxyCodeLine{               max: 10 9999;}
\DoxyCodeLine{            \}}
\DoxyCodeLine{         \}}
\DoxyCodeLine{         part \{ name: "score1";}
\DoxyCodeLine{            type: TEXT;}
\DoxyCodeLine{            description \{ state: "default" 0.0;}
\DoxyCodeLine{               color: COLOR\_FG;}
\DoxyCodeLine{               rel2.relative: 0.5 0.5;}
\DoxyCodeLine{               rel2.offset: -\/20 0;}
\DoxyCodeLine{               text \{}
\DoxyCodeLine{                  text: "0";}
\DoxyCodeLine{                  font: "Sans";}
\DoxyCodeLine{                  size: 50;}
\DoxyCodeLine{                  align: 1.0 0.0;}
\DoxyCodeLine{               \} }
\DoxyCodeLine{            \}}
\DoxyCodeLine{         \}}
\DoxyCodeLine{         part \{ name: "score2";}
\DoxyCodeLine{            type: TEXT;}
\DoxyCodeLine{            description \{ state: "default" 0.0;}
\DoxyCodeLine{               color: COLOR\_FG;}
\DoxyCodeLine{               rel1.relative: 0.5 0.0;}
\DoxyCodeLine{               rel1.offset: 20 0;}
\DoxyCodeLine{               text \{}
\DoxyCodeLine{                  text: "0";}
\DoxyCodeLine{                  font: "Sans";}
\DoxyCodeLine{                  size: 50;}
\DoxyCodeLine{                  align: 0.0 0.0;}
\DoxyCodeLine{               \} }
\DoxyCodeLine{            \}}
\DoxyCodeLine{         \}}
\DoxyCodeLine{         part \{ name: "pad1";}
\DoxyCodeLine{            type: RECT;}
\DoxyCodeLine{            description \{ state: "default" 0.0;}
\DoxyCodeLine{               color: COLOR\_FG;}
\DoxyCodeLine{               max: 10 PAD\_SIZE;}
\DoxyCodeLine{               fixed: 1 1;}
\DoxyCodeLine{               align: 0.0 0.0;}
\DoxyCodeLine{               rel1.offset: PAD\_DISTANCE 0;}
\DoxyCodeLine{            \}}
\DoxyCodeLine{         \}}
\DoxyCodeLine{         part \{ name: "pad2";}
\DoxyCodeLine{            type: RECT;}
\DoxyCodeLine{            description \{ state: "default" 0.0;}
\DoxyCodeLine{               color: COLOR\_FG;}
\DoxyCodeLine{               max: 10 PAD\_SIZE;}
\DoxyCodeLine{               fixed: 1 1;}
\DoxyCodeLine{               align: 1.0 0.0;}
\DoxyCodeLine{               rel2.offset: -\/PAD\_DISTANCE 0;}
\DoxyCodeLine{            \}}
\DoxyCodeLine{         \}}
\DoxyCodeLine{         part \{ name: "ball";}
\DoxyCodeLine{            type: RECT;}
\DoxyCodeLine{            description \{ state: "default" 0.0;}
\DoxyCodeLine{               color: COLOR\_FG;}
\DoxyCodeLine{               max: BALL\_SIZE BALL\_SIZE;}
\DoxyCodeLine{               fixed: 1 1;}
\DoxyCodeLine{               align: 0.0 0.0;}
\DoxyCodeLine{               rel1.offset: 100 100;}
\DoxyCodeLine{            \}}
\DoxyCodeLine{         \}}
\DoxyCodeLine{         \#if DEBUG\_ENABLE}
\DoxyCodeLine{         part \{ name: "dbg";}
\DoxyCodeLine{            type: TEXT;}
\DoxyCodeLine{            description \{ state: "default" 0.0;}
\DoxyCodeLine{               color: 255 255 255 200;}
\DoxyCodeLine{               text \{}
\DoxyCodeLine{                  font: "Sans";}
\DoxyCodeLine{                  size: 12;}
\DoxyCodeLine{                  align: 1.0 1.0;}
\DoxyCodeLine{               \}}
\DoxyCodeLine{            \}}
\DoxyCodeLine{         \}}
\DoxyCodeLine{         \#endif}
\DoxyCodeLine{      \}}
\DoxyCodeLine{      programs \{}
\DoxyCodeLine{         /* on load: reset the game and start the game loop */}
\DoxyCodeLine{         program \{}
\DoxyCodeLine{            signal: "load";}
\DoxyCodeLine{            source: "";}
\DoxyCodeLine{            script \{}
\DoxyCodeLine{               game\_reset();}
\DoxyCodeLine{               timer(0.1 , "game\_loop", 0);}
\DoxyCodeLine{            \}}
\DoxyCodeLine{         \}}
\DoxyCodeLine{         /* mouse left click: start the game, if not yet started */}
\DoxyCodeLine{         program \{}
\DoxyCodeLine{            signal: "mouse,down,1";}
\DoxyCodeLine{            source: "bg";}
\DoxyCodeLine{            script \{}
\DoxyCodeLine{               if (get\_float(ballspeedx) == 0.0)}
\DoxyCodeLine{                  start\_game();}
\DoxyCodeLine{            \}}
\DoxyCodeLine{         \}}
\DoxyCodeLine{         /* mouse right click: restart the game*/}
\DoxyCodeLine{         program \{}
\DoxyCodeLine{            signal: "mouse,down,3";}
\DoxyCodeLine{            source: "bg";}
\DoxyCodeLine{            script \{}
\DoxyCodeLine{               game\_reset();}
\DoxyCodeLine{            \}}
\DoxyCodeLine{         \}}
\DoxyCodeLine{      \}}
\DoxyCodeLine{   \}}
\DoxyCodeLine{\}}
\end{DoxyCodeInclude}
 