Пинг Понг, не могу понять как игроками(потоками) проверить удар по мячу и где сделать синхронизацию
работа в windows form. Игра в пинг-понг А. Программа игры двух потоков в пинг-понг. Потоки управляют ракетками – бьют по очереди по мячу. Сделать вариант программы в форме, с графикой, с управлением ракетками клавишами, счетом.
смог реализовать все без потоков, а вот с ними не могу, нужно запилить синхронизацию с потоками.
public partial class Form1 : Form
{
System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
Player [] player;
Thread [] potok;
int FPS = 60;
Graphics graphics;
int ballx;
int bally;
int ballspdx = 5;
int ballspdy = 5;
public Form1()
{
InitializeComponent();
graphics = CreateGraphics();
ballx = Width / 2;
bally = Height / 2;
potok = new Thread[2];
player = new Player[2];
player[0] = new Player(0,this.Height / 3);
player[1] = new Player(Width - 35, this.Height / 3);
timer.Enabled = true;
timer.Interval = 1000 / FPS;
timer.Tick += new EventHandler(TimerCallback);
Start();
}
void Start()
{
for (int i = 0;i < player.Length;i++)
{
potok[i] = new Thread(player[0].Play);
potok[i].Start();
}
}
void UpdateBall()
{
ballx += ballspdx;
bally += ballspdy;
if (bally < 0 || bally + 40 > this.Height)
{
ballspdy = -ballspdy;
}
if (IsCollided())
{
ballspdx = -ballspdx;
}
if (ballx < 0 || ballx > this.Width - 40)
{
label1.Show();
label1.Text = "GAME OVER";
timer.Stop();
}
}
public bool IsCollided()
{
if (ballx < 20 && bally > player[0].y && bally < player[0].y + 130 || ballx > this.Width - 50 && bally > player[1].y && bally < player[1].y + 130)
{
return true;
}
else
{
return false;
}
}
void TimerCallback(object sender, EventArgs e)
{
UpdateBall();
this.Invalidate();
return;
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
graphics.FillRectangle(new SolidBrush(Color.White), new Rectangle(ballx, bally, 20, 20));
graphics.FillRectangle(new SolidBrush(Color.Yellow),new Rectangle(Width/2,0,10,Height));
player[0].DrawPlayer(e.Graphics,new SolidBrush(Color.Black));
player[1].DrawPlayer(e.Graphics, new SolidBrush(Color.Red));
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
int key = e.KeyValue;
if (key == 87) { player[0].y -= 7; }
if (key == 83) { player[0].y += 7; }
if (key == 38) { player[1].y -= 7; }
if (key == 40) { player[1].y += 7; }
}
private void Form1_ResizeBegin(object sender, EventArgs e)
{
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
for (int i = 0; i < player.Length; i++)
{
player[i].live = false;
}
}
}
class Player
{
public bool live = true;
public int x;
public int y;
public Player(int x,int y)
{
this.x = x;
this.y = y;
}
public void DrawPlayer(Graphics graphics, SolidBrush Color)
{
graphics.FillRectangle(Color, new Rectangle(x, y, 20, 130));
}
public void Play()
{
}
}
думал в методе Play()(он и запускается в потоке) сделать вызов метода IsCollided() сделать в нём синхронизацию, но как из потока вызвать его, и пока один поток не ударит по мячу другой поток ждет, и так по кругу.
не обязательно такая реализация, может нужно выделить больше классов,и как-то отталкиваться от этого.