some one help me plz
m making snake game and i have used system("cls")
command in all the functions which i have made so when i call any function it will clear the screan and only single star is printed now i want to know how else i do my work this is my first phase of the game plzzz help me.
i want to complete it myself not by copying so thank in advance.
my code is the place which i have comment out are used to generate the food for the snake but system("cls")......

#include<iostream>
#include<conio.h>
#include<stdlib.h>
#include<windows.h>
using namespace std;

int x=25,y=10;

void gotoxy(int x, int y) 
{ 
	COORD coord; 
	coord.X = x; coord.Y = y; 
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); 
}


void move_up()
{
	while(!kbhit())
	{
		gotoxy(x,y);
		cout<<"*";
		Sleep(100);
		system("cls");
		y--;
	}
	
}

void move_down()
{
	while(!kbhit())
	{
		gotoxy(x,y);
		cout<<"*";
		Sleep(100);
		system("cls");
		y++;
	}
	
}

void move_right()
{
	while(!kbhit())
	{
		gotoxy(x,y);
		cout<<"*";
		Sleep(100);
		system("cls");
		x++;
	}
	
}

void move_left()
{
	while(!kbhit())
	{
		gotoxy(x,y);
		cout<<"*";
		Sleep(100);
		system("cls");
		x--;
	}
	
}
/*
void random_number(int a,int b)
{
	int chek=0;
	chek=rand()%10;
	a=x+chek;
	b=y+chek;
	gotoxy(a,b);
	cout<<"+";
}
*/
int main()
{
	char ch;
//	int a=0,b=0,chek,i=1;

/*	chek=rand()%10;
	a=x+chek;
	b=y+chek;
	
	random_number(a,b);
*/	
	while(1)
	{
		move_right();
		
		if(kbhit())
		{
			ch=getch();
			
			if(ch=='w')
			{
				move_up();
			}
			else
			{
				if(ch=='a')
				{
					move_left();
				}
				else
				{
					if(ch=='d')
					{
						move_right();
					}
					else
					{
						if(ch=='s')
						{
							move_down();
						}
					}
				}
			}
		}
	/*
		if(a==x)
		{
			random_number(a,b);
		}*/
	}
	return 0;
}

Recommended Answers

All 14 Replies

Member Avatar for iamthwee

It looks to be like a good start. Obviously, you're writing non-portable code but that isn't really an issue due to the nature of the program.

-I'd think about creating a boundary next. Then having the snake come back the other side if it goes past the boundary.

-Then I'd think about creating a snake longer than one '*' in length

-I think the 2d area for the game needs to be represented by a 2d array

etc

but the problem is with system("cls") i want to get rid of from it:'(

i have problem with system("cls") command as i use it in all the functions whenever i call it to generate the food for snake, it clear all the screan:@

Member Avatar for iamthwee

Well, no.

You need the system("cls")

Sure it will blank the screen, but you update it again.

dats wast the point, how i update it one of my senoir gave me hint to use cout.flush()
but it will just clear the stream not the screan

Member Avatar for iamthwee

Forget cout.flush() it's definitely clear screen.

Second, after a little thinking, you definitely need to start thinking about this in terms of a 2d array. It don't think setconsolecursorPosition is going to be much use, especially as you need to consider if the snake goes into its own body.

Consider the following snippet as an example.

#include <iostream>
#include <string>
#include <windows.h>

using namespace std;

char let = '*';



