Ask Question

Name:
Title:
Your Question:

Answer Question

Name:
Your Answer:
User Submitted Source Code!


Description:
  finalsnake1
Language: C/C++
Code:
#include<iostream>
#include<conio.h>
#include<stdlib.h>
#include<stdio.h>
#include<windows.h>
#include<process.h>
#include<time.h>
using namespace std;
void changedirection(char key);
int  makefood();
void printborder();
void speedupdate();
//void hori_snakebirth();
//void verti_snakebirth();
void gotoxy(int,int);
void movesnake();
#define ESC 27
clock_t start;
int mainintro();

//*********************************************************************
int i;

///********************************************************************
int instruct()///instructions here
{
    gotoxy(20,10);printf("HOW TO PLAY? ");
    gotoxy(10,15);printf(" 4: left");
    gotoxy(10,20);printf(" 6: right");
    gotoxy(10,25);printf(" 8: up");
    gotoxy(10,30);printf(" 2:down");
    gotoxy(20,40);printf(" your snake is sensitive,do not let it crash");
    gotoxy(20,42);printf(" eat food and money to nurture it");
    //for(i=0;i<=100000;i++)
    gotoxy(45,50);printf(" press any key to continue");getch();


    return 0;
}
///********************************************************************
int load() ///loads screen slowly
{
    cout<<"===============================================";_sleep(300);
    cout<<"******  *      *        *        *   *  *******";_sleep(300);
    cout<<"*       **     *       * *       *  *   *      ";_sleep(300);
    cout<<"*       * *    *      *   *      * *    *      ";_sleep(300);
    cout<<"******  *  *   *     *******     **     *******";_sleep(300);
    cout<<"     *  *   *  *    *       *    * *    *      ";_sleep(300);
    cout<<"     *  *    * *   *         *   *  *   *      ";_sleep(300);
    cout<<"******  *     **  *           *  *   *  *******";_sleep(300);
    cout<<"================================================";_sleep(700);
    mainintro();
}
///********************************************************************
int document()///documentation: mostly inactive
{
    system("cls");
    gotoxy(5,10);
    printf("SYSTEM documentation:");cout<<endl;
    printf("language :c++n IDE:CODEBLOCKS 13.12 (windows 32-bit)");cout<<endl;cout<<endl;
    printf("USER documentation:");cout<<endl;
    printf("creators: Shreya Sahay n Shashwati Jha ");cout<<endl;cout<<endl;

    //for(i=0;i<=100000;i++)
    gotoxy(10,20);printf(" press any key to continue");getch();
    return 0;



}
///********************************************************************
int mainintro()///main intro screen,menu driven
{
        int ch;
    do
    {
    system("cls");
    gotoxy(10,10);      printf("SNAKE SHASHAY!");cout<<endl;cout<<endl;
    gotoxy(5,14);      printf("1. START");cout<<endl;
    gotoxy(5,18);      printf("2. NOW WHAT? ");cout<<endl;
    gotoxy(5,22);      printf("3. DOCUMENTATION");cout<<endl;
    gotoxy(5,56);      printf("press esc to exit");cout<<endl;

    gotoxy(10,64);      printf("enter choice: ");cin>>ch;

        switch(ch)
        {
            case 1:movesnake();break;
            case 2:instruct();break;
            case 3:document();break;
        }
        ch=getch();
    }
    while(ch!=27);

}
///********************************************************************
int intoscr1()///first screen user sees
{
   gotoxy(50,30); printf("SNAKE SHASAY!");cout<<endl;
   gotoxy(50,35); printf("READY TO ENTER?");getch();
   //for(int i=0;i<=200000;i++);
   load();
}






//limit xaxis=20-60&yaxis=30-70
int canvas[70][70]={0,0};
int x=25,y=35;
int direction=6;
COORD snake={0,0};
COORD foodpos={35,45};
int length=0;
void bend()
{

}

