diff options
Diffstat (limited to 'pong')
| -rw-r--r-- | pong/pong.c | 48 | 
1 files changed, 23 insertions, 25 deletions
| diff --git a/pong/pong.c b/pong/pong.c index ea59f16..8e0e2fa 100644 --- a/pong/pong.c +++ b/pong/pong.c @@ -10,10 +10,7 @@ enum control_keys {  } control_key;  typedef struct player { -    float x; -    float y; -    int width; -    int height; +    Rectangle rec;      int velocity;      int controls[3];  } Player; @@ -21,14 +18,14 @@ int init_vel = 300;  void move_player(Player* p){      if (IsKeyDown(p->controls[MOVE_DOWN])){ -        p->y += p->velocity * GetFrameTime(); -        if (p->y > screenHeight){ -            p->y = -p->height; +        p->rec.y += p->velocity * GetFrameTime(); +        if (p->rec.y > screenHeight){ +            p->rec.y = -p->rec.height;          }      } else if (IsKeyDown(p->controls[MOVE_UP])){ -        p->y -= p->velocity * GetFrameTime(); -        if (p->y < -p->height){ -            p->y = screenHeight; +        p->rec.y -= p->velocity * GetFrameTime(); +        if (p->rec.y < -p->rec.height){ +            p->rec.y = screenHeight;          }      }  } @@ -44,29 +41,30 @@ void apply_boost(Player* p){  }  void draw_player(Player* p){ -    DrawRectangle(p->x, p->y, p->width, p->height, BLACK); +    DrawRectangleRec(p->rec, BLACK);  }  typedef struct ball { -    float x; -    float y; +    Vector2 pos;      float r;      int vel_x;      int vel_y;      Player* target;  } Ball; -void move_ball(Ball *b){ -    b->y += b->vel_y * GetFrameTime(); -    if (b->y < b->r || b->y > screenHeight - b->r) { +void move_ball(Ball *b, Player p[2]){ +    b->pos.y += b->vel_y * GetFrameTime(); +    if (b->pos.y < b->r || b->pos.y > screenHeight - b->r) {          b->vel_y *= -1; -        b->y += b->vel_y * GetFrameTime(); +        b->pos.y += b->vel_y * GetFrameTime();      } -    b->x += b->vel_x * GetFrameTime(); -    if (b->x < b->r || b->x > screenWidth - b->r) { +    b->pos.x += b->vel_x * GetFrameTime(); +    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->x += b->vel_x * GetFrameTime(); +        b->pos.x += b->vel_x * GetFrameTime();      }      /* if (p->x > p->target->){ */ @@ -74,17 +72,17 @@ void move_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){      InitWindow(screenWidth, screenHeight, "Pong");      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}}, -         { .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}}}; -    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};      while (!WindowShouldClose()){ @@ -95,7 +93,7 @@ int main(void){          move_player(&p[0]);          move_player(&p[1]); -        move_ball(&ball); +        move_ball(&ball, p);          BeginDrawing(); | 
