Paso 3: El código
Una vez que tengamos el cableado configurado, podemos usar el siguiente código para crear nuestro juego:
<br><p>#include <JUGL.h><br>#include <SPI.h> #include <Wire.h> #include <JUGL_SSD1306_128x64.h> using namespace JUGL; SSD1306_128x64 driver; const int xpin = A3; //Assign pin A3 to x const int ypin = A2; //Assign pin A2 to y int x, y, x1, y1, r, varx, vary, width, height; //Define variables int xy [2]; //Array to hold (x,y) coordinate //Declaration of functions void Circle(IScreen& scr); void move_right(IScreen& scr); void stop_right(IScreen& scr); void move_left(IScreen& scr); void stop_left(IScreen& scr); void move_up(IScreen& scr); void stop_up(IScreen& scr); void move_down(IScreen& scr); void stop_down(IScreen& scr); void setup(){ IScreen& screen = driver; //Make reference to driver screen.Begin(); //Initialize screen width = screen.GetWidth(); //Get width of screen (128) height = screen.GetHeight(); //Get height of screen (64) Circle(screen); //Draw circle } void loop(){ x1 = analogRead(xpin); //Read x analog data y1 = analogRead(ypin); //Read y analog data IScreen& screen = driver; //Make reference to driver if(x1<500){ //Check if sensor is tilted to the right move_right(screen); //Move ball right if(varx>=width-1-r ){ //Check if ball reached end of screen stop_right(screen); //Stop moving } } if(x1>520){ //Check if sensor is tilted to the left move_left(screen); //Move ball left if(varx=height-1-r){ //Check if ball reached end of screen stop_up(screen); //Stop moving } } if(y1>510){ //Check if sensor is tilted down move_down(screen); //Move ball down if(varyr){ //Check if ball is within boundaries scr.Flush(); //Display on screen } } void stop_left(IScreen& scr){ scr.Clear(); //Clear screen varx = r; //Update varx xy[0] = varx; //Store new varx value scr.FillCircle(Point(5,xy[1]),r); //Draw circle scr.Flush(); //Display on screen } void move_up(IScreen& scr){ scr.Clear(); //Clear screen vary += 10; //Move ball 10 pixels up, assign value to vary xy[1] = vary; //Store new vary value scr.FillCircle(Point(xy[0],vary),r); //Draw circle if(varyr){ //Check if ball is within boundaries scr.Flush(); //Display on screen } } void stop_down(IScreen& scr){ scr.Clear(); //Clear screen vary = r; //Update vary xy[1] = vary; //Store new vary value scr.FillCircle(Point(xy[0],5),r); //Draw circle scr.Flush(); //Display on screen }</p>