Far muovere lo snake affinchè l'input direzione non cambia

di il
3 risposte

Far muovere lo snake affinchè l'input direzione non cambia

Buongiorno ,

Sono un programmatore C++ ma non esperto , vorrei che il mio snake si muova continuamente affinche la direzione non cambia, ma al momento il mio snake si muove solo se inserisco ogni volta la direzione da dare allo snake. Penso che debba eseguire due funzioni contemporaneamente ( MOVE e DIRECTION) ma non so come farlo.

Vi posto qua sotto il mio codice, spero che riusciate ad aiutarmi, grazie.

#include <iostream>
#include <stdio.h>
#include <cstdlib>
#include <stdlib.h>
#include <ctime>

using namespace std;


class snake
{
public:
  int movep = 0;
  int dimx = 18;
  int dimy = dimx;
  char dir;
  int fruit[2];
  int player[2][28];
  int old[2][28];
  
  
  int snake_head[2];
  int snaketail[2];
  int player_score = 0;
  int c_fruit = 0;
  int c_gameover;
  int i;
  int x;
  int snake_lenght = 2;
  char last_dir;

  int y = 0;


  int initialize (char vet[30][30], int righe, int colonne);
  void stampagriglia (char vet[30][30], int righe, int colonne);
  int froot (char vet[30][30], int righe, int colonne);
  int init_snake (char vet[30][30], int righe, int colonne);
  int direction ();
  int move (char vet[30][30], int righe, int colonne);
  int check_fruit (char vet[30][30], int righe, int colonne);
  int add_body(char vet[30][30], int righe, int colonne);
  void check_go ();
  void score ();
  void view_score ();
};



int
main ()
{


  snake snake_;
  int gameover = 0;
  int run = 0;
  int a_restart;
  int restart = 0;
  int ncolonne = 30;
  int nrighe = 30;
  char griglia[30][30];
  



  while (restart != 1)
    {

      gameover = 0;
      snake_.c_gameover = 0;
      while (gameover != 1)
	{
	  snake_.initialize (griglia, nrighe, ncolonne);
	  //snake_.stampagriglia(griglia, nrighe, ncolonne);
	  snake_.froot (griglia, nrighe, ncolonne);
	  //snake_.stampagriglia(griglia, nrighe, ncolonne);
	  snake_.init_snake (griglia, nrighe, ncolonne);
	  snake_.stampagriglia (griglia, nrighe, ncolonne);

	  //gameover = 1;
	  run = 0;

	  while (run != 1)
	    {

	      snake_.direction();
	      snake_.move (griglia, nrighe, ncolonne);
	      gameover = snake_.c_gameover;
	      snake_.check_fruit (griglia, nrighe, ncolonne);
	      if (gameover == 1)
		{
		  run = 1;
		}
	      if (snake_.c_fruit == 1)
		{
		  snake_.froot (griglia, nrighe, ncolonne);
		  snake_.c_fruit = 0;
		}
	      if (gameover == 0)
		{
		  snake_.stampagriglia (griglia, nrighe, ncolonne);
		}


	    }

	}

      cout << "\n\n\nHAI PERSO!";
      //snake_.view_score();

      cout << "\nVuoi giocare ancora?";
      cout << "\nSI(1) NO(2): ";
      cin >> a_restart;
      if (a_restart == 1)
	{
	  restart = 0;
	}
      if (a_restart == 2)
	{
	  restart = 1;

	}
    }
  return 0;
}



int
snake::initialize (char vet[30][30], int righe, int colonne)
{
  int xx, yy;
  for (xx = 0; xx < righe; xx++)
    {
      for (yy = 0; yy < colonne; yy++)
	{
	  if ((yy == 0) || (yy == (colonne - 1)))
	    {
	      vet[yy][xx] = '#';
	    }
	  if ((yy > 0) && (yy < (colonne - 1)))
	    {
	      if ((xx == 0) || (xx == (colonne - 1)))
		{
		  vet[yy][xx] = '#';
		}
	      else
		{
		  vet[yy][xx] = ' ';
		}
	    }
	}
    }
  return 1;


};

