1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
25
26
27
28
29
30
31
32
33
34
35
36
37
42
43
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
174
179
180
181
193
194
195
205
206
207
215
216
217
218
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
252
256
257
258
259
260
261
265
269
270
271
272
273
274
275
276
277
282
283
295
296
297
298
299
300
301
302
303
304
305
306
307
/* ... */
#include <gui/game2D_screen/Game2DView.hpp>
#include "BitmapDatabase.hpp"
#include <texts/TextKeysAndLanguages.hpp>
#include <touchgfx/Color.hpp>
#include <touchgfx/EasingEquations.hpp>
#include <gui/common/Utils.hpp>
6 includes
#if defined(ST) && !defined(SIMULATOR)
extern volatile bool disableChromArt;
#endif
Game2DView::Game2DView() :
DemoView(),
parallaxBackground(640, 290),
chromArtOff(false),
velocity(-11),
collectibleItemCatched(0),
collectibleItemMissed(0),
playerDeadCounter(0),
onButtonPressed(this, &Game2DView::buttonPressedhandler)
{
playerCharacter.setBitmaps(BITMAP_SPRITE_00_ID, BITMAP_SPRITE_09_ID);
playerCharacter.setUpdateTicksInterval(3);
}{ ... }
Game2DView::~Game2DView()
{
}{ ... }
void Game2DView::setupScreen()
{
background.setPosition(0, 0, getActiveDisplayWidth(), getActiveDisplayHeight());
background.setBitmap(Bitmap(BITMAP_GAME_2D_FRAME_BACKGROUND_ID));
add(background);
flyButton.setBitmaps(Bitmap(BITMAP_GAME2D_FLY_BUTTON_ID), Bitmap(BITMAP_GAME2D_FLY_BUTTON_PRESSED_ID));
flyButton.setXY(80, 409);
flyButton.setAction(onButtonPressed);
add(flyButton);
#if defined(ST)
updateChromArtButtonBitmaps();
chromArtOnOffButton.setXY(160, 409);
chromArtOnOffButton.setAction(onButtonPressed);
add(chromArtOnOffButton);
chromArtHeader.setTypedText(TypedText(T_GAME_2D_CHROM_ART_HEADER));
chromArtHeader.setXY(chromArtOnOffButton.getRect().right() + 6, chromArtOnOffButton.getY() + 10);
chromArtHeader.setColor(Color::getColorFrom24BitRGB(0x7B, 0xAF, 0xB6));
add(chromArtHeader);
chromArtDescription.setTypedText(TypedText(T_GAME_2D_CHROM_ART_DESCRIPTION));
chromArtDescription.setXY(chromArtHeader.getX(), chromArtHeader.getY() + 24);
chromArtDescription.setColor(Color::getColorFrom24BitRGB(0x7B, 0xAF, 0xB6));
add(chromArtDescription);
/* ... */
#endif
gotoMenuButton.setBitmaps(Bitmap(BITMAP_GAME2D_MENU_BUTTON_ID), Bitmap(BITMAP_GAME2D_MENU_BUTTON_PRESSED_ID));
gotoMenuButton.setXY(20, 20);
add(gotoMenuButton);
mcuLoadTxt.setXY(676, 13);
mcuLoadTxt.setColor(Color::getColorFrom24BitRGB(0x18, 0x28, 0x28));
mcuLoadValueTxt.setTypedText(TypedText(T_DEMO_VIEW_MCU_LOAD_VALUE_CENTER));
mcuLoadValueTxt.setPosition(mcuLoadTxt.getX(), mcuLoadTxt.getY()+25, mcuLoadTxt.getWidth(), 30);
mcuLoadValueTxt.setColor(mcuLoadTxt.getColor());
add(mcuLoadTxt);
add(mcuLoadValueTxt);
bullet.setBitmaps(BITMAP_SPHERE1_ID, BITMAP_SPHERE1_ID);
bullet.stopAnimation();
parallaxBackground.addCollectibleItem(bullet);
playerCharacterStartX = parallaxBackground.getX() + 36;
playerCharacterStartY = parallaxBackground.getRect().bottom() - playerCharacter.getHeight() - 14;
playerCharacterMaxY = parallaxBackground.getY() + 30;
parallaxBackground.setXY(0, 0);
parallaxBackground.addPlayerCharacter(playerCharacter);
for (int i = 0; i < NUMBER_OF_COLLECTIBLE_ITEMS; i++)
{
collectibleItem[i].setX(parallaxBackground.getRect().right());
collectibleItem[i].stopAnimation();
parallaxBackground.addCollectibleItem(collectibleItem[i]);
}for (int i = 0; i < NUMBER_OF_COLLECTIBLE_ITEMS; i++) { ... }
touchArea.setPosition(0,0,parallaxBackground.getWidth(),parallaxBackground.getHeight());
gameArea.add(parallaxBackground);
gameArea.add(touchArea);
gameArea.setPosition(80, 115, parallaxBackground.getWidth(), parallaxBackground.getHeight());
add(gameArea);
scoreText.setTypedText(TypedText(T_GAME_2D_SCORE_TEXT));
scoreText.setPosition(640, flyButton.getY()+10, 80, 64);
scoreText.setColor(Color::getColorFrom24BitRGB(0x7B, 0xAF, 0xB6));
add(scoreText);
scoreValue.setWildcard(scoreValueBuf);
scoreValue.setTypedText(TypedText(T_GAME_2D_SCORE_VALUE));
scoreValue.setPosition(640, 440, 80, 64);
scoreValue.setColor(Color::getColorFrom24BitRGB(248,160,0));
add(scoreValue);
updateScoreText();
parallaxBackground.startAnimation();
#if defined(ST) && !defined(SIMULATOR)
disableChromArt = chromArtOff;
#endif
restartGame();
}{ ... }
void Game2DView::tearDownScreen()
{
#if defined(ST) && !defined(SIMULATOR)
disableChromArt = false;
#endif
}{ ... }
void Game2DView::buttonPressedhandler(const AbstractButton& button)
{
if (&button == &chromArtOnOffButton)
{
chromArtOff = !chromArtOff;
#if defined(ST) && !defined(SIMULATOR)
disableChromArt = chromArtOff;
#endif
updateChromArtButtonBitmaps();
}if (&button == &chromArtOnOffButton) { ... }
}{ ... }
void Game2DView::handleTickEvent()
{
for (int i = 0; i < NUMBER_OF_COLLECTIBLE_ITEMS; i++)
{
if (collectibleItem[i].getRect().right() < 0)
{
collectibleItem[i].setXY(parallaxBackground.getRect().right(), parallaxBackground.getY() + 50 + Utils::randomNumberBetween(0, 180));
collectibleItem[i].stopAnimation();
if (playerDeadCounter == 0)
{
collectibleItemMissed++;
updateScoreText();
}if (playerDeadCounter == 0) { ... }
}if (collectibleItem[i].getRect().right() < 0) { ... }
}for (int i = 0; i < NUMBER_OF_COLLECTIBLE_ITEMS; i++) { ... }
if (bullet.getRect().right() < 0)
{
bullet.setXY(parallaxBackground.getRect().right(), parallaxBackground.getY() + 50 + Utils::randomNumberBetween(0, 180));
bullet.stopAnimation();
}if (bullet.getRect().right() < 0) { ... }
if (Utils::randomNumberBetween(0, 100) < 8)
{
for (int i = 0; i < NUMBER_OF_COLLECTIBLE_ITEMS; i++)
{
if (!collectibleItem[i].isRunning())
{
collectibleItem[i].setXY(parallaxBackground.getRect().right(), parallaxBackground.getY() + 50 + Utils::randomNumberBetween(0, 180));
collectibleItem[i].startAnimation(Utils::randomNumberBetween(4, 14));
break;
}if (!collectibleItem[i].isRunning()) { ... }
}for (int i = 0; i < NUMBER_OF_COLLECTIBLE_ITEMS; i++) { ... }
}if (Utils::randomNumberBetween(0, 100) < 8) { ... }
if (playerDeadCounter > 0)
{
playerDeadCounter++;
if (playerDeadCounter > 100)
{
restartGame();
}if (playerDeadCounter > 100) { ... }
return;
}if (playerDeadCounter > 0) { ... }
if (Utils::randomNumberBetween(0, 100) < 3)
{
if (!bullet.isRunning())
{
bullet.setXY(parallaxBackground.getRect().right(), parallaxBackground.getY() + 50 + Utils::randomNumberBetween(0, 180));
bullet.startAnimation(Utils::randomNumberBetween(14, 20));
}if (!bullet.isRunning()) { ... }
}if (Utils::randomNumberBetween(0, 100) < 3) { ... }
Rect collisionArea(playerCharacter.getRect().x, playerCharacter.getRect().y + 10, playerCharacter.getRect().width - 20, playerCharacter.getRect().height - 10);
for (int i = 0; i < NUMBER_OF_COLLECTIBLE_ITEMS; i++)
{
if (collectibleItem[i].getRect().intersect(collisionArea))
{
collectibleItem[i].setX(parallaxBackground.getRect().right());
collectibleItem[i].stopAnimation();
collectibleItemCatched++;
updateScoreText();
}if (collectibleItem[i].getRect().intersect(collisionArea)) { ... }
}for (int i = 0; i < NUMBER_OF_COLLECTIBLE_ITEMS; i++) { ... }
if (bullet.getRect().intersect(collisionArea))
{
playerCharacter.stopAnimation();
playerCharacter.startMoveAnimation(playerCharacter.getX() - 160, playerCharacter.getY() + 100, 40, EasingEquations::quadEaseOut, EasingEquations::quadEaseIn);
playerDeadCounter = 1;
bullet.setX(-bullet.getWidth());
bullet.invalidate();
}if (bullet.getRect().intersect(collisionArea)) { ... }
else
{
if (playerCharacter.getX() == playerCharacterStartX)
{
if (flyButton.getPressedState() || touchArea.getPressedState())
{
velocity += 3;
velocity = (velocity > 7) ? 7 : velocity;
}if (flyButton.getPressedState() || touchArea.getPressedState()) { ... }
else if (velocity > -10)
{
velocity -= 2;
}else if (velocity > -10) { ... }
if (velocity != 0)
{
int newY = playerCharacter.getY() - velocity;
if (newY < playerCharacterMaxY)
{
newY = playerCharacterMaxY;
}if (newY < playerCharacterMaxY) { ... }
else if (newY > playerCharacterStartY)
{
newY = playerCharacterStartY;
}else if (newY > playerCharacterStartY) { ... }
playerCharacter.moveTo(playerCharacter.getX(), newY);
}if (velocity != 0) { ... }
}if (playerCharacter.getX() == playerCharacterStartX) { ... }
}else { ... }
}{ ... }
void Game2DView::updateScoreText()
{
Unicode::snprintf(scoreValueBuf, 16, "%i\0", (collectibleItemCatched * 2) - collectibleItemMissed);
scoreValue.invalidate();
}{ ... }
void Game2DView::updateChromArtButtonBitmaps()
{
if (chromArtOff)
{
chromArtOnOffButton.setBitmaps(Bitmap(BITMAP_GAME2D_ST_BUTTON_OFF_ID), Bitmap(BITMAP_GAME2D_ST_BUTTON_OFF_PRESSED_ID));
}if (chromArtOff) { ... }
else
{
chromArtOnOffButton.setBitmaps(Bitmap(BITMAP_GAME2D_ST_BUTTON_ON_ID), Bitmap(BITMAP_GAME2D_ST_BUTTON_PRESSED_ID));
}else { ... }
chromArtOnOffButton.invalidate();
}{ ... }
void Game2DView::restartGame()
{
playerDeadCounter = 0;
collectibleItemCatched = 0;
collectibleItemMissed = 0;
playerCharacter.setXY(-playerCharacter.getWidth(), playerCharacterStartY);
playerCharacter.startMoveAnimation(playerCharacterStartX, playerCharacterStartY, 32, EasingEquations::cubicEaseOut);
playerCharacter.startAnimation(false, true, true);
playerCharacter.invalidate();
updateScoreText();
}{ ... }