Monday, July 27, 2009

How to make a snakes game in c++ programming?

The goal is to implement a simple yet complete version of the game SNAKES(AKA nibbles). In the game the player directs a constantly moving snake with the 'a' (left),'w' (up), 's' (down), 'd' (right) keys. The player must successfully navigate the snake so that it avoids obstacles such as walls and it's own tail while it the continues it's never ending pursuit of candy. When the snake eats a candy (the head of the snake touches a candy) a new candy is placed randomly on the game board and the snake gets longer. Once the snake eats the candies the game advances to the next level. If the player correctly guides the snake through all of the levels the player wins. If at any time the head of the snake touches either wall or the snakes tail the player loses.

How to make a snakes game in c++ programming?
Well.. there is a quick possible resolution. Go to Google and do a search on "Snake Game C++ Source" and see what turns up.





Now.. for doing the actual job. I can't do the code for you but here's a way to start.





First break the game down into it's functional components. You need a 'board' with specific dimisions, you need a 'candy location' generator, you need a snake generator, you need a method to control the snake's next move, a control to handle the next location decision and a candy grab control.





Start by building a 2 demintion matrix of locations that you can then display on the screen in a graphical format. For the outside walls place a '1' in the correct location points in matrix. You can then draw the game screen based on the matrix with blocks at the appropriate location for the walls. This feature will allow you to create 'levels' later where you can have walls inside the main square.





Then determine a starting point and length for the snake and place a 2 in the matrix locations for the snake. Redraw the screen with the snake in place. Make this a 'callable' sub routine.





Now use a ramdom number process to select the row, column for the first 'candy'. Check that location in the matrix for a 1 or 2 and if it's empty place a 3 there and draw the candy at the appropriate location.





Now you have the play area, the starting snake and the first candy. You should then build the control to move the snake. This should poll the keyboard for input and if none move the snake in the last known direction. If there is input the snake should then be moved in the appropriate direction.





At each move cycle the snake will move one square in the matrix. Each time the snake moves check for anything other than a 0. Depending on what you find (1,2 or 3) you can then invoke the proper game process.





If a 1 or a 2 (or less than 3), then BOOM game over cause you hit the wall or your tail. If a 3 then remove candy and call routine to place next candy and up the length of your snake my the approriate number.





Ok.. there ya go.. the basic outline... the code you have to do yourself.





Good luck!

thank you cards

No comments:

Post a Comment