Rewritten to use structs for ball and paddle variables, thereby grouping everything together and keeping the functions simple/easy to read

This commit is contained in:
Joe Adams 2017-12-13 10:12:21 +00:00
parent 00a089f6f9
commit 7c66002c7a

View File

@ -17,11 +17,27 @@ typedef struct colour {
double a; double a;
} colour; } colour;
void updatePaddle(Uint32 tickrate, double *paddleX, char moveX, int paddleWidth, int winWidth, int winHeight); typedef struct paddle {
void updateBall(Uint32 tickrate, double *ballX, double *ballY, double *ballVX, double *ballVY, double paddleX, int paddleWidth, int winWidth, int winHeight); int width;
int height;
double x;
} paddle;
void drawPaddle(double paddleX, int paddleWidth, int winWidth, int winHeight, colour *c); typedef struct ball {
void drawBall(double ballX, double ballY, colour *c); double x;
double y;
double vX;
double vY;
double initVX;
double initVY;
double radius;
} ball;
void updatePaddle(Uint32 tickrate, paddle *thePaddle, char moveX, int winWidth, int winHeight);
void updateBall(Uint32 tickrate, ball *theBall, paddle *thePaddle, int winWidth, int winHeight);
void drawPaddle(paddle *thePaddle, int winWidth, int winHeight, colour *c);
void drawBall(ball *theBall, colour *c);
void drawBg(int winWidth, int winHeight, colour *c1, colour *c2); void drawBg(int winWidth, int winHeight, colour *c1, colour *c2);
int main(void) { int main(void) {
@ -124,12 +140,21 @@ int main(void) {
char moveX=0; char moveX=0;
int paddleWidth=50; ball theBall = {
double paddleX=(winWidth/2)-(paddleWidth/2); .x = 0,
double ballX = 0; .y = 0,
double ballY = 0; .vX = 0.4f,
double ballVX = 0.75; .vY = -1.5f,
double ballVY = -1.5; .initVX = theBall.vX,
.initVY = theBall.vY,
.radius = 4
};
paddle thePaddle = {
.width = 50,
.height = 10,
.x = (winWidth/2)-(thePaddle.width/2)
};
/* /*
* ---------------- * ----------------
@ -196,8 +221,8 @@ int main(void) {
* ---------------- * ----------------
*/ */
updatePaddle(tickrate, &paddleX, moveX, paddleWidth, winWidth, winHeight); updatePaddle(tickrate, &thePaddle, moveX, winWidth, winHeight);
updateBall(tickrate, &ballX, &ballY, &ballVX, &ballVY, paddleX, paddleWidth, winWidth, winHeight); updateBall(tickrate, &theBall, &thePaddle, winWidth, winHeight);
/* /*
* ---------------- * ----------------
@ -223,8 +248,8 @@ int main(void) {
bgcol1.b = 0.4431; /*113*/ bgcol2.b = 0.6078; /*155*/ bgcol1.b = 0.4431; /*113*/ bgcol2.b = 0.6078; /*155*/
bgcol1.a = 1.0000; /*255*/ bgcol2.a = 1.0000; /*255*/ bgcol1.a = 1.0000; /*255*/ bgcol2.a = 1.0000; /*255*/
drawPaddle( paddleX, paddleWidth, winWidth, winHeight, &paddleCol); drawPaddle(&thePaddle, winWidth, winHeight, &paddleCol);
drawBall(ballX, ballY, &paddleCol); drawBall(&theBall, &paddleCol);
drawBg(winWidth, winHeight, &bgcol1, &bgcol2); drawBg(winWidth, winHeight, &bgcol1, &bgcol2);
glFlush(); glFlush();
@ -249,40 +274,40 @@ int main(void) {
return 0; return 0;
} }
void updatePaddle(Uint32 tickrate, double *paddleX, char moveX, int paddleWidth, int winWidth, int winHeight) { void updatePaddle(Uint32 tickrate, paddle *thePaddle, char moveX, int winWidth, int winHeight) {
*paddleX += moveX * 0.5 * tickrate; thePaddle->x += moveX * 0.5 * tickrate;
if (*paddleX < 0-(paddleWidth/2)) *paddleX = 0-(paddleWidth/2); if (thePaddle->x < 0-(thePaddle->width/2)) thePaddle->x = 0-(thePaddle->width/2);
if (*paddleX > winWidth-(paddleWidth/2)) *paddleX = winWidth-(paddleWidth/2); if (thePaddle->x > winWidth-(thePaddle->width/2)) thePaddle->x = winWidth-(thePaddle->width/2);
} }
void updateBall(Uint32 tickrate, double *ballX, double *ballY, double *ballVX, double *ballVY, double paddleX, int paddleWidth, int winWidth, int winHeight) { void updateBall(Uint32 tickrate, ball *theBall, paddle *thePaddle, int winWidth, int winHeight) {
double ballNX = *ballX + *ballVX; // Calculates the position of the ball on the next step double ballNX = theBall->x + theBall->vX; // Calculates the position of the ball on the next step
double ballNY = *ballY + *ballVY; double ballNY = theBall->y + theBall->vY;
double paddleRX = paddleX - (double)(winWidth / 2); double paddleRX = thePaddle->x - (double)(winWidth / 2);
double paddleRY = 20 - (double)(winHeight / 2); double paddleRY = 20 - (double)(winHeight / 2);
if (ballNX > paddleRX && ballNY<paddleRY) { // If to the right or below the top left corner of paddle if (ballNX > paddleRX && ballNY<paddleRY) { // If to the right or below the top left corner of paddle
if (ballNX < paddleRX + (double)paddleWidth) { // ...and to the left of the right side of the paddle (i.e. about to go inside inside the paddle) if (ballNX < paddleRX + (double)thePaddle->width) { // ...and to the left of the right side of the paddle (i.e. about to go inside inside the paddle)
*ballVY *= -1.0; theBall->vY *= -1.0;
*ballVX = ((ballNX - paddleRX - (double)(paddleWidth/2))/((double)paddleWidth/2))*0.5; // Sets X velocity to be proportional to the relative position of the ball and the paddle on collision theBall->vX = ((ballNX - paddleRX - (double)(thePaddle->width/2))/((double)thePaddle->width/2))*0.5; // Sets X velocity to be proportional to the relative position of the ball and the paddle on collision
} }
} }
if (ballNX > winWidth / 2 || ballNX < (-1.0*winWidth/2)) { // Collision with walls if (ballNX > winWidth / 2 || ballNX < (-1.0*winWidth/2)) { // Collision with walls
*ballVX *= -1.0; theBall->vX *= -1.0;
} }
if (ballNY > winHeight / 2) { // Collision with ceiling if (ballNY > winHeight / 2) { // Collision with ceiling
*ballVY *= -1.0; theBall->vY *= -1.0;
} }
if (ballNY < (-1.0*winHeight/2)) { // Collision with floor if (ballNY < (-1.0*winHeight/2)) { // Collision with floor
*ballX = 0; theBall->x = 0;
*ballY = 0; theBall->y = 0;
*ballVX = -0.75; theBall->vX = theBall->initVX;
*ballVY = -1.5; theBall->vY = theBall->initVY;
} }
*ballX += *ballVX * 0.1 * tickrate; theBall->x += theBall->vX * 0.1 * tickrate;
*ballY += *ballVY * 0.1 * tickrate; theBall->y += theBall->vY * 0.1 * tickrate;
} }
void drawBg(int winWidth, int winHeight, colour *c1, colour *c2) { void drawBg(int winWidth, int winHeight, colour *c1, colour *c2) {
@ -303,29 +328,29 @@ void drawBg(int winWidth, int winHeight, colour *c1, colour *c2) {
glEnd(); glEnd();
} }
void drawPaddle(double paddleX, int paddleWidth, int winWidth, int winHeight, colour *c) { void drawPaddle(paddle *thePaddle, int winWidth, int winHeight, colour *c) {
GLint matrixmode = 0; GLint matrixmode = 0;
glGetIntegerv(GL_MATRIX_MODE, &matrixmode); glGetIntegerv(GL_MATRIX_MODE, &matrixmode);
glPushMatrix(); glPushMatrix();
glTranslated((GLdouble)(paddleX)-winWidth/2, (GLdouble)(20-winHeight/2), 0.0); glTranslated((GLdouble)(thePaddle->x)-winWidth/2, (GLdouble)(20-winHeight/2), 0.0);
glBegin(GL_QUADS); glBegin(GL_QUADS);
glColor3f(c->r, c->g, c->b); glColor3f(c->r, c->g, c->b);
glVertex3d(0.0, 0.0, 0.0); glVertex3d(0.0, 0.0, 0.0);
glVertex3d(0.0, 10.0, 0.0); glVertex3d(0.0, (double)thePaddle->height, 0.0);
glVertex3d((double)paddleWidth, 10.0, 0.0); glVertex3d((double)thePaddle->width, (double)thePaddle->height, 0.0);
glVertex3d((double)paddleWidth, 0.0, 0.0); glVertex3d((double)thePaddle->width, 0.0, 0.0);
glEnd(); glEnd();
glPopMatrix(); glPopMatrix();
glMatrixMode(matrixmode); glMatrixMode(matrixmode);
} }
void drawBall(double ballX, double ballY, colour *c) { void drawBall(ball *theBall, colour *c) {
GLint matrixmode = 0; GLint matrixmode = 0;
glGetIntegerv(GL_MATRIX_MODE, &matrixmode); glGetIntegerv(GL_MATRIX_MODE, &matrixmode);
glPushMatrix(); glPushMatrix();
glTranslated(ballX, ballY, 0.0); glTranslated(theBall->x, theBall->y, 0.0);
glBegin(GL_QUADS); glBegin(GL_QUADS);
glColor3f(c->r, c->g, c->b); glColor3f(c->r, c->g, c->b);