int main()
{
  char grid[50][50];

  for ( int i = 0; i < 20; i++ )
  {
      for ( int j = 0; j < 50; j++ )
      {
        grid[i][j] = ' ';
      }
      //cout << "\n";
  }
  
  grid[3][4] = let;
  grid[3][5] = let;
  grid[3][6] = let;
  grid[3][7] = let;
  grid[3][8] = let;
  
  for ( int i = 0; i < 20; i++ )
  {
      for ( int j = 0; j < 50; j++ )
      {
        cout<<grid[i][j];
      }
      cout << "\n";
  }
  
  
  Sleep(1000);
  
  system("cls");
  
  for ( int i = 0; i < 20; i++ )
  {
      for ( int j = 0; j < 50; j++ )
      {
        grid[i][j] = ' ';
      }
      //cout << "\n";
  }
  
  
  //grid[3][4] = let;
  grid[3][5] = let;
  grid[3][6] = let;
  grid[3][7] = let;
  grid[3][8] = let;
  grid[4][8] = let;
  
  for ( int i = 0; i < 20; i++ )
  {
      for ( int j = 0; j < 50; j++ )
      {
        cout<<grid[i][j];
      }
      cout << "\n";
  }
  
  
  Sleep(1000);
  
  system("cls");
  
  for ( int i = 0; i < 20; i++ )
  {
      for ( int j = 0; j < 50; j++ )
      {
        grid[i][j] = ' ';
      }
      //cout << "\n";
  }
  

  grid[3][6] = let;
  grid[3][7] = let;
  grid[3][8] = let;
  grid[4][8] = let;
  grid[5][8] = let;
  
  for ( int i = 0; i < 20; i++ )
  {
      for ( int j = 0; j < 50; j++ )
      {
        cout<<grid[i][j];
      }
      cout << "\n";
  }
  
  
  
  Sleep(1000);
  
  system("cls");
  
  for ( int i = 0; i < 20; i++ )
  {
      for ( int j = 0; j < 50; j++ )
      {
        grid[i][j] = ' ';
      }
      //cout << "\n";
  }

  grid[3][7] = let;
  grid[3][8] = let;
  grid[4][8] = let;
  grid[5][8] = let;
  grid[5][9] = let;
  
  for ( int i = 0; i < 20; i++ )
  {
      for ( int j = 0; j < 50; j++ )
      {
        cout<<grid[i][j];
      }
      cout << "\n";
  }
  
  
  Sleep(1000);
  
  system("cls");
  
  for ( int i = 0; i < 20; i++ )
  {
      for ( int j = 0; j < 50; j++ )
      {
        grid[i][j] = ' ';
      }
      //cout << "\n";
  }
  
  
  grid[3][8] = let;
  grid[4][8] = let;
  grid[5][8] = let;
  grid[5][9] = let;
  grid[5][10] = let;
  
  for ( int i = 0; i < 20; i++ )
  {
      for ( int j = 0; j < 50; j++ )
      {
        cout<<grid[i][j];
      }
      cout << "\n";
  }
  
  
  Sleep(1000);
  
  system("cls");
  
  for ( int i = 0; i < 20; i++ )
  {
      for ( int j = 0; j < 50; j++ )
      {
        grid[i][j] = ' ';
      }
      //cout << "\n";
  }
  

  grid[4][8] = let;
  grid[5][8] = let;
  grid[5][9] = let;
  grid[5][10] = let;
  grid[6][10] = let;
  
  for ( int i = 0; i < 20; i++ )
  {
      for ( int j = 0; j < 50; j++ )
      {
        cout<<grid[i][j];
      }
      cout << "\n";
  }
  
    
  Sleep(1000);
  
  system("cls");
  
  for ( int i = 0; i < 20; i++ )
  {
      for ( int j = 0; j < 50; j++ )
      {
        grid[i][j] = ' ';
      }
      //cout << "\n";
  }
  

  grid[5][8] = let;
  grid[5][9] = let;
  grid[5][10] = let;
  grid[6][10] = let;
  grid[6][9] = let;
  
  for ( int i = 0; i < 20; i++ )
  {
      for ( int j = 0; j < 50; j++ )
      {
        cout<<grid[i][j];
      }
      cout << "\n";
  }

  
  cin.get();
}

if i use 2d array then i dont think i have to use gotoxy() function
but i want to make it using gotoxy() function

Member Avatar for iamthwee

if i use 2d array then i dont think i have to use gotoxy() function
but i want to make it using gotoxy() function

No, a 2D array is the way to go.

hi AUTHOR,

Im I read your code, its ok. but i think u need to stop for a while and think of a beter way of doing it.. but what you did is really nice..
what im trying to say is to change the way you do you GAME DISPLAY UPDATE...

heres an idea..

The better way and easiest way approach is

LOOP

DISPLAY SNAKE
DISPLAY FOOD
DISPLAY ENEMY
CHECK Kbhit()
CHECK SNAKE_BOUNDERIES
pause(100)
CLS
END LOOP

that is your main loop... everything is there already..

now maybe you ask how you control your snake?
inside your CHECK KBHIT()
just update the VARIABLE you use for your snake coordinates..

ex.
if(ch=='w'){y--;}

i hope you understand...

hi AUTHOR,

Im I read your code, its ok. but i think u need to stop for a while and think of a beter way of doing it.. but what you did is really nice..
what im trying to say is to change the way you do you GAME DISPLAY UPDATE...

heres an idea..

The better way and easiest way approach is

LOOP

DISPLAY SNAKE
DISPLAY FOOD
DISPLAY ENEMY
CHECK Kbhit()
CHECK SNAKE_BOUNDERIES
pause(100)
CLS
END LOOP

that is your main loop... everything is there already..

now maybe you ask how you control your snake?
inside your CHECK KBHIT()
just update the VARIABLE you use for your snake coordinates..

ex.
if(ch=='w'){y--;}

i hope you understand...

i thought just like this and try to make it but the problem is with system("cls") command as i use it in every function so whenever it try to make food for snake (m generation food using random number )system("cls") command clear all the screan and i have nothing for the snake :(

hi AUTHOR,

Im I read your code, its ok. but i think u need to stop for a while and think of a beter way of doing it.. but what you did is really nice..
what im trying to say is to change the way you do you GAME DISPLAY UPDATE...

heres an idea..

The better way and easiest way approach is

LOOP

DISPLAY SNAKE
DISPLAY FOOD
DISPLAY ENEMY
CHECK Kbhit()
CHECK SNAKE_BOUNDERIES
pause(100)
CLS
END LOOP

that is your main loop... everything is there already..

now maybe you ask how you control your snake?
inside your CHECK KBHIT()
just update the VARIABLE you use for your snake coordinates..

ex.
if(ch=='w'){y--;}

i hope you understand...

i really think like this but i have problem with system("cls")
as i use it in every function so whenever i call any function it will clear all the screan as it clear all the screan i didnt become able to print food for the snake food for the snak will print but as any function call goes screan will become clear

plz tell me i want to complete it plz tell me :(

Member Avatar for iamthwee

Just use a 2D array. So simple. It's a shame you didn't listen.

but didnot come to know how to use it, m totally new with programming
give me some hint plz

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.