#include <TFT_eSPI.h>
#include <SPI.h>
#include <QList.h>
TFT_eSPI screen = TFT_eSPI();
class ball {
private:
float x;
float y;
int size;
int color;
int health;
float speed;
float direction;
int type;
void draw() {
screen.fillCircle(x, y, size, color);
screen.drawCircle(x, y, size + 1, TFT_BLACK);
screen.drawCircle(x, y, size + 2, TFT_BLACK);
}
void updateLoc() {
if (x < size) {
direction = (180 - direction);
}
if (x > (320 - size)) {
direction = (180 - direction);
}
if (y < size) {
direction = -direction;
}
if (y > (240 - size)) {
direction = -direction;
}
x += cos(direction * 3.1415 / 180) * speed;
y += sin(direction * 3.1415 / 180) * speed;
}
public:
ball() {}
ball(int type) {
this->type = type;
switch (type) {
case 1:
size = 5;
color = TFT_RED;
health = 10;
speed = 1.1;
break;
case 2:
size = 8;
color = TFT_GREEN;
health = 2;
speed = 0.8;
break;
case 3:
size = 3;
color = TFT_YELLOW;
health = 5;
speed = 0.5;
break;
case 4:
size = 12;
color = TFT_PINK;
health = 1;
speed = 1.3;
break;
}
direction = random(0, 369);
x = random(0, 240);
y = random(0, 160);
}
void run() {
updateLoc();
draw();
}
};
QList<ball> ballList;
void setup() {
Serial.begin(115200);
screen.init();
screen.setRotation(1);
screen.fillScreen(TFT_BLACK);
for(int i = 0; i < 5; i++){
ballList.push_back(ball(random(1,5)));
}
}
void loop() {
for(int i = 0; i < 3; i++){
ballList.at(i).run();
}
}