forked from slawa19/IKEA-LED-Table
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbricks.ino
178 lines (161 loc) · 4.52 KB
/
bricks.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/* LedTable
*
* Written by: Ing. David Hrbaty
*
*
* Main code for Bricks game
*/
#include "bricksCommon.h"
void bricksInit(){
scorePlayer = 0;
maxAttempt = 0;
ballY = 6;
ballX = 10;
for (int i=0; i<numBlocks; i++){
bricks[i][0] = 1;
}
}
void runBricks(){
bricksInit();
unsigned long prevUpdateTime = 0;
boolean bricksRunning = true;
while(bricksRunning){
if (scorePlayer == MAX_SCORE || maxAttempt == MAX_ATTEMPT){
bricksRunning = false;
break;
}
checkBallHitByPaddle();
checkBlockCollision();
ballX += xincrement;
ballY += yincrement;
checkBallOutOfBoundsTable();
clearTablePixels();
// Draw ball
setTablePixel(ballX,ballY,WHITE);
// Draw player paddle
for (int x=positionPlayer-PLAYER_HEIGHT/2; x<=positionPlayer+PLAYER_HEIGHT/2; ++x){
setTablePixel(x, FIELD_WIDTH-1, BLUE);
}
// Draw bricks
for (int i=0; i<numBlocks; i++){
if(bricks[i][0] == 1) {
setTablePixel(bricks[i][1],bricks[i][2], WHITE);
}
}
showPixels();
unsigned long curTime;
boolean dirChanged = false;
do{
readInput();
if (curControl == BTN_EXIT){
bricksRunning = false;
break;
}
if (curControl != BTN_NONE && !dirChanged){//Can only change direction once per loop
dirChanged = true;
setPositionPaddle();
}
curTime = millis();
}
while ((curTime - prevUpdateTime) <250);//Once enough time has passed, proceed. The lower this number, the faster the game is
prevUpdateTime = curTime;
}
fadeOut();
char buf[4];
int len = sprintf(buf, "%i", scorePlayer);
scrollTextBlocked(buf,len,WHITE);
}
void setPositionPaddle(){
switch(curControl){
case BTN_RIGHT:
if(positionPlayer + (PLAYER_HEIGHT-1) / 2 < FIELD_WIDTH-1){
++positionPlayer;
}
break;
case BTN_LEFT:
if(positionPlayer - PLAYER_HEIGHT / 2 > 0) {
--positionPlayer;
}
break;
case BTN_START:
break;
case BTN_UP:
break;
case BTN_DOWN:
break;
}
}
void checkBallHitByPaddle() {
if(ballY == FIELD_HEIGHT-2)
{
if(ballX == positionPlayer)
{
yincrement = -1;
ballY = FIELD_WIDTH-2;
}
else if(ballX < positionPlayer && ballX >= positionPlayer - PLAYER_HEIGHT / 2)
{
yincrement = -1;
xincrement = max(-1,xincrement-1);
ballY = FIELD_WIDTH-2;
ballX = positionPlayer - PLAYER_HEIGHT / 2-1;
}
else if(ballX > positionPlayer && ballX <= positionPlayer + (PLAYER_HEIGHT-1) / 2)
{
yincrement = -1;
xincrement = min(1,xincrement+1);
ballY = FIELD_WIDTH-2;
ballX = positionPlayer + (PLAYER_HEIGHT-1) / 2+1;
}
}
}
void checkBallOutOfBoundsTable() {
if(ballY < 0)
{
yincrement = - yincrement;
ballY = 1;
}
else if(ballY > FIELD_HEIGHT-1)
{
yincrement = - yincrement;
xincrement = 0;
ballY = FIELD_HEIGHT/2;
ballX = FIELD_WIDTH/2;
maxAttempt++;
}
if(ballX < 0)
{
xincrement = - xincrement;
ballX = 1;
}
else if(ballX > FIELD_WIDTH-1)
{
xincrement = - xincrement;
ballX = FIELD_WIDTH-2;
}
}
boolean checkBlockCollision(){
int ballTop = ballY-rad; // Values for easy reference
int ballBottom = ballY+rad;
int ballLeft = ballX-rad;
int ballRight = ballX+rad;
for(int i=0;i<numBlocks;i++){ // Loop through the blocks
if(bricks[i][0] == 1){ // If the block hasn't been eliminated
int blockX = bricks[i][1]; // Grab x and y location
int blockY = bricks[i][2];
if(ballBottom >= blockY && ballTop <= blockY+blockHeight){ // If hitting BLOCK
if(ballRight >= blockX && ballLeft <= blockX+blockWidth){
removeBlock(i); // Mark the block as out of play
return true;
}
}
}
}
return false; // No collision detected
}
/* Removes a block from game play */
void removeBlock(int index){
bricks[index][0] = 0; // Mark it as out of play
scorePlayer++; // Increment score
yincrement = -yincrement; // Flip the y increment
}