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:
parent
00a089f6f9
commit
7c66002c7a
@ -17,11 +17,27 @@ typedef struct colour {
|
||||
double a;
|
||||
} colour;
|
||||
|
||||
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);
|
||||
typedef struct paddle {
|
||||
int width;
|
||||
int height;
|
||||
double x;
|
||||
} paddle;
|
||||
|
||||
void drawPaddle(double paddleX, int paddleWidth, int winWidth, int winHeight, colour *c);
|
||||
void drawBall(double ballX, double ballY, colour *c);
|
||||
typedef struct ball {
|
||||
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);
|
||||
|
||||
int main(void) {
|
||||
@ -124,12 +140,21 @@ int main(void) {
|
||||
|
||||
char moveX=0;
|
||||
|
||||
int paddleWidth=50;
|
||||
double paddleX=(winWidth/2)-(paddleWidth/2);
|
||||
double ballX = 0;
|
||||
double ballY = 0;
|
||||
double ballVX = 0.75;
|
||||
double ballVY = -1.5;
|
||||
ball theBall = {
|
||||
.x = 0,
|
||||
.y = 0,
|
||||
.vX = 0.4f,
|
||||
.vY = -1.5f,
|
||||
.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);
|
||||
updateBall(tickrate, &ballX, &ballY, &ballVX, &ballVY, paddleX, paddleWidth, winWidth, winHeight);
|
||||
updatePaddle(tickrate, &thePaddle, moveX, 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.a = 1.0000; /*255*/ bgcol2.a = 1.0000; /*255*/
|
||||
|
||||
drawPaddle( paddleX, paddleWidth, winWidth, winHeight, &paddleCol);
|
||||
drawBall(ballX, ballY, &paddleCol);
|
||||
drawPaddle(&thePaddle, winWidth, winHeight, &paddleCol);
|
||||
drawBall(&theBall, &paddleCol);
|
||||
drawBg(winWidth, winHeight, &bgcol1, &bgcol2);
|
||||
|
||||
glFlush();
|
||||
@ -249,40 +274,40 @@ int main(void) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
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 updatePaddle(Uint32 tickrate, paddle *thePaddle, char moveX, int winWidth, int winHeight) {
|
||||
thePaddle->x += moveX * 0.5 * tickrate;
|
||||
if (thePaddle->x < 0-(thePaddle->width/2)) thePaddle->x = 0-(thePaddle->width/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) {
|
||||
double ballNX = *ballX + *ballVX; // Calculates the position of the ball on the next step
|
||||
double ballNY = *ballY + *ballVY;
|
||||
double paddleRX = paddleX - (double)(winWidth / 2);
|
||||
void updateBall(Uint32 tickrate, ball *theBall, paddle *thePaddle, int winWidth, int winHeight) {
|
||||
double ballNX = theBall->x + theBall->vX; // Calculates the position of the ball on the next step
|
||||
double ballNY = theBall->y + theBall->vY;
|
||||
double paddleRX = thePaddle->x - (double)(winWidth / 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 + (double)paddleWidth) { // ...and to the left of the right side of the paddle (i.e. about to go inside inside the paddle)
|
||||
*ballVY *= -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
|
||||
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)
|
||||
theBall->vY *= -1.0;
|
||||
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
|
||||
*ballVX *= -1.0;
|
||||
theBall->vX *= -1.0;
|
||||
}
|
||||
if (ballNY > winHeight / 2) { // Collision with ceiling
|
||||
*ballVY *= -1.0;
|
||||
theBall->vY *= -1.0;
|
||||
}
|
||||
if (ballNY < (-1.0*winHeight/2)) { // Collision with floor
|
||||
*ballX = 0;
|
||||
*ballY = 0;
|
||||
*ballVX = -0.75;
|
||||
*ballVY = -1.5;
|
||||
theBall->x = 0;
|
||||
theBall->y = 0;
|
||||
theBall->vX = theBall->initVX;
|
||||
theBall->vY = theBall->initVY;
|
||||
}
|
||||
|
||||
*ballX += *ballVX * 0.1 * tickrate;
|
||||
*ballY += *ballVY * 0.1 * tickrate;
|
||||
theBall->x += theBall->vX * 0.1 * tickrate;
|
||||
theBall->y += theBall->vY * 0.1 * tickrate;
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
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;
|
||||
glGetIntegerv(GL_MATRIX_MODE, &matrixmode);
|
||||
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);
|
||||
glColor3f(c->r, c->g, c->b);
|
||||
glVertex3d(0.0, 0.0, 0.0);
|
||||
glVertex3d(0.0, 10.0, 0.0);
|
||||
glVertex3d((double)paddleWidth, 10.0, 0.0);
|
||||
glVertex3d((double)paddleWidth, 0.0, 0.0);
|
||||
glVertex3d(0.0, (double)thePaddle->height, 0.0);
|
||||
glVertex3d((double)thePaddle->width, (double)thePaddle->height, 0.0);
|
||||
glVertex3d((double)thePaddle->width, 0.0, 0.0);
|
||||
glEnd();
|
||||
|
||||
glPopMatrix();
|
||||
glMatrixMode(matrixmode);
|
||||
}
|
||||
|
||||
void drawBall(double ballX, double ballY, colour *c) {
|
||||
void drawBall(ball *theBall, colour *c) {
|
||||
GLint matrixmode = 0;
|
||||
glGetIntegerv(GL_MATRIX_MODE, &matrixmode);
|
||||
glPushMatrix();
|
||||
glTranslated(ballX, ballY, 0.0);
|
||||
glTranslated(theBall->x, theBall->y, 0.0);
|
||||
|
||||
glBegin(GL_QUADS);
|
||||
glColor3f(c->r, c->g, c->b);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user