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();
}
Why did he choose 'H' and 'K'? Why shouldn’t he be much of the game area, otherwise he would have used "AWDS".
– mau humor
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.
– mau humor
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
– Anderson Henrique
@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()))
– Anderson Henrique
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 :/.
– mau humor
Maybe tomorrow, rsrsrs. Good night :D
– mau humor
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?
– Luiz Vieira
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.
– Luiz Vieira
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 loopwhile
makes the snake move on its own according to two variables, the direction (d
) and speed (velo
andvelo2
- 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 variabled
).– Luiz Vieira
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.
– Luiz Vieira