Use of arrow keys to generate movement in play

Asked

Viewed 867 times

0

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

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

'Cause when you press the arrow that it doesn’t happen to be these letters the snake moves?

Here comes 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();
    }
  • 1

    Why did he choose 'H' and 'K'? Why shouldn’t he be much of the game area, otherwise he would have used "AWDS".

  • I’m not able to run this code here (nor do I want to download an ide/compiler). It kills me a curiosity, how does this code work? Does the "snake" walk alone, or only when you press a key? For once I did something similar and read a key in a loop without making the terminal block was a "childbirth" rsrsrs.

  • That’s not what I’m saying... let’s assume if I press the arrow up the program understands that I pressed 'K' I don’t know how it happens if you want to Compile and see, the game is on the arrow same

  • @mauhumor if you start the side she’s going to go she walks alone until she makes another decision, as shows this part of the while code(key!= 's') { while(key!= ’s'&&! (key=kbhit()))

  • You’re asking me to interpret the subject’s code, which is complicated. Each has its own logic and specific way of solving a problem. And understanding how someone else did is not always easy, even with years of experience in a language or technology. And I harnessed for this code :/.

  • Maybe tomorrow, rsrsrs. Good night :D

  • 1

    Hi Anderson. Welcome to SOPT. First of all, note that this site is not a forum (do the [tour] and read [Ask]). Throwing someone else’s code in your question and commenting on "Compile there and see" will not help you get useful answers, okay?

  • 1

    Well, the "why" of the author having used the letters is merely a choice. To get the right answer, you will have to ask the author. Like fellow @mauhumor well commented, that is not the standard used in games.

  • 1

    About how the drive works, it does not depend on the keys. The keys just control the direction. This code is very poorly organized, then it is hard to know for sure just by looking. But it seems to me the following: basically the function mgotoxy is responsible for positioning; the loop while makes the snake move on its own according to two variables, the direction (d) and speed (velo and velo2 - I don’t know why the author uses two variables, and I didn’t stop to analyze them in detail); the keys only change direction (that is, they change the variable d).

  • 1

    If you want to know exactly how that code works, compile it and debug it yourself. It won’t do you much good to come ask here. Now, if you want to learn how to make a game in a decent way, I’m sorry to say but it won’t be analyzing that code. Look for a good book or course.

Show 5 more comments

1 answer

5


This code has nothing C++, is very 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 was thought and has errors, in fact it even compiles.

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

In the specific compiler he used, and probably only in it, 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 arrow keys.

  • +1 for the good answer. But a curiosity: how did you infer the compiler he used? If they have the same code as the arrows, the choice makes a little more sense. But it wouldn’t be strange for them to have the same code?

  • @Luizvieira I did not infer which is the compiler. Only that in its compiler should work it :) It has compiler that actually mixes keystrokes, but it is out of pattern. And by compiler, understand his library. Enlightened? I think you’d like this one: http://answall.com/q/177611/101 but it was hidden by the negatives. I said it right? :)

  • Ah, yes. Thank you! It’s just that I was curious (and as you know a lot more than I do in those meanders I thought I could learn something new). :)

  • 1

    @Luizvieira quite the contrary :D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.