65.9K
CodeProject 正在变化。 阅读更多。
Home

通用 Windows 应用程序字符匹配游戏

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.61/5 (6投票s)

2016年12月1日

CPOL

4分钟阅读

viewsIcon

12687

downloadIcon

165

在本文中,我们将了解如何使用 Windows 通用应用创建一个简单的字符匹配游戏。创建您自己的游戏,并通过通用应用开发体验 Windows Phone 的乐趣。

引言

在本文中,我们将了解如何使用 Windows 通用应用创建一个简单的字符匹配游戏。我们将创建自己的游戏,并通过通用应用开发体验 Windows Phone 的乐趣。

Windows 通用应用程序

如果我们想要一个需要在任何 Windows 设备上运行的应用程序,例如 Windows Phone、Windows 8 或 Windows 10 操作系统,我们可以开发一个单一的应用程序,使用 Windows 通用应用程序,它可以运行在任何 Windows 设备上。

什么是字符匹配游戏?

字符匹配游戏是一种用户可以一个接一个地点击两个按钮的游戏。如果第一个点击的按钮和第二个点击的按钮具有相同的字符,那么他就赢了一次;如果他/她连续赢了 3 次,那么他/她在益智游戏中非常出色。如果他/她点击的第一个按钮和第二个按钮的字符不同,那么他/她就输掉了游戏。玩游戏没有次数限制,我们可以玩任意次数。这个游戏有 4 个主要规则:

  1. 点击第一个按钮和第二个按钮的字符匹配 - 赢得游戏并继续玩更多次数。
  2. 点击第一个按钮和第二个按钮的字符不匹配 – 输掉游戏并继续玩更多次数。
  3. 点击第一个按钮和第二个按钮的字符匹配,如果他/她连续赢了 3 次,那么他/她就是这个游戏的超级玩家,并继续玩更多次数。
  4. 要随机排列按钮文本并开始新游戏,请点击“开始新游戏”按钮。

必备组件

  1. Visual Studio 2015。您可以在此处下载。
  2. Windows Phone 8.1 模拟器下载此处
注意

此示例是使用 Windows 8.1 操作系统开发的。相同的应用程序可用于 Windows 10 操作系统。

Using the Code

步骤 1:创建我们的 ASP.NET Core 1.0.1 Web 应用程序

安装 Visual Studio 2015 和 Windows Phone 模拟器后,点击“开始”。选择“程序”,然后选择“Visual Studio 2015” - 点击“Visual Studio 2015”。

点击“新建”,然后点击“项目”,并选择“Visual C# > 选择 Windows 8 > 选择空白应用(通用 Windows 8.1)”。

选择您的项目位置路径,并为您的 UWP 应用命名,然后点击“确定”。

添加控件取决于您的需求,并编写您的第一个代码来显示您的输出。在此示例中,我们使用了 TextblockTextblock 类似于 Label 控件)、按钮和 Grid 控件。

Grid 控件是主要部分,因为每当用户点击“开始新游戏”按钮时,我们都会动态地将按钮添加到 Grid 中。

要显示消息框,我们需要导入,使用 Windows.UI.Popups

公共变量

每个变量都已注释并附带其 uses.#region 字段。

  // for creating buttons at runtime  
Button[] puzzleButtons;  
//to compare the previous click character with new click  
Button oldButton;  
Button newButton;  
string oldChar = "";  
string newChar = "";  
//Counter variable to check the result  
int clickCount = 0;  
int ansCount = 0;  
int totalClickCount = 0;#  
endregion

开始新游戏按钮点击

在新游戏中,点击按钮,我们将检查总字符数。将按钮动态添加到网格中。对于动态添加的按钮,我们将创建一个点击事件来检查每个按钮的结果,该结果是与之前和最近一次按钮点击进行比较的。

private void button_Click(object sender, RoutedEventArgs e) {  
    // to shuffle the character and reorder the characters for new Puzzle Game start  
    string namePuzzle = "AZCHIJSARBQCNSKZDIFBOHCRQFEGLM";  
    Random rnd = new Random();  
    string findmyNameChar = new string(namePuzzle.ToCharArray().OrderBy
                            (s => (rnd.Next(2) % 2) == 0).ToArray());  
    //reset the game and to start new;  
    oldChar = "";  
    newChar = "";  
    clickCount = 0;  
    ansCount = 0;  
    totalClickCount = 0;  
    //to create dynamic buttons  
    int xVal = 4;  
    int yVal = 10;  
    int boxWidth = (Convert.ToInt32(pnlButtons.Width) / 4) - 20;  
    int boxHeight = 60;  
    pnlButtons.Children.Clear();  
    puzzleButtons = new Button[findmyNameChar.Length];  
    int column = 0;  
    int rows = 0;  
    //Create buttons and add to grid at runtime  
    for (int i = 0; i < findmyNameChar.Length; i++) {  
        puzzleButtons[i] = new Button();  
        puzzleButtons[i].Name = findmyNameChar[i].ToString() + i.ToString();  
        puzzleButtons[i].FontSize = 16;  
        puzzleButtons[i].Background = new SolidColorBrush(Windows.UI.Colors.OrangeRed);  
        puzzleButtons[i].Foreground = new SolidColorBrush(Windows.UI.Colors.Black);  
        puzzleButtons[i].Content = ""; // findmyNameChar[i].ToString();  
        puzzleButtons[i].HorizontalAlignment = HorizontalAlignment.Left;  
        puzzleButtons[i].VerticalAlignment = VerticalAlignment.Top;  
        puzzleButtons[i].Margin = new Thickness(xVal, yVal, 0, 0);  
        puzzleButtons[i].Click += new RoutedEventHandler(puzzleButton_Click);  
        // puzzleButtons[i].Width = 20;  
        puzzleButtons[i].Height = boxHeight;  
        pnlButtons.Children.Add(puzzleButtons[i]);  
        xVal = xVal + boxWidth + 10;  
        column = column + 1;  
        if (xVal + 100 >= pnlButtons.Width) {  
            rows = rows + 1;  
            column = 0;  
            xVal = 4;  
            yVal = yVal + boxHeight + 24;  
        }  
    }  
}   

