fixed issue where the game whould break when the ball was hitting a block while it was against the wall

This commit is contained in:
Seth Samuel 2024-10-11 23:12:21 +13:00
parent 31dd86a53c
commit c56b8f8d19
4 changed files with 19 additions and 11 deletions

Binary file not shown.

View file

@ -7,7 +7,7 @@
<unistd.h> <unistd.h>
<stdbool.h> <stdbool.h>
1728637902 source:/home/fluffy/codeBlocks/blockBreaker/main.c 1728641201 source:/home/fluffy/codeBlocks/blockBreaker/main.c
<stdio.h> <stdio.h>
<stdlib.h> <stdlib.h>
<stdint.h> <stdint.h>

28
main.c
View file

@ -4,15 +4,12 @@
#include <time.h> #include <time.h>
#include<unistd.h> #include<unistd.h>
#include<stdbool.h> #include<stdbool.h>
//#include <chrono>
//#include <thread>
#define SCREEN_WIDTH 11 #define SCREEN_WIDTH 11
#define SCREEN_HEIGHT 20 #define SCREEN_HEIGHT 20
#define PADDLE_SIZE 1 #define PADDLE_SIZE 1
#define SLEEP_TIME_MS 100 * 1000 #define SLEEP_TIME_MS 100 * 1000
#define PADDLE_HEIGHT SCREEN_HEIGHT-4 #define PADDLE_HEIGHT SCREEN_HEIGHT-4
#define Y_BLOCK_LAYERS 4 #define Y_BLOCK_LAYERS 10
#define X_BLOCK_LAYERS SCREEN_WIDTH #define X_BLOCK_LAYERS SCREEN_WIDTH
uint8_t screen[SCREEN_WIDTH][SCREEN_HEIGHT]; uint8_t screen[SCREEN_WIDTH][SCREEN_HEIGHT];
@ -33,12 +30,14 @@ void clearScreen() {
printf("\e[1;1H\e[2J");//clear the screen of the prev frame printf("\e[1;1H\e[2J");//clear the screen of the prev frame
} }
struct ballStruct {
typedef struct ballStruct {
uint8_t xPOS; uint8_t xPOS;
uint8_t yPOS; uint8_t yPOS;
int8_t xVEL; int8_t xVEL;
int8_t yVEL; int8_t yVEL;
} ball= {.xPOS=0,.yPOS=0,.xVEL=0,.yVEL=0}; } Ball_t;
struct Paddle { struct Paddle {
uint8_t location; uint8_t location;
@ -51,8 +50,14 @@ typedef struct block {
bool visable; bool visable;
} Block_t; } Block_t;
bool ballOnXEdge(Ball_t ball){
if(ball.xPOS==0) return true;
if (ball.xPOS==SCREEN_WIDTH-1) return true;
return false;
}
int main() { int main() {
//struct ballStruct ball; Ball_t ball = {.xPOS=0,.yPOS=0,.xVEL=0,.yVEL=0};
time_t UNIXTIME = time(NULL); time_t UNIXTIME = time(NULL);
srand(UNIXTIME); //seed random number with time at run time srand(UNIXTIME); //seed random number with time at run time
Block_t blockArr[X_BLOCK_LAYERS][Y_BLOCK_LAYERS]; Block_t blockArr[X_BLOCK_LAYERS][Y_BLOCK_LAYERS];
@ -72,7 +77,6 @@ int main() {
ball.xVEL=1; ball.xVEL=1;
ball.yVEL=1; ball.yVEL=1;
while(true) { while(true) {
// std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME_MS));
usleep(SLEEP_TIME_MS); usleep(SLEEP_TIME_MS);
clearScreen(); clearScreen();
blockFlushHit = false; blockFlushHit = false;
@ -140,8 +144,12 @@ int main() {
} }
if (screen[ball.xPOS+ball.xVEL][ball.yPOS+ball.yVEL]==PIXEL_BLOCK&&blockFlushHit==false) { 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; blockArr[ball.xPOS+ball.xVEL][ball.yPOS+ball.yVEL].visable=false;
ball.xVEL = ball.xVEL *-1; if(ballOnXEdge(ball)==false) {
ball.yVEL=ball.yVEL *-1; ball.xVEL = ball.xVEL *-1;
}
if (ball.yPOS!=0) {
ball.yVEL=ball.yVEL *-1;
}
} }
//detect when ball hits paddle //detect when ball hits paddle

Binary file not shown.