Me and my freinds are heading towards a C++ Game Project of Snake-Game in which a Snake is to be made of 6 "*"s and it is controlled with keybord....

We had progressed till here and weare finding difficulty in handaling more than 1 stars plz help

include<iostream.h>
include<conio.h>

void main()
{
int x=1,y=1;
char c;

``````for(;;)
{
_setcursortype(_NOCURSOR);    //Stops showing the cursor
clrscr();
gotoxy(x,y);   //Function that allows cursor to go on x,y coordinate of screen
cout<<"*";

c=getch();     //Alternative of cin,
//Difference : No need to press enter

switch((int)c)
{

case 77:
{
x++;

if(x>80)  //Sets up virtual wall on right side
{
x=80;
}

break;
}

case 75:
{
x--;
if(x<1)     //Sets up virtual wall on left side
{
x=1;
}
break;
}

case 72:
{
y--;
if(y<1)     //Sets up virtal ceiling
{
y=1;
}
break;
}

case 80:
{
y++;

if(y>25)    //Sets up virtual floor
{
y=25;
}

break;
}
}

if((int)c==13)
{
break;
}

}
``````

if((int)c!=13)
{
getch();
}
}

Plz Help henceon

## All 2 Replies

Drawing this stuff on a paper always helps me out.

When you have more then 1 element, just store the position of the previous one in an array, for example

``````struct point { //just a simple point class.
int x,y;
point() = default;
point(int X, int Y) :x(X), y(Y) {}
};

int Numblocks; //contains the number of *

point location = Snake[0];

point *Snake = new point[Numblocks];

Snake[0] = Update(); //in Update, implement the main game loop.

for (int i = 0; i < NumBlocks-1; i++) Snake[i] = Snake[i+1];
``````

Then just draw all of them on the screen.
Note: this is just a pseudocode example. Since I do not run windows, I do not have conio, and cannot modify your code to show you exactly how to do it.
Note that setting point constructor to default just gives the default constructor back after making different constructor.

I think that what sergent may be getting at is the idea of a more event-driven programming structure for games. I myself also made a working console snake game (I used colour, variable snake length, and extended ASCII to make it look good :P) and found that having code in this format helps:

``````#define WINDOW_HEIGHT //define a number here
#define WINDOW_WIDTH  //define a number here too
enum Direction{UP,DOWN,LEFT,RIGHT,NOT};
class Serpent
{
private:
Direction dir;
int length;
int *x;//array
int *y;//array
public:
//I will just show you the functions, it is up to you to fill them in!
Serpent();
void Draw(unsigned char grid[WINDOW_HEIGHT][WINDOW_WIDTH]);//print the snake to the given grid
void Move(Direction newDir);//rotate the snake in the given direction and move along its path by one (hint: shift your array elements!)
bool Collision(int X, int Y);//return true if the snake collides with an object at X,Y (basically test if (X,Y) falls in your (x,y) array
void Extend();//add a new segment to the tail (it should be a doubled value of the x,y coords of the very back of the tail
bool Check();//return true if gameOver
int getLength();
}
``````

Once you have that class you can make one like this:

``````class Board
{
private:
unsigned char grid[WINDOW_HEIGHT][WINDOW_WIDTH];
Serpent s;
int goodX;//x-coord of the 1-up
int goodY;//y-coord of the 1-up
public:
Board();//initialize the board
int Step();//return 0 if the game continues, or score
};
``````

Then main can look like this:

``````int main()
{
Board SnakeLand;
int score=SnakeLand.Step();
while (score==0)
{
//here you COULD force an fps!
score=SnakeLand.Step();
}
return 0;
}
``````
Be a part of the DaniWeb community

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