Compare commits
7 commits
e76901b567
...
64e70570e1
| Author | SHA1 | Date | |
|---|---|---|---|
| 64e70570e1 | |||
| 3bb78f6b44 | |||
| 83d51667c2 | |||
| 0796688df7 | |||
| c56b8f8d19 | |||
| 31dd86a53c | |||
| 0405287481 |
8 changed files with 145 additions and 62 deletions
BIN
bin/Debug/blockBreaker
Executable file
BIN
bin/Debug/blockBreaker
Executable file
Binary file not shown.
BIN
bin/Debug/cTest
BIN
bin/Debug/cTest
Binary file not shown.
|
|
@ -2,12 +2,12 @@
|
|||
<CodeBlocks_project_file>
|
||||
<FileVersion major="1" minor="6" />
|
||||
<Project>
|
||||
<Option title="cTest" />
|
||||
<Option title="blockBreaker" />
|
||||
<Option pch_mode="2" />
|
||||
<Option compiler="gcc" />
|
||||
<Build>
|
||||
<Target title="Debug">
|
||||
<Option output="bin/Debug/cTest" prefix_auto="1" extension_auto="1" />
|
||||
<Option output="bin/Debug/blockBreaker" prefix_auto="1" extension_auto="1" />
|
||||
<Option object_output="obj/Debug/" />
|
||||
<Option type="1" />
|
||||
<Option compiler="gcc" />
|
||||
|
|
@ -16,7 +16,7 @@
|
|||
</Compiler>
|
||||
</Target>
|
||||
<Target title="Release">
|
||||
<Option output="bin/Release/cTest" prefix_auto="1" extension_auto="1" />
|
||||
<Option output="bin/Release/blockBreaker" prefix_auto="1" extension_auto="1" />
|
||||
<Option object_output="obj/Release/" />
|
||||
<Option type="1" />
|
||||
<Option compiler="gcc" />
|
||||
9
blockBreaker.depend
Normal file
9
blockBreaker.depend
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
# depslib dependency file v1.0
|
||||
1729498522 source:/home/fluffy/codeBlocks/blockBreaker/main.c
|
||||
<stdio.h>
|
||||
<stdlib.h>
|
||||
<stdint.h>
|
||||
<time.h>
|
||||
<unistd.h>
|
||||
<stdbool.h>
|
||||
|
||||
|
|
@ -4,7 +4,7 @@
|
|||
<ActiveTarget name="Debug" />
|
||||
<File name="main.c" open="1" top="1" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
|
||||
<Cursor>
|
||||
<Cursor1 position="1370" topLine="86" />
|
||||
<Cursor1 position="4610" topLine="150" />
|
||||
</Cursor>
|
||||
</File>
|
||||
</CodeBlocks_layout_file>
|
||||
17
cTest.depend
17
cTest.depend
|
|
@ -1,17 +0,0 @@
|
|||
# depslib dependency file v1.0
|
||||
1727778794 source:/home/fluffy/codeBlocks/cTest/main.c
|
||||
<stdio.h>
|
||||
<stdlib.h>
|
||||
<stdint.h>
|
||||
<time.h>
|
||||
<unistd.h>
|
||||
<stdbool.h>
|
||||
|
||||
1728183838 source:/home/fluffy/codeBlocks/blockBreaker/main.c
|
||||
<stdio.h>
|
||||
<stdlib.h>
|
||||
<stdint.h>
|
||||
<time.h>
|
||||
<unistd.h>
|
||||
<stdbool.h>
|
||||
|
||||
173
main.c
173
main.c
|
|
@ -4,26 +4,27 @@
|
|||
#include <time.h>
|
||||
#include<unistd.h>
|
||||
#include<stdbool.h>
|
||||
#define SCREEN_WIDTH 11 //set the width 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 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 Y_BLOCK_LAYERS 10 //add the amount of layers of blocks from the top of the screen
|
||||
#define X_BLOCK_LAYERS SCREEN_WIDTH
|
||||
|
||||
//#include <chrono>
|
||||
//#include <thread>
|
||||
#define SCREEN_WIDTH 11
|
||||
#define SCREEN_HEIGHT 20
|
||||
#define PADDLE_SIZE 1
|
||||
#define SLEEP_TIME_MS 100 * 1000
|
||||
#define PADDLE_HEIGHT SCREEN_HEIGHT-4
|
||||
#define Y_BLOCK_LAYERS 4
|
||||
uint8_t screen[SCREEN_WIDTH][SCREEN_HEIGHT]; //this is the array the game lives in
|
||||
int moveArr[] = {-2,-1,0,1,2};
|
||||
|
||||
uint8_t screen[SCREEN_WIDTH][SCREEN_HEIGHT];
|
||||
|
||||
enum PIXEL_TYPE {
|
||||
enum PIXEL_TYPE { //used to set what kind of pixel is shown on the screen array
|
||||
PIXEL_EMPTY=0u,
|
||||
PIXEL_BALL=1u,
|
||||
PIXEL_BLOCK=2u,
|
||||
PIXEL_PADDLE=3u
|
||||
PIXEL_PADDLE_MIDDLE=3u,
|
||||
PIXEL_PADDLE_LEFT=4u,
|
||||
PIXEL_PADDLE_RIGHT=5u
|
||||
} PIXEL_TYPE;
|
||||
|
||||
void clearScreen() {
|
||||
void clearScreen() { //clears the screen of all pixels to redraw the screen
|
||||
for (int y = 0; y < SCREEN_HEIGHT; y++) {
|
||||
for (int x=0; x<SCREEN_WIDTH; x++) {
|
||||
screen[x][y]=PIXEL_EMPTY;
|
||||
|
|
@ -32,54 +33,88 @@ void clearScreen() {
|
|||
printf("\e[1;1H\e[2J");//clear the screen of the prev frame
|
||||
}
|
||||
|
||||
struct ballStruct {
|
||||
|
||||
|
||||
typedef struct ballStruct {
|
||||
uint8_t xPOS;
|
||||
uint8_t yPOS;
|
||||
int8_t xVEL;
|
||||
int8_t yVEL;
|
||||
} Ball_t;
|
||||
|
||||
} ball= {.xPOS=0,.yPOS=0,.xVEL=0,.yVEL=0};
|
||||
|
||||
struct Paddle {
|
||||
typedef struct Paddle {
|
||||
uint8_t location;
|
||||
uint8_t paddleSize;
|
||||
} paddle= {.location=(SCREEN_WIDTH/2),.paddleSize=PADDLE_SIZE};
|
||||
int paddleSize;
|
||||
} Paddle_t;
|
||||
|
||||
typedef struct block {
|
||||
uint8_t xLocation;
|
||||
uint8_t yLocation;
|
||||
bool visable;
|
||||
bool visable; //controls if the block is visible or not
|
||||
} Block_t;
|
||||
|
||||
bool ballOnXEdge(Ball_t ball){ //work around to determine if the ball is on the edge of the screen
|
||||
if(ball.xPOS==0) return true;
|
||||
if (ball.xPOS==SCREEN_WIDTH-1) return true;
|
||||
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() {
|
||||
//struct ballStruct ball;
|
||||
time_t UNIXTIME = time(NULL);
|
||||
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
|
||||
srand(UNIXTIME); //seed random number with time at run time
|
||||
int noXBlocks = SCREEN_WIDTH /2;
|
||||
Block_t blockArr[noXBlocks][Y_BLOCK_LAYERS];
|
||||
for (int x=0;x< noXBlocks;x++) {
|
||||
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};
|
||||
blockArr[x][y]=(Block_t) {
|
||||
.xLocation =x,.yLocation=y,.visable=true
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
ball.xPOS=rand()% SCREEN_WIDTH;
|
||||
ball.yPOS= rand()% SCREEN_HEIGHT;
|
||||
ball.yPOS= PADDLE_HEIGHT-1;
|
||||
ball.xVEL=1;
|
||||
ball.yVEL=1;
|
||||
while(true) {
|
||||
// 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<X_BLOCK_LAYERS; x++) {
|
||||
for (int y =0; y<Y_BLOCK_LAYERS; y++) {
|
||||
if(blockArr[x][y].visable==true) {
|
||||
screen[x][y]=PIXEL_BLOCK;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//draw paddel
|
||||
screen[paddle.location][PADDLE_HEIGHT]=PIXEL_PADDLE;
|
||||
for(int x= 0; x<=PADDLE_SIZE; x++) {
|
||||
screen[paddle.location-x][PADDLE_HEIGHT]=PIXEL_PADDLE;
|
||||
screen[paddle.location+x][PADDLE_HEIGHT]=PIXEL_PADDLE;
|
||||
for (int x=0-PADDLE_SIZE;x<PADDLE_SIZE;x++){
|
||||
if(x==0-PADDLE_SIZE){
|
||||
screen[paddle.location+x][PADDLE_HEIGHT]=PIXEL_PADDLE_LEFT;
|
||||
} else if (x==PADDLE_SIZE-1){
|
||||
screen[paddle.location+x][PADDLE_HEIGHT]=PIXEL_PADDLE_RIGHT;
|
||||
} else {
|
||||
screen[paddle.location+x][PADDLE_HEIGHT]=PIXEL_PADDLE_MIDDLE;
|
||||
}
|
||||
}
|
||||
//draw ball
|
||||
screen[ball.xPOS][ball.yPOS]=PIXEL_BALL;
|
||||
|
|
@ -94,10 +129,16 @@ int main() {
|
|||
for (int x=0; x<SCREEN_WIDTH; x++) {
|
||||
if (screen[x][y]==PIXEL_EMPTY) {
|
||||
printf(" ");
|
||||
} else if(screen[x][y]==PIXEL_BLOCK) {
|
||||
printf("[]");
|
||||
} else if (screen[x][y]==PIXEL_BALL) {
|
||||
printf("<>");
|
||||
} else if(screen[x][y]==PIXEL_PADDLE) {
|
||||
} else if(screen[x][y]==PIXEL_PADDLE_MIDDLE) {
|
||||
printf("==");
|
||||
} else if(screen[x][y]==PIXEL_PADDLE_LEFT){
|
||||
printf("<=");
|
||||
} else if(screen[x][y]==PIXEL_PADDLE_RIGHT){
|
||||
printf("=>");
|
||||
}
|
||||
}
|
||||
printf("|\n");
|
||||
|
|
@ -114,20 +155,70 @@ 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;
|
||||
if(ballOnXEdge(ball)==false) {
|
||||
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 (ball.yPOS!=0) {
|
||||
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
|
||||
ball.xVEL = ball.xVEL *-1;
|
||||
ball.xVEL=ball.yVEL *-1;
|
||||
}
|
||||
|
||||
//detect when ball hits paddle
|
||||
if(screen[ball.xPOS+ball.xVEL][ball.yPOS]==PIXEL_PADDLE_MIDDLE) {
|
||||
ball.xVEL = ball.xVEL *-1;
|
||||
}
|
||||
if (screen[ball.xPOS][ball.yPOS+ball.yVEL]== PIXEL_PADDLE_MIDDLE) { //hit detection y direction
|
||||
ball.yVEL= ball.yVEL*-1;
|
||||
}
|
||||
if (screen[ball.xPOS+ball.xVEL][ball.yPOS+ball.yVEL]==PIXEL_PADDLE_MIDDLE) { //diagonal hit detection
|
||||
ball.xVEL = ball.xVEL *-1;
|
||||
ball.yVEL=ball.yVEL *-1;
|
||||
}
|
||||
//paddle left
|
||||
if (screen[ball.xPOS+ball.xVEL][ball.yPOS+ball.yVEL]==PIXEL_PADDLE_LEFT) { //diagonal hit detection
|
||||
if(ball.xVEL==-1){
|
||||
ball.xVEL=-1;
|
||||
} else if (ball.xVEL==0){
|
||||
ball.xVEL=-1;
|
||||
} else {
|
||||
ball.xVEL=-1;
|
||||
}
|
||||
ball.yVEL=ball.yVEL *-1;
|
||||
}
|
||||
|
||||
//paddle right
|
||||
if (screen[ball.xPOS+ball.xVEL][ball.yPOS+ball.yVEL]==PIXEL_PADDLE_RIGHT) { //diagonal hit detection
|
||||
if (ball.xVEL==-1){ //moving left
|
||||
ball.xVEL=1;
|
||||
} else if (ball.xVEL==0){ //moving straight down
|
||||
ball.xVEL=1;
|
||||
} else { //moving right
|
||||
ball.xVEL=1;
|
||||
}
|
||||
ball.yVEL=ball.yVEL *-1;
|
||||
}
|
||||
|
||||
|
||||
//update ball position
|
||||
ball.xPOS=ball.xPOS+ball.xVEL;
|
||||
ball.yPOS= ball.yPOS+ball.yVEL;
|
||||
|
||||
|
||||
//move paddle
|
||||
movePaddle(&paddle,moveArr[rand()%6]);
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
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