Estoy haciendo un juego de snake en c#, y necesitaría un par de cosas que no se muy bien como hacer:
Necesito que el juego mande un mensaje similar a "has muerto" en cuanto la serpiente toque el muro o se toque a si misma, pero antes que esto, necesito saber cómo puedo hacer que la serpiente avance con un tamaño inicial establecido, por ejemplo 2 o 3 anillas
Cabe decir que tengo cierta idea sobre cómo hacer lo del muro, estaba pensando en algo como:
si (abs(cx) == 30 o abs(cy) == 100) entonces te saca del programa
Pero no se si esta sería la forma más correcta, así que si alguien puede darme alguna orientación más, le estaré inmensamente agradecido. Os dejo el código que tengo hasta ahora
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace snake_dia_1
{
class Program
{
public struct Coords
{
public int cX;
public int cY;
}
public struct Cuc
{
public int cX;
public int cY;
//public coords cap; seria mes correcte
public char direccio;
}
static void Main(string[] args)
{
Cuc cuc = new Cuc();
bool sortir = false;
ConsoleKeyInfo cki;
pintaMarc();
cuc.direccio = 'd';
pintaCuc(ref cuc.cX, ref cuc.cY);
while (!sortir)
{
if (Console.KeyAvailable)
{
cki = Console.ReadKey();
if (cki.Key == ConsoleKey.UpArrow)
{
cuc.direccio = 'w';
}
if (cki.Key == ConsoleKey.DownArrow)
{
cuc.direccio = 's';
}
if (cki.Key == ConsoleKey.LeftArrow)
{
cuc.direccio = 'a';
}
if (cki.Key == ConsoleKey.RightArrow)
{
cuc.direccio = 'd';
}
if (cki.Key == ConsoleKey.Escape)
{
exit();
}
}
mouCuc(ref cuc.cX, ref cuc.cY, cuc.direccio);
System.Threading.Thread.Sleep(150);
}
} //end main()
static void pintaMarc()
{
//caracters marc 186 ║, 187 ╗, 188 ╝, 200 ╚, 201 ╔, 205 ═
Console.SetCursorPosition(1, 1);
Console.WriteLine("╔");
Console.SetCursorPosition(1, 30);
Console.WriteLine("╚");
Console.SetCursorPosition(100, 1);
Console.WriteLine("╗");
Console.SetCursorPosition(100, 30);
Console.WriteLine("╝");
for (int y = 2; y < 100; y++)
{
int x = 1;
Console.SetCursorPosition(y, x);
Console.WriteLine("═");
}
for (int x = 2; x < 30; x++)
{
int y = 1;
Console.SetCursorPosition(y, x);
Console.WriteLine("║");
y = 100;
Console.SetCursorPosition(y, x);
Console.WriteLine("║");
}
for (int y = 2; y < 100; y++)
{
int x = 30;
Console.SetCursorPosition(y, x);
Console.WriteLine("═");
}
}
static void pintaCuc(ref int cx, ref int cy)
{
cx = 30;
cy = 10;
Console.SetCursorPosition(cx, cy);
Console.Write("█");
}
static void mouCuc(ref int cx, ref int cy, char direccio)
{
if(direccio == 'w')
{
Console.SetCursorPosition(cx, cy);
Console.Write("█");
cy--;
Console.SetCursorPosition(cx, cy);
Console.Write("0");
}
if (direccio == 'a')
{
Console.SetCursorPosition(cx, cy);
Console.Write("█");
cx--;
Console.SetCursorPosition(cx, cy);
Console.Write("0");
}
if (direccio == 's')
{
Console.SetCursorPosition(cx, cy);
Console.Write("█");
cy++;
Console.SetCursorPosition(cx, cy);
Console.Write("0");
}
if (direccio == 'd')
{
Console.SetCursorPosition(cx, cy);
Console.Write("█");
cx++;
Console.SetCursorPosition(cx, cy);
Console.Write("0");
}
}
static void exit ()
{
Environment.Exit(0);
}
} //end class
}//end namespace