共用方式為


教學課程:在相符的遊戲 WinForms 應用程式中顯示訊息

在這四部教學課程中,您會製作一個配對遊戲,讓玩家配對隱藏的圖示。

在本教學課程中,您會修改比對遊戲,讓相符配對保持可見,並在玩家獲勝時顯示恭喜訊息。

在本教學課程中,您將瞭解如何:

  • 讓配對保持可見。
  • 確認玩家是否贏了。
  • 請嘗試其他功能。

先決條件

本教學課程以下列先前的教學課程為基礎:

  1. 建立相符的遊戲應用程式
  2. 將圖示新增至你的相符遊戲
  3. 在您的比對遊戲中新增定時器

讓配對保持可見

當玩家符合配對時,遊戲應該自行重設,使其不再追蹤任何使用 firstClickedsecondClicked 參考變數的標籤。 它不應該重設兩個已匹配標籤的色彩。 這些標籤會繼續顯示。

  1. 將下列 if 語句新增至 label_Click() 事件處理程式方法。 將它放在程式代碼的結尾處,就在您啟動定時器的 語句上方。
        // If the player gets this far, the timer isn't
        // running and firstClicked isn't null,
        // so this must be the second icon the player clicked
        // Set its color to black
        secondClicked = clickedLabel;
        secondClicked.ForeColor = Color.Black;

        // If the player clicked two matching icons, keep them 
        // black and reset firstClicked and secondClicked 
        // so the player can click another icon
        if (firstClicked.Text == secondClicked.Text)
        {
            firstClicked = null;
            secondClicked = null;
            return;
        }

        // If the player gets this far, the player 
        // clicked two different icons, so start the 
        // timer (which will wait three quarters of 
        // a second, and then hide the icons)
        timer1.Start();
    }
}

if 語句會檢查玩家選擇的第一個標籤中的圖示是否與第二個標籤中的圖示相同。 如果圖示相同,程式會執行其三個語句。 前兩個語句會重設 firstClickedsecondClicked 參考變數。 他們不再追蹤任何標籤。 第三個語句是 return 語句,它會略過 方法中的其餘語句,而不執行它們。

  1. 執行程式,然後開始選擇表單上的正方形。

您在本教學課程中建立之比對遊戲的螢幕快照。

如果您選擇不相符的配對,定時器的Tick事件就會觸發。 這兩個圖示都會消失。

如果您選擇相符的配對,則會執行新的 if 語句。 return 語句會導致 方法略過啟動定時器的程序代碼。 圖示會保持可見。

確認玩家是否贏了

您已建立一個有趣的遊戲。 玩家獲勝后,比賽應該結束。 本節會新增方法來驗證玩家是否獲勝。

  1. CheckForWinner() 方法新增至程序代碼底部,timer1_Tick() 事件處理程式下方。
/// <summary>
/// Check every icon to see if it is matched, by 
/// comparing its foreground color to its background color. 
/// If all of the icons are matched, the player wins
/// </summary>
private void CheckForWinner()
{
    // Go through all of the labels in the TableLayoutPanel, 
    // checking each one to see if its icon is matched
    foreach (Control control in tableLayoutPanel1.Controls)
    {
        Label iconLabel = control as Label;

        if (iconLabel != null) 
        {
            if (iconLabel.ForeColor == iconLabel.BackColor)
                return;
        }
    }

    // If the loop didn’t return, it didn't find
    // any unmatched icons
    // That means the user won. Show a message and close the form
    MessageBox.Show("You matched all the icons!", "Congratulations");
    Close();
}

方法會在 C# 中使用另一個 foreach 迴圈,或在 Visual Basic 中使用 For Each 迴圈,以遍歷 TableLayoutPanel中的每個標籤。 它會檢查每個標籤的圖示色彩,以確認它是否符合背景。 如果色彩相符,圖示會維持不可見狀態,且玩家尚未配對所有剩餘的圖示。

在此情況下,程式會使用 return 語句來略過方法的其餘部分。 如果迴圈會通過所有標籤而不執行 return 語句,這表示表單上的所有圖示都相符。 程式顯示一個 MessageBox 祝賀玩家獲勝,然後呼叫 Close() 方法來結束遊戲。

  1. 讓標籤的 Click 事件處理程式呼叫新的 CheckForWinner() 方法。
// If the player gets this far, the timer isn't
// running and firstClicked isn't null, 
// so this must be the second icon the player clicked
// Set its color to black
secondClicked = clickedLabel;
secondClicked.ForeColor = Color.Black;

// Check to see if the player won
CheckForWinner();

// If the player clicked two matching icons, keep them 
// black and reset firstClicked and secondClicked 
// so the player can click another icon
if (firstClicked.Text == secondClicked.Text)
{
    firstClicked = null;
    secondClicked = null;
    return;
}

請確定您的程式會在顯示玩家選擇的第二個圖示之後,立即檢查勝利者。 尋找您設定第二個選定的圖示色彩的那一行,然後在該行之後呼叫 CheckForWinner() 方法。

  1. 儲存並執行程式。 玩遊戲並比對所有圖示。 當您獲勝時,程式會顯示賀電。

    螢幕快照顯示包含訊息框的配對遊戲。

    選取 確定之後,比對遊戲就會關閉。

嘗試其他功能

您的比對遊戲已完成。 您可以新增更多功能,讓此遊戲更具挑戰性和有趣性。 以下是一些選項。

  • 將圖示和色彩取代為您所選擇的。

    請嘗試查看標籤的 ForeColor 屬性。

  • 新增遊戲定時器,追蹤玩家獲勝所需的時間。

    您可以新增標籤,以顯示表單上經過的時間。 將它放在 TableLayoutPanel上方。 將另一個定時器新增至窗體以追蹤時間。 使用程式代碼在玩家啟動遊戲時啟動定時器,並在符合最後兩個圖示之後停止定時器。

  • 當玩家配對成功時新增音效,當玩家翻開兩個不相符的圖示時新增另一個音效,當程式再次隱藏圖示時新增第三個音效。

    若要播放音效,您可以使用 System.Media 命名空間。 如需詳細資訊,請參閱在 Windows Forms 應用程式中 播放音效Visual Basic 中的音訊播放方法

  • 讓棋盤變大,使遊戲更加困難。

    您需要做的,不僅僅是將列和行新增至 TableLayoutPanel。 您也需要考慮您所建立的圖示數目。

  • 如果玩家反應太慢,隱藏第一個圖示,讓遊戲更具挑戰性。

後續步驟

祝賀! 您已完成這一系列的教學課程。 您已在 Visual Studio IDE 中完成這些程式設計和設計工作:

  • 清單中的預存物件,例如圖示
  • 使用 C# 或 Visual Basic 中的循環遍歷清單
  • 使用參考變數來追蹤狀態
  • 建置事件處理程式以回應多個物件的事件
  • 已新增一個倒數計時並觸發事件的定時器

請前往本文,深入瞭解 Windows Forms 設計工具。