iT邦幫忙

0

Visual C#打飛機遊戲無法得分

YJYJ 2020-11-02 16:16:191694 瀏覽
  • 分享至 

  • xImage

目前剛學程式一陣子,想嘗試做出打飛機的遊戲
飛機可以動了,也可以發子彈,但不知道為什麼沒辦法顯示分數
麻煩各位大師幫忙解惑了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();
}
再努力一下。這樣問問題很不好。
不要將程式po出就問哪邊有問題。這代表你沒努力查過原因。
fillano iT邦超人 1 級 ‧ 2020-11-02 21:19:50 檢舉
只有一個case的switch?
優悠 iT邦新手 3 級 ‧ 2020-11-04 08:54:56 檢舉
這應該是UNITY吧?
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
海綿寶寶
iT邦大神 1 級 ‧ 2020-11-02 21:44:47

先把這兩列

Score1.Text = "0";
Score2.Text = "0";

加進private void StartButton_Click(object sender, EventArgs e)裡試試看

我要發表回答

立即登入回答