Skip to main content
GameDev.net gamedev.net
🔒 Locked

Pong Source Code

Started by Bluseed Jun 30, 2006 at 10:33 PM 0 replies 450+ views
Original Post
Bluseed
Bluseed
So here is my Pong Source Code. This is everything that is making the ball move back and forth and bounce off of the paddles. Im noticing that the code is a little backwards(Theres a better way to doing it). Can someone tell me what im doing wrong. Im not really understanding the Sphere and plane collision thing, and all the code i have isnt in VC++ so i dont know how to convert many function to C++, so i have to do it this way until then. The only thing that isnt happening that i need is that the ball doesnt reflect off the paddles like it should realisticly. You cant shoot the ball in a way you normally would in pong, and its pissing me off!! Any suggestions please? It looks like i have to rewrite the code!!!

int moveBall()
{
	
	MinPaddleY = PaddleY - 1.5f;
	MaxPaddleY = PaddleY + 1.5f;

	MinPaddleY2 = PaddleY2 - 1.5f;
    MaxPaddleY2 = PaddleY2 + 1.5f;

	Dist = (BallX - PaddleX) - 1.0f;
	Dist2 = (BallX - PaddleX2);

	if (Dist < -1.5 && BallY > MinPaddleY && BallY < MaxPaddleY  && BallX > -5.0)
	{
		blnBallX = true;
	}
	
	if (Dist2 > -1.5f && BallY > MinPaddleY2 && BallY < MaxPaddleY2 && BallX < 5.0)
	{
		blnBallX = false;
	}
	

	if (BallX < -7.0f)
	{
		++Player2Score;
		Player2Score = (Player2Score + 1.0f) - 1.0f;
		blnBallX = true;		
	}

	if (BallX > 7.0f)
	{
		++PlayerScore;
		PlayerScore = (PlayerScore + 1.0f) - 1.0f;
		blnBallX = false;				
	}

	if (blnBallX == true)
	{	
		BallVX = cosf(BallAng) * 0.2f / 8;
		BallX +=  BallVX;
		--Xrot;
	}
	
	if (blnBallX == false)
	{
		BallVX = cosf(BallAng) * 0.2f / 8;
		BallX -=  BallVX;
		++Xrot;
	}
	

	if (BallX > 4.0)
	{
		if (PaddleY2 < BallY)// && PaddleY2 != BallY)
		{
			PaddleY2 += 0.09f ;
		}

		if (PaddleY2 > BallY)// && PaddleY2 != BallY)
		{
			PaddleY2 -= 0.02f ;
		}
	}

	
	if (BallY < -3.8f)
	{
		blnBallY = true;
	}
	
	if (BallY > 3.8f)
	{
		blnBallY = false;
	}

	if (blnBallY == true)
	{
		BallVY = cosf(BallAng) * 0.2f / 8;
		BallY += BallVY;		
	}
	
	if (blnBallY == false)
	{
		BallVY = cosf(BallAng) * 0.2f / 8;
		BallY -= BallVY;

	}
	
	

return 0;
}

int DrawGLScene(GLvoid)									// Here's Where We Do All The Drawing
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// Clear Screen And Depth Buffer
	glLoadIdentity();	
	
	//Texture
	glBindTexture(GL_TEXTURE_2D, texture[filter]);

	//Sphere
	glTranslatef(BallX,BallY,BallZ);
	glRotatef(Xrot,0.0f,0.0f,1.0f);
	gluSphere(quadratic,0.5,32,32);				// Draw A Sphere With A Radius Of 1 And 16 Longitude And 16 Latitude Segments									

	glLoadIdentity();
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	

	glTranslatef(PaddleX,PaddleY,-10.0f);
	glBegin(GL_QUADS);
		// Front Face
		glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
		glTexCoord2f(1.0f, 0.0f); glVertex3f(-0.7f, -1.0f,  1.0f);
		glTexCoord2f(1.0f, 1.0f); glVertex3f(-0.7f,  1.0f,  1.0f);
		glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);

		// Right Face
		glNormal3f( 1.0f, 0.0f, 0.0f);
		glTexCoord2f(1.0f, 0.0f); glVertex3f( -0.7f, -1.0f, -1.0f);
		glTexCoord2f(1.0f, 1.0f); glVertex3f( -0.7f,  1.0f, -1.0f);
		glTexCoord2f(0.0f, 1.0f); glVertex3f( -0.7f,  1.0f,  1.0f);
		glTexCoord2f(0.0f, 0.0f); glVertex3f( -0.7f, -1.0f,  1.0f);

		// Top Face
		glNormal3f( 0.0f, 1.0f, 0.0f);
		glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.9f,  1.0f, -1.0f);
		glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.9f,  1.0f,  1.0f);
		glTexCoord2f(1.0f, 0.0f); glVertex3f(-0.7f,  1.0f,  1.0f);
		glTexCoord2f(1.0f, 1.0f); glVertex3f(-0.7f,  1.0f, -1.0f);

		// Bottom Face
		glNormal3f( 0.0f,-1.0f, 0.0f);
		glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
		glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.7f, -1.0f, -1.0f);
		glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.7f, -1.0f,  1.0f);
		glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
	
	
    glEnd();
	
	glLoadIdentity();
	

	glTranslatef(PaddleX2,PaddleY2,-10.0f);
	glBegin(GL_QUADS);
		// Front Face
		glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
		glTexCoord2f(1.0f, 0.0f); glVertex3f(-0.7f, -1.0f,  1.0f);
		glTexCoord2f(1.0f, 1.0f); glVertex3f(-0.7f,  1.0f,  1.0f);
		glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);

		// Right Face
		glNormal3f( 1.0f, 0.0f, 0.0f);
		glTexCoord2f(1.0f, 0.0f); glVertex3f( -1.0f, -1.0f, -1.0f);
		glTexCoord2f(1.0f, 1.0f); glVertex3f( -1.0f,  1.0f, -1.0f);
		glTexCoord2f(0.0f, 1.0f); glVertex3f( -1.0f,  1.0f,  1.0f);
		glTexCoord2f(0.0f, 0.0f); glVertex3f( -1.0f, -1.0f,  1.0f);

		// Top Face
		glNormal3f( 0.0f, 1.0f, 0.0f);
		glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);
		glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f,  1.0f,  1.0f);
		glTexCoord2f(1.0f, 0.0f); glVertex3f(-0.7f,  1.0f,  1.0f);
		glTexCoord2f(1.0f, 1.0f); glVertex3f(-0.7f,  1.0f, -1.0f);

		// Bottom Face
		glNormal3f( 0.0f,-1.0f, 0.0f);
		glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
		glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.7f, -1.0f, -1.0f);
		glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.7f, -1.0f,  1.0f);
		glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
	

    glEnd();


	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	//EndTexture
	glBindTexture(GL_TEXTURE_2D, 0);

	
	//Text
	glColor3f(1.0f,1.0f,1.0f);
	glTranslatef(-8.1f,5.6f,-15.0f);	
	glRasterPos2f(0.0f, 0.0f);
	

 	glPrint("SphereX - %f SphereY - %f  ", BallX ,BallY);	// Print GL Text To The Screen
	
	glLoadIdentity();

	//Text
	glColor3f(1.0f,1.0f,1.0f);
	glTranslatef(-8.1f,5.3f,-15.0f);	
	glRasterPos2f(0.0f, 0.0f);
	

 	glPrint("Player1 - %f Player2 - %f  ", PlayerScore ,Player2Score);	// Print GL Text To The Screen
	

	moveBall();
	
	

	return TRUE;										// Everything Went OK
}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.