Ten spaceships and four asteroids fly across a starfield at 320x200 (64 colors) while Jupiter slowly crosses the background. When a spaceship hits another spaceship or an asteroid, the spaceship plays an explosion animation and the asteroid disappears; after the explosion both objects come back from a random screen side. Asteroids do not collide with each other.
The demo runs on its own: there are no controls.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27#include "esp_random.h"
29
30
31
32static inline int randomInt(int max) { return max > 0 ? (int) (esp_random() % (uint32_t) max) : 0; }
33
34static inline int randomInt(int min, int max) { return max > min ? min + (int) (esp_random() % (uint32_t) (max - min)) : min; }
35
36
37#include "bitmaps.h"
38
39
40
42
43
44#define SPACESHIP_COUNT 10
45#define ASTEROID_COUNT 4
46
47#define OBJECTS_COUNT (SPACESHIP_COUNT + ASTEROID_COUNT + 1)
48
49
51 int velX;
52 int velY;
53 bool isAlive;
54 bool isAsteroid;
55};
56
57
58struct MyScene :
public Scene {
59
60 MySprite objects_[OBJECTS_COUNT];
61
62 MyScene()
63 :
Scene(OBJECTS_COUNT, 20, DisplayController.getViewPortWidth(), DisplayController.getViewPortHeight())
64 {
65 }
66
67 void startSprite(MySprite * sprite)
68 {
69
70 int q = randomInt(4);
71 int x = (q == 0 ? randomInt(getWidth()) : (q == 1 ? getWidth() - sprite->getWidth() : (q == 2 ? randomInt(getWidth()) : 0)));
72 int y = (q == 0 ? 0 : (q == 1 ? randomInt(getHeight()) : (q == 2 ? getHeight() - sprite->getHeight() : randomInt(getHeight()))));
73 sprite->moveTo(x, y);
74 sprite->visible = true;
75 sprite->velX = - sprite->getWidth() / 4 + randomInt(sprite->getWidth() / 2);
76 sprite->velY = - sprite->getHeight() / 4 + randomInt(sprite->getHeight() / 2);
77 sprite->isAlive = true;
78 }
79
80 void init()
81 {
82 paintSpace();
83
84
85 for (int i = 0; i < OBJECTS_COUNT; ++i) {
86 MySprite * sprite = &objects_[i];
87 if (i == 0) {
88
89 sprite->addBitmap(&jupiter);
90 sprite->moveTo(0, getHeight() / 2);
91 } else if (i <= SPACESHIP_COUNT) {
92
93 sprite->addBitmap(&spaceship)->addBitmap(explosion, 8);
94 sprite->isAsteroid = false;
95 addSprite(sprite);
96 startSprite(sprite);
97 } else {
98
99 sprite->addBitmap(&asteroid);
100 sprite->isAsteroid = true;
101 addSprite(sprite);
102 startSprite(sprite);
103 }
104 }
105
106 DisplayController.
setSprites(objects_, OBJECTS_COUNT);
107 }
108
109 void update(int updateCount)
110 {
111 for (int i = 1; i < OBJECTS_COUNT; ++i) {
112
113 MySprite * sprite = &objects_[i];
114
115
116 if (sprite->isAlive) {
117
118
120
121
122 updateSpriteAndDetectCollisions(sprite);
123
124 } else if ((updateCount % 8) == 0) {
125
126
127 sprite->nextFrame();
128 if (sprite->currentFrame == 0)
129 startSprite(sprite);
130
131 }
132 }
133
134
135 if ((updateCount % 20) == 0)
137
139 }
140
141 void collisionDetected(Sprite * spriteA, Sprite * spriteB, Point collisionPoint)
142 {
143 MySprite * a = (MySprite*) spriteA;
144 MySprite * b = (MySprite*) spriteB;
145
146 if (a->isAsteroid && b->isAsteroid)
147 return;
148
149 a->isAlive = b->isAlive = false;
150
151 if (a->isAsteroid)
152 a->visible = false;
153 else
154 a->nextFrame();
155 if (b->isAsteroid)
156 b->visible = false;
157 else
158 b->nextFrame();
159 }
160
161 void paintSpace()
162 {
163 Canvas cv(&DisplayController);
164 cv.setBrushColor(Color::Black);
165 cv.clear();
166 cv.setPenColor(Color::White);
167 cv.setBrushColor(Color::White);
168
169 cv.setPenColor(64, 64, 64);
170 for (int i = 0; i < 400; ++i)
171 cv.setPixel(randomInt(getWidth()), randomInt(getHeight()));
172
173 cv.setPenColor(128, 128, 128);
174 for (int i = 0; i < 50; ++i)
175 cv.setPixel(randomInt(getWidth()), randomInt(getHeight()));
176
177 cv.drawBitmap(80, 35, &galaxy);
178 cv.drawBitmap(220, 130, &galaxy);
179 }
180
181};
182
183
184void setup()
185{
186
187
188 DisplayController.
begin();
190
192}
193
194
195void loop()
196{
197 MyScene scene;
198 scene.start();
199 vTaskSuspend(nullptr);
200}
201
202
203extern "C" void app_main()
204{
205 setup();
206 while (true)
207 loop();
208}
Scene is an abstract class that encapsulates the functionality of a scene (sprites,...
int getViewPortHeight()
Determines the height of the viewport.
int getViewPortWidth()
Determines the width of the viewport.
void setSprites(T *sprites, int count)
Sets the list of active sprites.
void refreshSprites()
Forces the sprites to be updated.
void begin(gpio_num_t redGPIO, gpio_num_t greenGPIO, gpio_num_t blueGPIO, gpio_num_t HSyncGPIO, gpio_num_t VSyncGPIO)
Initializer for 8 colors (5 GPIOs).
void moveScreen(int offsetX, int offsetY)
Moves the screen by the specified horizontal and vertical offsets.
VGA bitmapped display controller (64 colors).
Single include file of the FabGL library.