Pong c++

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
herki
Posts: 4
Joined: Thu Feb 04, 2010 11:31 am

Pong c++

Post by herki »

Hi everybody i am new to this forum.
OK let s go to point i made Pong game. It is half finished but you can play it already. Some Credit goes finalsayan because i used his pong has example.

Here is code then. But it is Config to DirectX9 so if you wanna use other Api you have to change code your self.
Main.h

Code: Select all

//////////////////////////////////////////////////
/*	Credit to finalsayan for base code.			//
	Implanted score, gamepause and				//
	changed code for some basic calculations.	//
	Atm Score is displayed only in console.		//
*/												//
//////////////////////////////////////////////////

#include <irrlicht.h> 
#include <iostream> 

using namespace irr; 
using namespace gui;

#pragma comment(lib, "Irrlicht.lib") 

// 
class MyEventReceiver: public IEventReceiver { 
public: 
   // This is the one method that we have to implement 
   virtual bool OnEvent(const SEvent& event) { 
      // Remember whether each key is down or up 
      if (event.EventType == irr::EET_KEY_INPUT_EVENT) 
         KeyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown; 

      return false; 
   } 

   // This is used to check whether a key is being held down 
   virtual bool IsKeyDown(EKEY_CODE keyCode) const { 
      return KeyIsDown[keyCode]; 
   } 

   MyEventReceiver() { 
      for (u32 i = 0; i < KEY_KEY_CODES_COUNT; ++i) 
         KeyIsDown[i] = false; 
   } 

private: 
   // We use this array to store the current state of each key 
   bool KeyIsDown[KEY_KEY_CODES_COUNT]; 
}; 



bool collisiondetection(scene::ISceneNode* n1, scene::ISceneNode* n2) { 

   core::aabbox3d<f32> box1 = n1->getTransformedBoundingBox(); 
   core::aabbox3d<f32> box2 = n2->getTransformedBoundingBox(); 

   if (box1.intersectsWithBox(box2)) { 
      return true; 
   } else 
      return false; 

} 

void movebar(scene::ISceneNode* left, scene::ISceneNode* right, MyEventReceiver receiver)
{
	core::vector3df poscorr = left->getPosition();
	if(receiver.IsKeyDown(irr::KEY_KEY_W) && poscorr.Y < 28)
	{
		poscorr.Y = poscorr.Y + 0.01;
		left->setPosition(poscorr);
	}
	else if(receiver.IsKeyDown(irr::KEY_KEY_S) && poscorr.Y > -28)
	{
		poscorr.Y = poscorr.Y - 0.01;
		left->setPosition(poscorr);
	}

	core::vector3df poscorr2 = right->getPosition();
	if(receiver.IsKeyDown(irr::KEY_UP) && poscorr2.Y < 28)
	{
		poscorr2.Y = poscorr2.Y + 0.01;
		right->setPosition(poscorr2);
	}
	else if(receiver.IsKeyDown(irr::KEY_DOWN) && poscorr2.Y > -28)
	{
		poscorr2.Y = poscorr2.Y - 0.01;
		right->setPosition(poscorr2);
	}
}

void moveballnode(scene::ISceneNode * node, float BallAngel, int BallTrajector) 
{ 
   core::vector3df bPositsion; 
   if (BallTrajector == 1) { 
      bPositsion = node->getPosition(); 
      bPositsion.X = bPositsion.X - 0.01; 
      if (BallAngel == 1) 
         bPositsion.Y = bPositsion.Y + 0.01; 
      else if (BallAngel == -1) 
         bPositsion.Y = bPositsion.Y - 0.01; 
      node->setPosition(bPositsion); 

   } else { 

      bPositsion = node->getPosition(); 
      bPositsion.X = bPositsion.X + 0.01; 
      if (BallAngel == 1) 
         bPositsion.Y = bPositsion.Y + 0.01; 
      else if (BallAngel == -1) 
         bPositsion.Y = bPositsion.Y - 0.01; 
      node->setPosition(bPositsion); 
   } 

} 

