24天學完C#第一篇
本章節(jié)中將要構建一個動物配對游戲,向玩家展示一個包含十六種動物的網(wǎng)格,玩家需要點擊成對的動物使它們消失。
1. 如何構建游戲
首先要在VS中創(chuàng)建一個新的桌面應用項目、
使用XMAL構建窗口
編寫C#代碼想這個窗口增加后端邏輯
增加計時器

1.2 使用XMAL構建窗口
設計需求:窗口需要5行4列的網(wǎng)格建立布局;每個動物顯示再一個TextBlock控件中;計時器顯示在一個TexBlock中,放在最下面一行,橫跨4列。
(選擇主頁面)

(修改窗口大?。?/p>

(修改窗口標題)

(為 XMAL網(wǎng)格增加行和列)

tips:此處寬度和高度中的1*代表比例。
(為網(wǎng)格增加TextBlock控件)
關于TextBlock的屬性:
Text告訴TextBlock要在窗口中顯示什么文本
HorizontalAlignment控制文本左、右對齊或水平居中
Margin設置它與其窗口四周的偏移量(頁邊距)
TextWarpping指出是否增加換行符將文本換行

<Window x:Class="MatchGame.MainWindow"
? ? ? ?xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
? ? ? ?xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
? ? ? ?xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
? ? ? ?xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
? ? ? ?xmlns:local="clr-namespace:MatchGame"
? ? ? ?mc:Ignorable="d"
? ? ? ?Title="Find all of the matching animals" Height="450" Width="400">
? ?<Grid Margin="1,1,-1,-1">
? ? ? ?<!-- 劃分網(wǎng)格 -->
? ? ? ?<Grid.RowDefinitions>
? ? ? ? ? ?<RowDefinition Height="1*"/>
? ? ? ? ? ?<RowDefinition Height="1*"/>
? ? ? ? ? ?<RowDefinition Height="1*"/>
? ? ? ? ? ?<RowDefinition Height="1*"/>
? ? ? ? ? ?<RowDefinition Height="1*"/>
? ? ? ?</Grid.RowDefinitions>
? ? ? ?<Grid.ColumnDefinitions>
? ? ? ? ? ?<ColumnDefinition Width="1*"/>
? ? ? ? ? ?<ColumnDefinition Width="1*"/>
? ? ? ? ? ?<ColumnDefinition Width="1*"/>
? ? ? ? ? ?<ColumnDefinition Width="1*"/>
? ? ? ?</Grid.ColumnDefinitions>
? ? ? ?<!--第一行-->
? ? ? ?<TextBlock Grid.Column="0" Grid.Row="0" HorizontalAlignment="Center" FontSize="36" TextWrapping="Wrap" Text="?" VerticalAlignment="Center"/>
? ? ? ?<TextBlock Grid.Column="1" Grid.Row="0" HorizontalAlignment="Center" FontSize="36" TextWrapping="Wrap" Text="?" VerticalAlignment="Center"/>
? ? ? ?<TextBlock Grid.Column="2" Grid.Row="0" HorizontalAlignment="Center" FontSize="36" TextWrapping="Wrap" Text="?" VerticalAlignment="Center"/>
? ? ? ?<TextBlock Grid.Column="3" Grid.Row="0" HorizontalAlignment="Center" FontSize="36" TextWrapping="Wrap" Text="?" VerticalAlignment="Center"/>
? ? ? ?<!--第二行-->
? ? ? ?<TextBlock Grid.Column="0" Grid.Row="1" HorizontalAlignment="Center" FontSize="36" TextWrapping="Wrap" Text="?" VerticalAlignment="Center"/>
? ? ? ?<TextBlock Grid.Column="1" Grid.Row="1" HorizontalAlignment="Center" FontSize="36" TextWrapping="Wrap" Text="?" VerticalAlignment="Center"/>
? ? ? ?<TextBlock Grid.Column="2" Grid.Row="1" HorizontalAlignment="Center" FontSize="36" TextWrapping="Wrap" Text="?" VerticalAlignment="Center"/>
? ? ? ?<TextBlock Grid.Column="3" Grid.Row="1" HorizontalAlignment="Center" FontSize="36" TextWrapping="Wrap" Text="?" VerticalAlignment="Center"/>
? ? ? ?<!--第三行-->
? ? ? ?<TextBlock Grid.Column="0" Grid.Row="2" HorizontalAlignment="Center" FontSize="36" TextWrapping="Wrap" Text="?" VerticalAlignment="Center"/>
? ? ? ?<TextBlock Grid.Column="1" Grid.Row="2" HorizontalAlignment="Center" FontSize="36" TextWrapping="Wrap" Text="?" VerticalAlignment="Center"/>
? ? ? ?<TextBlock Grid.Column="2" Grid.Row="2" HorizontalAlignment="Center" FontSize="36" TextWrapping="Wrap" Text="?" VerticalAlignment="Center"/>
? ? ? ?<TextBlock Grid.Column="3" Grid.Row="2" HorizontalAlignment="Center" FontSize="36" TextWrapping="Wrap" Text="?" VerticalAlignment="Center"/>
? ? ? ?<!--第四行-->
? ? ? ?<TextBlock Grid.Column="0" Grid.Row="3" HorizontalAlignment="Center" FontSize="36" TextWrapping="Wrap" Text="?" VerticalAlignment="Center"/>
? ? ? ?<TextBlock Grid.Column="1" Grid.Row="3" HorizontalAlignment="Center" FontSize="36" TextWrapping="Wrap" Text="?" VerticalAlignment="Center"/>
? ? ? ?<TextBlock Grid.Column="2" Grid.Row="3" HorizontalAlignment="Center" FontSize="36" TextWrapping="Wrap" Text="?" VerticalAlignment="Center"/>
? ? ? ?<TextBlock Grid.Column="3" Grid.Row="3" HorizontalAlignment="Center" FontSize="36" TextWrapping="Wrap" Text="?" VerticalAlignment="Center"/>
? ?</Grid>
</Window>
1.3 使用C#編寫后端
(打開主頁面對應的cs文件)

(編寫SetUpGame方法)
重命名網(wǎng)格的名字

public void SetUpGame() {
? ? ? ? ? ?// 創(chuàng)建一個有關動物表情的容器并初始化
? ? ? ? ? ?List<string> animalEmoji = new List<string>() {
? ? ? ? ? ? ? ?"??","??",
? ? ? ? ? ? ? ?"??","??",
? ? ? ? ? ? ? ?"?? ","??",
? ? ? ? ? ? ? ?"?? ","?? ",
? ? ? ? ? ? ? ?"?? ","?? ",
? ? ? ? ? ? ? ?"?? ","?? ",
? ? ? ? ? ? ? ?"?? ","?? ",
? ? ? ? ? ? ? ?"?? ","?? ",
? ? ? ? ? ?};
? ? ? ? ? ?//初始化一個Random對象
? ? ? ? ? ?Random random = new Random();
? ? ? ? ? ?//遍歷在名為mainGird組件中所有類型為TextBlock的子集
? ? ? ? ? ?foreach (TextBlock textBlock in mainGrid.Children.OfType<TextBlock>())
? ? ? ? ? ?{
? ? ? ? ? ? ? ?//隨機生成一個0-animalEmoji長度之間的索引
? ? ? ? ? ? ? ?int index = random.Next(animalEmoji.Count);
? ? ? ? ? ? ? ?//獲得這個隨機的Emoji并寫入textBlock中
? ? ? ? ? ? ? ?string nextEmoji = animalEmoji[index];
? ? ? ? ? ? ? ?textBlock.Text = nextEmoji;
? ? ? ? ? ? ? ?//為了防止重復產生3個以上的相同Emoji,需要在賦值后對容器內對應的Emoji進行刪除
? ? ? ? ? ? ? ?animalEmoji.RemoveAt(index);
? ? ? ? ? ?}
? ? ? ?}
將項目加入源代碼控制Git

(處理鼠標點擊)
當玩家點擊某個動物時都應該調用一個名為TextBlock_MouseDown的方法。



也可以直接在XAML中添加屬性

TextBlock lastTextBlockClicked; //上一個點擊的文字塊
? ? ? ?bool findingMatch = false; ?//用來判斷是否已經(jīng)連續(xù)點擊兩個文字塊(算上當前點擊的)
? ? ? ?private void TextBlock_MouseDown(object sender, MouseButtonEventArgs e)
? ? ? ?{
? ? ? ? ? ?TextBlock textBlock = sender as TextBlock;
? ? ? ? ? ?if (findingMatch == false) // 還沒有連續(xù)選中兩個文字塊
? ? ? ? ? ?{
? ? ? ? ? ? ? ?textBlock.Visibility = Visibility.Hidden; //將選中的文字塊先隱藏
? ? ? ? ? ? ? ?lastTextBlockClicked = textBlock;
? ? ? ? ? ? ? ?findingMatch = true;
? ? ? ? ? ?}
? ? ? ? ? ?else if (textBlock.Text != lastTextBlockClicked.Text) //已經(jīng)連續(xù)選中兩個文字塊但是不匹配
? ? ? ? ? ?{
? ? ? ? ? ? ? ?lastTextBlockClicked.Visibility = Visibility.Visible; //恢復上一個選中文字塊為顯示
? ? ? ? ? ? ? ?findingMatch = false;
? ? ? ? ? ?}
? ? ? ? ? ?else { //選中了兩個文字塊且文字匹配
? ? ? ? ? ? ? ?textBlock.Visibility = Visibility.Hidden;
? ? ? ? ? ? ? ?findingMatch = false;
? ? ? ? ? ?}
? ? ? ?}
? ?}
1.4 增加計時器
(創(chuàng)建內部參數(shù)和委托)

