Last active
August 29, 2015 14:08
-
-
Save brayancruces/88bbf88de1a0d86734a2 to your computer and use it in GitHub Desktop.
Mover personaje (snippet)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Mover personaje (snippet) | |
// http://www.forosdelweb.com/f14/codigo-ascii-las-flechas-navegacion-236009/ | |
#include <iostream> | |
#include <conio.h> | |
#include <windows.h> | |
using namespace std; | |
using namespace System; | |
void Borrar_Personaje(int X,int Y) | |
{ | |
Console::SetCursorPosition(X,Y);cout<<" "; | |
} | |
void Dibujar_Personaje(int X, int Y) | |
{ | |
Console::SetCursorPosition(X,Y);cout<<char(1); | |
} | |
void Mover_Personaje(int &X, int &Y, int &Dx, int &Dy) | |
{ | |
Dx=Dy=0; | |
if(kbhit()==true)//Si es que presiono algo | |
{ | |
char tecla_presionada = getch(); | |
tecla_presionada = toupper(tecla_presionada); | |
switch(tecla_presionada) | |
{ | |
case 'W': Dy = -1; Dx = 0; break; | |
case 'S': Dy = 1; Dx = 0; break; | |
case 'A': Dx = -1 ; Dy = 0; break; | |
case 'D': Dx = 1 ; Dy = 0; | |
} | |
} | |
if(X+Dx <0 || X+Dx>79) Dx = 0; | |
if(Y+Dy <0 || Y+Dy>24) Dy = 0; | |
X = X + Dx; | |
Y = Y + Dy; | |
Dibujar_Personaje(X,Y); | |
Sleep(100);//Retardo | |
Borrar_Personaje(X,Y); | |
} | |
void Controlador(int &Xpersonaje, int &Ypersonaje, int &Dx, int &Dy) | |
{ | |
while(1){ | |
Mover_Personaje(Xpersonaje,Ypersonaje,Dx,Dy); | |
} | |
} | |
void main() | |
{ | |
int Xpersonaje,Ypersonaje; //Dibujar algo | |
int Dx,Dy; // Mover algo | |
Xpersonaje=40;//40 (ancho consola 80) | |
Ypersonaje=12;//12 (alto consola es 25) | |
Dx=Dy=0; | |
Controlador(Xpersonaje,Ypersonaje,Dx,Dy); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment