hi can anyone help me in making this code work ..i will be very grateful to him/her Code: #include <iostream> #include <stdio.h> #include <conio.h> #include <windows.h> using namespace std; #define BLACK 0 #define BLUE 1 #define GREEN 2 #define CYAN 3 #define RED 4 #define MAGENTA 5 #define BROWN 6 #define LIGHTGREY 7 #define DARKGREY 8 #define LIGHTBLUE 9 #define LIGHTGREEN 10 #define LIGHTCYAN 11 #define LIGHTRED 12 #define LIGHTMAGENTA 13 #define YELLOW 14 #define WHITE 15 #define BLINK 128 void printxy(int xpos, int ypos, int col, char ch) { COORD scrn; HANDLE hOuput = GetStdHandle(STD_OUTPUT_HANDLE); scrn.X = xpos; scrn.Y = ypos; SetConsoleCursorPosition(hOuput,scrn); HANDLE screen=GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleTextAttribute(screen,col); DWORD useless; WriteConsole(screen,&ch,1,(LPDWORD)&useless,NULL); SetConsoleTextAttribute(screen,15); SMALL_RECT windowSize = {0, 0, 100, 100}; SetConsoleWindowInfo(screen, TRUE, &windowSize); } //Printing code ends // Macros #define LEFT 1 #define RIGHT 2 #define UP 3 #define DOWN 4 int score; struct snakeData { int length; int headX; int headY; int headDir; int tailX; int tailY; int tailDir; int BendX[1000]; int BendY[1000]; int BendDir[1000]; }snake; //variable of snake structure void userinput ()// This Function changes the direction of motion of snake according to input. { static int i = 0; if ( i > 1000) i = 0; // Makes the bend array a circular queue static int j = 0; if ( j > 1000) j = 0; char input; if (_kbhit ()) { input = _getch (); //Change Respective Return value to our MACRO Direction Code Value if (input == 80) input = DOWN; if (input == 72) input = UP; if (input == 75) input = LEFT; if (input == 77) input = RIGHT; //Change head direction based on logic if (input == LEFT && snake.headDir != RIGHT && snake.headDir != LEFT) { snake.headDir = LEFT; snake.BendX [i] = snake.headX; snake.BendY [i] = snake.headX; snake.BendDir [i] = LEFT; i++; } if (input == RIGHT && snake.headDir != LEFT && snake.headDir != RIGHT) { snake.headDir = RIGHT; snake.BendX [i] = snake.headX; snake.BendY [i] = snake.headY; snake.BendDir [i] = RIGHT; i++; } if (input == UP && snake.headDir != DOWN && snake.headDir != UP) { snake.headDir = UP; snake.BendX [i] = snake.headX; snake.BendY [i] = snake.headY; snake.BendDir [i] = UP; i++; } if (input == DOWN && snake.headDir != UP && snake.headDir != DOWN) { snake.headDir = DOWN; snake.BendX [i] = snake.headX; snake.BendY [i] = snake.headY; snake.BendDir [i] = DOWN; i++; } } //Code to change the y direction at respective time if (snake.tailX == snake.BendX [j] && snake.tailY == snake.BendY [j]) { snake.tailDir = snake.BendDir [j]; j++; } } void movesnake () //Moves The Snake Across the Screen { //Move the Head if (snake.headDir == LEFT) { snake.headX --; } if (snake.headDir == RIGHT) { snake.headX ++; } if (snake.headDir == UP) { snake.headY --; } if (snake.headDir == DOWN) { snake.headY ++; } printxy(snake.headX, snake.headY,YELLOW,'@'); //Move the Tail printxy(snake.tailY, snake.tailY,YELLOW,'@'); if (snake.tailDir == LEFT) { snake.tailX --; } if (snake.tailDir == RIGHT) { snake.tailX ++; } if (snake.tailDir == UP) { snake.tailY --; } if (snake.tailDir == DOWN) { snake.tailY ++; } } void gameengine ()//Soul of our game. { while (1) { Sleep(10); } } void initscreen(){ for (int i=snake.length;i>0;i--){ printxy(snake.headX-i,snake.headY,YELLOW,'@'); } } void initGame(){ int i; snake.length=10; snake.headX =20; snake.headY =20; snake.headDir = RIGHT; snake.tailX = snake.headX - snake.length; snake.tailY = snake.headY; snake.tailDir = RIGHT; for (int i = 0; i <1000;i++) // There is no bend initally { snake.BendX[i] = 0; snake.BendY[i] = 0; } } int main() { snakeData(); initGame(); initscreen(); gameengine (); userinput (); movesnake () ; return 0; }
___________________________________________________________________ Please check the following code, the snake movement has been rectified. The snake needs to be moved within the screen area.... Code: #include <iostream> #include <stdio.h> #include <conio.h> #include <windows.h> #include <fstream> using namespace std; #define BLACK 0 #define BLUE 1 #define GREEN 2 #define CYAN 3 #define RED 4 #define MAGENTA 5 #define BROWN 6 #define LIGHTGREY 7 #define DARKGREY 8 #define LIGHTBLUE 9 #define LIGHTGREEN 10 #define LIGHTCYAN 11 #define LIGHTRED 12 #define LIGHTMAGENTA 13 #define YELLOW 14 #define WHITE 15 #define BLINK 128 // Macros #define LEFT 1 #define RIGHT 2 #define UP 3 #define DOWN 4 int score; int LatestBendIndex = 0 ; int TailBendIndex = 0 ; struct snakeData { int length; int headX; int headY; int headDir; int tailX; int tailY; int tailDir; int BendX[1000]; int BendY[1000]; int BendDir[1000]; }snake; //variable of snake structure void printxy(int xpos, int ypos, int col, char ch) { COORD scrn; HANDLE hOuput = GetStdHandle(STD_OUTPUT_HANDLE); scrn.X = xpos; scrn.Y = ypos; SetConsoleCursorPosition(hOuput,scrn); HANDLE screen=GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleTextAttribute(screen,col); DWORD useless; WriteConsole(screen,&ch,1,(LPDWORD)&useless,NULL); SetConsoleTextAttribute(screen,15); SMALL_RECT windowSize = {0, 0, 100, 100}; SetConsoleWindowInfo(screen, TRUE, &windowSize); } //Printing code ends int userinput ()// This Function changes the direction of motion of snake according to input. { int retval = 0 ; //if ( i > 1000) i = 0; // Makes the bend array a circular queue //static int j = 0; //if ( j > 1000) j = 0; char input; if (_kbhit()) { retval = 1 ; input = _getch(); if ( (input == 'q') || (input == 'Q') ) return -1; //Change Respective Return value to our MACRO Direction Code Value if (input == 80) input = DOWN; if (input == 72) input = UP; if (input == 75) input = LEFT; if (input == 77) input = RIGHT; //Change head direction based on logic if ( (input == LEFT) && (snake.headDir != RIGHT) && (snake.headDir != LEFT)) { snake.headDir = LEFT; snake.BendX [LatestBendIndex] = snake.headX; snake.BendY [LatestBendIndex] = snake.headY; snake.BendDir [LatestBendIndex] = LEFT; LatestBendIndex++; } if ((input == RIGHT) && (snake.headDir != LEFT) && (snake.headDir != RIGHT)) { snake.headDir = RIGHT; snake.BendX [LatestBendIndex] = snake.headX; snake.BendY [LatestBendIndex] = snake.headY; snake.BendDir [LatestBendIndex] = RIGHT; LatestBendIndex++; } if ((input == UP) && (snake.headDir != DOWN) && (snake.headDir != UP)) { snake.headDir = UP; snake.BendX [LatestBendIndex] = snake.headX; snake.BendY [LatestBendIndex] = snake.headY; snake.BendDir [LatestBendIndex] = UP; LatestBendIndex++; } if ((input == DOWN) && (snake.headDir != UP) && (snake.headDir != DOWN)) { snake.headDir = DOWN; snake.BendX [LatestBendIndex] = snake.headX; snake.BendY [LatestBendIndex] = snake.headY; snake.BendDir [LatestBendIndex] = DOWN; LatestBendIndex++; } } //Code to change the y direction at respective time if ((snake.tailX == snake.BendX [TailBendIndex]) && (snake.tailY == snake.BendY [TailBendIndex])) { snake.tailDir = snake.BendDir [TailBendIndex]; //Reset the Bend values snake.BendX [TailBendIndex] = 0; snake.BendY [TailBendIndex] = 0; snake.BendDir [TailBendIndex] = 0; //Point to the next bend that the tail is going to encounter TailBendIndex++; } return retval ; } void movesnake () //Moves The Snake Across the Screen { //Move the Head if (snake.headDir == LEFT) { snake.headX --; } if (snake.headDir == RIGHT) { snake.headX ++; } if (snake.headDir == UP) { snake.headY --; } if (snake.headDir == DOWN) { snake.headY ++; } printxy(snake.headX, snake.headY,YELLOW,'@'); printxy(snake.tailX, snake.tailY,YELLOW,' '); if (snake.tailDir == LEFT) { snake.tailX --; } if (snake.tailDir == RIGHT) { snake.tailX ++; } if (snake.tailDir == UP) { snake.tailY --; } if (snake.tailDir == DOWN) { snake.tailY ++; } //Move the Tail printxy(snake.tailX, snake.tailY,YELLOW,' '); } void gameengine ()//Soul of our game. { bool flag = 1; while (flag) { Sleep(100); flag = 0; } } void initscreen() { for (int i=snake.length;i>0;i--) { printxy(snake.headX-i,snake.headY,YELLOW,'@'); } } void initGame() { int i; snake.length=10; snake.headX =20; snake.headY =20; snake.headDir = RIGHT; snake.tailX = snake.headX - snake.length; snake.tailY = snake.headY; snake.tailDir = RIGHT; for (int i = 0; i <1000;i++) // There is no bend initally { snake.BendX[i] = 0; snake.BendY[i] = 0; snake.BendDir[i] = 0; } } int main() { int ret ; snakeData(); initGame(); initscreen(); while (true) { gameengine (); ret = userinput (); movesnake () ; if ( -1 == ret ) //QUIT the loop as the user has entered the character 'Q' break; } return 0; } Hope that it will solve your issue. Regards Sandip Sule