Hello Guys! it's been a while.

Lately, I've been wanting to study & explore C/C++ more. Because of this, I've done a lot of exercises that was given to us at school and some more around the net, but sadly the one that I'm really looking forward to the the most (snake game) wasn't given to us.

I'm really interested in how the snake game works and I really want to code it myself. I've looked around and googled it but I can't seem to find a guide, all I saw was source codes. I just want to have and idea on how the game works and what are the necessary "things" that I need in order to do that (do I need to know about: structures? array? etc.)

can someone give me an overview or just a slight guide?

Thanks a lot! Peace

Recommended Answers

All 9 Replies

Why don't you try your own solution?
Rather than looking at someone else's code, and trying to do basically the same thing over again.

Snake:
- snake moves every 'tick' (ticks get quicker as the game progresses)
- snake hits wall - dead
- snake hits snake - dead
- player presses key - snake changes direction
- snake hits food - snake gets longer

That's pretty much all there is to it.

> do I need to know about: structures? array? etc.
Yes.
Or rather, your life will be a lot easier with careful use of these things.

Thanks Salem!

I'm actually trying to do it by myself, I don't really understand the codes that I saw & I have no intentions of learning the program using those codes. I just have a couple of questions here: How do I bind the up/down/left/right keys? and the whole game is just loop right? with every movement it goes through an iteration.

Thanks in advance!

> How do I bind the up/down/left/right keys?
That depends on your OS/Compiler/Kind of program.
A Linux Console program has a different answer to a windows GUI program.

Yeah, it's just a loop with some inputs.

I'm using Dev C++ on Windows Vista, is it the same on C & C++ (The Key bind function) ?

cud anyone guide me to build the coding in c/c++ for snake game.. general game used in mobiles....

Reading others code code is a good way to learn but if you code by yourself you 'll get to learn lots of things ..

I coded a similar program ..
Al you need is ...

// Big while loop 
int direction;
while(1)
{
              if(khbit())
              {
                 //program which returns key, may be like 0 for Up 1 for down 2 n 3 so on
                 direction = getKeyPressed();                  
                 // Mechanism which sets direction of snake
              }
            KeepMovingSnake();
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.