目前剛學程式一陣子,想嘗試做出打飛機的遊戲
飛機可以動了,也可以發子彈,但不知道為什麼沒辦法顯示分數
麻煩各位大師幫忙解惑了QQ
private void Form1_Load(object sender, EventArgs e)
{
button1.Select();
AttackTimer1.Stop();
AttackTimer2.Stop();
ClearButton.Enabled = false;
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.A:
fighter1.Left -= 5;
break;
case Keys.D:
fighter1.Left += 5;
break;
case Keys.J:
fighter2.Left -= 5;
break;
case Keys.L:
fighter2.Left += 5;
break;
case Keys.Space:
Shot1();
break;
case Keys.P:
Shot2();
break;
}
}
private void Shot1()
{
Label B = new Label();
B.Tag = "B";
B.BackColor = Color.OrangeRed;
B.Width = 6;
B.Height = 6;
B.Left = fighter1.Left + fighter1.Width / 2 - B.Width / 2;
B.Top = fighter1.Top - B.Height;
GamePanel.Controls.Add(B);
}
private void Shot2()
{
Label X = new Label();
X.Tag = "X";
X.BackColor = Color.RoyalBlue;
X.Width = 6;
X.Height = 6;
X.Left = fighter2.Left + fighter2.Width / 2 - X.Width / 2;
X.Top = fighter2.Bottom + X.Height;
GamePanel.Controls.Add(X);
}
private bool chkHit (Label B, Label C)
{
if (B.Right < C.Left)
{ return false; }
else if (B.Right > C.Left)
{ return false; }
else if (B.Bottom < C.Top)
{ return false; }
else if (B.Top < C.Bottom)
{ return false; }
else
{ return true; }
}
private void AttackTimer1_Tick(object sender, EventArgs e)
{
foreach(Control c in GamePanel.Controls)
{
string s = c.Tag.ToString();
switch (s)
{
case "B":
c.Top -= 5;
if(c.Bottom<0)
{ c.Dispose(); }
if (chkHit((Label)c, fighter2))
{
c.Dispose();
Score1.Text = (int.Parse(Score1.Text) + 15).ToString();
}
break;
}
}
}
private void AttackTimer2_Tick(object sender, EventArgs e)
{
foreach (Control c in GamePanel.Controls)
{
string s = c.Tag.ToString();
switch (s)
{
case "X":
c.Top += 5;
if (c.Bottom < 0)
{ c.Dispose(); }
if (chkHit((Label)c, fighter1))
{
c.Dispose();
Score2.Text = (int.Parse(Score2.Text) + 15).ToString();
}
break;
}
}
}
private void StartButton_Click(object sender, EventArgs e)
{
AttackTimer1.Start();
AttackTimer2.Start();
StartButton.Text = "遊戲開始";
StartButton.Enabled = false;
ClearButton.Enabled = true;
fighter1.Location = new Point(188, 352);
fighter2.Location = new Point(188, 16);
}
private void ClearButton_Click(object sender, EventArgs e)
{
StartButton.Enabled = true;
ClearButton.Enabled = false;
Score1.Text = "0";
Score2.Text = "0";
StartButton.Text = "再次開始";
fighter1.Location = new Point(188, 352);
fighter2.Location = new Point(188, 16);
AttackTimer1.Stop();
AttackTimer2.Stop();
}
先把這兩列
Score1.Text = "0";
Score2.Text = "0";
加進private void StartButton_Click(object sender, EventArgs e)
裡試試看