Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Snake Game please help me

Snake Game please help me

Scheduled Pinned Locked Moved C / C++ / MFC
c++game-devhelpquestion
1 Posts 1 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 11377727
    wrote on last edited by
    #1

    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.

    #include
    #include
    #include
    #include

    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)
    {
    i

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups