added diagonal hitters on ends of paddle
This commit is contained in:
parent
0796688df7
commit
83d51667c2
4 changed files with 59 additions and 18 deletions
75
main.c
75
main.c
|
|
@ -5,11 +5,11 @@
|
|||
#include<unistd.h>
|
||||
#include<stdbool.h>
|
||||
#define SCREEN_WIDTH 11
|
||||
#define SCREEN_HEIGHT 10
|
||||
#define PADDLE_SIZE 1
|
||||
#define SCREEN_HEIGHT 20
|
||||
#define PADDLE_SIZE 2
|
||||
#define SLEEP_TIME_MS 100 * 1000
|
||||
#define PADDLE_HEIGHT SCREEN_HEIGHT-1
|
||||
#define Y_BLOCK_LAYERS 4
|
||||
#define PADDLE_HEIGHT SCREEN_HEIGHT-4
|
||||
#define Y_BLOCK_LAYERS 10
|
||||
#define X_BLOCK_LAYERS SCREEN_WIDTH
|
||||
|
||||
uint8_t screen[SCREEN_WIDTH][SCREEN_HEIGHT];
|
||||
|
|
@ -18,7 +18,9 @@ enum PIXEL_TYPE {
|
|||
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() {
|
||||
|
|
@ -73,7 +75,7 @@ int main() {
|
|||
}
|
||||
|
||||
ball.xPOS=rand()% SCREEN_WIDTH;
|
||||
ball.yPOS= rand()% SCREEN_HEIGHT;
|
||||
ball.yPOS= PADDLE_HEIGHT-1;
|
||||
ball.xVEL=1;
|
||||
ball.yVEL=1;
|
||||
while(true) {
|
||||
|
|
@ -90,11 +92,21 @@ int main() {
|
|||
}
|
||||
}
|
||||
|
||||
//draw paddel
|
||||
screen[paddle.location][PADDLE_HEIGHT]=PIXEL_PADDLE;
|
||||
/* //draw paddel
|
||||
screen[paddle.location][PADDLE_HEIGHT]=PIXEL_PADDLE_MIDDLE;
|
||||
for(int x= 0; x<=PADDLE_SIZE; x++) {
|
||||
screen[paddle.location-x][PADDLE_HEIGHT]=PIXEL_PADDLE;
|
||||
screen[paddle.location+x][PADDLE_HEIGHT]=PIXEL_PADDLE;
|
||||
screen[paddle.location-x][PADDLE_HEIGHT]=PIXEL_PADDLE_LEFT;
|
||||
screen[paddle.location+x][PADDLE_HEIGHT]=PIXEL_PADDLE_RIGHT;
|
||||
} */
|
||||
|
||||
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;
|
||||
|
|
@ -109,12 +121,16 @@ int main() {
|
|||
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("==");
|
||||
} else if(screen[x][y]==PIXEL_BLOCK) {
|
||||
printf("[]");
|
||||
} else if (screen[x][y]==PIXEL_BALL) {
|
||||
printf("<>");
|
||||
} 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");
|
||||
|
|
@ -153,16 +169,41 @@ int main() {
|
|||
}
|
||||
|
||||
//detect when ball hits paddle
|
||||
if(screen[ball.xPOS+ball.xVEL][ball.yPOS]==PIXEL_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) { //hit detection y direction
|
||||
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) { //diagonal hit detection
|
||||
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;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue