Go Back   CodingForums.com > :: Computing & Sciences > Computer Programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-06-2011, 10:28 AM   PM User | #1
learner guy
New Coder

 
Join Date: Jul 2011
Posts: 19
Thanks: 6
Thanked 0 Times in 0 Posts
learner guy is an unknown quantity at this point
Problem with Snake game ..snake movement

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;

}
learner guy is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:05 AM.


Advertisement
Log in to turn off these ads.