...

View Full Version : Problem with Snake game ..snake movement



learner guy
11-06-2011, 10:28 AM
hi can anyone help me in making this code work ..i will be very grateful to him/her



#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;

}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum