Click here to Skip to main content
11,922,788 members (55,363 online)
Click here to Skip to main content
Add your own
alternative version


53 bookmarked

Middle East Map Game with C# in WPF

, 3 Dec 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
A simple game with C# and Expression Blend 2 and Visual Studio 2008 SP1.



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.


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)
        Path senderPath = (Path)sender;
    catch (Exception ex)
        MessageBox.Show(ex.Message, "Exception", MessageBoxButton.OK);

void country_MouseLeave(object sender, MouseEventArgs e)
    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)
        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; = Capital;
        this.languages.Text = OfficialLanguage;
    catch { }

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

void changeColorValue(string countryName)
        //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()
        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)];
    catch (Exception ex)
        MessageBox.Show(ex.Message, "Exception", MessageBoxButton.OK);

void StoryboardInfo1_Completed(object sender, EventArgs e)
    catch (Exception ex)
        MessageBox.Show(ex.Message, "Exception", MessageBoxButton.OK);


  • 03 December, 2008: First post.


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)
No Biography provided

You may also be interested in...

Comments and Discussions

Generalmer30 Pin
farsizadeh31-Jul-14 19:46
memberfarsizadeh31-Jul-14 19:46 
GeneralRe: mer30 Pin
Mohammad Dayyan1-Aug-14 18:41
memberMohammad Dayyan1-Aug-14 18:41 
QuestionRegarding MouseEnter/MouseLeave etc. Pin
Member 95414275-Nov-12 11:01
memberMember 95414275-Nov-12 11:01 
QuestionGood Job helpful for Kids Pin
chowdary200517-Oct-12 20:56
memberchowdary200517-Oct-12 20:56 
AnswerRe: Good Job helpful for Kids Pin
Mohammad Dayyan17-Oct-12 21:19
memberMohammad Dayyan17-Oct-12 21:19 
GeneralRe: Good Job helpful for Kids Pin
chowdary200517-Oct-12 21:36
memberchowdary200517-Oct-12 21:36 
GeneralRe: Good Job helpful for Kids Pin
Mohammad Dayyan17-Oct-12 23:19
memberMohammad Dayyan17-Oct-12 23:19 
GeneralMy vote of 5 Pin
Saed Leghaee15-Apr-12 23:50
memberSaed Leghaee15-Apr-12 23:50 
GeneralGulf not Golf :) Pin
Amir_Saniyan5-Nov-10 10:37
memberAmir_Saniyan5-Nov-10 10:37 
GeneralI have problem Pin
AhmedHassanyFarrag9-May-09 5:05
memberAhmedHassanyFarrag9-May-09 5:05 
GeneralRe: I have problem Pin
Mohammad Dayyan9-May-09 6:36
memberMohammad Dayyan9-May-09 6:36 
GeneralVB version Pin
mbaocha4-May-09 17:19
membermbaocha4-May-09 17:19 
GeneralFYI: this requires .NET 3.5 SP1 Pin
Joe Woodbury4-Feb-09 7:03
memberJoe Woodbury4-Feb-09 7:03 
GeneralRe: FYI: this requires .NET 3.5 SP1 Pin
Mohammad Dayyan8-Feb-09 10:06
memberMohammad Dayyan8-Feb-09 10:06 
GeneralError [modified] Pin
mohsen.karami24-Jan-09 1:30
membermohsen.karami24-Jan-09 1:30 
GeneralRe: Error Pin
Mohammad Dayyan24-Jan-09 5:35
memberMohammad Dayyan24-Jan-09 5:35 
GeneralRe: Error Pin
mohsen.karami2-Feb-09 2:39
membermohsen.karami2-Feb-09 2:39 
GeneralRe: Error Pin
Mohammad Dayyan8-Feb-09 10:11
memberMohammad Dayyan8-Feb-09 10:11 
GeneralNicely done Pin
KenBeckett10-Dec-08 16:52
memberKenBeckett10-Dec-08 16:52 
GeneralRe: Nicely done Pin
Mohammad Dayyan10-Dec-08 20:31
memberMohammad Dayyan10-Dec-08 20:31 
GeneralRe: Nicely done Pin
TobiasP16-Dec-08 2:28
memberTobiasP16-Dec-08 2:28 
GeneralRe: Nicely done Pin
Mohammad Dayyan16-Dec-08 2:45
memberMohammad Dayyan16-Dec-08 2:45 
GeneralAwesome Pin
Mahdi Khodadadi Fard3-Dec-08 9:31
memberMahdi Khodadadi Fard3-Dec-08 9:31 
GeneralRe: Awesome Pin
Mohammad Dayyan3-Dec-08 9:41
memberMohammad Dayyan3-Dec-08 9:41 

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

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

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