created and init block array

This commit is contained in:
Seth Samuel 2024-10-11 17:19:47 +13:00
parent f6cc6a3cfc
commit e76901b567
6 changed files with 199 additions and 0 deletions

BIN
bin/Debug/cTest Executable file

Binary file not shown.

39
cTest.cbp Normal file
View file

@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="cTest" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="bin/Debug/cTest" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Debug/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-g" />
</Compiler>
</Target>
<Target title="Release">
<Option output="bin/Release/cTest" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Release/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-O2" />
</Compiler>
<Linker>
<Add option="-s" />
</Linker>
</Target>
</Build>
<Compiler>
<Add option="-Wall" />
</Compiler>
<Unit filename="main.c">
<Option compilerVar="CC" />
</Unit>
<Extensions />
</Project>
</CodeBlocks_project_file>

17
cTest.depend Normal file
View file

@ -0,0 +1,17 @@
# 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>

10
cTest.layout Normal file
View file

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_layout_file>
<FileVersion major="1" minor="0" />
<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" />
</Cursor>
</File>
</CodeBlocks_layout_file>

133
main.c Normal file
View file

@ -0,0 +1,133 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <time.h>
#include<unistd.h>
#include<stdbool.h>
//#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];
enum PIXEL_TYPE {
PIXEL_EMPTY=0u,
PIXEL_BALL=1u,
PIXEL_BLOCK=2u,
PIXEL_PADDLE=3u
} PIXEL_TYPE;
void clearScreen() {
for (int y = 0; y < SCREEN_HEIGHT; y++) {
for (int x=0; x<SCREEN_WIDTH; x++) {
screen[x][y]=PIXEL_EMPTY;
}
}
printf("\e[1;1H\e[2J");//clear the screen of the prev frame
}
struct ballStruct {
uint8_t xPOS;
uint8_t yPOS;
int8_t xVEL;
int8_t yVEL;
} ball= {.xPOS=0,.yPOS=0,.xVEL=0,.yVEL=0};
struct Paddle {
uint8_t location;
uint8_t paddleSize;
} paddle= {.location=(SCREEN_WIDTH/2),.paddleSize=PADDLE_SIZE};
typedef struct block {
uint8_t xLocation;
uint8_t yLocation;
bool visable;
} Block_t;
int main() {
//struct ballStruct ball;
time_t UNIXTIME = time(NULL);
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++) {
for (int y=0; y<Y_BLOCK_LAYERS; y++){
blockArr[x][y]=(Block_t) {.xLocation =x,.yLocation=y,.visable=true};
}
}
ball.xPOS=rand()% SCREEN_WIDTH;
ball.yPOS= rand()% SCREEN_HEIGHT;
ball.xVEL=1;
ball.yVEL=1;
while(true) {
// std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME_MS));
usleep(SLEEP_TIME_MS);
clearScreen();
//draw blocks
//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;
}
//draw ball
screen[ball.xPOS][ball.yPOS]=PIXEL_BALL;
//render step
for (int x =0; x<=SCREEN_WIDTH; x++) {
printf("--"); //top border
}
printf("\n");
for (int y = 0; y < SCREEN_HEIGHT; y++) {
printf("|");
for (int x=0; x<SCREEN_WIDTH; x++) {
if (screen[x][y]==PIXEL_EMPTY) {
printf(" ");
} else if (screen[x][y]==PIXEL_BALL) {
printf("<>");
} else if(screen[x][y]==PIXEL_PADDLE) {
printf("==");
}
}
printf("|\n");
}
for (int x =0; x<=SCREEN_WIDTH; x++) {
printf("--");
}
printf("\n");
//detect when ball hits the edge of the screen
if (ball.xPOS==0||ball.xPOS==(SCREEN_WIDTH-1)) {
ball.xVEL=ball.xVEL*-1;
}
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) {
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
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;
}
//update ball position
ball.xPOS=ball.xPOS+ball.xVEL;
ball.yPOS= ball.yPOS+ball.yVEL;
}
}

BIN
obj/Debug/main.o Normal file

Binary file not shown.