I am making a maze game using irrlicht. Later on 3d objects will be used. There seems to be a problem after I click the left mouse button. I don't if it's a logic problem or whether I am not using the events object properly. I am trying to make it so that when I click the left mouse button, a left wall is added to the cell that the mouse cursor is pointing to. I will add the logic later to add the other walls. When you click the cell, add its all the left walls for that entire row, but I just want it for that single cell. Don't know why it's doing this ?
Any help would be appreciated. Thanks
Here is the code
Code: Select all
#include <irrlicht.h>
#include "driverChoice.h"
#include "irrlist.h"
using namespace irr;
#ifdef _MSC_VER
#pragma comment(lib, "Irrlicht.lib")
#endif
#define XCells 8;
class CMazeItem
{
public:
bool bLeft, bRight, bUp, bDown;
};
class CMaze
{
public:
CMazeItem TheMaze[8][8];
void ClearMaze(void);
void GenerateMaze(void);
void UpdateAlien(void);
int iHumanX,iHumanY;
int iAlienX,iAlienY;
};
////////////////////////////////////////////// GLOBALS ///////////////////////////////////////////
// create the maze
CMaze Maze;
////////////////////////////////////////////// GLOBALS ///////////////////////////////////////////
// | , |, -. _. |_, |-, -|, _|, ||,-_,
void CMaze::ClearMaze(void)
{
for (int i=0;i<8;i++)
for (int j=0;j<8;j++)
{
TheMaze[i,j]->bLeft=false;
TheMaze[i,j]->bRight=false;
TheMaze[i,j]->bUp=false;
TheMaze[i,j]->bDown=false;
} // for j
iHumanX = 5; iHumanY=5;
iAlienX = 0; iAlienY = 0;
}
void CMaze::GenerateMaze(void)
{
int iNum;
srand(8);
for (int i=0;i<8;i++)
for (int j=0;j<8;j++)
{
iNum = rand() % 10;
if (iNum==0)
{
TheMaze[i,j]->bLeft=true;
if (i>0) Maze.TheMaze[i-1,j]->bRight=true;
}
else if (iNum==1)
{
TheMaze[i,j]->bRight=true;
if (i<7) Maze.TheMaze[i+1,Maze.iHumanY]->bLeft=true;
}
else if (iNum==2)
{
TheMaze[i,j]->bUp=true;
if (j>0) Maze.TheMaze[i,j-1]->bDown=true;
} // else if
else if (iNum==3)
{
TheMaze[i,j]->bDown=true;
if (j<7) Maze.TheMaze[i,j+1]->bUp=true;
}
else if (iNum==4)
{
TheMaze[i,j]->bLeft=true;
if (i>0) Maze.TheMaze[i-1,j]->bRight=true;
TheMaze[i,j]->bDown=true;
if (j<7) Maze.TheMaze[i,j+1]->bUp=true;
}
else if (iNum==5)
{
TheMaze[i,j]->bLeft=true;
if (i>0) Maze.TheMaze[i-1,j]->bRight=true;
TheMaze[i,j]->bUp=true;
if (j>0) Maze.TheMaze[i,j-1]->bDown=true;
}
else if (iNum==6)
{
TheMaze[i,j]->bUp=true;
if (j>0) Maze.TheMaze[i,j-1]->bDown=true;
TheMaze[i,j]->bRight=true;
if (i<7) Maze.TheMaze[i+1,Maze.iHumanY]->bLeft=true;
}
else if (iNum==7)
{
TheMaze[i,j]->bDown=true;
if (j<7) Maze.TheMaze[i,j+1]->bUp=true;
TheMaze[i,j]->bRight=true;
if (i<7) Maze.TheMaze[i+1,Maze.iHumanY]->bLeft=true;
}
else if (iNum==8)
{
TheMaze[i,j]->bLeft=true;
if (i>0) Maze.TheMaze[i-1,j]->bRight=true;
TheMaze[i,j]->bRight=true;
if (i<7) Maze.TheMaze[i+1,Maze.iHumanY]->bLeft=true;
}
else if (iNum==9)
{
TheMaze[i,j]->bUp=true;
if (j>0) Maze.TheMaze[i,j-1]->bDown=true;
TheMaze[i,j]->bDown=true;
if (j<7) Maze.TheMaze[i,j+1]->bUp=true;
}
} // for j
}
void CMaze::UpdateAlien(void)
{
if ((iHumanX<iAlienX)&&(Maze.TheMaze[iAlienX,iAlienY]->bLeft==false))
iAlienX--;
else if ((iHumanX>iAlienX)&&(Maze.TheMaze[iAlienX,iAlienY]->bRight==false))
iAlienX++;
if ((iHumanY<iAlienY)&&(Maze.TheMaze[iAlienX,iAlienY]->bUp==false))
iAlienY--;
else if ((iHumanY>iAlienY)&&(Maze.TheMaze[iAlienX,iAlienY]->bDown==false))
iAlienY++;
}
class MyEventReceiver : public IEventReceiver
{
public:
MyEventReceiver(video::IVideoDriver* driver)
{
}
bool OnEvent(const SEvent& event)
{
if (event.EventType == irr::EET_KEY_INPUT_EVENT)
{
if ((event.KeyInput.Key == irr::KEY_LEFT)&&(Maze.iHumanX>0)&&(Maze.TheMaze[Maze.iHumanX,Maze.iHumanY]->bLeft==false))
{
Maze.iHumanX--;
Maze.UpdateAlien();
}
else if ((event.KeyInput.Key == irr::KEY_RIGHT)&&(Maze.iHumanX<7)&&(Maze.TheMaze[Maze.iHumanX,Maze.iHumanY]->bRight==false))
{
Maze.iHumanX++;
Maze.UpdateAlien();
}
else if ((event.KeyInput.Key == irr::KEY_UP)&&(Maze.iHumanY>0)&&(Maze.TheMaze[Maze.iHumanX,Maze.iHumanY]->bUp==false))
{
Maze.iHumanY--;
Maze.UpdateAlien();
}
else if ((event.KeyInput.Key == irr::KEY_DOWN)&&(Maze.iHumanY<7)&&(Maze.TheMaze[Maze.iHumanX,Maze.iHumanY]->bDown==false))
{
Maze.iHumanY++;
Maze.UpdateAlien();
}
else if (event.KeyInput.Key == irr::KEY_KEY_0)
{
Maze.TheMaze[Maze.iHumanX,Maze.iHumanY]->bLeft=true;
if (Maze.iHumanX>0) Maze.TheMaze[Maze.iHumanX-1,Maze.iHumanY]->bRight=true;
}
else if (event.KeyInput.Key == irr::KEY_KEY_1)
{
Maze.TheMaze[Maze.iHumanX,Maze.iHumanY]->bRight=true;
if (Maze.iHumanX<7) Maze.TheMaze[Maze.iHumanX+1,Maze.iHumanY]->bLeft=true;
} // else if
else if (event.KeyInput.Key == irr::KEY_KEY_2)
{
Maze.TheMaze[Maze.iHumanX,Maze.iHumanY]->bUp=true;
if (Maze.iHumanY>0) Maze.TheMaze[Maze.iHumanX,Maze.iHumanY-1]->bDown=true;
} // else if
else if (event.KeyInput.Key == irr::KEY_KEY_3)
{
Maze.TheMaze[Maze.iHumanX,Maze.iHumanY]->bDown=true;
if (Maze.iHumanY<7) Maze.TheMaze[Maze.iHumanX,Maze.iHumanY+1]->bUp=true;
} // else if
else if (event.KeyInput.Key == irr::KEY_KEY_4)
{
Maze.TheMaze[Maze.iHumanX,Maze.iHumanY]->bLeft=true;
if (Maze.iHumanX>0) Maze.TheMaze[Maze.iHumanX-1,Maze.iHumanY]->bRight=true;
Maze.TheMaze[Maze.iHumanX,Maze.iHumanY]->bDown=true;
if (Maze.iHumanY<7) Maze.TheMaze[Maze.iHumanX,Maze.iHumanY+1]->bUp=true;
}
else if (event.KeyInput.Key == irr::KEY_KEY_5)
{
Maze.TheMaze[Maze.iHumanX,Maze.iHumanY]->bLeft=true;
if (Maze.iHumanX>0) Maze.TheMaze[Maze.iHumanX-1,Maze.iHumanY]->bRight=true;
Maze.TheMaze[Maze.iHumanX,Maze.iHumanY]->bUp=true;
if (Maze.iHumanY>0) Maze.TheMaze[Maze.iHumanX,Maze.iHumanY-1]->bDown=true;
}
else if (event.KeyInput.Key == irr::KEY_KEY_6)
{
Maze.TheMaze[Maze.iHumanX,Maze.iHumanY]->bUp=true;
if (Maze.iHumanY>0) Maze.TheMaze[Maze.iHumanX,Maze.iHumanY-1]->bDown=true;
Maze.TheMaze[Maze.iHumanX,Maze.iHumanY]->bRight=true;
if (Maze.iHumanX<7) Maze.TheMaze[Maze.iHumanX+1,Maze.iHumanY]->bLeft=true;
}
else if (event.KeyInput.Key == irr::KEY_KEY_7)
{
Maze.TheMaze[Maze.iHumanX,Maze.iHumanY]->bDown=true;
if (Maze.iHumanY<7) Maze.TheMaze[Maze.iHumanX,Maze.iHumanY+1]->bUp=true;
Maze.TheMaze[Maze.iHumanX,Maze.iHumanY]->bRight=true;
if (Maze.iHumanX<7) Maze.TheMaze[Maze.iHumanX+1,Maze.iHumanY]->bLeft=true;
}
else if (event.KeyInput.Key == irr::KEY_KEY_8)
{
Maze.TheMaze[Maze.iHumanX,Maze.iHumanY]->bLeft=true;
if (Maze.iHumanX>0) Maze.TheMaze[Maze.iHumanX-1,Maze.iHumanY]->bRight=true;
Maze.TheMaze[Maze.iHumanX,Maze.iHumanY]->bRight=true;
if (Maze.iHumanX<7) Maze.TheMaze[Maze.iHumanX+1,Maze.iHumanY]->bLeft=true;
}
else if (event.KeyInput.Key == irr::KEY_KEY_9)
{
Maze.TheMaze[Maze.iHumanX,Maze.iHumanY]->bUp=true;
if (Maze.iHumanY>0) Maze.TheMaze[Maze.iHumanX,Maze.iHumanY-1]->bDown=true;
Maze.TheMaze[Maze.iHumanX,Maze.iHumanY]->bDown=true;
if (Maze.iHumanY<7) Maze.TheMaze[Maze.iHumanX,Maze.iHumanY+1]->bUp=true;
}
else if (event.KeyInput.Key == irr::KEY_KEY_C)
Maze.ClearMaze();
else if (event.KeyInput.Key == irr::KEY_KEY_G)
Maze.GenerateMaze();
else
return false;
}
else if (event.EventType == irr::EET_MOUSE_INPUT_EVENT)
{
if (event.MouseInput.isLeftPressed())
{
Maze.TheMaze[event.MouseInput.X/50,event.MouseInput.Y/50]->bLeft=true;
if (Maze.iHumanX>0) Maze.TheMaze[event.MouseInput.X/50,event.MouseInput.Y/50]->bRight=true;
} // if
} // else if
return false;
}
};
/*
At first, we let the user select the driver type, then start up the engine, set
a caption, and get a pointer to the video driver.
*/
int main()
{
//Maze.ClearMaze();
//Maze.GenerateMaze();
// ask user for driver
video::E_DRIVER_TYPE driverType=driverChoiceConsole();
if (driverType==video::EDT_COUNT)
return 1;
// create device
IrrlichtDevice *device = createDevice(driverType,
core::dimension2d<u32>(640, 480),16U,true);
if (device == 0)
return 1; // could not create selected driver.
device->setWindowCaption(L"MazeGame");
video::IVideoDriver* driver = device->getVideoDriver();
gui::IGUIFont* font = device->getGUIEnvironment()->getBuiltInFont();
gui::IGUIFont* font2 =
device->getGUIEnvironment()->getFont("../../media/fonthaettenschweiler.bmp");
//srand(device->getTimer()->getRealTime());
int dir; // 0 =left 1 = right, 2 = up, 3 = down
int iNewPosX, iNewPosY;
//Maze.GenerateMaze();
//irr::io::IFileSystem file;
//file.createAndWriteFile("C:\Users\York\Desktop\Debug",true);
//file += "5";
Maze.iHumanX=5;
Maze.iHumanY=5;
Maze.iAlienX=5;
Maze.iAlienY=0;
irr::core::vector2d<irr::s32> start;
irr::core::vector2d<irr::s32> end;
MyEventReceiver receiver(driver);
device->setEventReceiver(&receiver);
while(device->run() && driver)
{
if (device->isWindowActive())
{
u32 time = device->getTimer()->getTime();
driver->beginScene(true, true, video::SColor(255,255,255,255));
// EET_KEY_INPUT_EVENT , KEY_LEFT , irr::IEventReceiver, bool irr::IEventReceiver::OnEvent ( const SEvent & event )
for (int i=0;i<8;i++)
for (int j=0;j<8;j++)
{
irr::core::stringw str;
//str += Maze.iAlienX;
//str += ",";
//str += Maze.iAlienY;
str = "HumanbLeft = ";
str += (irr::core::stringw) Maze.TheMaze[Maze.iHumanX,Maze.iHumanY]->bLeft;
str += ", HumanbRight = ";
str += (irr::core::stringw) Maze.TheMaze[Maze.iHumanX,Maze.iHumanY]->bRight;
str += "HumanbUp = ";
str += (irr::core::stringw) Maze.TheMaze[Maze.iHumanX,Maze.iHumanY]->bUp;
str += ", HumanbDown = ";
str += (irr::core::stringw) Maze.TheMaze[Maze.iHumanX,Maze.iHumanY]->bDown;
/*
str += ",";
str += (irr::core::stringw) Maze.TheMaze[i,j]->bUp;
str += ",";
str+= (irr::core::stringw) Maze.TheMaze[i,j]->bDown;
*/
if (font)
font->draw(str,irr::core::rect<s32>(400,20,600,40),irr::video::SColor(255,255,0,255));
str = "AlienbLeft = ";
str += (irr::core::stringw) Maze.TheMaze[Maze.iAlienX,Maze.iAlienY]->bLeft;
str += ", AlienbRight = ";
str += (irr::core::stringw) Maze.TheMaze[Maze.iAlienX,Maze.iAlienY]->bRight;
str += "AlienbUp = ";
str += (irr::core::stringw) Maze.TheMaze[Maze.iAlienX,Maze.iAlienY]->bUp;
str += ", AlienbDown = ";
//str += (irr::core::stringw) Maze.TheMaze[Maze.iAlienX,Maze.iAlienY]->bDown;
str += (irr::core::stringw) (device->getTimer()->getRealTime()%10);
//str += ", rand() % 10 = ";
if (font)
font->draw(str,irr::core::rect<s32>(300,50,600,70),irr::video::SColor(255,255,0,255));
str = "";
str += (irr::core::stringw) (device->getTimer()->getRealTime()%10);
if (font)
font->draw(str,irr::core::rect<s32>(300,50+(j*10),600,70+(j*10)),irr::video::SColor(255,255,0,255));
if (Maze.TheMaze[i,j]->bLeft==true)
{
start.X=i*50;
start.Y=j*50;
end.X=i*50;
end.Y=j*50+50;
driver->draw2DLine(start,end,irr::video::SColor(255,255,0,255));
/*
str+= " ,";
str += start.X;
str += ",";
str += start.Y;
*/
//if (font)
///font->draw(str,irr::core::rect<s32>(i*50,j*50,i*50+50,j*50+50),irr::video::SColor(255,255,0,255));
} // if
if (Maze.TheMaze[i,j]->bRight==true)
{
start.X=i*50+50;
start.Y=j*50;
end.X=i*50+50;
end.Y=j*50+50;
driver->draw2DLine(start,end,irr::video::SColor(255,255,0,255));
} // if
if (Maze.TheMaze[i,j]->bUp==true)
{
start.X=i*50;
start.Y=j*50;
end.X=i*50+50;
end.Y=j*50;
driver->draw2DLine(start,end,irr::video::SColor(255,255,0,255));
} // if
if (Maze.TheMaze[i,j]->bDown==true)
{
start.X=i*50;
start.Y=j*50+50;
end.X=i*50+50;
end.Y=j*50+50;
driver->draw2DLine(start,end,irr::video::SColor(255,255,0,255));
} // if
} // for j
// draw human
driver->draw2DRectangle(irr::video::SColor(255,255,0,255),
irr::core::rect<s32>(Maze.iHumanX*50+5,Maze.iHumanY*50+5,Maze.iHumanX*50+50-5,Maze.iHumanY*50+50-5));
// draw alien
driver->draw2DRectangle(irr::video::SColor(255,0,0,255),
irr::core::rect<s32>(Maze.iAlienX*50+5,Maze.iAlienY*50+5,Maze.iAlienX*50+50-5,Maze.iAlienY*50+50-5));
driver->endScene();
}
}
device->drop();
return 0;
}