Click here to Skip to main content
Licence CPOL
First Posted 3 Dec 2008
Views 38,060
Downloads 991
Bookmarked 47 times

Middle East Map Game with C# in WPF

By Mohammad Dayyan | 3 Dec 2008
A simple game with C# and Expression Blend 2 and Visual Studio 2008 SP1.

1

2
2 votes, 13.3%
3
3 votes, 20.0%
4
10 votes, 66.7%
5
4.84/5 - 15 votes
2 removed
μ 4.66, σa 1.33 [?]

MiddleEastGame.gif

Introduction

This is a simple game in WPF that gives you some information about working with XAML and C#. I also use a little LINQ for reading the XML files.

Background

There are some Flash games like this project here, I actually got the idea from there.

Using the code

First, I created Middle East's map. For that, I used a Pen of Expression Blend 2 EBpen.jpg. I put the original map image in the back of my layout space, and then I drew the lines according to the country borders with the Pen. At last, I had a Path code in XAML like this:

<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, ..." />

After creating the map, I created four Storyboards for my game:

<!-- 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 -->

OK, now I have tocontrol them (Storyboards) with C#:

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 { }
}

After that, I check whether the user answers the question correctly or not:

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);
    }
}

History

  • 03 December, 2008: First post.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Mohammad Dayyan



Iran (Islamic Republic Of) Iran (Islamic Republic Of)

Member


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralGulf not Golf :) PinmemberAmir_Saniyan10:37 5 Nov '10  
GeneralI have problem PinmemberAhmedHassanyFarrag5:05 9 May '09  
GeneralRe: I have problem PinmemberMohammad Dayyan6:36 9 May '09  
GeneralVB version Pinmembermbaocha17:19 4 May '09  
GeneralFYI: this requires .NET 3.5 SP1 PinmemberJoe Woodbury7:03 4 Feb '09  
GeneralRe: FYI: this requires .NET 3.5 SP1 PinmemberMohammad Dayyan10:06 8 Feb '09  
GeneralError [modified] Pinmembermohsen.karami1:30 24 Jan '09  
GeneralRe: Error PinmemberMohammad Dayyan5:35 24 Jan '09  
GeneralRe: Error Pinmembermohsen.karami2:39 2 Feb '09  
GeneralRe: Error PinmemberMohammad Dayyan10:11 8 Feb '09  
GeneralNicely done PinmemberKenBeckett16:52 10 Dec '08  
GeneralRe: Nicely done PinmemberMohammad Dayyan20:31 10 Dec '08  
GeneralRe: Nicely done PinmemberTobiasP2:28 16 Dec '08  
GeneralRe: Nicely done PinmemberMohammad Dayyan2:45 16 Dec '08  
GeneralAwesome PinmemberMahdi Khodadadi Fard9:31 3 Dec '08  
I like your map Mohammad, and i really love the "Persian Gulf" statement on the map. I like to see you have extended the map to the whole world and added some more details like Roads data. this can be a good start on "How to make Google Earth", huh ?
GeneralRe: Awesome PinmemberMohammad Dayyan9:41 3 Dec '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120210.1 | Last Updated 3 Dec 2008
Article Copyright 2008 by Mohammad Dayyan
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid