Merge pull request #199 from mcauser/bouncing-squares
BouncingSquares were going offscreen
This commit is contained in:
commit
faccc17bbb
1 changed files with 30 additions and 23 deletions
|
@ -42,18 +42,17 @@ void setup()
|
||||||
// Create some Squares
|
// Create some Squares
|
||||||
for (int i = 0; i < numSquares; i++)
|
for (int i = 0; i < numSquares; i++)
|
||||||
{
|
{
|
||||||
Squares[i].xpos = random(0, display->width());
|
Squares[i].square_size = random(2,10);
|
||||||
Squares[i].ypos = random(0, display->height());
|
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].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].xdir = (random(2) == 1) ? true:false;
|
||||||
//Squares[i].ydir = (random(2) == 1) ? true:false;
|
//Squares[i].ydir = (random(2) == 1) ? true:false;
|
||||||
Squares[i].square_size = random(2,10);
|
|
||||||
|
|
||||||
int random_num = random(6);
|
int random_num = random(6);
|
||||||
Squares[i].colour = colours[random_num];
|
Squares[i].colour = colours[random_num];
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop()
|
void loop()
|
||||||
|
@ -67,11 +66,19 @@ void loop()
|
||||||
// Draw rect and then calculatae
|
// Draw rect and then calculatae
|
||||||
display->fillRect(Squares[i].xpos, Squares[i].ypos, Squares[i].square_size, Squares[i].square_size, Squares[i].colour);
|
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].square_size + Squares[i].xpos >= display->width()) {
|
||||||
if (Squares[i].ypos >= display->height()) { Squares[i].velocityy *= -1; } else if (Squares[i].ypos <= 0) { Squares[i].velocityy = abs (Squares[i].velocityy); }
|
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].xpos += Squares[i].velocityx;
|
||||||
Squares[i].ypos += Squares[i].velocityy;
|
Squares[i].ypos += Squares[i].velocityy;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue