static int ReceivePacket() { ms.Position = 0; socket.Receive(ms.GetBuffer()); int code = reader.ReadInt32(); int id; int x; int y; char sprite; ConsoleColor color; switch (code) { case 0: return(reader.ReadInt32()); case 1: id = reader.ReadInt32(); x = reader.ReadInt32(); y = reader.ReadInt32(); Player plr = players.Find(p => p.ID == id); if (plr != null) { plr.Remove(); plr.X = x; plr.Y = y; plr.Draw(); if (check() != -1) { Console.SetCursorPosition(1, 22); Console.Write("Last Winner: Player" + plr.ID); } } else { sprite = reader.ReadChar(); color = (ConsoleColor)reader.ReadInt32(); plr = new Player(x, y, sprite, color, id); players.Add(plr); plr.Draw(); } break; } return(-1); }
static void Main(string[] args) { Console.Title = "Client"; Console.WriteLine("EnterIp"); var a = Console.ReadLine(); Console.WriteLine("Подключение к серверу..."); socket.Connect(a, 2048); Console.WriteLine("Подключено"); Thread.Sleep(1000); Console.Clear(); Console.WriteLine("Введите спрайт"); char spr = Convert.ToChar(Console.ReadLine()); Console.Clear(); Console.WriteLine("Выберите цвет"); for (int i = 0; i <= 14; i++) { Console.ForegroundColor = (ConsoleColor)i; Console.WriteLine(i); } Console.ResetColor(); ConsoleColor clr = (ConsoleColor)int.Parse(Console.ReadLine()); Console.Clear(); int x = random.Next(1, 19); int y = random.Next(1, 19); Console.WriteLine("Получение идентификатора"); SendPacket(PacketInfo.ID); int id = ReceivePacket(); Console.WriteLine("Получен ID :" + id); Thread.Sleep(1000); Console.Clear(); player = new Player(x, y, spr, clr, id); SendPacket(PacketInfo.Position); Task.Run(() => { while (true) { ReceivePacket(); } }); while (true) { switch (Console.ReadKey(true).Key) { case ConsoleKey.LeftArrow: player.Remove(); player.X = Math.Max(player.X - 1, 0); player.Draw(); SendPacket(PacketInfo.Position); break; case ConsoleKey.RightArrow: player.Remove(); player.X = Math.Min(player.X + 1, 19); player.Draw(); SendPacket(PacketInfo.Position); break; case ConsoleKey.UpArrow: player.Remove(); player.Y = Math.Max(player.Y - 1, 0); player.Draw(); SendPacket(PacketInfo.Position); break; case ConsoleKey.DownArrow: player.Remove(); player.Y = Math.Min(player.Y + 1, 19); player.Draw(); SendPacket(PacketInfo.Position); break; } if (check() != -1) { Console.SetCursorPosition(1, 22); Console.Write("Last Winner: Player" + id); } } }