954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Questions about Snake (game) in C or C++

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

esy928
Light Poster
42 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

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.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

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!

esy928
Light Poster
42 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

> 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.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

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

esy928
Light Poster
42 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 
Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

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

lakshman_d
Newbie Poster
1 post since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

hey i founded a working and simple c code for snake game.. at

http://latesthub.in/2010/12/03/simple-c-code-for-a-snake-game-project/

check it its really simple and interesting.The main thing it runns really...

technocrats700
Newbie Poster
1 post since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

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();
}
Shankye
Junior Poster
168 posts since Nov 2010
Reputation Points: 48
Solved Threads: 16
 

Hey tht snake game is really cool ..
u may be interested in these too

http://bit.ly/iefllI

http://bit.ly/bDNGap

Shankye
Junior Poster
168 posts since Nov 2010
Reputation Points: 48
Solved Threads: 16
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You