forked from alexxsxnchez/BrickBreaker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
brick.ino
83 lines (79 loc) · 2.24 KB
/
brick.ino
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
static brick bricks[ROWS][COLS] = {0};
static int brickCount = 0;
void initBricks() {
for(int i = 0; i < ROWS; i++) {
for(int j = 0; j < COLS; j++) {
brick brick;
// random brick generation
randomSeed(analogRead(0));
int r = random(4);
if(r == 0) {
brick.hp = 0;
}
else {
brickCount++;
if(r == 1) {
brick.hp = MAX_HP;
}
else {
brick.hp = 1;
}
}
brick.x = j * (BRICK_WIDTH + 1);
brick.y = OLED_WIDTH - 1 - i * (BRICK_HEIGHT + 1);
bricks[i][j] = brick;
}
}
}
void drawBricks() {
for(int i = 0; i < ROWS; i++) {
for(int j = 0; j < COLS; j++) {
drawBrick(&bricks[i][j]);
}
}
}
// draw a single brick
void drawBrick(brick *brick) {
OrbitOledMoveTo(brick -> y, brick -> x);
int hp = brick -> hp;
int pattern;
switch(hp) {
case 0:
pattern = iptnBlank;
break;
case 2:
pattern = iptnSolid;
break;
default:
pattern = iptnCross;
}
OrbitOledSetFillPattern(OrbitOledGetStdPattern(pattern));
OrbitOledFillRect(brick -> y - BRICK_HEIGHT + 1, brick -> x + BRICK_WIDTH - 1);
}
brick *getBrickAt(int xCoor, int yCoor) {
int x = xCoor / (BRICK_WIDTH + 1);
int y = (OLED_WIDTH - 1 - yCoor) / (BRICK_HEIGHT + 1);
if(x >= COLS || y >= ROWS || bricks[y][x].hp <= 0) {
return NULL;
}
return &bricks[y][x];
}
void lowerBrickHealth(brick *brick) {
if(brick != NULL) {
brick -> hp--;
if(brick -> hp <= 0) {
brickCount--;
uint32_t currentBricksBroken = getValue(BRICKS_BROKEN_ADDRESS);
currentBricksBroken++;
updateValue(currentBricksBroken, BRICKS_BROKEN_ADDRESS); // update the stats stored in the EEPROM
}
randomSeed(analogRead(0));
int r = random(5);
if(r == 0 && !lifeAvailable()) { // 20% chance of generating an extra life
initLife(brick -> x, brick -> y);
}
}
}
int getBrickCount() {
return brickCount;
}