soka2oo7 0 Newbie Poster

i'm trying to make a snake game & i can't make the lenght of the snake increase & it's score & the fruit appears one time only & i have to press on the arrow all the time please help me in this code today please.

#include<graphics.h>
#include<conio.h>
#include<math.h>
#include<time.h>
#include<stdlib.h>
#include<dos.h>
#include <cstdlib>
#include <iostream>
#include <cstdio>
#include <windows.h> 
#include <fstream>
#include <string>
using namespace std;

#define LEFT 1
#define RIGHT 2
#define UP 3
#define DOWN 4



class snake
{
      public:
             int length;
             int head_x;
             int head_y;
             int head_dir;
             int tail_x;
             int tail_y;
             int tail_dir;
             int bend_x[1000];
             int bend_y[1000];
             int bend_dir[1000];
             void GameDate();
};
snake snake;

int score;
char scorestring[100];

//screen function
void screen()
{
     score = 0;
     setcolor(LIGHTRED);
     line(10,30,10,470);
     line(10,30,630,30);
     line(630,30,630,470);
     line(10,470,630,470);
     settextstyle(3,0,2);
     char Theta2[]=" High score : ";
     sprintf(scorestring,"score : %d",score);
     outtextxy(1,0,scorestring);
     outtextxy(300,0,Theta2);
}

//start function game Date
void GameDate()
{
     snake.length = 50;
     snake.head_x = 320;
     snake.head_y = 240;
     snake.head_dir = RIGHT;
     snake.tail_x = snake.head_x - snake.length;
     snake.tail_y = snake.head_y;
     snake.tail_dir = snake.head_dir;
     setcolor(LIGHTGREEN);
     line(snake.tail_x,snake.tail_y,snake.head_x,snake.head_y);
}

//start funcyion movement
void move()
{
     static int i = 0;
     static int j = 0;
     if(i > 100)
        i = 0;
     if(j > 100)
        j = 0;
     char arrow;
     if(kbhit)
     {
              arrow = getch();
              if(arrow == 77)
                 arrow = RIGHT;
              if(arrow == 75)
                 arrow = LEFT;
              if(arrow == 72)
                 arrow = UP;
              if(arrow == 80)
                 arrow = DOWN;
              if(arrow == RIGHT && snake.head_dir != LEFT && snake.head_dir != RIGHT)
              {
                       snake.head_dir = RIGHT;
                       snake.bend_x[i] = snake.head_x;
                       snake.bend_y[i] = snake.head_y;
                       snake.bend_dir[i] = RIGHT;
                       i++;
              }
              if(arrow == LEFT && snake.head_dir != RIGHT && snake.head_dir != LEFT)
              {
                       snake.head_dir = LEFT;
                       snake.bend_x[i] = snake.head_x;
                       snake.bend_y[i] = snake.head_y;
                       snake.bend_dir[i] = LEFT;
                       i++;
              }
              if(arrow == UP && snake.head_dir != DOWN && snake.head_dir != UP)
              {
                       snake.head_dir = UP;
                       snake.bend_x[i] = snake.head_x;
                       snake.bend_y[i] = snake.head_y;
                       snake.bend_dir[i] = UP;
                       i++;
              }
              if(arrow == DOWN && snake.head_dir != UP && snake.head_dir != DOWN)
              {
                       snake.head_dir = DOWN;
                       snake.bend_x[i] = snake.head_x;
                       snake.bend_y[i] = snake.head_y;
                       snake.bend_dir[i] = DOWN;
                       i++;
              }                  
              if(snake.tail_x == snake.bend_x[j] && snake.tail_y == snake.bend_y[j])
              {
                              snake.tail_dir = snake.bend_dir[j];
                              j++;
              }
     }      
     if(snake.head_dir == LEFT)
     {
                       
                       snake.head_x--;
     }
     if(snake.head_dir == RIGHT)
     {
                       snake.head_x++;
     }
     if(snake.head_dir == DOWN)
     {
                       snake.head_y++;
     }
     if(snake.head_dir == UP)
     {
                       snake.head_y--;
     }
     putpixel(snake.head_x,snake.head_y,LIGHTGREEN);
     putpixel(snake.tail_x,snake.tail_y,0);
     delay(50);
     if(snake.tail_dir == LEFT)
     {
                       snake.tail_x--;
     }
     if(snake.tail_dir == RIGHT)
     {
                       snake.tail_x++;
     }
     if(snake.tail_dir == DOWN)
     {
                       snake.tail_y++;
     }
     if(snake.tail_dir == UP)
     {
                       snake.tail_y--;
     }
}

