#include "bitmaps.h"
#define SPACESHIP_COUNT 10
#define ASTEROID_COUNT 4
#define OBJECTS_COUNT (SPACESHIP_COUNT + ASTEROID_COUNT + 1)
struct MySprite : Sprite {
int velX;
int velY;
bool isAlive;
bool isAsteroid;
};
struct MyScene : public Scene {
MySprite objects_[OBJECTS_COUNT];
MyScene()
: Scene(OBJECTS_COUNT, 20, DisplayController.getViewPortWidth(), DisplayController.getViewPortHeight())
{
}
void startSprite(MySprite * sprite)
{
int q = random(4);
int x = (q == 0 ? random(getWidth()) : (q == 1 ? getWidth() - sprite->getWidth() : (q == 2 ? random(getWidth()) : 0)));
int y = (q == 0 ? 0 : (q == 1 ? random(getHeight()) : (q == 2 ? getHeight() - sprite->getHeight() : random(getHeight()))));
sprite->moveTo(x, y);
sprite->visible = true;
sprite->velX = - sprite->getWidth() / 4 + random(sprite->getWidth() / 2);
sprite->velY = - sprite->getHeight() / 4 + random(sprite->getHeight() / 2);
sprite->isAlive = true;
}
void init()
{
paintSpace();
for (int i = 0; i < OBJECTS_COUNT; ++i) {
MySprite * sprite = &objects_[i];
if (i == 0) {
sprite->addBitmap(&jupiter);
sprite->moveTo(0, getHeight() / 2);
} else if (i <= SPACESHIP_COUNT) {
sprite->addBitmap(&spaceship)->addBitmap(explosion, 8);
sprite->isAsteroid = false;
addSprite(sprite);
startSprite(sprite);
} else {
sprite->addBitmap(&asteroid);
sprite->isAsteroid = true;
addSprite(sprite);
startSprite(sprite);
}
}
DisplayController.
setSprites(objects_, OBJECTS_COUNT);
}
void update(int updateCount)
{
for (int i = 1; i < OBJECTS_COUNT; ++i) {
MySprite * sprite = &objects_[i];
if (sprite->isAlive) {
updateSpriteAndDetectCollisions(sprite);
} else if ((updateCount % 8) == 0) {
sprite->nextFrame();
if (sprite->currentFrame == 0)
startSprite(sprite);
}
}
if ((updateCount % 20) == 0)
}
void collisionDetected(Sprite * spriteA, Sprite * spriteB, Point collisionPoint)
{
MySprite * a = (MySprite*) spriteA;
MySprite * b = (MySprite*) spriteB;
if (a->isAsteroid && b->isAsteroid)
return;
a->isAlive = b->isAlive = false;
if (a->isAsteroid)
a->visible = false;
else
a->nextFrame();
if (b->isAsteroid)
b->visible = false;
else
b->nextFrame();
}
void paintSpace()
{
Canvas cv(&DisplayController);
cv.clear();
cv.setPenColor(64, 64, 64);
for (int i = 0; i < 400; ++i)
cv.setPixel(random(getWidth()), random(getHeight()));
cv.setPenColor(128, 128, 128);
for (int i = 0; i < 50; ++i)
cv.setPixel(random(getWidth()), random(getHeight()));
cv.drawBitmap(80, 35, &galaxy);
cv.drawBitmap(220, 130, &galaxy);
}
};
void setup()
{
Serial.begin(115200);
DisplayController.begin();
DisplayController.moveScreen(-8, 0);
}
void loop()
{
MyScene scene;
scene.start();
vTaskSuspend(nullptr);
}
int getViewPortHeight()
Determines vertical size of the viewport.
int getViewPortWidth()
Determines horizontal size of the viewport.
void setSprites(T *sprites, int count)
Sets the list of active sprites.
void refreshSprites()
Forces the sprites to be updated.
Represents the VGA bitmapped controller.
This file is the all in one include file. Application can just include this file to use FabGL library...