first game.. pong

This topic is 4613 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

well i just started on this a while ago, and it's my first attempt and making an actual game... so far it's doing fine, but the ball dosent move as i've programmed it to.. i though about using a loop to make it move, but then i would have no keyboard input and a ball that just moves accross the screen forever, here is the code i've written so far.. i've commented it as much as possible since i ussualy think a little wierd when coming up with things... well here it is.. i really cant figure out how to make that ball move..
#include <gl/glut.h>
int balld1=0;//0 if the ball is going left, 1 if it's heading right
int balld2=0;//0 if it's going up, 1 if it's going down
float ballx;//the balls current and old x position
float bally;//the balls current and old y position
float paddlex;//well you get the idea..
void key();
void draw();
int main(int argc, char** argv){
ballx=-0.10;
bally=0;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA);
glutInitWindowSize(300, 300);
glutInitWindowPosition(100, 100);
glutCreateWindow("Ill Pong");
glClearColor(0.0, 0.0, 0.0, 0.0);
glutDisplayFunc(draw);
//glutKeyboardFunc(key); //haven't made this function yet..
glutMainLoop();
return 0;
}
void draw(){
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glEnd();
glEnd();
if(balld1==0 && balld2 == 0){ballx=ballx-0.1;bally=bally+0.2;}//makes the ball go up and left
glVertex2f(ballx, bally);//bottom left corner
glVertex2f(ballx+0.10, bally);// bottom right corner
glVertex2f(ballx+0.10, bally+0.10);//top right corner
glVertex2f(ballx, bally+0.10);//top left corner
glEnd();
glFlush();
}

Share on other sites
What happens when you run the program, is the ball stuck or aren't it drawn at all?

Share on other sites
the ball is drawn, but only once. after that it just holds still

Share on other sites
I'm not sure how you can fix it, but by adding this code:

static int Count = 0;
++Count;
char* Buffer = new char[512];
sprintf(Buffer,"%d\n",Count);
OutputDebugString(Buffer);

to the end of draw function I could see that it's only called once, I don't know glut or opengl so can't help you with why it is only called one time. One time I noticed though is that if I but a breakpoint in the draw function and just hit continue everytime it will be executed as many time as I press continue.

EDIT: I don't know if you are running Linux, but on Windows OutputDebugString writes the string to the debugger.

Share on other sites
alright, i found out about a function called postredisplay which redraws the screen.. it works now but the ball flies off the screen too fast to see it.. but i'll change that soon.. thanks for the help.

illmaster

Share on other sites
alright, after a few hours and this whole morning, i am done.. EXCEPT... player one always wins and the ball is too fast.. so fast that is actually seems to just.. disappear to thin air... i dont know why player one allways wins, and i dont have any clue how to slow down the ball.. heres the code... any help is much appreciated..

