Thread: Snake Game help on it please...

  1. #1
    Registered User
    Join Date
    Jan 2015
    Posts
    1

    Question Snake Game help on it please...

    This is a code for snake game using dev c++. now i would like to know how can i create:


    - a new snake (2nd snake) which eats the apple(so called apple) on its own..
    - which means.. 1st snake will be handled by user. while the 2nd snake works on its own. and both snake fights to get the apple.


    can any1 suggest me how can i do this.


    Code:
      #include <iostream>
    #include <windows.h>
    #include <stdlib.h>
    #include <conio.h>
    
    
    using namespace std;
    
    
    typedef struct tailpos
    {
      int x;   
      int y;  
      struct tailpos *next;  
      struct tailpos *prev;    
    } 
      tail;
    
    
    // d used to set the direction
    int d=4; // up = 1 , down = 2 , left =3 , right = 4;
    
    
    class snake
    {
    public:  
          
           int foodx,foody;             
          
           HANDLE console_handle;   
           COORD cur_cord;        
          
           tail *start,*current,*newtail;
           snake(); 
           void insert(int x , int y); 
           void draw();    
           void drawWall(); 
           void move();    
           bool collision();  
           void drawfood(int x=0);      
         
    };
    
    
    snake::snake()
    {
      start = NULL; 
      current = NULL; 
      newtail = NULL;   
      console_handle=GetStdHandle( STD_OUTPUT_HANDLE );  
    
    
      foodx=12;                   
      foody=14;   
    }
    
    
    
    
    void snake::drawWall()
    {
         // draw left column
         cur_cord.X=0;
         for(int y=0;y<=30;y++)
               {.....
           
    
    
    }  
    
    
    void snake::drawfood(int x)
    {
      tail *tmp;
      tmp=start->next;
      if(x==1) .....
    }
    
    
    void snake :: insert(int x , int y)
    {
         
         if(start == NULL)
         {
           newtail = new tail;
           newtail->x=x;
           newtail->y=y;
           newtail->next=NULL;
           newtail->prev=NULL;
           start=newtail;
           current=newtail;
         }
         else            
         {
           newtail = new tail;
           newtail->x=x;
           newtail->y=y;
           newtail->next=NULL;
           newtail->prev=current;  
           current->next=newtail;
           current=newtail;
         }
         
    }
    
    
    void snake::move()
    {
        tail *tmp,*cur; 
    
    
        tmp =current; 
                      
    
    
      while(tmp->prev!=NULL)
      {
        tmp->x=tmp->prev->x;        
        tmp->y=tmp->prev->y;
        tmp=tmp->prev;              
                         
      }
               
    if(d==1)
      start->y--;
    
    
    if(d==2)
      start->y++; 
    
    
    if(d==3)
      start->x--;
    
    
    if(d==4)
      start->x++;                 
                         
    }
    
    
    bool snake::collision()
    {
        tail *tmp;
        tmp=start->next;
        //check collision with itself
      while(tmp->next!=NULL)
         {
           if(start->x == tmp->x && start->y == tmp->y)
           return true;
                                                        
           tmp=tmp->next;                
         }
         //check collision with food
      if(start->x == foodx && start->y == foody)
         {
           insert(foodx,foody);          
           drawfood(1);  // draw food at new position
                          
         }
    //check collision with wall
         //collision top
      for(int x=0;x<=30;x++)
         {
           if(start->x == x .....
    }
    
    
    
    
    void snake::draw()
    {
    
    
    tail *tmp , *last;
    tmp=start;
    last = current;
    
    
                         
    
    
    while(tmp!=NULL)
                          {
                            cur_cord.X=tmp->x;
                            cur_cord.Y=tmp->y;
                            SetConsoleCursorPosition(console_handle,cur_cord);
                           
                            cout << "#";
                            tmp=tmp->next;                         
                          }
    // remove tail
    cur_cord.X=last->x;
    cur_cord.Y=last->y;
    SetConsoleCursorPosition(console_handle,cur_cord);
    cout << ' ';
    
    
    //draw the food
     cur_cord.X.....
     
    }
    
    
    
    
    int main()
    {  
    ......
    }
    
    
    getch();
    return 0;   
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    I would suggest making the snakes body out of a vector of a simple class that looks like this:

    Code:
    class Position
    {
    private:
        int x, y;
    
    public:
        // Getter/Setter methods
    };
    Then "Snake" can use that class to store the coordinates of the body. You can then handle movement by erasing an end element in the "Body" vector of positions and pushing a new one (or maybe emplacing, depending on how you store the things).

    Code:
    class Snake
    {
    private:
        std::vector<Position> m_Body; // You can use pointers to Position as well, ect.
    
    public:
        void snakeMove( const int& direction );
        // Other code here
    }
    
    void Snake::snakeMove( const int& direction )
    {
        // Make a new Position object based on direction here
    
        m_Body.erase( m_Body.begin() );
        m_Body.push_back( newPosition ); // Where 'newPosition' is the new head position
    }
    Also you should probably make the direction an enum to make it less confusing.

    For drawing a wall, if this is on console you would have the topmost y coordinate of the wall be the number of '\n' characters to print (before any other loop), with x coordinate representing spaces.

    If you are drawing multiple walls on the console, it will get tricky. You have to draw everything in one shot, going top to bottom. It might be simpler to have a class that contains a vector of Boolean type, the represents the window (or a vector of char).

    You could give this class a method named 'addWall', passing in x, y, length, and facing (horizontal or vertical). The method would then flip the bits in the vector of Boolean corresponding to the wall's position. In this way, you could fill the window every time you draw, so you wouldn't need to work out tricky logic to avoid not drawing a wall until too late.
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Snake game in C.
    By RyRy in forum C Programming
    Replies: 15
    Last Post: 04-06-2014, 06:04 PM
  2. Dat snake game...
    By esrelmantis in forum C++ Programming
    Replies: 3
    Last Post: 05-19-2013, 04:24 AM
  3. Snake game..
    By Marcus007 in forum C Programming
    Replies: 0
    Last Post: 12-05-2011, 06:58 PM
  4. Help with snake game in sdl/c
    By joellllmal in forum Game Programming
    Replies: 4
    Last Post: 08-22-2010, 12:14 AM
  5. My Snake Game
    By ()Q() in forum Game Programming
    Replies: 2
    Last Post: 07-23-2002, 03:03 PM