//start food function
void food()
{
     int difrencex = 10 - 630;
     int difrencey = 30 - 470;
     int salma = 0;
     int foodx;
     int foody;
     time_t t;
     srand((unsigned) time(&t));
     for(int i=0;i<salma;i++)
     rand();
     foodx = (rand() % difrencex);
     foody = (rand() % difrencey); 
     for(int i=0; 630>foodx>10 && 470>foody>30;i++) 
     {
     foodx = (rand() % difrencex);
     foody = (rand() % difrencey);    
     }       
     putpixel(foodx,foody,15);
     salma++; 
     static int foodcount = 0;
     score = 0;
     int nextx;
     int nexty;
     nextx = snake.head_x;
     nexty = snake.head_y;
     if(snake.head_dir == RIGHT)
     {
                       nextx++;
     }
     if(snake.head_dir == LEFT)
     {
                       nextx--;
     }
     if(snake.head_dir == UP)
     {
                       nexty--;
     }
     if(snake.head_dir == DOWN)
     {
                       nexty++;
     }
     int nextpixel;
     nextpixel = getpixel(nextx,nexty);
     if(nextpixel==15) 
     {
                  
     if (snake.tail_dir == UP)
           {
             for ( int i = 0; i<101;i++) 
             putpixel (snake.tail_x,snake.tail_y+i,15); 
             snake.tail_y +=100;
           }
        if (snake.tail_dir == DOWN)
          {
            for (int i = 0; i<101;i++) 
            putpixel (snake.tail_x,snake.tail_y-i,15); 
            snake.tail_y -=100;
          } 
        if (snake.tail_dir == LEFT)
          {
            for (int i = 0; i<101;i++)
            putpixel (snake.tail_x+i,snake.tail_y,15); 
            snake.tail_x +=100;
            //printf ("LEFT");
          }
        if (snake.tail_dir == RIGHT)
          {
             for ( int i = 0; i<101;i++)
             putpixel (snake.tail_x-i,snake.tail_y,15); 
             snake.tail_x -=100;
             //printf ("RIGHT");
          } 
   

                  foodcount--;
                  score+=10;
                  settextstyle(3,0,2);
                  sprintf(scorestring,"score : %d ",score);
                  outtextxy(1,0,scorestring);
     }
}

//start function bouns
/*void bouns()
{
     int difrencex = 620 - 20;
     int difrencey = 460 - 40;
     int salma = 0;
     int bounsx;
     int bounsy;
     time_t t;
     srand((unsigned) time(&t));
     for(int i=0;i<salma;i++)
     rand();
     bounsx = (rand() % difrencex);
     bounsy = (rand() % difrencey);
     int j = 20;
     setcolor(LIGHTBLUE);
     circle(bounsx+j,bounsy+j,1);
     salma++;
}*/

//Game Over
void GameOver()
{
     if(snake.head_x <= 10 || snake.head_x >= 630 || snake.head_y <= 30 || snake.head_y >= 470)
     {
                     settextstyle(3,0,2);
                     outtextxy(290,220,"Game Over");
                     delay(3000);
                     exit(1);
     }
     int nextx;
     int nexty;
     nextx = snake.head_x;
     nexty = snake.head_y;
     if(snake.head_dir == RIGHT)
     {
                       nextx++;
     }
     if(snake.head_dir == LEFT)
     {
                       nextx--;
     }
     if(snake.head_dir == UP)
     {
                       nexty--;
     }
     if(snake.head_dir == DOWN)
     {
                       nexty++;
     }
     int nextpixel;
     nextpixel = getpixel(nextx,nexty);
     if(nextpixel == LIGHTGREEN)
     {
                 settextstyle(3,0,2);
                 outtextxy(290,220,"Game Over");
                 delay(3000);
                 exit(1);
     }
}

//start function Game
void Game()
{
     food();
     //bouns();
     while(1)
     {
             move();
             GameOver();
     }
}

//start function main
int main()
{
    int g = 0,m;
    initgraph(&g,&m,"");
    int X =getmaxx();
    int Y =getmaxy();
    char string1[]=" Welcome to our Snake Game ";
    char string2[]=" please choose from this menu ";
    char string3[]=" 1.New Game ";
    char string4[]=" 2.High Scores ";
    char string5[]=" 3.Control Options ";
    char string6[]=" 4.Exit ";
    setcolor(9);
    settextstyle(6,0,2);
    outtextxy(X-555,Y-400,string1);
    outtextxy(X-555,Y-350,string2);
    outtextxy(X-450,Y-300,string3);
    outtextxy(X-450,Y-250,string4);
    outtextxy(X-450,Y-200,string5);
    outtextxy(X-450,Y-150,string6);
    char c;
    do

    {

        c = (char) getch( );

        if (c != 0)
         {
              cout<<(int) c;
            switch (c)

            {
              case 49:
                   {
                              do
                           {
                           clearviewport();
                               screen();
                             GameDate();
                            Game();
                            }while(c!='e'|| c!='E');
                           break;
                           }
              case 50:
                   {
                      clearviewport();
                      ifstream myfile;
                      myfile.open("highscores.txt"); 
                      if(myfile.fail())
                      cout<<"fail\n";
                      else
                      {
                          int l=0;
                          string k;
                          while(!myfile.eof())
                          {
                            getline(myfile,k);
                            int r=k.size();
                            char p[r];
                            for(int i=0;i<=r;i++)
                            {
                                    p[i]=k[i];
                            }
                            outtextxy(X-450,Y-400," High Scores");
                             outtextxy(X-555,Y-350+l,p);
                              l+=20;
                            }
                      }
                      break;
                      }
              case 51: 
                   {
                       clearviewport();
                        ifstream myfile;
                      myfile.open("controloptions.txt"); 
                      if(myfile.fail())
                      cout<<"fail\n";
                      else
                      {
                          int l=0;
                          string k;
                          while(!myfile.eof())
                          {
                            getline(myfile,k);
                            int r=k.size();
                            char p[r];
                            for(int i=0;i<=r;i++)
                            {
                                    p[i]=k[i];
                            }
                            outtextxy(X-450,Y-400," Control Options");
                              outtextxy(X-555,Y-350+l,p);
                              l+=20;
                         }
                   }
                       break;
                   }
            }
         }
  }while (c != 52);

    //screen();
    //GameDate();
    //Game();
    getch();
    closegraph();
    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.