#include <gl/glut.h>#include <stdio.h>int score1=0, score2=0;//current scoresint balld1=0;//0 if the ball is going left, 1 if it's heading rightint balld2=0;//0 if it's going up, 1 if it's going downfloat ballx;//the balls current and old x positionfloat bally;//the balls current and old y positionfloat paddlex;//well you get the idea..float paddley;float paddle2x;float paddle2y;void control(unsigned char key, int x, int y);void draw();int main(int argc, char** argv){    paddlex=0.75;//default values for the paddle's positions    paddley=0.0;    paddle2x=-0.75;    paddle2y=0.0;    ballx=-0.10;    bally=0;    glutInit(&argc, argv);    glutInitDisplayMode(GLUT_DOUBLE);    glutInitWindowSize(300, 300);    glutInitWindowPosition(100, 100);    glutCreateWindow("Ill Pong");    glClearColor(0.0, 0.0, 0.0, 0.0);    glutDisplayFunc(draw);    glutKeyboardFunc(control); //handles keypresses    glutMainLoop();    return 0;    }void draw(){     glClear(GL_COLOR_BUFFER_BIT);     glColor3f(1.0, 1.0, 1.0);     glBegin(GL_QUADS);//the right paddle     glVertex2f(paddlex, paddley);//bottom left corner     glVertex2f(paddlex+0.10, paddley);//bottom right corner     glVertex2f(paddlex+0.10, paddley+0.50);//top right corner     glVertex2f(paddlex, paddley+0.50);//top left corner     glEnd();     glBegin(GL_QUADS);//the left paddle     glVertex2f(paddle2x, paddle2y);//bottom left corner     glVertex2f(paddle2x-0.10, paddle2y);//bottom right corner     glVertex2f(paddle2x-0.10, paddle2y-0.50);//top right corner     glVertex2f(paddle2x, paddle2y-0.50);//top left corner     glEnd();     if(balld1==0 && balld2==0){ballx=ballx-0.1;bally=bally+0.2;}//makes the ball go left and up     if(balld1==0 && balld2==1){ballx=ballx-0.1;bally=bally-0.2;}//makes the ball go left and down     if(balld1==1 && balld2==0){ballx=ballx+0.1;bally=bally+0.2;}//makes the ball go right and up     if(balld1==1 && balld2==1){ballx=ballx+0.1;bally=bally-0.2;}//makes the ball go right and down     if(ballx>1 && balld2==1){score2++;balld1=0; balld2=0;}//these four handle the scores and turns the ball     if(ballx>1 && balld2==0){score2++;balld1=0; balld2=1;}     if(ballx>-1 && balld2==1){score1++;balld1=1; balld2=0;}     if(ballx>-1 && balld2==0){score1++;balld1=1; balld2=1;}     if(bally>0.50 || bally>1.0)balld2=1;//sends the ball down if it hits the top     if(bally<-0.50 || bally<-1.0)balld2=0;//sends the ball up if it hits the bottom     if(ballx==paddlex && bally>=paddley && bally<=paddley && balld2==0){balld1=0;balld2=1;}//sends the ball left and down     if(ballx==paddlex && bally>=paddley && bally<=paddley && balld2==1){balld1=0;balld2=0;}//sends the ball left and up     if(ballx==paddle2x && bally>=paddle2y && bally<=paddle2y && balld2==0){balld1=1;balld2=1;}//sends the ball right and down     if(ballx==paddle2x && bally>=paddle2y && bally<=paddle2y && balld2==1){balld1=1;balld2=0;}//sends the ball right and up     if(paddley>0.50)paddley=paddley-0.10;//these four handle the collision with the paddle and the walls     if(paddley<-1.0)paddley=paddley+0.10;     if(paddle2y>1.0)paddle2y=paddle2y-0.10;     if(paddle2y<-0.50)paddle2y=paddle2y+0.10;     glBegin(GL_QUADS);//the ball     glVertex2f(ballx, bally);//bottom left corner     glVertex2f(ballx+0.10, bally);// bottom right corner     glVertex2f(ballx+0.10, bally+0.10);//top right corner     glVertex2f(ballx, bally+0.10);//top left corner     glEnd();     glFlush();     //if(score1>5)MessageBox(NULL, "Player one wins!", "Winner", MB_ICONINFORMATION|MB_OK);     //if(score2>5)MessageBox(NULL, "Player two wins!", "Winner", MB_ICONINFORMATION|MB_OK);     glutSwapBuffers();     glutPostRedisplay();     }void control(unsigned char key, int x, int y){//player controls     switch(key){                 case 'k':                      paddley=paddley+0.2;                      break;                 case 'm':                      paddley=paddley-0.2;                      break;                 case 'w':                      paddle2y=paddle2y+0.2;                      break;                 case 's':                      paddle2y=paddle2y-0.2;                      break;                 }     }

Share on other sites
Well, instead of having so many different if statements in your code you could use a switch to change the balls directions. As such.....
#define UP_RIGHT    0#define UP_LEFT     1#define DOWN_RIGHT  2#define DOWN_LEFT   3int balld; //to hold the balls current directionint CCBallD() { //Check and Change the balls direction accordingly   balld = DOWN_LEFT;   switch(balld) {      case DOWN_LEFT:      //your code here

I hope you get the idea from that. To slow it down I guess you could limit the frames with a time cap. I learned this a few days ago right from here at GameDev.
#define FPS 60int tint tick() {   t++;   return t;}void render() {   int starttime = tick();   //draw your stuff to the screen once   int nexttime = starttime + 1000/FPS;   if(nexttime > tick()) {      //draw your stuff to the screen      nexttime += 1000/FPS;   }}

That should make it stay at 60 Frames per Second. Hope that helps....