Иногда мяч проходит через ракетку (обычно когда очень часто нажимаются кнопки управления или они зажимаются). Особенно это заметно, если увеличить Thread.Sleep() (также появляется inputlag)
public class Player
{
public int X { set; get; }
public int Y { set; get; }
public int Lenght {set; get; }
int mapHeight;
public Player(int x, int mapHeight)
{
this.mapHeight = mapHeight;
X = x;
Y = mapHeight / 2;
Lenght = mapHeight / 3;
}
public void Write()
{
Console.ForegroundColor = ConsoleColor.Red;
for(int i=(Y-(Lenght/2));i<(Y+(Lenght/2));i++)
{
Console.SetCursorPosition(X, i);
Console.Write("│");
}
Console.ForegroundColor = ConsoleColor.White;
}
public void Up()
{
if((Y-1-(Lenght/2))!=0)
{
Console.SetCursorPosition(X, (Y + (Lenght / 2)) - 1);
Console.Write(" ");
Y--;
Write();
}
}
public void Down()
{
if ((Y + Lenght - (Lenght / 2)) != mapHeight+2)
{
Console.SetCursorPosition(X, (Y -(Lenght / 2)));
Console.Write(" ");
Y++;
Write();
}
}
}
введите public class Map
{
public int Height { set; get; }
public int Width { set; get; }
public Map()
{
Height = 20;
Width = 60;
}
public Map(int width, int height)
{
Height = height;
Width = width;
}
public void Write()
{
for (int i=1; i<=Width; i++)
{
Console.SetCursorPosition(i, 0);
Console.Write("‒");
} // Верхняя граница
for (int i = 1; i <= Width; i++)
{
Console.SetCursorPosition(i, (Height + 1));
Console.Write("‒");
} // Нижняя граница
for (int i = 1; i <= Height; i++)
{
Console.SetCursorPosition(0, i);
Console.Write("│");
} // Левая граница
for (int i = 1; i <= Height; i++)
{
Console.SetCursorPosition(Width+1, i);
Console.Write("│");
} // Правая граница
Console.SetCursorPosition(0, 0);
Console.Write("┌");
Console.SetCursorPosition(Width+1, 0);
Console.Write("┐");
Console.SetCursorPosition(0, Height+1);
Console.Write("└");
Console.SetCursorPosition(Width+1, Height+1);
Console.Write("┘");
}
}
public class Ball
{
public int X { set; get; }
public int Y { set; get; }
public int rotationX;
public int rotationY;
public int i;
public int mapHeight;
public int mapWidth;
public int route { set; get; }
public Ball (int x, int y, int mapHeight, int mapWidth)
{
X = x;
Y = y;
this.mapHeight = mapHeight;
this.mapWidth= mapWidth;
rotationX = -1;
rotationY = 1;
}
public void Logic(Player player1, Player player2)
{
Console.SetCursorPosition(X, Y);
Console.Write(" ");
if(Y<=1||Y>=mapHeight)
{
rotationY *= -1;
}
if (((X == 3 || X == mapWidth - 3) && (player1.Y - (player1.Lenght / 2)) <= Y && (player1.Y + (player1.Lenght / 2)) > Y))
{
rotationX *= -1;
if (Y == player1.Y)
{
route = 0;
}
if ((Y >= (player1.Y - (player1.Lenght / 2)) && Y <= player1.Y) || (Y > player1.Y && Y < (player1.Y + (player1.Lenght / 2))))
{
route = 1;
}
}
switch(route)
{
case 0:
X += rotationX;
break;
case 1:
X += rotationX;
Y += rotationY;
break;
}
}
public void Write()
{
Console.ForegroundColor = ConsoleColor.Blue;
Console.SetCursorPosition(X, Y);
Console.Write("■");
Console.ForegroundColor = ConsoleColor.White;
}
}
public class Pong
{
public int width;
public int height;
public Map map;
Player player1;
Player player2;
ConsoleKeyInfo keyInfo;
ConsoleKey consoleKey;
Ball ball;
public Pong(int width, int height)
{
this.width = width;
this.height = height;
map = new Map(width, height);
ball = new Ball(width / 2, height / 2, height, width) ;
}
public void Start()
{
while(true)
{
Console.Clear();
Init();
map.Write();
player1.Write();
player2.Write();
ball.Write();
while(ball.X!=1&&ball.X!=width-1)
{
ControlInput();
switch(consoleKey)
{
case ConsoleKey.W:
player1.Up();
break;
case ConsoleKey.S:
player1.Down();
break;
case ConsoleKey.UpArrow:
player2.Up();
break;
case ConsoleKey.DownArrow:
player2.Down();
break;
}
consoleKey = ConsoleKey.A;
ball.Logic(player1, player2);
ball.Write();
Thread.Sleep(30);
}
}
}
public void Init()
{
player1 = new Player(2, height);
player2 = new Player(width-2, height);
keyInfo = new ConsoleKeyInfo();
consoleKey = new ConsoleKey();
ball.X = width / 2;
ball.Y=height / 2;
ball.route = 0;
}
public void ControlInput ()
{
if(Console.KeyAvailable)
{
keyInfo = Console.ReadKey(true);
consoleKey = keyInfo.Key;
}
}
}
Console.CursorVisible = false;
Pong pong = new Pong(60, 20);
pong.Start();
Console.ReadKey();