void
snake::stampagriglia (char vet[30][30], int righe, int colonne)
{
  int ii, jj;
  system ("clear");
  //cout << "\n";
  for (ii = 0; ii < righe; ii++)
    {
      for (jj = 0; jj < colonne; jj++)
	{
	  cout << vet[jj][ii] << " ";

	}
      cout << endl;

    }
  cout << endl;


};





int
snake::froot (char vet[30][30], int righe, int colonne)
{
  srand (time (NULL));
  fruit[0] = 0;
  fruit[1] = 2;
  vet[fruit[0]][fruit[1]] = '0';

  while((vet[fruit[0]][fruit[1]] == '0')||(vet[fruit[0]][fruit[1]] == '^')||(vet[fruit[0]][fruit[1]] == '<')||(vet[fruit[0]][fruit[1]] == '>')||(vet[fruit[0]][fruit[1]] == 'v'))
  {

   fruit[0] = rand () % (colonne - 2) + 1;
   fruit[1] = rand () % (righe - 2) + 1;
  }
  
  vet[fruit[0]][fruit[1]] = '@';
  vet[0][2] = '#';
  c_fruit = 0;

  return 1;
};


int
snake::init_snake (char vet[30][30], int righe, int colonne)
{
  srand (time (NULL));
  player[0][0] = 0;
  player[1][0] = 0;
  player[0][1] = 0;
  player[1][1] = 1;

  vet[player[0][0]][player[1][0]] = '@';
  vet[player[0][1]][player[1][1]] = '@';

  while ((vet[player[0][0]][player[1][0]] == '@')
	 || (vet[player[0][1]][player[1][1]] == '@'))
    {
      player[0][0] = rand () % (colonne - 3) + 1;
      player[1][0] = rand () % (righe - 3) + 1;
      player[0][1] = player[0][0];
      player[1][1] = player[1][0] + 1;
    }


  vet[player[0][0]][player[1][0]] = '^';
  vet[player[0][1]][player[1][1]] = '0';
  vet[0][0] = '#';
  vet[0][1] = '#';
  old[0][0] = player[0][0];
  old[1][0] = player[1][0];
  old[0][1] = player[0][1];
  old[1][1] = player[1][1];
  /*cout << "\nCOLONNA HEAD : ";
     cout << player[1][0];
     cout << "\nRIGA HEAD: ";
     cout << player[0][0];
     cout << "\nCOLONNA tail : ";
     cout << player[0][1];
     cout << "\nRIGA tail: ";
     cout << player[1][1]; */


  return 1;
};

int
snake::direction ()
{
  cin >> dir;
  //cout << "\nThe direction input: ";
  //cout << dir;
  movep = 0;
  return 1;

};