(在XAML中添加負責計時的文字塊)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
namespace MatchGame
{
? ?/// <summary>
? ?/// Interaction logic for MainWindow.xaml
? ?/// </summary>
? ?public partial class MainWindow : Window
? ?{
? ? ? ?DispatcherTimer timer = new DispatcherTimer();
? ? ? ?int tenthsOfSeconsElapsed = 0;
? ? ? ?int mathcesFound;
? ? ? ?public MainWindow()
? ? ? ?{ ?
? ? ? ? ? ?InitializeComponent();
? ? ? ? ? ?SetUpGame();
? ? ? ? ? ?timer.Interval = TimeSpan.FromSeconds(.1); //設置計時器的回調頻率
? ? ? ? ? ?timer.Tick += Timer_Tick; ? //為計時器綁定回調函數(shù)(委托)
? ? ? ?}
? ? ? ?private void Timer_Tick(object sender,EventArgs e) {
? ? ? ? ? ?tenthsOfSeconsElapsed++;
? ? ? ? ? ?timeTextBlock.Text = (tenthsOfSeconsElapsed / 10F).ToString("0.0s");
? ? ? ? ? ?if (mathcesFound == 8) {
? ? ? ? ? ? ? ?timer.Stop();
? ? ? ? ? ? ? ?timeTextBlock.Text = timeTextBlock + " - Play again ?";
? ? ? ? ? ?}
? ? ? ?}
? ? ? ?public void SetUpGame() {
? ? ? ? ? ?// 創(chuàng)建一個有關動物表情的容器并初始化
? ? ? ? ? ?List<string> animalEmoji = new List<string>() {
? ? ? ? ? ? ? ?"??","??",
? ? ? ? ? ? ? ?"??","??",
? ? ? ? ? ? ? ?"?? ","??",
? ? ? ? ? ? ? ?"?? ","?? ",
? ? ? ? ? ? ? ?"?? ","?? ",
? ? ? ? ? ? ? ?"?? ","?? ",
? ? ? ? ? ? ? ?"?? ","?? ",
? ? ? ? ? ? ? ?"?? ","?? ",
? ? ? ? ? ?};
? ? ? ? ? ?//初始化一個Random對象
? ? ? ? ? ?Random random = new Random();
? ? ? ? ? ?//遍歷在名為mainGird組件中所有類型為TextBlock的子集
? ? ? ? ? ?foreach (TextBlock textBlock in mainGrid.Children.OfType<TextBlock>())
? ? ? ? ? ?{
? ? ? ? ? ? ? ?if (textBlock.Name == "timeTextBlock") continue;
? ? ? ? ? ? ? ?//隨機生成一個0-animalEmoji長度之間的索引
? ? ? ? ? ? ? ?int index = random.Next(animalEmoji.Count);
? ? ? ? ? ? ? ?//獲得這個隨機的Emoji并寫入textBlock中
? ? ? ? ? ? ? ?string nextEmoji = animalEmoji[index];
? ? ? ? ? ? ? ?textBlock.Text = nextEmoji;
? ? ? ? ? ? ? ?//為了防止重復產生3個以上的相同Emoji,需要在賦值后對容器內對應的Emoji進行刪除
? ? ? ? ? ? ? ?animalEmoji.RemoveAt(index);
? ? ? ? ? ?}
? ? ? ? ? ?timer.Start();
? ? ? ? ? ?tenthsOfSeconsElapsed = 0;
? ? ? ? ? ?mathcesFound= 0;
? ? ? ?}
? ? ? ?TextBlock lastTextBlockClicked; //上一個點擊的文字塊
? ? ? ?bool findingMatch = false; ?//用來判斷是否已經(jīng)連續(xù)點擊兩個文字塊(算上當前點擊的)
? ? ? ?private void TextBlock_MouseDown(object sender, MouseButtonEventArgs e)
? ? ? ?{
? ? ? ? ? ?TextBlock textBlock = sender as TextBlock;
? ? ? ? ? ?if (findingMatch == false) // 還沒有連續(xù)選中兩個文字塊
? ? ? ? ? ?{
? ? ? ? ? ? ? ?textBlock.Visibility = Visibility.Hidden; //將選中的文字塊先隱藏
? ? ? ? ? ? ? ?lastTextBlockClicked = textBlock;
? ? ? ? ? ? ? ?findingMatch = true;
? ? ? ? ? ?}
? ? ? ? ? ?else if (textBlock.Text != lastTextBlockClicked.Text) //已經(jīng)連續(xù)選中兩個文字塊但是不匹配
? ? ? ? ? ?{
? ? ? ? ? ? ? ?lastTextBlockClicked.Visibility = Visibility.Visible; //恢復上一個選中文字塊為顯示
? ? ? ? ? ? ? ?findingMatch = false;
? ? ? ? ? ?}
? ? ? ? ? ?else { //選中了兩個文字塊且文字匹配
? ? ? ? ? ? ? ?mathcesFound++;
? ? ? ? ? ? ? ?textBlock.Visibility = Visibility.Hidden;
? ? ? ? ? ? ? ?findingMatch = false;
? ? ? ? ? ?}
? ? ? ?}
? ? ? ?private void TimeTextBlock_MouseDown(object sender, MouseButtonEventArgs e)
? ? ? ?{
? ? ? ? ? ?if (tenthsOfSeconsElapsed == 8) {
? ? ? ? ? ? ? ?SetUpGame();
? ? ? ? ? ?}
? ? ? ?}
? ?}
}