void collisionupdate(scene::ISceneNode* node, scene::ISceneNode* left, scene::ISceneNode* right,
					 scene::ISceneNode* up, scene::ISceneNode* down,
					 MyEventReceiver receiver, float* BallAngel, int* BallTrajector)
{
   if (collisiondetection(node, left) && !(receiver.IsKeyDown(irr::KEY_KEY_W) 
         || receiver.IsKeyDown(irr::KEY_KEY_S))) { 
    *BallTrajector = 1;  
	*BallAngel = 1;
   } 
   if (collisiondetection(node, left) && receiver.IsKeyDown(irr::KEY_KEY_W)) { 

      *BallAngel = 1; 
      *BallTrajector = 1; 
   } 
   if (collisiondetection(node, left) && receiver.IsKeyDown(irr::KEY_KEY_S)) { 
      *BallAngel = -1; 
      *BallTrajector = 1; 
   } 

   //Collisione con P2 
   if (collisiondetection(node, right) && !(receiver.IsKeyDown(irr::KEY_UP) 
         || receiver.IsKeyDown(irr::KEY_DOWN))) {

  	  *BallTrajector = -1;
      *BallAngel = -1; 

   } 
   if (collisiondetection(node, right) && receiver.IsKeyDown(irr::KEY_UP)) { 

      *BallAngel = 1; 
      *BallTrajector = -1; 

   } 
   if (collisiondetection(node, right) && receiver.IsKeyDown(irr::KEY_DOWN)) { 

      *BallAngel = -1; 
      *BallTrajector = -1; 
   }
	if (collisiondetection(node, up)) { 
       *BallAngel = -1; 
    } 
    if (collisiondetection(node, down)) { 
       *BallAngel = 1; 
    }

}
void score(scene::ISceneNode* node, scene::ISceneNode* leftBox, scene::ISceneNode* rightBox, int* GamePause, 
		   int* PlayerOneScore, int* PlayerTwoScore)
{
	if (collisiondetection(node, leftBox)) {
		core::vector3df bPositsion;
		bPositsion.X = 0;
		bPositsion.Y = 0;
		node->setPosition(bPositsion);
		*PlayerTwoScore += 1;
		printf("Player One Score: %d\n Player Two Score: %d\n", *PlayerOneScore, *PlayerTwoScore);
		*GamePause = 0;
    } 
    if (collisiondetection(node, rightBox)) {
		core::vector3df bPositsion;
		bPositsion.X = 0;
		bPositsion.Y = 0;
		node->setPosition(bPositsion);
		*PlayerOneScore += 1;
		printf("Player One Score: %d\n Player Two Score: %d\n", *PlayerOneScore, *PlayerTwoScore);
		*GamePause = 0;
    }
}

void startGame(MyEventReceiver receiver, int* GamePause, int* PlayerOneScore, int* PlayerTwoScore)
{
	if(receiver.IsKeyDown(irr::KEY_SPACE))
	{
		*GamePause = 1;
	}
}
Know main.cpp

Code: Select all

#include "main.h"


int main()
{
	MyEventReceiver receiver;
	
	video::E_DRIVER_TYPE driverType;

	printf("Please select the driver you want for this example:\n"\
		" (a) Direct3D 9.0c\n (b) Direct3D 8.1\n (c) OpenGL 1.5\n"\
		" (d) Software Renderer\n (e) Burning's Software Renderer\n"\
		" (f) NullDevice\n (otherKey) exit\n\n");

	char i;
	std::cin >> i;

	switch(i)
	{
		case 'a': driverType = video::EDT_DIRECT3D9;break;
		case 'b': driverType = video::EDT_DIRECT3D8;break;
		case 'c': driverType = video::EDT_OPENGL;   break;
		case 'd': driverType = video::EDT_SOFTWARE; break;
		case 'e': driverType = video::EDT_BURNINGSVIDEO;break;
		case 'f': driverType = video::EDT_NULL;     break;
		default: return 1;
	}

	IrrlichtDevice* device = createDevice(driverType, core::dimension2d<u32>(420,300), 16, false, false, false, &receiver);

	if(!device)
		return 1;

	video::IVideoDriver* driver = device->getVideoDriver();
	scene::ISceneManager* smgr = device->getSceneManager();

	scene::ISceneNode * node = smgr->addSphereSceneNode(); 
	if (node) 
	{ 
		node->setPosition(core::vector3df(0, 0, 1)); 
		node->setScale(core::vector3df(0.3, 0.3, 0.3));  
		node->setMaterialFlag(video::EMF_LIGHTING, false); 
	} 

	scene::ISceneNode* left = smgr->addCubeSceneNode();
	if(left)
	{
		left->setPosition(core::vector3df(46, 0, 1));
		left->setScale(core::vector3df(0.1, 1, 0.1));
		left->setMaterialFlag(video::EMF_LIGHTING, false);
	}

	scene::ISceneNode* right = smgr->addCubeSceneNode();
	if(right)
	{
		right->setPosition(core::vector3df(-46, 0, 1));
		right->setScale(core::vector3df(0.1, 1, 0.1));
		right->setMaterialFlag(video::EMF_LIGHTING, false);
	}

	scene::ISceneNode* up = smgr->addCubeSceneNode();
	if(up)
	{
		up->setPosition(core::vector3df(0, 35, 1));
		up->setScale(core::vector3df(10, 0.1, 0.1));
		up->setMaterialFlag(video::EMF_LIGHTING, false);
	}
	
	scene::ISceneNode* down = smgr->addCubeSceneNode();
	if(down)
	{
		down->setPosition(core::vector3df(0, -35, 0));
		down->setScale(core::vector3df(10, 0.1, 0.1));
		down->setMaterialFlag(video::EMF_LIGHTING, false);
	}

	scene::ISceneNode* leftBox = smgr->addCubeSceneNode();
	if(leftBox)
	{
		leftBox->setPosition(core::vector3df(49, 0, 1));
		leftBox->setScale(core::vector3df(0.1, 8, 0.1));
		leftBox->setMaterialFlag(video::EMF_LIGHTING, false);
	}

	scene::ISceneNode* rightBox = smgr->addCubeSceneNode();
	if(rightBox)
	{
		rightBox->setPosition(core::vector3df(-49, 0, 1));
		rightBox->setScale(core::vector3df(0.1, 8, 0.1));
		rightBox->setMaterialFlag(video::EMF_LIGHTING, false);
	}
	
	scene::ICameraSceneNode * cam = smgr->addCameraSceneNode(0, core::vector3df(0, 0, 50), core::vector3df(0, 0, 0), 1); 
	
	float BallAngel = 0;
	int BallTrajector = -1;
	//variable if game runs or not. If GamePause is 0 then game runs. And if it is 1 then game is paused.
	int GamePause = 0;
	//Player One Score, Player one is left side.
	int PlayerOneScore = 0;
	//Player Two Score, Player two is right side.
	int PlayerTwoScore = 0;
	//Game Loop
	while(device->run())
	{
		//Begin Scene, All draws after this
		driver->beginScene(true, true, video::SColor(255, 113, 113, 113));
		if(GamePause)
		{	
			collisionupdate(node, left, right, up, down, receiver, &BallAngel, &BallTrajector);
			moveballnode(node, BallAngel, BallTrajector);
			movebar(left, right, receiver);
			score(node, leftBox, rightBox, &GamePause, &PlayerOneScore, &PlayerTwoScore);
		}
		else
		{
			startGame(receiver, &GamePause, &PlayerOneScore, &PlayerTwoScore);
		}
		smgr->drawAll();
		//End Scene all drawing before this.
		driver->endScene();
	}

	device->drop();

	return 0;
}
http://www.megaupload.com/?d=JC7RHWTN
http://www.megaupload.com/?d=O629URW6 pong 0.01 can choose Graphic api.

