diff --git a/breakout/breakout.c b/breakout/breakout.c index a462fc0..4230f60 100644 --- a/breakout/breakout.c +++ b/breakout/breakout.c @@ -17,7 +17,8 @@ typedef struct colour { double a; } colour; -void updateBall(double *ballX, double *ballY, double *ballVX, double *ballVY, double paddleX, int paddleWidth, int winWidth, int winHeight); +void updatePaddle(Uint32 tickrate, double *paddleX, char moveX, int paddleWidth, int winWidth, int winHeight); +void updateBall(Uint32 tickrate, double *ballX, double *ballY, double *ballVX, double *ballVY, double paddleX, int paddleWidth, int winWidth, int winHeight); void drawPaddle(double paddleX, int paddleWidth, int winWidth, int winHeight, colour *c); void drawBall(double ballX, double ballY, colour *c); @@ -127,8 +128,8 @@ int main(void) { double paddleX=(winWidth/2)-(paddleWidth/2); double ballX = 0; double ballY = 0; - double ballVX = 0.2; - double ballVY = -0.3; + double ballVX = 0.75; + double ballVY = -1.5; /* * ---------------- @@ -146,10 +147,10 @@ int main(void) { */ - Uint32 tickspassed=SDL_GetTicks()-timer; + Uint32 tickrate=SDL_GetTicks()-timer; timer=SDL_GetTicks(); - int fps=1000/(tickspassed+1); - printf("Time to draw: %u, fps: %i \r", tickspassed, fps); fflush(stdout); + int fps=1000/(tickrate+1); + printf("Time to draw: %u, fps: %i \r", tickrate, fps); fflush(stdout); /* * ---------------- @@ -188,12 +189,15 @@ int main(void) { break; } } - - paddleX += moveX*0.1; - updateBall(&ballX, &ballY, &ballVX, &ballVY, paddleX, paddleWidth, winWidth, winHeight); - ballX += ballVX*0.1; - ballY += ballVY*0.1; + /* + * ---------------- + * Update elements + * ---------------- + */ + + updatePaddle(tickrate, &paddleX, moveX, paddleWidth, winWidth, winHeight); + updateBall(tickrate, &ballX, &ballY, &ballVX, &ballVY, paddleX, paddleWidth, winWidth, winHeight); /* * ---------------- @@ -219,7 +223,7 @@ int main(void) { bgcol1.b = 0.4431; /*113*/ bgcol2.b = 0.6078; /*155*/ bgcol1.a = 1.0000; /*255*/ bgcol2.a = 1.0000; /*255*/ - drawPaddle(paddleX, paddleWidth, winWidth, winHeight, &paddleCol); + drawPaddle( paddleX, paddleWidth, winWidth, winHeight, &paddleCol); drawBall(ballX, ballY, &paddleCol); drawBg(winWidth, winHeight, &bgcol1, &bgcol2); @@ -245,7 +249,13 @@ int main(void) { return 0; } -void updateBall(double *ballX, double *ballY, double *ballVX, double *ballVY, double paddleX, int paddleWidth, int winWidth, int winHeight) { +void updatePaddle(Uint32 tickrate, double *paddleX, char moveX, int paddleWidth, int winWidth, int winHeight) { + *paddleX += moveX * 0.5 * tickrate; + if (*paddleX < 0-(paddleWidth/2)) *paddleX = 0-(paddleWidth/2); + if (*paddleX > winWidth-(paddleWidth/2)) *paddleX = winWidth-(paddleWidth/2); +} + +void updateBall(Uint32 tickrate, double *ballX, double *ballY, double *ballVX, double *ballVY, double paddleX, int paddleWidth, int winWidth, int winHeight) { double ballNX = *ballX + *ballVX; // Calculates the position of the ball on the next step double ballNY = *ballY + *ballVY; double paddleRX = paddleX - (double)(winWidth / 2); @@ -267,9 +277,12 @@ void updateBall(double *ballX, double *ballY, double *ballVX, double *ballVY, do if (ballNY < (-1.0*winHeight/2)) { // Collision with floor *ballX = 0; *ballY = 0; - *ballVX = -0.1; - *ballVY = -0.5; + *ballVX = -0.75; + *ballVY = -1.5; } + + *ballX += *ballVX * 0.1 * tickrate; + *ballY += *ballVY * 0.1 * tickrate; } void drawBg(int winWidth, int winHeight, colour *c1, colour *c2) {