在动态创建的按钮点击中,我们将检查点击的字符,并将其与第一次点击的字符、当前点击的字符进行比较并显示消息。在代码部分,我们可以看到每行的注释,它们详细解释了程序逻辑。

注意:要使用消息框,我们必须在按钮点击事件中使用 async

private async void puzzleButton_Click(object sender, RoutedEventArgs e) {  
        // we store the currently clicked button  
        Button puzzleButton = (Button) sender;  
        //If the currently clicked button already clicked we give the message to click 
        //other button to play game.  
        if (puzzleButton.Content != null) {  
            MessageDialog msgbox2 = new MessageDialog("You have already clicked this 
                                    !Kindly click another puzzle button :)", "Warning");  
            await msgbox2.ShowAsync();  
            return;  
        }  
        //first we increment the value to check the first and second clicked character  
        clickCount = clickCount + 1;  
        //we get the clicked button character and display in button   
        string clickedChar = puzzleButton.Name[0].ToString();  
        puzzleButton.Content = clickedChar;  
        //If user clicked for first time we store the button clicked character information   
        if (clickCount == 1) {  
            oldChar = clickedChar;  
            oldButton = (Button) sender;  
            newChar = "";  
        } else if (clickCount == 2) //If the user click for the second time we store the 
                                    //latest button clicked information  
        {  
            clickCount = 0;  
            newChar = clickedChar;  
            newButton = (Button) sender;  
            //we compare both previous and newly clicked button character 
            //and if both match we vie the message as correct.  
            if (oldChar == newChar) {  
                totalClickCount = totalClickCount + 1;  
                ansCount = ansCount + 1;  
                // if user correctly clicked the answer continuously 3 times,
                // we give him congrats msg as he's a genius in this game.  
                if (ansCount == 3) {  
                    ansCount = 0;  
                    MessageDialog msgbox2 = new MessageDialog("Wow you did 3 times and 
                                                          you are the genius now :)", "Congrats");  
                    await msgbox2.ShowAsync();  
                } else {  
                    MessageDialog msgbox2 = new MessageDialog("Its perfect :)", "Congrats");  
                    await msgbox2.ShowAsync();  
                }  
            } else {  
                //If we not both the previous and present button has same character 
                //we give sorry try again message to the user and clear the data to play again.  
                newButton.Content = "";  
                oldButton.Content = "";  
                if (ansCount > 0) {  
                    ansCount = ansCount - 1;  
                    // ansCount = 0;  
                }  
                MessageDialog msgbox2 = new MessageDialog("Sorry !Try Again :(", "Try Again");  
                await msgbox2.ShowAsync();  
            }  
        }  
    }  
    // This is just a sample script. Paste your real code (JavaScript or HTML) here.  
if ('this_is' == /an_example/) {  
    of_beautifier();  
} else {  
    var a = b ? (c % d) : e[f];  
} 

在 Windows 模拟器中运行

选择模拟器并点击运行。这里,我使用了模拟器 10.0.10 UVGA 4 英寸。

当我们运行应用时,我们可以看到我们的程序将在模拟器中运行。

开始新游戏

点击“开始新游戏”按钮来开始我们的游戏。之后,点击此按钮。我们可以看到按钮将动态地创建在我们的网格上。按钮文本将为空,因为我们需要找到任意两个按钮的匹配字符。

正确答案

如果会员点击了第一个按钮,并且第二个按钮具有相同的字符,我们可以看到“恭喜”消息,如下所示:

天才玩家

如果用户连续赢了 3 次,那么他/她就是这个游戏的超级玩家。

错误答案

如果用户点击了第一个按钮和第二个按钮,并且它们没有相同的字符,那么他/她就输掉了游戏,并且可以再次玩。

警告消息

如果用户点击了已选为答案的同一个按钮,那么他/她就不能点击同一个按钮。应用程序将给出消息,要求点击其他按钮来玩游戏。

关注点

希望您喜欢 Windows 手机字符匹配游戏。下载它并亲自尝试。如果您有任何疑问,请留下评论。

历史

  • 2016/12/01:ShanuUWPPuzzleGame.zip
© . All rights reserved.