#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2014
    Posts
    6
    Rep Power
    0

    Need Help in Snake Game


    i'm trying to create a Snake game using Linked List.

    Code:
    # include <stdio.h>
    # include <conio.h>
    # include <time.h>
    # include <windows.h>
    # include <stdlib.h>
    
    # define UP 70
    # define DOWN 20
    
    int x=0;
    int y=0;
    int fx;
    int fy;
    int xL=10;
    int yL=7;
    int eaten=0,left=10;
    int Hscore;
    char Name[80];
    int Score;
    
    COORD CursorPosition;
    HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
    
    void gotoXY(short x, short y) {
    	COORD pos = {x, y};
    	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
    }
    
    struct node{
          char data;
          node *back;
          node *forward;
          
    }*first,*temp,*last,*next,*p;
    
    void Create(){
         char data='O';
        
         last=new (node);
         last->data=data;
         last->back=NULL;
         last->forward=NULL;
         first=last;
    
    }
    void Insert(){
         char data='O';
         
              temp=new(node);
              temp->data=data;
              temp->back=first->forward;
              temp->forward=NULL;
              first=temp;
              
    }
    void Display(){
         p=last;
         if(last){
                  while(p){
                           printf("%c",p->data);
                           p=p->forward;
                  }
         }
    }
            
    void display(int move){
    //move = getch();
        switch(move){    
                     case 1:{
                          if(move==1){
                          xL--;
                          gotoXY(x,y);
                          p=last;
                          if(last){
                                   while(p){
                                            printf("%c",p->data);
                                            xL++;
                                            gotoXY(x,y);
                                            p=p->forward;
                                   }
                          }
                          }
                          break;
                     }
                     case 2:
                          {
                              if(move==2){
                          yL--;
                          gotoXY(x,y);
                          p=first;
                          if(first){
                                    while(p){
                                             printf("%c",p->data);
                                             yL++;
                                             gotoXY(x,y);
                                             
                                    }
                          }
                          }
                          break;
                     }
                     case 3:{
                          if(move==3){
                               int r=0;
                               xL++;
                               gotoXY(x,y);
                               p=last;
                               if(last){
                                   while(r==0){
                                            printf("%c",p->data);
                                            r=1;
                                   }
                                   yL++;
                                   xL=yL;
                                   while(p){
                                            gotoXY(x,y);
                                            printf("%c",p->data);   
                                            xL--;
                                            p=p->back;
                                   }              
                          }
                          }
                          break;
                     }
                          case 4:{
                               if(move==4){
                              int r=0;
                               yL++;
                               gotoXY(x,y);
                               p=last;
                               if(last){
                                   while(r==0){
                                            printf("%c",p->data);
                                            r=1;
                                   }
                                   yL--;
                                   xL=yL;
                                   while(p){
                                            gotoXY(x,y);
                                            printf("%c",p->data);   
                                            xL--;
                                            p=p->back;
                                   }              
                          }}
                          break;
                          }
                     }
    }
    void Body(int pos){
         int move;
       //  pos = getch();
         switch(pos){
                     case 1:{
                            move=1;
                            next=new (node);
                            next->data='O';
                            last->back=next->forward;
                            next->back=NULL;
                            last=next;
                            
                            first=first->back;
                            first->forward=NULL;
                            
                            
                            break;
                     }
                     case 3:{
                          move=3;
                          next=new (node);
                            next->data='O';
                            first->forward=next->back;
                            next->forward=NULL;
                            first=next;
                            
                            last=last->forward;
                            last->back=NULL;
                            
                            break;
                          }
                     case 2:{
                          
                          move=2;
                            next=new (node);
                            next->data='O';
                            first->forward=next->back;
                            next->forward=NULL;
                            first=next;
                            
                            last=last->forward;
                            last->back=NULL;
                            
                            break;
                            }
                     case 4:{
                          move=4;
                            next=new (node);
                            next->data='O';
                            first->forward=next->back;
                            next->forward=NULL;
                            first=next;
                            
                            last=last->forward;
                            last->back=NULL;      
                            
                            break;
                            }
         }
         display(move);                          
    }     
    
    
    
    void Boundary(){
        
         int i,a,j,k,l;
         gotoXY(5,2);
         for (i=0;i<68;i++){
             printf("-");
         }
    
         
         gotoXY(5,20);
         a=5;
         for (j=0;j<68;j++){
             printf("-");
             a++;
             gotoXY(a,20);
         }
         
         gotoXY(72,2);
         a=2;
         for (k=0;k<19;k++){
             printf("|");
             a++;
             gotoXY(72,a);
         }
         gotoXY(5,2);
         a=2;
         for (l=0;l<19;l++){
             printf("|");
             a++;
             gotoXY(5,a);
         }
    }
         
    void Food(){
         char c='+';
         fx=rand()%68;
         fy=rand()%18;
         
         gotoXY(fx,fy);
         printf("%c",c);
         
    }
    void Scores(){
         
         gotoXY(15,22);
         printf("Eaten:  %d",eaten);
         gotoXY(40,22);
         printf("Left:  %d",left);
    }     
    
    void Play(){
         Boundary();
         Food();
         Scores();
         Insert();
         gotoXY(35,10);
         Display();
         Insert();
         Display();
    }
    void Check(){
         int q=0;
         if (fx==yL && fx==xL){
                    eaten++;
                    left--;
                    system("cls");
                    Boundary();
                    Food();
                    Scores();
                    display(q);
         }
    }
    void Left(){
                int position=1;
                Body(1);
                   // Check();
         if(xL<6){
                  system("cls");
                  printf("Game Over\n");
         }
    }
    
    void Right(){
        int position=3;
         Body(3);
        
          
          //          Check();
         if(xL>71){
                  system("cls");
                  printf("Game Over\n");
         }
    }
    
    void Up(){
                    int position=2;
                    Body(2);                
            //        Check();
         if(yL<3){
                  system("cls");
                  printf("Game Over\n");
         }
    }
    
    void Down(){
         int position=4;
         Body(4);
                 
                 // Check();
         if(yL>19){
                  system("cls");
                  printf("Game Over\n");
         }
    }
    
    void Modes(){
        char key;
    //    system ("cls");
    //    gotoXY(36,10);
    //    printf("LOADING...");
    //    gotoXY(20,12);
    //    for(int r=1;r<=40;r++){
    //    Sleep(100);
    //    printf("%c",177);
    //    }
    //     Sleep(100);
      system("cls");
      
         Play();
    //     system("cls");
    
         while(left<20){
                        key=getch();
                             if(key==72){
                                    Up();
                             }
                             else if(key==77){
                                  Right();
                             }
                             else if(key==75){
                                  Left();
                             }
                             else if(key==80){
                                  Down();
                             }
         }
    }
    void Level(){
         
         
    }
    void High_Score(){
         
         system("cls");
         system("color C");
         printf("\n\n\n\tName: %s \n",Name);
         printf("Score: %d",Score);
         
    }
    void Credits(){
         
    }
    void Help(){
         
         system("cls");
         system("color E");
         
         printf("\n\n\t\tHOW TO PLAY:\n\n");
         printf("\n > Use arrow keys to move the snake.\n\n > You will be provided foods at the several coordinates of the screen which you have to eat. Everytime you eat a food the length of the snake will be increased by 1 element and thus the score.\n\n > Here you are provided with three lives. Your life will decrease as you hit the wall or snake's body.\n\n > YOu can pause the game in its middle by pressing any key. To continue the paused game press any other key once again\n\n > If you want to exit press esc. \n");
         printf("\n\n Press any key to play game...");
         Sleep(300);
    }
    void start(char op){
         Create();
         switch(op){
                    case '1':{
                         system("cls");
                         Modes();
                         break;
                         
                         }
                    case '2':{
                         Level();
                         break;
                         }
                    case '3':{
                         High_Score();
                         break;
                         }
                    case '4':{
                         Help();
                         break;
                         }
                    case '5':{
                         Credits();
                         break;
                         }
                    case '27':{
                         printf("\n\n\n\n\t\t\tTHANK YOU FOR PLAYING");
                         break;
                         }
                    default:{
                            break;
                    }
         }
    }
    
    void Welcome(){
         printf("\n\n\n\n\n");
         printf("\t\t _______  __    _  _______  ___   _  _______   \n");
         printf("\t\t|       ||  |  | ||   _   ||   | | ||       |  \n");
         printf("\t\t|  _____||   |_| ||  |_|  ||   |_| ||    ___|  \n");
         printf("\t\t| |_____ |       ||       ||      _||   |___   \n");
         printf("\t\t|_____  ||  _    ||       ||     |_ |    ___|  \n");
         printf("\t\t _____| || | |   ||   _   ||    _  ||   |___   \n");
         printf("\t\t|_______||_|  |__||__| |__||___| |_||_______|  \n");
         Sleep(2000);
    }     
         
    char menu(){
         char che;
         printf("\n\n\n\t\t\t\tWelcome to Snake Game\n\n\n\n\n");
         printf("\t\t1. Modes \n");
         printf("\t\t2. Speed Level \n");
         printf("\t\t3. High Score \n");
         printf("\t\t4. Help \n");
         printf("\t\t5. Credits \n");
         printf("\t\t6. ESC \n");
         
         che=getch();
         return che;
    }
    main (){
    char ch;
       system("cls");
       int i;
       //for(i=10;i<71;i++)
    //   {
    //       gotoXY(i,10);
    //           printf("!");
    //       gotoXY(i,30);
    //           printf("!");
    //   }
    //   for(i=10;i<31;i++)
    //   {
    //       gotoXY(10,i);
    //           printf("!");
    //       gotoXY(70,i);
    //       printf("!");
    //   }
    //     
         
         Welcome();
         system("cls");
         system("color 11");
         ch=menu();
         start(ch);
         
         system("cls");
    
    
    
         getch();
         return 0;
    }
    This is my code.

    I have problem with the snake movement

    Please Help.
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,609
    Rep Power
    1945
    You should start to write more comments.


    One point though is this:
    Code:
                                   while(p){
                                            printf("%c",p->data);
                                            xL++;
                                            gotoXY(x,y);
                                            p=p->forward;
                                   }
    I think you want to call gotoXY with xL instead of x.
    You have this mistake several places, also for y vs yL.

    Do you draw all the points in the snake every time it is moved?
    I can see you have something with first and last, so it might be that you only update the two end-points.

IMN logo majestic logo threadwatch logo seochat tools logo