aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authormrfaptastic <12006953+mrfaptastic@users.noreply.github.com>2021-10-15 17:06:58 +0100
committerGitHub <noreply@github.com>2021-10-15 17:06:58 +0100
commitfaccc17bbb10f95594a88f0a2b42d6cf89ebd0af (patch)
treee6317de69dd2ce0db903f25001bb6b6e696c1aa3 /examples
parentcf66382d75d5fb9b9a6f890d0d92c5d09c0fbc96 (diff)
parent5ab429152e5b1819e3605e39cbdd489af892e789 (diff)
Merge pull request #199 from mcauser/bouncing-squares
BouncingSquares were going offscreen
Diffstat (limited to 'examples')
-rw-r--r--examples/BouncingSquares/BouncingSquares.ino51
1 files changed, 29 insertions, 22 deletions
diff --git a/examples/BouncingSquares/BouncingSquares.ino b/examples/BouncingSquares/BouncingSquares.ino
index 09d3ddd..5ea09b1 100644
--- a/examples/BouncingSquares/BouncingSquares.ino
+++ b/examples/BouncingSquares/BouncingSquares.ino
@@ -8,14 +8,14 @@ uint16_t myRED = display->color565(255, 0, 0);
uint16_t myGREEN = display->color565(0, 255, 0);
uint16_t myBLUE = display->color565(0, 0, 255);
-uint16_t colours[5] = { myDARK, myWHITE, myRED, myGREEN, myBLUE};
+uint16_t colours[5] = { myDARK, myWHITE, myRED, myGREEN, myBLUE };
struct Square
{
float xpos, ypos;
float velocityx;
float velocityy;
- boolean xdir, ydir;
+ boolean xdir, ydir;
uint16_t square_size;
uint16_t colour;
};
@@ -23,10 +23,10 @@ struct Square
const int numSquares = 25;
Square Squares[numSquares];
-void setup()
+void setup()
{
// put your setup code here, to run once:
- delay(1000);
+ delay(1000);
Serial.begin(115200);
delay(200);
@@ -36,42 +36,49 @@ void setup()
mxconfig.clkphase = false;
// OK, now we can create our matrix object
- display = new MatrixPanel_I2S_DMA(mxconfig);
+ display = new MatrixPanel_I2S_DMA(mxconfig);
display->begin(); // setup display with pins as pre-defined in the library
// Create some Squares
for (int i = 0; i < numSquares; i++)
{
- Squares[i].xpos = random(0, display->width());
- Squares[i].ypos = random(0, display->height());
+ Squares[i].square_size = random(2,10);
+ Squares[i].xpos = random(0, display->width() - Squares[i].square_size);
+ Squares[i].ypos = random(0, display->height() - Squares[i].square_size);
Squares[i].velocityx = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
- Squares[i].velocityy = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
+ Squares[i].velocityy = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
//Squares[i].xdir = (random(2) == 1) ? true:false;
- //Squares[i].ydir = (random(2) == 1) ? true:false;
- Squares[i].square_size = random(2,10);
+ //Squares[i].ydir = (random(2) == 1) ? true:false;
int random_num = random(6);
- Squares[i].colour = colours[random_num];
+ Squares[i].colour = colours[random_num];
}
-
}
-void loop()
-{
- display->flipDMABuffer(); // not used if double buffering isn't enabled
- delay(25);
- display->clearScreen();
+void loop()
+{
+ display->flipDMABuffer(); // not used if double buffering isn't enabled
+ delay(25);
+ display->clearScreen();
for (int i = 0; i < numSquares; i++)
{
// Draw rect and then calculatae
display->fillRect(Squares[i].xpos, Squares[i].ypos, Squares[i].square_size, Squares[i].square_size, Squares[i].colour);
- if (Squares[i].xpos >= display->width()) { Squares[i].velocityx *= -1; } else if (Squares[i].xpos <= 0) { Squares[i].velocityx = abs (Squares[i].velocityx); }
- if (Squares[i].ypos >= display->height()) { Squares[i].velocityy *= -1; } else if (Squares[i].ypos <= 0) { Squares[i].velocityy = abs (Squares[i].velocityy); }
+ if (Squares[i].square_size + Squares[i].xpos >= display->width()) {
+ Squares[i].velocityx *= -1;
+ } else if (Squares[i].xpos <= 0) {
+ Squares[i].velocityx = abs (Squares[i].velocityx);
+ }
+
+ if (Squares[i].square_size + Squares[i].ypos >= display->height()) {
+ Squares[i].velocityy *= -1;
+ } else if (Squares[i].ypos <= 0) {
+ Squares[i].velocityy = abs (Squares[i].velocityy);
+ }
- Squares[i].xpos += Squares[i].velocityx ;
- Squares[i].ypos += Squares[i].velocityy ;
+ Squares[i].xpos += Squares[i].velocityx;
+ Squares[i].ypos += Squares[i].velocityy;
}
-
}