int
snake::move (char vet[30][30], int righe, int colonne)
{

  int ww, aa, dd, ss, ll, cc;
  int xhead, yhead, xtail, ytail;
  xhead = player[0][0];
  yhead = player[1][0];
  xtail = player[0][snake_lenght - 1];
  ytail = player[1][snake_lenght - 1];

 
  
  if (dir == 'x')
    {
      c_gameover = 1;
    }

  if (dir == 'w')
    {
        //move head
        xhead = xhead;
        yhead = yhead - 1;
        player[0][0] = xhead;
        player[1][0] = yhead;
        vet[xhead][yhead] = '^';
        
        for(cc = 1;cc < snake_lenght;cc++)
        {
        
            if((player[0][0] == player[0][cc])&&(player[1][0] == player[1][cc]))
            {
            c_gameover = 1;
            }
        }
        
        
        for(ww = 1;ww < snake_lenght;ww++)
        {
            //x
            player[0][ww] = old[0][ww - 1];
            //y
            player[1][ww] = old[1][ww - 1];
            
            vet[player[0][ww]][player[1][ww]] = '0';
        }
        if(c_fruit == 0)
        {
            vet[old[0][snake_lenght - 1]][old[1][snake_lenght - 1]] = ' ';
        }
        if(c_fruit == 1)
        {
            vet[old[0][snake_lenght]][old[1][snake_lenght]] = ' ';
        }
    }
    if (dir == 'a')
    {
        //move head
        yhead = yhead;
        xhead = xhead - 1;
        player[0][0] = xhead;
        player[1][0] = yhead;
        
        vet[xhead][yhead] = '<';
        
        for(cc = 1;cc < snake_lenght;cc++)
        {
        
            if((player[0][0] == player[0][cc])&&(player[1][0] == player[1][cc]))
            {
            c_gameover = 1;
            }
        }
        
        
        for(aa = 1;aa < snake_lenght;aa++)
        {
            //x
            player[0][aa] = old[0][aa - 1];
            //y
            player[1][aa] = old[1][aa - 1];
            
            vet[player[0][aa]][player[1][aa]] = '0';
        }
        if(c_fruit == 0)
        {
            vet[old[0][snake_lenght - 1]][old[1][snake_lenght - 1]] = ' ';
        }
        if(c_fruit == 1)
        {
            vet[old[0][snake_lenght]][old[1][snake_lenght]] = ' ';
        }
    }
    if (dir == 'd')
    {
        //move head
        yhead = yhead;
        xhead = xhead + 1;
        player[0][0] = xhead;
        player[1][0] = yhead;
        vet[xhead][yhead] = '>';
        
        for(cc = 1;cc < snake_lenght;cc++)
        {
        
            if((player[0][0] == player[0][cc])&&(player[1][0] == player[1][cc]))
            {
            c_gameover = 1;
            }
        }
        
        
        for(dd = 1;dd < snake_lenght;dd++)
        {
            //x
            player[0][dd] = old[0][dd - 1];
            //y
            player[1][dd] = old[1][dd - 1];
            
            vet[player[0][dd]][player[1][dd]] = '0';
        }
        if(c_fruit == 0)
        {
            vet[old[0][snake_lenght - 1]][old[1][snake_lenght - 1]] = ' ';
        }
        if(c_fruit == 1)
        {
            vet[old[0][snake_lenght]][old[1][snake_lenght]] = ' ';
        }
    }
    if (dir == 's')
    {
        //move head
        xhead = xhead;
        yhead = yhead + 1;
        player[0][0] = xhead;
        player[1][0] = yhead;
        vet[xhead][yhead] = 'v';
        
        for(cc = 1;cc < snake_lenght;cc++)
        {
        
            if((player[0][0] == player[0][cc])&&(player[1][0] == player[1][cc]))
            {
            c_gameover = 1;
            }
        }
        
        
        
        
        for(ss = 1;ss < snake_lenght;ss++)
        {
            //x
            player[0][ss] = old[0][ss - 1];
            //y
            player[1][ss] = old[1][ss - 1];
            vet[player[0][ss]][player[1][ss]] = '0';
            
        }
        if(c_fruit == 0)
        {
            vet[old[0][snake_lenght - 1]][old[1][snake_lenght - 1]] = ' ';
        }
        if(c_fruit == 1)
        {
            vet[old[0][snake_lenght]][old[1][snake_lenght]] = ' ';
        }
    }
    
    for(ll = 0;ll < snake_lenght;ll++)
    {
        old[0][ll] = player[0][ll];
        old[1][ll] = player[1][ll];
    }
    if((player[0][0] == 0)||(player[0][0] == 29)||(player[1][0] == 0)||(player[1][0] == 29))
    {
        
        c_gameover = 1;
    }
    
    
 
    
    

  return 1;
};


int
snake::check_fruit (char vet[30][30], int righe, int colonne)
{

  if ((player[0][0] == fruit[0]) && (player[1][0] == fruit[1]))
    {
      player_score++;
      c_fruit++;
      snake_lenght++;
      //cout << "\n\n\n\nSNAKE LENGHT: ";
      //cout << snake_lenght;
    }



  return 1;
};





3 Risposte

Devi accedere o registrarti per scrivere nel forum
3 risposte