created and init block array
This commit is contained in:
parent
f6cc6a3cfc
commit
e76901b567
6 changed files with 199 additions and 0 deletions
133
main.c
Normal file
133
main.c
Normal 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;
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue