// Count elements in array
#define COUNT_OF(x) ((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x])))))
#include <Arduboy2.h>
// make an instance of arduboy used for many functions
Arduboy2 arduboy;
class ball{
public:
int angle = 0;
float x = 64;
float y = 32;
float spd = 0;
int radius = 5;
};
long prevMillis;
long lastPressedButton;
ball *balls;
int ballCount = 3;
// This function runs once in your game.
// use it for anything that needs to be set only once in your game.
void setup() {
randomSeed(analogRead(A3));
balls = new ball[ ballCount ];
for(int i=0; i<ballCount; i++){
balls[i].angle = random(0,360);
balls[i].spd = random(1,5);
}
// initiate arduboy instance
arduboy.begin();
// here we set the framerate to 15, we do not need to run at
// default 60 and it saves us battery life
arduboy.setFrameRate(60);
}
// our main game loop, this runs once every cycle/frame.
// this is where our game logic goes.
void loop() {
if (millis() > prevMillis+1000/60){
for(int i=0; i<ballCount; i++){
balls[i].x = round(balls[i].x + cos(balls[i].angle*PI/180)*balls[i].spd);
balls[i].y = round(balls[i].y + sin(balls[i].angle*PI/180)*balls[i].spd);
if(balls[i].x - balls[i].radius < 0 || balls[i].x + balls[i].radius > 128){
balls[i].angle = 180-balls[i].angle;
}
if (balls[i].y - balls[i].radius < 0 || balls[i].y + balls[i].radius > 64){
balls[i].angle *= -1;
}
balls[i].angle %= 360;
}
prevMillis = millis();
}
// pause render until it's time for the next frame
if (!(arduboy.nextFrame()))
return;
// first we clear our screen to black
arduboy.clear();
if(millis() > lastPressedButton + 50){
if(arduboy.pressed(A_BUTTON)){
delete [] balls;
ballCount += 1;
balls = new ball[ballCount];
for(int i=0; i<ballCount; i++){
balls[i].x = 64;
balls[i].y = 32;
balls[i].angle = random(0,360);
balls[i].spd = random(1,5);
}
}
if(arduboy.pressed(B_BUTTON) ){
if(ballCount > 0){
ballCount -= 1;
}
}
lastPressedButton = millis();
}
for(int i=0; i<ballCount; i++){
arduboy.drawCircle(balls[i].x,balls[i].y,balls[i].radius,WHITE);
}
arduboy.setCursor(4, 64-8);
arduboy.print(F("Ball Count: "));
arduboy.setCursor(4+64+4, 64-8);
arduboy.print(ballCount);
arduboy.setCursor(4+64+4+8+4, 64-8);
arduboy.print("CPU: ");
arduboy.setCursor(4+64+4+8+24+4, 64-8);
arduboy.print(arduboy.cpuLoad());
// then we finaly we tell the arduboy to display what we just wrote to the display
arduboy.display();
}