Snake game

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
fcalixl
Posts: 2
Joined: Fri Jan 25, 2008 5:12 pm

Snake game

Post by fcalixl »



I am in new in irrlitch and im working in a simple snake game this is what ive made so far.. need help with the snake collision and making the food

#include <irrlicht.h>
#include <windows.h>
#include <stdio.h>


using namespace irr;

#pragma comment(lib, "Irrlicht.lib")
IrrlichtDevice* device = 0;
bool Keys[256] = {false};// tells me if i pressed down a key




class MyEventReceiver : public IEventReceiver
{
public:
virtual bool OnEvent(SEvent event)
{


if (event.EventType == irr::EET_KEY_INPUT_EVENT){
Keys [event.KeyInput.Key]= event.KeyInput.PressedDown;
return false;
}
return false;
}
};
irr::core::array<irr::scene::ISceneNode*> snakeParts;


class Snake
{
private:

irr::IrrlichtDevice* device;

irr::u8 snakeDirection; // 0 left / 1 up / 2 right / 3 down

irr::f32 snakeSpeed;

irr::f32 snakeRadius;


public:
Snake(irr::IrrlichtDevice* device) :
device(device),
snakeDirection(0),
snakeSpeed(50.0f),
snakeRadius(10.0f)
{

//make head
addparts();
}
irr::core::array<irr::scene::ISceneNode*> snakeParts;
void addparts()
{
irr::scene::ISceneNode* newPart = device->getSceneManager()->addCubeSceneNode(snakeRadius);
newPart->setMaterialTexture(0,device->getVideoDriver()->getTexture("../../media/wall.bmp"));

newPart->setMaterialFlag(irr::video::EMF_LIGHTING, false);

this->snakeParts.push_back(newPart);

}

void move(const irr::f32 elapsed) const
{
irr::u32 parts = snakeParts.size();
--parts;

irr::u32 i;
for (i = parts; i >= 1; i--)
snakeParts->setPosition(snakeParts[i-1]->getAbsolutePosition());

// move head of the snake
irr::core::vector3df headPos = snakeParts[0]->getAbsolutePosition();

switch (snakeDirection)
{
case 0: headPos.X = headPos.X - snakeSpeed* elapsed; break;
case 1: headPos.Z = headPos.Z + snakeSpeed* elapsed;break;
case 2: headPos.X = headPos.X + snakeSpeed* elapsed;break;
case 3: headPos.Z = headPos.Z - snakeSpeed* elapsed;break;
}

snakeParts[0]->setPosition(headPos);
}



void setSnakeDirection(irr::u8 direction)
{
this->snakeDirection = direction;
}

const irr::u8 getSnakeDirection() const { return this->snakeDirection; }
};




int main()
{
MyEventReceiver receiver;
IrrlichtDevice* device = createDevice( irr::video::EDT_DIRECT3D9, core::dimension2d<s32>(640, 480),
32, false, false, false, &receiver);

if (device == 0) return 1;

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

Snake snake(device);





irr::scene::ICameraSceneNode* camera = smgr->addCameraSceneNode();
camera->setPosition(irr::core::vector3df(0.0f, 200.0f, 0.0f));


//Timer
irr::ITimer* timer = device->getTimer();
irr::u32 then = timer->getTime();
irr::u32 now = 0;
irr::f32 elapsed = 0.0f;

while (device->run())
{
//compute time since last frame
now = timer->getTime();
elapsed = (now - then) / 1000.f;
then = now;
core::stringw tmp(L"Snake vidas [");
tmp += vidas;
tmp += L"]";
device->setWindowCaption(tmp.c_str());



driver->beginScene(true, true, irr::video::SColor(255, 200, 50, 50));
snake.move(elapsed);
snake.colisiona();
smgr->drawAll();
driver->endScene();


if (Keys[KEY_LEFT]){
if (snake.getSnakeDirection() != 2){
snake.setSnakeDirection(0);
}

}

if(Keys[KEY_UP]){
if (snake.getSnakeDirection() != 3){
snake.setSnakeDirection(1);
}

}

if(Keys[KEY_RIGHT]){
if (snake.getSnakeDirection() != 0){
snake.setSnakeDirection(2);
}

}

if(Keys[KEY_DOWN]){
if (snake.getSnakeDirection() != 1){
snake.setSnakeDirection(3);
}

}

if(Keys[KEY_SPACE]){
snake.addparts();
}






}

device->drop();
return 0;
}
wizecoder
Posts: 13
Joined: Thu Jan 03, 2008 9:36 pm

Post by wizecoder »

You may be able to find your answer here :D
sfncook
Posts: 36
Joined: Sun Jul 01, 2007 6:32 pm

Post by sfncook »

Wait a minute! I'm getting a psychic transmission from the mystics! They tell me... this 'snake game'... is for a ... school project!

(Look about ten posts down from yours in the forum, homee).
Vsk
Posts: 343
Joined: Thu Sep 27, 2007 4:43 pm

Post by Vsk »

In that school seem that teachers send their students to irrlicht :D.
randomMesh
Posts: 1186
Joined: Fri Dec 29, 2006 12:04 am

Post by randomMesh »

nibbles.bas, anyone?
QBasic rocked my world, some time ago... ^^
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Re: Snake game

Post by rogerborg »

fcalixl wrote:im working in a simple snake game this is what ive made so far..
Since you didn't "make" that, you can pretty much suck my sphere nodes as far as any further help is concerned. You do not appropriate other peoples' work and claim it as your own.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
dlangdev
Posts: 1324
Joined: Tue Aug 07, 2007 7:28 pm
Location: Beaverton OR
Contact:

Post by dlangdev »

i'm getting tired seeing this going on like street begging.

mendicancy has its limits, you know.

see: http://www.m-w.com/cgi-bin/netdict?mendicancy

please, do not beg!

can we lock this thread now.

this is a form of spam, admins should know this vicous trick.
Image
fcalixl
Posts: 2
Joined: Fri Jan 25, 2008 5:12 pm

Re: Snake game

Post by fcalixl »

rogerborg wrote:
fcalixl wrote:im working in a simple snake game this is what ive made so far..
Since you didn't "make" that, you can pretty much suck my sphere nodes as far as any further help is concerned. You do not appropriate other peoples' work and claim it as your own.
hey im not telling is my code, iam working with one classmate who gave me this code, so a little of respect please.. i just need help im making my own project
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Re: Snake game

Post by rogerborg »

fcalixl wrote:
rogerborg wrote:
fcalixl wrote:im working in a simple snake game this is what ive made so far..
Since you didn't "make" that, you can pretty much suck my sphere nodes as far as any further help is concerned. You do not appropriate other peoples' work and claim it as your own.
hey im not telling is my code, iam working with one classmate who gave me this code, so a little of respect please.. i just need help im making my own project
:roll:

You do clearly need help, but nobody here is obliged to offer it, and you and your chum have given us no incentive to do so. Neither of you have shown any sign that you are prepared to actually do work of your own, so I'm unsure what degree of respect you expect.

How about you start over?

It's not clear what it is that you're supposed to be learning or producing. If you post the original, unedited requirements for your work, then you may get people interested in helping you to achieve your goals. Simply asking us to write code for you one bit at a time is not an attractive proposition.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
Post Reply