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

使用 C# 和 WPF 创建中东地图游戏

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.84/5 (17投票s)

2008年12月3日

CPOL
viewsIcon

95951

downloadIcon

3628

使用 C# 和 Expression Blend 2 以及 Visual Studio 2008 SP1 制作的一个简单游戏。

MiddleEastGame.gif

引言

这是一个用 WPF 制作的简单游戏,它向您提供有关使用 XAML 和 C# 的一些信息。我还使用了一点 LINQ 来读取 XML 文件。

背景

像这个项目这样的 Flash 游戏可以在这里找到,我实际上是从那里获得的灵感。

使用代码

首先,我创建了中东的地图。为此,我使用了 Expression Blend 2 的一个Pen EBpen.jpg。我将原始地图图像放在我的布局空间的背景中,然后使用Pen根据国家/地区的边界绘制线条。最后,我在 XAML 中得到了一个Path代码,如下所示

<Path Fill="#FF3DAB0F" Stretch="Fill" Stroke="#FF000000" 
    HorizontalAlignment="Right" Margin="0,39.687,0,192.468" 
    x:Name="Iran" Width="243.538" 
    Data="M151.72299,176.838 L154.42299,177.93799 151.82297,180.43799 
        149.823,181.53798 146.62303, ..." />

创建地图后,我为我的游戏创建了四个 Storyboard

<!-- When Mouse enters the bound of a country -->
<Storyboard x:Key="StoryboardMouseEnter">...</storyboard>
<!-- When Mouse leaves the bound of a country -->
<Storyboard x:Key="StoryboardMouseLeave">...</storyboard>
<!-- When the program should shows a new question -->
<Storyboard x:Key="StoryboardInfo1">...</storyboard><!-- fade out -->
<Storyboard x:Key="StoryboardInfo2">...</storyboard><!-- fade in -->

好的,现在我需要用 C# 来控制它们(Storyboard)

void country_MouseEnter(object sender, MouseEventArgs e)
{
    try
    {
        Path senderPath = (Path)sender;
        changeColorValue(senderPath.Name);
        StoryboardMouseEnter.Begin();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Exception", MessageBoxButton.OK);
    }
}

void country_MouseLeave(object sender, MouseEventArgs e)
{
    try
    {
        StoryboardMouseLeave.Begin();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Exception", MessageBoxButton.OK);
    }
}

//I've created a XML file and wrote country information in it (XMLcountriesInfo.XML)
//In this method I've used LINQ for reading information from XML file
void updateCountryInfo(string countryName)
{
    try
    {
        var countryNode = from c in xmlFile.Descendants(countryName)
        select c;
        var Area = (from q in countryNode.Descendants("Area")
                    select q).First().Value;
        var Population = (from q in countryNode.Descendants("Population")
                        select q).First().Value;
        var Capital = (from q in countryNode.Descendants("Capital")
                    select q).First().Value;
        var OfficialLanguage = (from q in countryNode.Descendants("OfficialLanguage")
                        select q).First().Value;
        var flagName = (from q in countryNode.Descendants("flagName")
                    select q).First().Value;
        //////////////////////////////////////////////
        this.NameOfCountry.Text = countryName;
        this.Flag.Source = new 
                BitmapImage(new Uri("Flags\\" + flagName, UriKind.Relative));
        this.area.Text = Area + " KM2";
        this.population.Text = Population;
        this.capital.Text = Capital;
        this.languages.Text = OfficialLanguage;
    }
    catch { }
}

之后,我检查用户是否正确回答了问题

void changeColorValue(string countryName)
{
    try
    {
        /////////////////////////////////
        //changing color of target path
        Color newColor = (Color)ColorConverter.ConvertFromString(this.enterColorString);
        ColorAnimationUsingKeyFrames colorAnimationUsingKeyFrames 
            = (from c in StoryboardMouseEnter.Children
              where (Storyboard.GetTargetName(c) == countryName) 
              select c).First() as ColorAnimationUsingKeyFrames;
        colorAnimationUsingKeyFrames.KeyFrames[0].Value = newColor;
        ////////////////////////////////////////////// 
        //changing color of others path except target
        Color grayColor = (Color)ColorConverter.ConvertFromString(this.leaveColorString);
        var othersColorAnimationUsingKeyFrames = from c in StoryboardMouseEnter.Children
        where (Storyboard.GetTargetName(c) != countryName)
        select c;
        foreach (ColorAnimationUsingKeyFrames tmp in othersColorAnimationUsingKeyFrames)
        tmp.KeyFrames[0].Value = grayColor;
    }
    catch (Exception ex)
    {
            MessageBox.Show(ex.Message, "Exception", MessageBoxButton.OK);
    }
}


void changeQuestion()
{
    try
    {
        string[] countries = new string[] { "Iran", "Turkey", 
            "Iraq", "Kuwait", "Bahrain", "Oman", 
            "Qatar", "SaudiArabia", "UAE", 
            "Yemen", "Israel", "Jordan", 
            "Lebanon", "Syria", "Egypt", 
            "Palestine" };
        Random rand = new Random(DateTime.Now.Second.GetHashCode());
        this.answer = countries[rand.Next(0, countries.Count() - 1)];
        StoryboardInfo1.Begin();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Exception", MessageBoxButton.OK);
    }
}

void StoryboardInfo1_Completed(object sender, EventArgs e)
{
    try
    {
        updateCountryInfo(this.answer);
        StoryboardInfo2.Begin();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Exception", MessageBoxButton.OK);
    }
}

历史

  • 2008年12月3日:首次发布。
© . All rights reserved.