blocks disapear when hit by the ball
This commit is contained in:
parent
0405287481
commit
31dd86a53c
4 changed files with 38 additions and 16 deletions
BIN
bin/Debug/cTest
BIN
bin/Debug/cTest
Binary file not shown.
|
|
@ -7,7 +7,7 @@
|
|||
<unistd.h>
|
||||
<stdbool.h>
|
||||
|
||||
1728183838 source:/home/fluffy/codeBlocks/blockBreaker/main.c
|
||||
1728637902 source:/home/fluffy/codeBlocks/blockBreaker/main.c
|
||||
<stdio.h>
|
||||
<stdlib.h>
|
||||
<stdint.h>
|
||||
|
|
|
|||
52
main.c
52
main.c
|
|
@ -13,6 +13,7 @@
|
|||
#define SLEEP_TIME_MS 100 * 1000
|
||||
#define PADDLE_HEIGHT SCREEN_HEIGHT-4
|
||||
#define Y_BLOCK_LAYERS 4
|
||||
#define X_BLOCK_LAYERS SCREEN_WIDTH
|
||||
|
||||
uint8_t screen[SCREEN_WIDTH][SCREEN_HEIGHT];
|
||||
|
||||
|
|
@ -37,7 +38,6 @@ struct ballStruct {
|
|||
uint8_t yPOS;
|
||||
int8_t xVEL;
|
||||
int8_t yVEL;
|
||||
|
||||
} ball= {.xPOS=0,.yPOS=0,.xVEL=0,.yVEL=0};
|
||||
|
||||
struct Paddle {
|
||||
|
|
@ -55,11 +55,15 @@ int main() {
|
|||
//struct ballStruct ball;
|
||||
time_t UNIXTIME = time(NULL);
|
||||
srand(UNIXTIME); //seed random number with time at run time
|
||||
int noXBlocks = SCREEN_WIDTH;
|
||||
Block_t blockArr[noXBlocks][Y_BLOCK_LAYERS];
|
||||
for (int x=0;x< noXBlocks;x++) {
|
||||
for (int y=0; y<Y_BLOCK_LAYERS; y++){
|
||||
blockArr[x][y]=(Block_t) {.xLocation =x,.yLocation=y,.visable=true};
|
||||
Block_t blockArr[X_BLOCK_LAYERS][Y_BLOCK_LAYERS];
|
||||
bool blockFlushHit = false; //used to show if the ball has hit a block vertically or horizontally
|
||||
|
||||
//init blockArr array
|
||||
for (int x=0; x< X_BLOCK_LAYERS; x++) {
|
||||
for (int y=0; y<Y_BLOCK_LAYERS; y++) {
|
||||
blockArr[x][y]=(Block_t) {
|
||||
.xLocation =x,.yLocation=y,.visable=true
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -71,11 +75,12 @@ int main() {
|
|||
// std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME_MS));
|
||||
usleep(SLEEP_TIME_MS);
|
||||
clearScreen();
|
||||
blockFlushHit = false;
|
||||
|
||||
//draw blocks
|
||||
for (int x = 0;x<noXBlocks;x++){
|
||||
for (int y =0;y<Y_BLOCK_LAYERS;y++){
|
||||
if(blockArr[x][y].visable==true){
|
||||
for (int x = 0; x<X_BLOCK_LAYERS; x++) {
|
||||
for (int y =0; y<Y_BLOCK_LAYERS; y++) {
|
||||
if(blockArr[x][y].visable==true) {
|
||||
screen[x][y]=PIXEL_BLOCK;
|
||||
}
|
||||
}
|
||||
|
|
@ -104,7 +109,7 @@ int main() {
|
|||
printf("<>");
|
||||
} else if(screen[x][y]==PIXEL_PADDLE) {
|
||||
printf("==");
|
||||
} else if(screen[x][y]==PIXEL_BLOCK){
|
||||
} else if(screen[x][y]==PIXEL_BLOCK) {
|
||||
printf("[]");
|
||||
}
|
||||
}
|
||||
|
|
@ -122,16 +127,33 @@ int main() {
|
|||
if (ball.yPOS==0||ball.yPOS==(SCREEN_HEIGHT-1)) {
|
||||
ball.yVEL=ball.yVEL*-1;
|
||||
}
|
||||
//detect when ball hits a block or paddle
|
||||
if(screen[ball.xPOS+ball.xVEL][ball.yPOS]==PIXEL_BLOCK||screen[ball.xPOS+ball.xVEL][ball.yPOS]==PIXEL_PADDLE) {
|
||||
//detect when ball hits a block
|
||||
if (screen[ball.xPOS+ball.xVEL][ball.yPOS]==PIXEL_BLOCK) {
|
||||
blockArr[ball.xPOS+ball.xVEL][ball.yPOS].visable=false;
|
||||
ball.xVEL = ball.xVEL *-1;
|
||||
blockFlushHit=true;
|
||||
}
|
||||
if (screen[ball.xPOS][ball.yPOS+ball.yVEL]==PIXEL_BLOCK) {
|
||||
blockArr[ball.xPOS][ball.yPOS+ball.yVEL].visable=false;
|
||||
ball.yVEL = ball.yVEL *-1;
|
||||
blockFlushHit=true;
|
||||
}
|
||||
if (screen[ball.xPOS+ball.xVEL][ball.yPOS+ball.yVEL]==PIXEL_BLOCK&&blockFlushHit==false) {
|
||||
blockArr[ball.xPOS+ball.xVEL][ball.yPOS+ball.yVEL].visable=false;
|
||||
ball.xVEL = ball.xVEL *-1;
|
||||
ball.yVEL=ball.yVEL *-1;
|
||||
}
|
||||
|
||||
//detect when ball hits paddle
|
||||
if(screen[ball.xPOS+ball.xVEL][ball.yPOS]==PIXEL_PADDLE) {
|
||||
ball.xVEL = ball.xVEL *-1;
|
||||
}
|
||||
if (screen[ball.xPOS][ball.yPOS+ball.yVEL]==PIXEL_BLOCK||screen[ball.xPOS][ball.yPOS+ball.yVEL]== PIXEL_PADDLE) { //hit detection y direction
|
||||
if (screen[ball.xPOS][ball.yPOS+ball.yVEL]== PIXEL_PADDLE) { //hit detection y direction
|
||||
ball.yVEL= ball.yVEL*-1;
|
||||
}
|
||||
if (screen[ball.xPOS+ball.xVEL][ball.yPOS+ball.yVEL]==PIXEL_BLOCK||screen[ball.xPOS+ball.xVEL][ball.yPOS+ball.yVEL]==PIXEL_PADDLE) { //diagonal hit detection
|
||||
if (screen[ball.xPOS+ball.xVEL][ball.yPOS+ball.yVEL]==PIXEL_PADDLE) { //diagonal hit detection
|
||||
ball.xVEL = ball.xVEL *-1;
|
||||
ball.xVEL=ball.yVEL *-1;
|
||||
ball.yVEL=ball.yVEL *-1;
|
||||
}
|
||||
//update ball position
|
||||
ball.xPOS=ball.xPOS+ball.xVEL;
|
||||
|
|
|
|||
BIN
obj/Debug/main.o
BIN
obj/Debug/main.o
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue