added code to move paddle

This commit is contained in:
Seth Samuel 2024-10-21 21:25:28 +13:00
parent 3bb78f6b44
commit 64e70570e1
4 changed files with 29 additions and 10 deletions

Binary file not shown.

View file

@ -1,5 +1,5 @@
# depslib dependency file v1.0 # depslib dependency file v1.0
1729487645 source:/home/fluffy/codeBlocks/blockBreaker/main.c 1729498522 source:/home/fluffy/codeBlocks/blockBreaker/main.c
<stdio.h> <stdio.h>
<stdlib.h> <stdlib.h>
<stdint.h> <stdint.h>

37
main.c
View file

@ -4,15 +4,16 @@
#include <time.h> #include <time.h>
#include<unistd.h> #include<unistd.h>
#include<stdbool.h> #include<stdbool.h>
#define SCREEN_WIDTH 11 //set the width of the game screen #define SCREEN_WIDTH 11 //set the width of the game screen
#define SCREEN_HEIGHT 20 //set the height of the game screen #define SCREEN_HEIGHT 20 //set the height of the game screen
#define PADDLE_SIZE 2 //set the paddle size. the blocks are added this amount either size of a central block. #define PADDLE_SIZE 2 //set the paddle size. the blocks are added this amount either size of a central block.
#define SLEEP_TIME_MS 100 * 1000 //set the time to sleep between frames #define SLEEP_TIME_MS 100 * 1000 //set the time to sleep between frames
#define PADDLE_HEIGHT SCREEN_HEIGHT-4 //the negative number is distance from the bottom #define PADDLE_HEIGHT SCREEN_HEIGHT-4 //the negative number is distance from the bottom
#define Y_BLOCK_LAYERS 10 //add the amount of layers of blocks from the top of the screen #define Y_BLOCK_LAYERS 10 //add the amount of layers of blocks from the top of the screen
#define X_BLOCK_LAYERS SCREEN_WIDTH #define X_BLOCK_LAYERS SCREEN_WIDTH
uint8_t screen[SCREEN_WIDTH][SCREEN_HEIGHT]; //this is the array the game lives in uint8_t screen[SCREEN_WIDTH][SCREEN_HEIGHT]; //this is the array the game lives in
int moveArr[] = {-2,-1,0,1,2};
enum PIXEL_TYPE { //used to set what kind of pixel is shown on the screen array enum PIXEL_TYPE { //used to set what kind of pixel is shown on the screen array
PIXEL_EMPTY=0u, PIXEL_EMPTY=0u,
@ -41,10 +42,10 @@ typedef struct ballStruct {
int8_t yVEL; int8_t yVEL;
} Ball_t; } Ball_t;
struct Paddle { typedef struct Paddle {
uint8_t location; uint8_t location;
uint8_t paddleSize; int paddleSize;
} paddle= {.location=(SCREEN_WIDTH/2),.paddleSize=PADDLE_SIZE}; } Paddle_t;
typedef struct block { typedef struct block {
uint8_t xLocation; uint8_t xLocation;
@ -58,8 +59,22 @@ bool ballOnXEdge(Ball_t ball){ //work around to determine if the ball is on the
return false; return false;
} }
bool movePaddle(Paddle_t* Paddle, int dir){ //negitave dir to move left positive to move right
if (dir==0) return false;
if (dir<=0){
if (Paddle ->location-Paddle->paddleSize+dir<=0) return false;
Paddle->location = Paddle->location+dir;
return true;
} else if (dir>=0) {
if (Paddle->location+Paddle->paddleSize+dir>= SCREEN_WIDTH-1) return false;
Paddle->location= Paddle->location+dir;
return true;
}
}
int main() { int main() {
Ball_t ball = {.xPOS=0,.yPOS=0,.xVEL=0,.yVEL=0}; //setup game ball Ball_t ball = {.xPOS=0,.yPOS=0,.xVEL=0,.yVEL=0}; //setup game ball
Paddle_t paddle= {.location=(SCREEN_WIDTH/2),.paddleSize=PADDLE_SIZE};
time_t UNIXTIME = time(NULL); //create UNIXTIME struct time_t UNIXTIME = time(NULL); //create UNIXTIME struct
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];
@ -201,5 +216,9 @@ int main() {
ball.xPOS=ball.xPOS+ball.xVEL; ball.xPOS=ball.xPOS+ball.xVEL;
ball.yPOS= ball.yPOS+ball.yVEL; ball.yPOS= ball.yPOS+ball.yVEL;
//move paddle
movePaddle(&paddle,moveArr[rand()%6]);
} }
} }

Binary file not shown.