diff --git a/src/Makefile.am b/src/Makefile.am index 8c71a01..027165d 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -9,4 +9,4 @@ pong_SOURCES = \ racket.c \ racket.h pong_CFLAGS = $(SDL_CFLAGS) -pong_LDFLAGS = $(SDL_LIBS) +pong_LDFLAGS = $(SDL_LIBS) -lm diff --git a/src/ball.c b/src/ball.c index da6d7d5..9ccde49 100644 --- a/src/ball.c +++ b/src/ball.c @@ -1,9 +1,13 @@ +#include // srand, rand +#include // time +#include // sin, cos, M_PI #include "ball.h" #include "racket.h" Ball* Ball_init(int screen_width, int screen_height) { + srand(time(NULL)); Ball *b = malloc(sizeof(Ball)); *(int *)&b->BALL_WIDTH = 20; *(int *)&b->BALL_HEIGHT = 20; @@ -16,10 +20,12 @@ Ball_init(int screen_width, int screen_height) void Ball_reset(Ball *b, int direction) { + // 120 deg angle with norm horizontal vector + const int angle = rand() % 60 + 60; b->posX = b->SCREEN_WIDTH/2; b->posY = b->SCREEN_HEIGHT/2; - b->velX = direction * 7; - b->velY = 5; + b->velX = direction * b->BALL_VELOCITY * cos(angle * 2 * M_PI / 180); + b->velY = b->BALL_VELOCITY * sin(angle * 2 * M_PI / 180); } void