added collisions with ball
This commit is contained in:
48
pong/pong.c
48
pong/pong.c
@@ -10,10 +10,7 @@ enum control_keys {
|
|||||||
} control_key;
|
} control_key;
|
||||||
|
|
||||||
typedef struct player {
|
typedef struct player {
|
||||||
float x;
|
Rectangle rec;
|
||||||
float y;
|
|
||||||
int width;
|
|
||||||
int height;
|
|
||||||
int velocity;
|
int velocity;
|
||||||
int controls[3];
|
int controls[3];
|
||||||
} Player;
|
} Player;
|
||||||
@@ -21,14 +18,14 @@ int init_vel = 300;
|
|||||||
|
|
||||||
void move_player(Player* p){
|
void move_player(Player* p){
|
||||||
if (IsKeyDown(p->controls[MOVE_DOWN])){
|
if (IsKeyDown(p->controls[MOVE_DOWN])){
|
||||||
p->y += p->velocity * GetFrameTime();
|
p->rec.y += p->velocity * GetFrameTime();
|
||||||
if (p->y > screenHeight){
|
if (p->rec.y > screenHeight){
|
||||||
p->y = -p->height;
|
p->rec.y = -p->rec.height;
|
||||||
}
|
}
|
||||||
} else if (IsKeyDown(p->controls[MOVE_UP])){
|
} else if (IsKeyDown(p->controls[MOVE_UP])){
|
||||||
p->y -= p->velocity * GetFrameTime();
|
p->rec.y -= p->velocity * GetFrameTime();
|
||||||
if (p->y < -p->height){
|
if (p->rec.y < -p->rec.height){
|
||||||
p->y = screenHeight;
|
p->rec.y = screenHeight;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -44,29 +41,30 @@ void apply_boost(Player* p){
|
|||||||
}
|
}
|
||||||
|
|
||||||
void draw_player(Player* p){
|
void draw_player(Player* p){
|
||||||
DrawRectangle(p->x, p->y, p->width, p->height, BLACK);
|
DrawRectangleRec(p->rec, BLACK);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
typedef struct ball {
|
typedef struct ball {
|
||||||
float x;
|
Vector2 pos;
|
||||||
float y;
|
|
||||||
float r;
|
float r;
|
||||||
int vel_x;
|
int vel_x;
|
||||||
int vel_y;
|
int vel_y;
|
||||||
Player* target;
|
Player* target;
|
||||||
} Ball;
|
} Ball;
|
||||||
|
|
||||||
void move_ball(Ball *b){
|
void move_ball(Ball *b, Player p[2]){
|
||||||
b->y += b->vel_y * GetFrameTime();
|
b->pos.y += b->vel_y * GetFrameTime();
|
||||||
if (b->y < b->r || b->y > screenHeight - b->r) {
|
if (b->pos.y < b->r || b->pos.y > screenHeight - b->r) {
|
||||||
b->vel_y *= -1;
|
b->vel_y *= -1;
|
||||||
b->y += b->vel_y * GetFrameTime();
|
b->pos.y += b->vel_y * GetFrameTime();
|
||||||
}
|
}
|
||||||
b->x += b->vel_x * GetFrameTime();
|
b->pos.x += b->vel_x * GetFrameTime();
|
||||||
if (b->x < b->r || b->x > screenWidth - b->r) {
|
if (b->pos.x < b->r || b->pos.x > screenWidth - b->r ||
|
||||||
|
CheckCollisionCircleRec(b->pos, b->r, p[0].rec) ||
|
||||||
|
CheckCollisionCircleRec(b->pos, b->r, p[1].rec)) {
|
||||||
b->vel_x *= -1;
|
b->vel_x *= -1;
|
||||||
b->x += b->vel_x * GetFrameTime();
|
b->pos.x += b->vel_x * GetFrameTime();
|
||||||
}
|
}
|
||||||
/* if (p->x > p->target->){ */
|
/* if (p->x > p->target->){ */
|
||||||
|
|
||||||
@@ -74,17 +72,17 @@ void move_ball(Ball *b){
|
|||||||
}
|
}
|
||||||
|
|
||||||
void draw_ball(Ball* b){
|
void draw_ball(Ball* b){
|
||||||
DrawCircle(b->x, b->y, b->r, BLACK);
|
DrawCircleV(b->pos, b->r, BLACK);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(void){
|
int main(void){
|
||||||
InitWindow(screenWidth, screenHeight, "Pong");
|
InitWindow(screenWidth, screenHeight, "Pong");
|
||||||
Player p[2] =
|
Player p[2] =
|
||||||
{{ .x = 30, .y = 30, .width = 10, .height = 60, .velocity = init_vel,
|
{{ .rec = {.x = 30, .y = 30, .width = 10, .height = 60}, .velocity = init_vel,
|
||||||
.controls = {KEY_W, KEY_S, KEY_LEFT_SHIFT}},
|
.controls = {KEY_W, KEY_S, KEY_LEFT_SHIFT}},
|
||||||
{ .x = screenWidth - 30, .width = 10, .height = 60, .velocity = init_vel,
|
{ .rec = {.x = screenWidth - 30, .width = 10, .height = 60}, .velocity = init_vel,
|
||||||
.controls = {KEY_UP, KEY_DOWN, KEY_RIGHT_SHIFT}}};
|
.controls = {KEY_UP, KEY_DOWN, KEY_RIGHT_SHIFT}}};
|
||||||
Ball ball = { .x = screenWidth / 2, .y = screenHeight / 2, .r = 10,
|
Ball ball = { .pos = {.x = screenWidth / 2, .y = screenHeight / 2}, .r = 10,
|
||||||
.vel_x = 300, .vel_y = 300};
|
.vel_x = 300, .vel_y = 300};
|
||||||
|
|
||||||
while (!WindowShouldClose()){
|
while (!WindowShouldClose()){
|
||||||
@@ -95,7 +93,7 @@ int main(void){
|
|||||||
move_player(&p[0]);
|
move_player(&p[0]);
|
||||||
move_player(&p[1]);
|
move_player(&p[1]);
|
||||||
|
|
||||||
move_ball(&ball);
|
move_ball(&ball, p);
|
||||||
|
|
||||||
BeginDrawing();
|
BeginDrawing();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user