-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.c
223 lines (197 loc) · 6.89 KB
/
game.c
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include <stdlib.h>
#include "myLib.h"
#include "game.h"
/**
* Function to draw the slider on every screen
*
* @param sldptr the pointer to the slider to be drawn
*/
void drawSlider(Slider *sldptr) {
sldptr->height = SLIDERHEIGHT;
sldptr->width = SLIDERWIDTH;
sldptr->row = SCREENHEIGHT - 15 - sldptr->height;
sldptr->col = (SCREENWIDTH / 2) - 1 - (sldptr->width / 2);
sldptr->oldRow = sldptr->row;
sldptr->oldCol = sldptr->col;
drawRect(sldptr->row, sldptr->col, sldptr->height, sldptr->width, YELLOW);
}
/**
* Function to enable movement of slider across sides of screen
*
* @param sldptr the pointer to the slider to be moved
*/
void enableSlider(Slider *sldptr) {
int cdel = 2;
const int height = sldptr->height;
if (KEY_DOWN_NOW(BUTTON_RIGHT)) {
// Get the width of the slider
int width = sldptr->width;
// Erase previous slider position
drawRect(sldptr->oldRow, sldptr->oldCol, height, width, BGCOLOR);
sldptr->oldRow = sldptr->row;
sldptr->oldCol = sldptr->col;
sldptr->col = sldptr->col + cdel;
if (sldptr->col + width > SCREENWIDTH) {
sldptr->col = SCREENWIDTH - width;
}
drawRect(sldptr->row, sldptr->col, height, width, YELLOW);
}
if (KEY_DOWN_NOW(BUTTON_LEFT)) {
// Get the width of the slider
int width = sldptr->width;
// Erase previous slider position
drawRect(sldptr->oldRow, sldptr->oldCol, height, width, BGCOLOR);
sldptr->oldRow = sldptr->row;
sldptr->oldCol = sldptr->col;
sldptr->col = sldptr->col - cdel;
if (sldptr->col < 0) {
sldptr->col = 0;
}
drawRect(sldptr->row, sldptr->col, height, width, YELLOW);
}
}
/**
* Function to create the game ball
*
* @param row the row to start the ball
* @param col the col to start the ball
* @param size the size of the ball
* @param ballptr the pointer to the ball to move
*/
void createBall(int row, int col, int size, Ball *ballptr) {
ballptr->xDir = 1;
ballptr->yDir = -1;
ballptr->row = row;
ballptr->col = col;
ballptr->oldRow = ballptr->row;
ballptr->oldCol = ballptr->col;
ballptr->size = size;
drawRect(row, col, size, size, WHITE);
}
/**
* Function to start ball movement
*
* @param ballptr the pointer to the ball to move
* @param sldptr the pointer to the slider of the level
* @param brptr the pointer to the array of bricks in the game
* @param numBricks the pointer to the number of bricks in the game
* @param bricksSize the size of the array of bricks
*/
void ballMovement(Ball *ballptr, Slider *sldptr, Brick *brptr, int *numBricks, int bricksSize) {
drawRect(ballptr->oldRow, ballptr->oldCol, ballptr->size, ballptr->size, BGCOLOR);
ballptr->col += ballptr->xDir;
ballptr->row += ballptr->yDir;
handleCollisions(ballptr, sldptr);
handleBrickCollisions(brptr, ballptr, numBricks, bricksSize);
ballptr->oldRow = ballptr->row;
ballptr->oldCol = ballptr->col;
drawRect(ballptr->row, ballptr->col, ballptr->size, ballptr->size, WHITE);
}
/**
* Function to handle collisions in the game
*
* @param ballptr the pointer to the ball of the game
* @param sldptr the pointer to the slider of the game
*/
void handleCollisions(Ball *ballptr, Slider *sldptr) {
// int seed = 0;
// while (!KEY_DOWN_NOW(BUTTON_RIGHT)) {
// seed++;
// }
// srand(seed);
if (ballptr->col < 0) {
ballptr->col = 0;
ballptr->xDir *= -1;
}
if (ballptr->row < 0) {
ballptr->row = 0;
ballptr->yDir *= -1;
}
if (ballptr->col + BALLSIZE > SCREENWIDTH) {
ballptr->col = SCREENWIDTH - BALLSIZE;
ballptr->xDir *= -1;
}
// Collision resolution for when bottom of ball hits top of slider
if ((ballptr->row + BALLSIZE) > sldptr->row
&& ballptr->col >= (sldptr->col - BALLSIZE)
&& ballptr->col <= (sldptr->col + SLIDERWIDTH)) {
ballptr->row = sldptr->row - BALLSIZE;
ballptr->yDir *= -1;
ballptr->xDir = (rand() % 2 + 1) * ballptr->xDir / abs(ballptr->xDir);
ballptr->yDir = (rand() % 2 + 1) * ballptr->yDir / abs(ballptr->yDir);
}
}
/**
* Function to generate bricks on the level
*
* @param brptr the pointer to the first brick of the array of bricks
* @param numBricks the number of bricks on the screen
*/
void generateBricks(Brick *brptr, int numBricks) {
u16 colors[] = {RED, GREEN, BLUE, YELLOW, WHITE};
int numcolors = sizeof(colors)/sizeof(colors[0]);
int row = 25;
int col = 45;
for (int i = 0; i < numBricks; i++) {
Brick *cur = brptr + i;
u16 color = colors[i % numcolors];
cur->row = row;
cur->col = col;
cur->height = BRICKHEIGHT;
cur->width = BRICKWIDTH;
cur->color = color;
cur->isHit = 0;
col = col + 30 + (cur->width);
if ((i + 1) % 3 == 0) {
row += BRICKHEIGHT + 3;
col = 45;
}
}
}
/**
* Function to handle ball collision with a brick
*
* @param brptr the pointer to the brick
* @param ballptr the pointer to the ball
* @param numBricks the pointer to the number of bricks to generate
* @param bricksSize the number of bricks in the array
*/
void handleBrickCollisions(Brick *brptr, Ball *ballptr, int *numBricks, int bricksSize) {
// int seed = 0;
// while (!KEY_DOWN_NOW(BUTTON_RIGHT)) {
// seed++;
// }
// srand(seed);
for (int i = 0; i < bricksSize; i++) {
Brick *cur = brptr + i;
if (cur->isHit) {
drawRect(cur->row, cur->col, BRICKHEIGHT, BRICKWIDTH, BGCOLOR);
for (int j = i; j < bricksSize - 1; j++) {
brptr[j] = brptr[j + 1];
}
bricksSize = *(numBricks);
i--;
} else {
if (((ballptr->col == (cur->col + BRICKWIDTH))
|| ((ballptr->col + BALLSIZE) == cur->col))
&& ((ballptr->row <= (cur->row + BRICKHEIGHT))
&& (ballptr->row >= (cur->row - BALLSIZE)))) {
*(numBricks) -= 1;
cur->isHit = 1;
ballptr->xDir *= -1;
//ballptr->xDir = ballptr->xDir * ((*(numBricks) % 2) + 1);
ballptr->col = ballptr->col + (ballptr->xDir * 1);
} else if ((((ballptr->row + BALLSIZE) == cur->row)
|| (ballptr->row == (cur->row + BRICKHEIGHT)))
&& ((ballptr->col >= (cur->col - BALLSIZE))
&& (ballptr->col <= (cur->col + BRICKWIDTH)))) {
*(numBricks) -= 1;
cur->isHit = 1;
ballptr->yDir *= -1;
//ballptr->yDir = ballptr->yDir * ((*(numBricks) % 2) + 1);
ballptr->row = ballptr->row + (ballptr->yDir * 1);
}
drawRect(cur->row, cur->col, BRICKHEIGHT, BRICKWIDTH, cur->color);
}
}
}