我正在用 c# 制作一种射击游戏。我使用 c# 图形创建了一个目标和一个目标。这迫使我使用Invalidate();
,但这会导致严重的闪烁和滞后。我修复了闪烁,DoubleBuffered = true;
但在运行程序时我仍然有很严重的滞后。
public Form1()
{
InitializeComponent();
minTavla1 = new Mål(ClientSize.Width / 2, ClientSize.Height / 2, 50);
sikte = new Sikte(ClientSize.Width / 3, ClientSize.Height / 3, 20);
skott = new skott(ClientSize.Width, ClientSize.Height / 3, 5);
minTavla1.X = 150;
minTavla1.Y = 150;
timer1.Start();
DoubleBuffered = true;
//minTavla2 = new Mål(50, 50, 50);
//minTavla3 = new Mål(150, 200, 50);
}
int i = 0;
protected override void OnMouseClick(MouseEventArgs e)
{
base.OnMouseClick(e);
if (pictureBox1.Visible == true)
{
skott.X = e.X;
skott.Y = e.Y;
}
Skott.Play();
if (pictureBox1.Visible == false)
{
Reload.Play();
progressBar1.Value = 0;
timer3.Start();
}
if (pictureBox2.Visible == false)
{
pictureBox1.Visible = false;
}
if (pictureBox3.Visible == false)
{
pictureBox2.Visible = false;
}
if (pictureBox4.Visible == false)
{
pictureBox3.Visible = false;
}
if (pictureBox5.Visible == false)
{
pictureBox4.Visible = false;
}
pictureBox5.Visible = false;
if (skott.X > minTavla1.X - 50 && skott.Y < minTavla1.Y + 50)
{
if (skott.X < minTavla1.X + 50 && skott.Y > minTavla1.Y - 50)
{
i += 5;
label1.Text = i.ToString();
timer2.Start();
}
if (skott.X > minTavla1.X - 10 && skott.Y < minTavla1.Y + 10)
{
if (skott.X < minTavla1.X + 10 && skott.Y > minTavla1.Y - 10)
{
i += 5;
label1.Text = i.ToString();
timer4.Start();
}
}
}
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
minTavla1.Rita(e.Graphics);
sikte.Ritasikte(e.Graphics);
skott.ritaskott(e.Graphics);
//minTavla2.Rita(e.Graphics);
//minTavla3.Rita(e.Graphics);
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
sikte.X = e.X;
sikte.Y = e.Y;
}
private void pbxMal_Click(object sender, EventArgs e)
{
MessageBox.Show("5points");
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
minTavla1.X += 5;
this.Invalidate();
}
private void timer2_Tick(object sender, EventArgs e)
{
if (timer2.Interval > 100)
{
minTavla1.X = -200;
}
}
private void timer3_Tick(object sender, EventArgs e)
{
progressBar1.Maximum = 100;
progressBar1.Increment(+1);
if (progressBar1.Value == 5)
{
pictureBox1.Visible = true;
pictureBox2.Visible = true;
pictureBox3.Visible = true;
pictureBox4.Visible = true;
pictureBox5.Visible = true;
timer3.Stop();
}
}
private void timer4_Tick(object sender, EventArgs e)
{
progressBar2.Maximum = 100;
progressBar2.Increment(+1);
pictureBox6.Visible = true;
if (progressBar2.Value == 10)
{
pictureBox6.Visible = false;
progressBar2.Value = 0;
timer4.Stop();
}
}
}
}