I was trying to practice doing a game on the console before going to unity, or rather to boot with unity. Particularly I had problems making the enemies, I came to the conclusion that what controls the enemies is the time and the conditions they have. Because of this I tried to create a timer that did not work so in lines where read timespan skip it was something wrong.
The other way that occurred to me, because time span did not work was to use Thread.Sleep()
, which is a pause of X amount of time. The problem that this presented was the following, when pressing a key the enemy that descends to hit us is accelerated by the pressing of the player's movements, because the conditions of if they are fulfilled quickly goes faster in sleep.
My questions are 2:
- Could this be done in a single thread?
-
How are timers instantiated correctly?
namespace juego_consola { class Program { static void Main(string[] args) { TimeSpan stop; TimeSpan start = new TimeSpan(DateTime.Now.Ticks); while (true) { stop = new TimeSpan(DateTime.Now.Ticks); interfaz.marco(); jugador.imprimo("/A\", nave.navex+nave.mov, nave.navey); Thread hilo = new Thread(new ThreadStart(roca.atake)); hilo.Start(); ConsoleKeyInfo tecla = Console.ReadKey(); if (tecla.Key == ConsoleKey.LeftArrow) { nave.mov += -1; if (nave.navex + nave.mov <= 2) { nave.mov = 0; nave.navex = 2; jugador.imprimo("/A\", nave.navex, nave.navey); continue; } jugador.imprimo("/A\", nave.navex + nave.mov, nave.navey); } if (tecla.Key == ConsoleKey.RightArrow) { nave.mov += 1; if (nave.navex + nave.mov >= 56) { nave.mov = 0; nave.navex = 55; jugador.imprimo("/A\", nave.navex, nave.navey); continue; } jugador.imprimo("/A\", nave.navex + nave.mov, nave.navey); } } } } //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% class nave { public static int navex = 16; public static int navey = 21; public static int mov = 0; } class roca { public static int rokx = 15; public static int roky = 0; public static void atake() { //var t = new System.Timers.Timer(1000); //t.Elapsed += (sender, args) => roca.atake(); //t.Start(); while (roky<=21) { roca.roky += 1; jugador.imprimo("O", roca.rokx, roca.roky); Thread.Sleep(1000); interfaz.marco(); } } } //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% class jugador { public static void imprimo(string s, int x, int y) { Console.SetCursorPosition(x, y); Console.WriteLine(s); } } class interfaz { public static void marco() { jugador.imprimo("████████████████████████████████████████████████████████████", 0, 0); jugador.imprimo("██ ██", 0, 1); jugador.imprimo("██ ██", 0, 2); jugador.imprimo("██ ██", 0, 3); jugador.imprimo("██ ██", 0, 4); jugador.imprimo("██ ██", 0, 5); jugador.imprimo("██ ██", 0, 6); jugador.imprimo("██ ██", 0, 7); jugador.imprimo("██ ██", 0, 8); jugador.imprimo("██ ██", 0, 9); jugador.imprimo("██ ██", 0, 10); jugador.imprimo("██ ██", 0, 11); jugador.imprimo("██ ██", 0, 12); jugador.imprimo("██ ██", 0, 13); jugador.imprimo("██ ██", 0, 14); jugador.imprimo("██ ██", 0, 15); jugador.imprimo("██ ██", 0, 16); jugador.imprimo("██ ██", 0, 17); jugador.imprimo("██ ██", 0, 18); jugador.imprimo("██ ██", 0, 19); jugador.imprimo("██ ██", 0, 20); jugador.imprimo("██ ██", 0, 21); jugador.imprimo("██ ██", 0, 22); jugador.imprimo("████████████████████████████████████████████████████████████", 0, 23); jugador.imprimo("/A\", nave.navex + nave.mov, nave.navey); } }
}