Use of arrow keys to generate movement in game

-2

I wanted to know how the developer of the Snake game in C ++ merged with C made the snake move from the keyboard arrows.

I think the part it moves on is the time that compares tecla=='K' tecla=='H' and so on, but I wanted to know why it used those letters, and the main one.

Why does not the snake move when you press the arrow that happens to be the letters?

Here's the code

  #include<windows.h>
  #include<stdio.h>
  #include<conio.h>
  #include<iostream>
  #include <stdlib.h>
  #include <time.h>


  //Código feito com base em tutoriais na internet, desenvolvido por  
  Gustavo    Ferreira do 2 semestre do Curso de Jogos Digitais da FATEC   
  SAO CAETANO DO SUL.

  void mgotoxy(int x, int y)
  {
  SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),(COORD)          
  {x,y});//Percorre a matriz    
  }

  main() 
  {
  int x,d=2,cx[300]={1,2},cy[300]={7,7},t=1,mx,my,velo=100,velo2=5;
  char niv;

  char tecla='a';
  int opcao;
  int pontos=0;
  int nivel = 1;



  for(x=0;x<18;x++)
  { mgotoxy(0,x); //vertical esquerda.//

  printf("%c",219);
  }
  for(x=0;x<50;x++)
  { mgotoxy(x,0); //horizontal superior//
  printf("%c",219);
  }
  for(x=0;x<18;x++)
  { mgotoxy(50,x); //vertical direita//
   printf("%c",219);
  }
  for(x=0;x<51;x++)
  { mgotoxy(x,18); //horizontal inferior.//
  printf("%c",219);
   }

  srand(time(NULL));
  mx=(rand()%49)+1;
  my=(rand()%17)+1;

  velo = 200;

  while(tecla!='s')
  { while(tecla!='s'&&!(tecla=kbhit()))

  { for(x=t;x>0;x--)
  { cx[x]=cx[x-1];
  cy[x]=cy[x-1];
   }

  if(d==0)cx[0]--;
  if(d==1)cy[0]--;
  if(d==2)cx[0]++;
  if(d==3)cy[0]++;
  mgotoxy(cx[t],cy[t]);
  printf(" ");
  if(mx==cx[0]&&my==cy[0])
  { t++;
  pontos++;
  mx=(rand()%25)+1;
  my=(rand()%17)+1;
  velo-=5;
  velo2+=5;

    }
   mgotoxy(cx[0],cy[0]);

   printf("%c",219);

   mgotoxy(mx,my);
   printf("%c",1);
   mgotoxy(55,10);

   printf ("Pontos: %d",pontos);
   mgotoxy(55,5);
   printf ("Nivel: %d",nivel);
   mgotoxy(55,3);
   printf ("Velocidade: %d",velo2);
   mgotoxy(3,22);

   printf ("Jogo desenvolvido por Gustavo Ferreira");
   Sleep(velo);
   for(x=1;x<t;x++)
   { if(cx[0]==cx[x]&&cy[0]==cy[x])tecla='s';
   }
   if(cy[0]==0||cy[0]==18||cx[0]==0||cx[0]==50)tecla='s';

     }
   if(tecla!='s')tecla=getch();
   printf("%c",tecla);
   if(tecla=='K')d=0;
   if(tecla=='H')d=1;
   if(tecla=='M')d=2;
   if(tecla=='P')d=3;
   if(cy[0]==0||cy[0]==18||cx[0]==0||cx[0]==26)tecla='s';

  }
 system("cls");
 system("pause");

 printf ("\n\n\tVOCE PERDEU\n\n");

 printf ("\n\n\tVOCE FEZ %d PONTOS",pontos);







   getch();
    }
    
asked by anonymous 14.01.2017 / 06:00

1 answer

5

This code has nothing C ++. It's pretty bad and I would not waste time with it. Learn with good codes. It uses bad techniques, bad style, is confusing, uses what is not available in all compilers, I doubt it does what it was thought of and has errors, in fact it does not even compile.

The allowed keys are K , H , M , P .

In the specific compiler that he used, and probably only in them, these keys have the same code as the arrow keys and so the choice must have been made, the intention is not to use them but to use the arrows. >     

14.01.2017 / 11:38