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;
}
}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=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.