void createborder()
{int i,j;
    for(i=20;i<60;i++)
        for(j=30;j<70;j++)
    {
        if(i==20||j==30||i==59||j==69)
            canvas[i][j]=-1;
        else canvas[i][j]=0;
    }
}
void hitborder()
{
    if((snake.X>=20||snake.X<=60) &&(snake.Y==30||snake.Y==70))
       {
           //gotoxy(35,45);
          // cout<<"score is "<<(length-3)*10;
           cout<<"looser!!"<<endl;
           exit(0);
       }
      else if((snake.Y>=30||snake.Y<=70) &&(snake.X==20||snake.X==60))
       {
           cout<<"looser!!"<<endl;
           exit(0);
       }
}
void gotoxy(int x, int y)
{

    snake.X=x;
    snake.Y=y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),snake);
}
void printborder()
{

                for(int i=0;i<=40;i++)
                {
                   gotoxy(i+20,30);printf("^");
                   gotoxy(i+20,70);printf("v");
                   gotoxy(20,i+30);printf("^");
                   gotoxy(60,i+30);printf("^");
                }

}
void movesnake()
{  start= clock();
  while(clock()-start<500)
     {B:
        makefood();

   A:
       while(!kbhit())//make food
       {

           speedupdate();
           //system("cls");
           printborder();
           gotoxy(foodpos.X,foodpos.Y);
           cout<<"$";
           gotoxy(10,10);
           cout<<"SCORE :"<<length-1;
           if(direction==6)
           {

               x++;
               gotoxy(x,y);
               cout<<"#";

           }
           else if(direction==4)
           {

               x--;
               gotoxy(x,y);
               cout<<"#";

           }
           if(direction==8)
           {

               y--;
               gotoxy(x,y);
               cout<<"#";


           }
           if(direction==2)
           {

               y++;
               gotoxy(x,y);
               cout<<"#";

           }
           if(snake.X==foodpos.X&&snake.Y==foodpos.Y)
           {

               cout<<"level "<<length+1;
               length++;
               goto B;
           }
           hitborder();

       }
       char key=getch();
       changedirection(key);
       if(snake.X!=foodpos.X||snake.Y!=foodpos.Y)
        goto A;
 }
}
/*void hori_snakebirth()
{
    for(int i=0;i<length;i++)
        cout<<"*";
}
void verti_snakebirth()
{
   for(int i=0;i<length;i++)
        gotoxy(x,y);printf("*");
}*/
void changedirection(char key)
{

    if(key=='a'&&(direction==8||direction==2))
    {

        system("cls");
        x--;
        gotoxy(x,y);
        cout<<"#";
        direction=4;
    }
    if(key=='d'&&(direction==8||direction==2))
    {

        system("cls");
        x++;
        gotoxy(x,y);
        cout<<"#";
        direction=6;
    }
    if(key=='w'&&(direction==4||direction==6))
    {

        system("cls");
        y--;
        gotoxy(x,y);
        cout<<"#";
        direction=8;
    }
    if(key=='s'&&(direction==4||direction==6))
    {

        system("cls");
        y++;
        gotoxy(x,y);
        cout<<"#";
        direction=2;
    }
}

int makefood()
{
    if(snake.X==foodpos.X && snake.Y==foodpos.Y)
    {
        //length++;


        foodpos.X=rand()%59;
            if(foodpos.X<=20)
                    foodpos.X+=21;

        foodpos.Y=rand()%69;
            if(foodpos.Y<=30)
                foodpos.Y+=31;
    }
}

int main()
{
   cout<<"===============================================";_sleep(300);
    cout<<"******  *      *        *        *   *  *******";_sleep(300);
    cout<<"*       **     *       * *       *  *   *      ";_sleep(300);
    cout<<"*       * *    *      *   *      * *    *      ";_sleep(300);
    cout<<"******  *  *   *     *******     **     *******";_sleep(300);
    cout<<"     *  *   *  *    *       *    * *    *      ";_sleep(300);
    cout<<"     *  *    * *   *         *   *  *   *      ";_sleep(300);
    cout<<"******  *     **  *           *  *   *  *******";_sleep(300);
    cout<<"================================================";_sleep(700);
    mainintro();

}
void speedupdate()
{
    if(clock()-start<=9000)
    {     //system("cls");
        _sleep(100);//gotoxy(40,50);cout<<"level 1";
    }
    else if(clock()-start>9000&&clock()-start<16000)
    {     // system("cls");
        _sleep(50);// gotoxy(40,50);cout<<"level 2";
    }
    else
    {    // system("cls");
        _sleep(25);//gotoxy(40,50);cout<<"level 3";
    }
}

          
Comments: