Set ball starting position next to the rackets

This commit is contained in:
Pradana AUMARS 2021-06-28 15:09:39 +02:00
parent 05addb3baa
commit c5e8473d79
3 changed files with 11 additions and 4 deletions

View File

@ -5,13 +5,14 @@
#include "racket.h" #include "racket.h"
Ball* Ball*
Ball_init(int screen_width, int screen_height) Ball_init(int screen_width, int screen_height, int posX_start)
{ {
srand(time(NULL)); srand(time(NULL));
Ball *b = malloc(sizeof(Ball)); Ball *b = malloc(sizeof(Ball));
*(int *)&b->BALL_WIDTH = 20; *(int *)&b->BALL_WIDTH = 20;
*(int *)&b->BALL_HEIGHT = 20; *(int *)&b->BALL_HEIGHT = 20;
*(int *)&b->BALL_VELOCITY = 7; *(int *)&b->BALL_VELOCITY = 7;
*(int *)&b->BALL_POSX_START = posX_start;
*(int *)&b->SCREEN_WIDTH = screen_width; *(int *)&b->SCREEN_WIDTH = screen_width;
*(int *)&b->SCREEN_HEIGHT = screen_height; *(int *)&b->SCREEN_HEIGHT = screen_height;
Ball_reset(b, 1); Ball_reset(b, 1);
@ -25,7 +26,12 @@ Ball_reset(Ball *b, int direction) {
do { do {
angle = rand() % 60 + 60; angle = rand() % 60 + 60;
} while (angle == 90); } while (angle == 90);
b->posX = b->SCREEN_WIDTH/2; if (direction < 0) {
b->posX = b->BALL_POSX_START;
}
else {
b->posX = b->SCREEN_WIDTH - b->BALL_POSX_START;
}
b->posY = b->SCREEN_HEIGHT/2; b->posY = b->SCREEN_HEIGHT/2;
b->velX = direction * b->BALL_VELOCITY * cos(angle * 2 * M_PI / 180); b->velX = direction * b->BALL_VELOCITY * cos(angle * 2 * M_PI / 180);
b->velY = b->BALL_VELOCITY * sin(angle * 2 * M_PI / 180); b->velY = b->BALL_VELOCITY * sin(angle * 2 * M_PI / 180);

View File

@ -10,6 +10,7 @@ typedef struct Ball {
const int BALL_WIDTH; const int BALL_WIDTH;
const int BALL_HEIGHT; const int BALL_HEIGHT;
const int BALL_VELOCITY; const int BALL_VELOCITY;
const int BALL_POSX_START;
const int SCREEN_WIDTH; const int SCREEN_WIDTH;
const int SCREEN_HEIGHT; const int SCREEN_HEIGHT;
int posX; int posX;
@ -18,7 +19,7 @@ typedef struct Ball {
int velY; int velY;
} Ball; } Ball;
Ball* Ball_init(int screen_width, int screen_height); Ball* Ball_init(int screen_width, int screen_height, int posX_start);
void Ball_reset(Ball *b, int direction); void Ball_reset(Ball *b, int direction);
void Ball_free(Ball *b); void Ball_free(Ball *b);
//void Ball_handle_event(Ball *b, SDL_Event *e); //void Ball_handle_event(Ball *b, SDL_Event *e);

View File

@ -10,7 +10,7 @@ Pong_init() {
*(int *)&p->SCREEN_WIDTH = 640; *(int *)&p->SCREEN_WIDTH = 640;
*(int *)&p->SCREEN_HEIGHT = 480; *(int *)&p->SCREEN_HEIGHT = 480;
p->ball = Ball_init(p->SCREEN_WIDTH, p->SCREEN_HEIGHT); p->ball = Ball_init(p->SCREEN_WIDTH, p->SCREEN_HEIGHT, 100);
p->racketL = Racket_init(p->SCREEN_WIDTH, p->SCREEN_HEIGHT, 50, 50, SDL_SCANCODE_E, SDL_SCANCODE_D); p->racketL = Racket_init(p->SCREEN_WIDTH, p->SCREEN_HEIGHT, 50, 50, SDL_SCANCODE_E, SDL_SCANCODE_D);
p->racketR = Racket_init(p->SCREEN_WIDTH, p->SCREEN_HEIGHT, p->SCREEN_WIDTH - 50, 50, SDL_SCANCODE_I, SDL_SCANCODE_K); p->racketR = Racket_init(p->SCREEN_WIDTH, p->SCREEN_HEIGHT, p->SCREEN_WIDTH - 50, 50, SDL_SCANCODE_I, SDL_SCANCODE_K);