-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathbounceballs.cpp
100 lines (87 loc) · 2.31 KB
/
bounceballs.cpp
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
#include <library.h>
class Ball
{
int x;
int y;
int vx;
int vy;
uint16_t gradient[25];
CRect backRect;
public:
Ball()
{
x = (random()%(BIOS::LCD::Width-40))+20;
y = (random()%(BIOS::LCD::Height-40))+20;
vx = (random() & 3)+1;
vy = (random() & 3)+1;
for (int i=0; i<COUNT(gradient); i++)
{
switch (x%3)
{
case 0:
gradient[i] = RGB565RGB(50-i*2, 120-i*5, 255);
break;
case 1:
gradient[i] = RGB565RGB(50-i*2, 255, 120-i*5);
break;
case 2:
gradient[i] = RGB565RGB(255, 50-i*2, 120-i*5);
break;
}
}
}
void Draw()
{
static constexpr int curve[11] = {10, 10, 10, 9, 9, 8, 8, 7, 6, 4, 0};
backRect = CRect(x-10, y-10, x+11, y+11);
const int r = 10;
for (int _y=-10; _y<=10; _y++)
{
int px = curve[abs(_y)];
int ofsx = 10-px;
BIOS::LCD::BufferBegin(CRect(x-10+ofsx, y+_y, x+21, y+_y+1));
BIOS::LCD::BufferWrite(gradient+(_y+10)/2, px*2);
BIOS::LCD::BufferEnd();
}
}
void Hide()
{
BIOS::LCD::Bar(backRect, RGB565(404040));
}
void Move()
{
x += vx;
y += vy;
if (x + vx + 10 >= BIOS::LCD::Width-1 || x + vx <= 10)
{
vx = -vx;
BIOS::SYS::Beep(5);
}
if (y + vy + 10 > BIOS::LCD::Height-16 || y + vy <= 10+16)
{
vy = -vy;
BIOS::SYS::Beep(5);
}
}
};
Ball ballsData[16];
CArray<Ball> balls(ballsData, COUNT(ballsData));
int main(void)
{
balls.SetSize(balls.GetMaxSize());
while (!BIOS::KEY::GetKey())
{
for (int i=0; i<balls.GetSize(); i++)
balls[i].Draw();
BIOS::SYS::DelayMs(10);
for (int i=0; i<balls.GetSize(); i++)
balls[i].Move();
for (int i=0; i<balls.GetSize(); i++)
balls[i].Hide();
}
return 72;
}
void _HandleAssertion(const char* file, int line, const char* cond)
{
BIOS::DBG::Print("Assertion failed in %s [%d]: %s\n", file, line, cond);
}