EDIT: Added game exe.
EDIT: Edited code fixed some bugs and added some code to choose graphic app.
Last edited by herki on Thu Feb 04, 2010 1:31 pm, edited 4 times in total.
CuteAlien
Admin
Posts: 9930
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

Hm, I can move the paddles, but the ball just moves to the left-top and vanishes. Doesn't collide with the top-wall.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
herki
Posts: 4
Joined: Thu Feb 04, 2010 11:31 am

Post by herki »

Intresting what Graphic api you are using. And what IDE.
CuteAlien
Admin
Posts: 9930
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

I checked it with OpenGL (and gcc).
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
herki
Posts: 4
Joined: Thu Feb 04, 2010 11:31 am

Post by herki »

i compiled it with Visual studio c++ Express and i try ed all Graphic apis and it worked fine. don t know about gcc. I will uplode new exe where you can choose Graphic api.
CuteAlien
Admin
Posts: 9930
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

Hm, rather interesting... it works with 1.6. It does not work with Irrlicht 1.7 ... probably something I should debug... but I'm already rather too long awake. All collisions with "down" and "up" return always true right now.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
CuteAlien
Admin
Posts: 9930
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

Oh **** - new Irrlicht bug. Someone used the >= operators of the vectors for checking if _all_ elements of a vector are larger than those of another vector. And I changed the >= operator recently. Uh oh...
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
DavidJE13
Posts: 165
Joined: Tue Jan 09, 2007 7:17 pm

Post by DavidJE13 »

CuteAlien;
Surely vector1 > vector2 should be a compile error since it has no meaning. By making it a compile error it will encourage people to use explicit vector length, components, or angle to do the comparison. Much better practice.
But if anything at all, I think it should be by length, since this is the only property which can truly define a larger vector. Anything else has ambiguous areas of A != B and A not > B and B not > A, or requires some other reference such as an axis / other vector for comparison.

Sorry to hijack your thread herki.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

No, we have a pretty well-defined total order right now, which is ok for container uses. Having the old comparison was probably a convenience feature for just this situation, so no reason to argue that we should keep the old functions. But of course we need to fix this soon.
herki
Posts: 4
Joined: Thu Feb 04, 2010 11:31 am

Post by herki »

damm some help needed with 1.7 irrlicht engine my collision codes goes crazy. ball will fly out of screen.
CuteAlien
Admin
Posts: 9930
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

herki, sorry - that is the Irrlicht bug I mentioned aboved. I wish we had had your game-code 2 days earlier, then we could have fixed it before 1.7 release :-(
Bug will be discussed here: http://irrlicht.sourceforge.net/phpBB2/ ... hp?t=37189

As a workaround for now replace the line:

Code: Select all

if (box1.intersectsWithBox(box2)) { 
with that:

Code: Select all

	if (	box1.MinEdge.X <= box2.MaxEdge.X && box1.MinEdge.Y <= box2.MaxEdge.Y && box1.MinEdge.Z <= box2.MaxEdge.Z
	    	&& box1.MaxEdge.X >= box2.MinEdge.X && box1.MaxEdge.Y >= box2.MinEdge.Y && box1.MaxEdge.Z >= box2.MinEdge.Z ) {
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

This is fixed now in SVN, wait for 1.7.1 or use the SVN code if you have problems with collisions.
ErUs
Posts: 165
Joined: Thu Oct 07, 2004 6:13 pm

Post by ErUs »

also find and replace angel with angle
Post Reply