0

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;

}
2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by WaltP
0

[boilerplate_help_info]

Posing requests for help must be well thought out if you want help quickly and correctly.  Your post did not meet the criteria for quality help. You may get some posts, but are they going to be useful?  Check your post with these checkpoints - what is it [i]you[/i] missed:
[list=1]
[*]Ask a question that can be answered. Do not ask
- What's wrong with my code?
- Why doesn't this work?
- Anything else that does not give us useful information
[*]Post your code.  If we don't know what you did, how can we possibly help?
- Use [b]PROPER FORMATTING[/b] -- see this
- Use CODE Tags so your formatting is preserved.
If we can't follow your code, it's difficult to help. We don't care that you're still working on it. If you want us to read it, it must be readable
[*]Explain what the code is supposed to do.  If we don't know where the target is, how can we help you hit it?
[*]Explain what actually happened! If we don't know where the arrow went when you shot it, how can we tell what went wrong and how far from the target you are?
[*]If you have errors, post them! We can't see your screen.  We can't read your mind. You need to tell us what happened.
[*]To [b]not[/b] ask for code. We are not a coding service. We will help you fix your code. 
    If anyone posts working code for you, they are a cheater. 
    If you use that code [i]you[/i] are a cheater.
[*]Do [b]not[/b] bore us with how new you are. We can tell by your code.
- Do not apologize. We were all new, and unless you are completely 
  brain dead you will get better.
- Do not ask us to "take it easy on you."
- Do not say "I don't know what's going on." That's obvious since
  you posted for help. Use that time wisely by [b]explaining[/b] as best 
  you can so we can help.
[*][b]Do not post your requirements and nothing else. [/b]We view that as a lazy do-nothing student that wants us to do their work for them. That's cheating and we [i]will[/i] be hard on you.
[*][b]Do not tell us how urgent it is.[/b] Seriously, for us there is no urgency at all. Many that can help will ignore any URGENT or ASAP requests.
[/list]
Think more about your next post so we don't have to play 20 questions to get the info we need to help you.

[/boilerplate_help_info]

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.