You are browsing the archive for games.

Hidden Object: Episode 9 – Add the Splash, Menu & Options Screens

September 29, 2009 in Hidden Object Game, Silverlight

This is episode 9 of Creating a Hidden Object Game is Silverlight 3. In the last episode we added background music. Now we will create a screen to set the music volume. While we are at it, we will create the splash and menu screens.

When the game is finished, it will have a screen flow as follows:

HiddenObject-ScreenFlow

 

 Currently we have been working on the Game screen which in the project is a UserControl called MainPage.xaml. We are going to create the menu, options, and splash screens as Canvas objects that are off screen of MainPage and are at the bottom of the Objects tree (or top of the z-order) so they show up over all other controls. I chose to do this since I want the background music to play during all of these screens and the MediaElement on MainPage is the one that is playing it. There are various other ways to approach this.

Let’s open Blend, add 3 Canvas controls, and name them splashCanvas, menuCanvas, and optionsCanvas. Make each Canvas have a black background and position them on the artboard so that they are to the left of MainPage:

 

On splashCanvas, add 4 TextBlock controls and enter the title information:

 

On menuCanvas, add the page title in a TextBlock and 2 Buttons:

 

On optionsCanvas, add a TextBlock for the page title, a TextBlock for the slider title, a Slider control, and a Button:

Since the silder’s Value property will be used in a Binding for the MediaElement’s Volume property, we will name the slider as sliderMusicVolume.

 

To control the screen flow, we will use the States panel to create a ScreenStates group with the following states: GameScreen, SplashScreen, MenuScreen, OptionsScreen.

On the States tab, click the Add state group button:

Then next to the ScreenStates group, click the Add state button:

As I entered the following states, I didn’t worry about setting any properties for any of the controls and after adding OptionsScreen I just turned off recording.

The GameScreen state represents when the MainPage is visible which is how it currently is shown on the artboard. No change will need to be made for this state. Click on the SplashScreen state in the States tab to turn on recording mode. Then click splashCanvas and change its Top and Left properties both to 0. Turn off recording of the SplashScreen state.

Repeat for the MenuScreen and OptionsScreen states so that when one of these states is active, MainPage will be covered by menuCanvas or optionsCanvas.

Click on the Base state at the top of the States tab to make sure you are completely out of state editing mode.

We now need to add a TextBlock with the Text property set to “MENU” and add it to LayoutRoot in MainPage. Because I added this control after the Canvas controls for the other screens, I need to change its order in the Objects tree so that it doesn’t appear above those screens. We will worry about making the MENU TextBlock into a Button at a later time.

 

Now it is time to use the GoToStateAction to connect the screen flow. Drop the first GoToStateAction onto LayoutRoot and change the EventName to Loaded and the StateName to SplashScreen:

This will cause the splash screen to show first.

NOTE: While we are still in development, we can set the loaded StateName to GameScreen to reduce testing time.

 

Drop another GoToStateAction onto splashCanvas and change the StateName to MenuScreen. When the splash screen is clicked, we will advance to the menu screen:

Drop two more GoToStateAction actions onto the Start and Options buttons on menuCanvas. Change the StateNames to GameScreen and OptionsScreen respectively.

Start button:

 

Options button:

 

Drop another GoToStateAction onto the Close button in optionsCanvas and change the StateName to MenuScreen:

 

Finally, drop the last GoToStateAction onto the MENU TextBlock in MainPage and set the StateName to MenuScreen:

 

We have just put together the majority of our screen flow with 6 instances of GoToStateAction and no code!

 

The last thing we need to do is to connect the slider value to the volume of the background music. In the Objects tree, select the musicMediaElement and in the Properties pane, click the peg to the right of the Volume property and click Data Binding to open the Create Data Binding window:

 

Select the ElementProperty tab. In the Scene elements list, open optionsCanvas and select sliderMusicVolume. In the Properties list, select Value. Now when the slider in the options screen is changed, it will set the volume on the MediaElement playing the background music.

The last thing we need to do is set some values on the slider control. Select the control and change the following properties found in the Common Properties group: LargeChange, Maximum, and Value.

 

In the next episode, we will try to figure out a way to determine when all the objects have been found so we can show a Win screen.

Demo
Source code

Hidden Object: Episode 8 – Loop Game Music with a Behavior

September 28, 2009 in Hidden Object Game, Silverlight

In the last episode of Creating a Hidden Object Game is Silverlight 3 we finished off the magnifier feature. Now it is time to add some background music to the game and have it continually play.

First off, we need to find some appropriate music. I did a search for royalty-free & public domain music and came across a site by Derek R. Audette that contained such music. On the site, I found a piece of music called “Combustible Coffee Pot” written & performed by Derek R. Audette – ©MMV(Socan). Derek’s own description of the music is:

“Out of control drum fills, singing string sections and odd, tense, electronic pads make up this bizarre composition. Created to add a mood of intrigue to an independent film produced in 2005.”

Download the mp3 file and add it to the Audio folder.

In order to use the music in our game, we need to add a MediaElement control to LayoutRoot:

<MediaElement x:Name="musicElement"
    Source="/Audio/Combustible_Coffee_Pot.mp3"
    MediaEnded="musicElement_MediaEnded"/>

To get the continuous loop, we need to handle the MediaEnded event which sets the media position back to 0 and plays it again:

private void musicElement_MediaEnded(object sender, System.Windows.RoutedEventArgs e)
{
    musicElement.Position = new TimeSpan(0);
    musicElement.Play();
}

But as developers, we don’t want to make our designers go into code behind and enter C# code. Since Silverlight doesn’t support continuously lookping media, we are going to package this functionality into a custom behavior.

Create a folder under Interactivity called ContinuousMediaPlayBehavior and create new class based on the Behavior template by the same name. All we are going to do is attach an event handler to the MediaEnded event in the OnAttached method and remove the handler in the OnDetaching method. To get the AssociatedObject property to be strongly typed to a MediaElement and thus allow this behavior to only be attached to the MediaElement type we change the behavior so it is typed to MediaElement. When the MediaEnded event is fired, we set the position of the media back to 0 and replay it:

public class ContinuousPlayMediaBehavior : Behavior<MediaElement>
{
    public ContinuousPlayMediaBehavior() {}

    protected override void OnAttached()
    {
        base.OnAttached();
        AssociatedObject.MediaEnded += AssociatedObject_MediaEnded;
    }

    protected override void OnDetaching()
    {
        base.OnDetaching();
        AssociatedObject.MediaEnded -= AssociatedObject_MediaEnded;
    }

    void  AssociatedObject_MediaEnded(object sender, RoutedEventArgs e)
    {
        AssociatedObject.Position = new TimeSpan(0);
        AssociatedObject.Play();
    }
}

This is what the XAML looks like for this:

<MediaElement x:Name="musicMediaElement" Source="/Audio/Combustible_Coffee_Pot.mp3">
    <i:Interaction.Behaviors>
        <local:ContinuousPlayMediaBehavior/>
    </i:Interaction.Behaviors>
</MediaElement>

In the next episode, we will continue to work on the UI by adding various screens including one to control the volume of the background music we just added.

Hidden Object: Episode 7 – Use an Action to Toggle the Magnifier Behavior

September 26, 2009 in Hidden Object Game, Silverlight

This is episode 7 of Creating a Hidden Object Game is Silverlight 3.

In the last few posts we have been working on the magnifier feature of the game which we will finish in this episode.

Please review the following posts:

First we will add in the following files into the ClutteredCube project:

The first thing we want to do is to change MagnifierOverBehavior so that it inherits from our custom behavior base class thus inheriting the IsEnabled property:

public class MagnifierOverBehavior : BaseBehavior<FrameworkElement>

The only other change we need to make this behavior is not set the Effect property on MouseEnter but instead in MouseMove based on the value of the IsEnabled property:

private void AssociatedObject_MouseEnter( object sender, MouseEventArgs e )
{
  this.AssociatedObject.MouseMove += new MouseEventHandler( AssociatedObject_MouseMove );
  //this.AssociatedObject.Effect = this.magnifier;
}

private void AssociatedObject_MouseMove( object sender, MouseEventArgs e )
{
  if (IsEnabled)
  {
    if (this.AssociatedObject.Effect != this.magnifier)
    {
      this.AssociatedObject.Effect = this.magnifier;
    }
    ...
  }
}

In Blend, open the MainPage and select the MagnifierOverBehavior in the Object tree under magnifierCanvas. Name the behavior magnifierBehavior and set IsEnabled to false:

From the Assets tab drag two instances of SetInteractionPropertyAction onto the CheckBox magnifier control:

On the first one, set the properties as follows:

  • EventName: Checked
  • TargetName: magnifierCanvas
  • ObjectName: magnifierBehavior
  • PropertyName: IsEnabled
  • Value: true

Set the second the same as the first, except:

  • EventName: Unchecked
  • Value: false

Make sure that the CheckBox control IsChecked property is false to match the InEnabled value of false for magniferBehavior.

Run the game and verify that magnification is only turned on when the CheckBox is checked.

Let’s do one more thing to finish this episode. Notice that magnifier.png has a white background behind the glass part of the magnifying glass. The image should be on top of something white like a notepad. Import the notepad image and drop it at the bottom of LayoutRoot.

notepad

Position it how you want it and move the magnifier CheckBox on top of it.

Source Code

Demo

There are many features to add so I will keep the next episode contents a surprise.

Hidden Object: Episode 5 – Add a Magnifier Behavior

September 18, 2009 in Hidden Object Game, Silverlight

 This is episode 5 in the series, Creating a Hidden Object Game in Silverlight 3.

In this episode, we will add an existing MagnifierOverBehavior to our project to allow the user to get a closer look at areas of the picture.

On the SilverlightShow site, there is an article titled, Behaviors and Triggers in Silverlight 3, by Pencho Popadiyn. The article does a great job showing the base classes for Actions and Behaviors as well as shows some custom Behaviors. One of them is the MagnifierOverBehavior which magnifiers an oval area as you move the mouse around the image:

 

It does this by setting the Effect property on the AssociatedObject to a custom pixel shader effect.

Go to the Behaviors and Triggers in Silverlight 3 and download the source. Open Visual Studio and under the Interactivity folder create a folder called MagnifierOverBehavior. Into that folder copy the following files from the Behaviors and ShaderEffectsLibrary projects:

  • MagnifierOverBehavior.cs
  • EffectLibrary.cs
  • Magnifier.cs
  • Magnifier.fx
  • Magnifier.ps
  • ShaderEffectBase.cs

 

Include the files into your project.

I have spent little time researching effects (classes derived from System.Windows.Media.Effects.Effect), but here is an introduction. Silverlight 3 ships with two concrete effects (BlurEffect & DropShadowEffect) and one abstract effect (ShaderEffect). Any class that derives from UIElement has a single Effect property that can be set. To apply multiple effects, you need to use a hierarchy of UIElement-derived objects assigning the Effect property on different objects to get a composite effect. The pixel shader effect is software rendered in Silverlight and hardware rendered in WPF.

In our case, the Magnifier.cs file contains the Magnifier class that derives from ShaderEffectBase which derives from ShaderEffect which derives from Effect. A good article is Pixel Effects in Silverlight 3. The next file to look at is Magnifier.fx which contains code in High Level Shading Language (HLSL). The important thing to know is that the fx file must be compiled into a ps file. Since we already have Magnifier.ps then we don’t have to worry about it.

More details on HLSL, can be found at the following links:

The EffectLibrary.cs file contains helper method, MakePackUri, which is used in line 42 of Magnifer.cs. We need to change the string to identify the correct location in our project of the pixel shader file. Change “Source/Magnifier.ps” to “Interactivity/MagnifierOverBehavior/Magnifier.ps” since the ps file resides in the MagnifierOverBehavior project folder under the Interactivty folder.

Compile and then open the solution in Blend 3.

We want the magnifier to show when over the main office picture. But if we attach the behavior to the image, then magnification will turn off when we mouse over one of the path objects. To solve this problem, we will group the office image and all paths into a canvas object which we will name magnifierCanvas. Now drag the MagnifierOverBehavior from the Assets panel to associate it with magnifierCanvas:

 

Run the game and now you will see that when mouse is over the image it shows a magnifier. You can click when in magnification mode and the items will be removed from the list:

 

We don’t want to always be in magnification mode, we need a way to toggle the magnifier on or off. We will tackle that in the next episode as we create an action that is able to set a property on an action or behavior that is associated to an object.

Hidden Object: Episode 4 – Adding Particles with a Behavior

September 17, 2009 in Hidden Object Game, Silverlight

In the previous episodes of Creating a Hidden Object Game in Silverlight 3 we spent all of our time in Blend. Now we are going to put on our developer hat and create our own custom behavior that will shoot particles when an item is clicked. This will give a nice visual effect that compliments marking the item off the list and the sound effect.

Click here to see a demo of what the game will look like after we finish this episode.

I want to make sure that I give tons of credit to Robby Ingebretsen from nerdplusart for his Silverlight Particle Generator. If you haven’t had a chance to play with it before (and I do mean play) then take a few minutes to do so.

The plan is to take the ParticleControl from that solution and wrap it in a ParticlesBehavior that will be triggered when the item’s path (ex: staplerPath) is clicked.

First we need to take a side-step into the differences between an action and a behavior and their relationship to triggers. We will start to see some patterns emerge. In the previous episodes we looked at two actions (ChangeProperyAction & PlaySoundAction) and noticed that an action specifies what will happen and to which object. When the action will occur is specified by a trigger. There are two main types of actions (TriggerAction & TargetedTriggerAction). The first allows us to attach to an object and invoke some code when a trigger is fired whereas the second does that and defines a target for the action that is different from the object that the action is associated with. Both of these actions can only be called by a single trigger. A behavior in some ways appears simpler than an action because there is no direct connection to a trigger and therefore no Invoke method to be called. All we get are overrides for OnAttached and OnDetaching. But behaviors are powerful, can hold state, and can do various things based on many triggers.

For a class diagram of the Action and Behavior base classes as well as some great sample behaviors, check out the Behaviors and Triggers in Silverlight 3 article on SilverlightShow.

Hint: We will use the MagnifierOverBehavior in the next episode.

Enough talking lets open Blend and get coding. In the Projects panel, create a folder called Interactivity and under it a folder called ParticlesBehavior. Interactivity seems to be the term Microsoft uses to talk about both actions and behaviors and by the time we are done with this tutorial we will have created both types. I am doing this in Blend, because it has a template to create a basic behavior.

Right-click the ParticlesBehavior folder, pick Add New Item, select Behavior, and enter the name: ParticlesBehavior.

Click OK to create the behavior.

After saving all the files, open the solution concurrently in Visual Studio 2008 so that you can switch back and forth between Visual Studio and Blend as needed.

Go to the Silverlight Particle Generator page and download the source code. Copy the following files into the Interactivity\ParticlesBehavior folder and include them in the project:

  • ParticleControl.xaml
  • ParticleControl.xaml.cs
  • LICENSE.TXT

The quick idea behind the ParticlesBehavior is that it will be attached to the LayoutRoot canvas. The behavior will handle the canvas’s MouseMove event and store the current mouse position. When the ParticlesBehavior is notified that it should show particles, it creates an instance of the ParticleControl and positions it by setting the OffsetX and OffsetY properties.

We start by changing the class declaration so that ParticlesBehavior inherits from Behavior<Canvas>. Now throughout code, AssociatedObject will be of type Canvas. This also means that this behavior can only be attached to objects of type Canvas. In the OnAttached method an event handler is created for the canvas’ MouseMove event whereas in the OnDetaching method the event handler is removed. When the mouse is moved, the current position is recorded.

public class ParticlesBehavior : Behavior<Canvas>
{
private Point currentMousePosition;

protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.MouseMove += new MouseEventHandler(AssociatedObject_MouseMove);
}

protected override void OnDetaching()
{
base.OnDetaching();
AssociatedObject.MouseMove -= new MouseEventHandler(AssociatedObject_MouseMove);
}

void AssociatedObject_MouseMove(object sender, MouseEventArgs e)
{
currentMousePosition = e.GetPosition(null);
}
}

Now we need a way to trigger when the control will be shown or hidden. This is achieved by adding a command to ParticlesBehavior called ShowParticles. In the constructor, instantiate a new ActionCommand and pass it an Action delegate. The method that you use can have no parameters or one parameter (of type object). Later when triggers are associated with this behavior you can set the value of the CommandParameter. If you have a method that passes an object parameter, you can get access to the parameter.

This command then creates and sets properties on the ParticleControl and adds it to the Children collection of LayoutRoot. Update the ParticlesBehavior class to include this code:

public class ParticlesBehavior : Behavior<Canvas>
{
    private Point currentMousePosition;

    public ParticlesBehavior()
    {
        this.ShowParticles = new ActionCommand(this.OnShowParticles);
    }

    protected override void OnAttached()
    {
        base.OnAttached();
        AssociatedObject.MouseMove += new MouseEventHandler(AssociatedObject_MouseMove);
    }

    protected override void OnDetaching()
    {
        base.OnDetaching();
        AssociatedObject.MouseMove -= new MouseEventHandler(AssociatedObject_MouseMove);
    }

    void AssociatedObject_MouseMove(object sender, MouseEventArgs e)
    {
        currentMousePosition = e.GetPosition(null);
    }

    public ICommand ShowParticles
    {
        get;
        private set;
    }

    private void OnShowParticles()
    {
        ParticleControl p = new ParticleControl();

        p.OffsetX = currentMousePosition.X;
        p.OffsetY = currentMousePosition.Y;

        AssociatedObject.Children.Add(p);
    }

}

If you compile the project, you will now see the ParticlesBehavior in the Assets panel. Drag and drop it on LayoutRoot. The properties panel will have a ShowParticles group created for the ShowParticles Command and an empty Triggers list:

Click the plus sign to the right of Triggers and pick EventTrigger and set the SourceName (staplerPath) and EventName (MouseLeftButtonDown):

What this is saying is that when the staplerPath object is clicked then it will call the ShowParticles command on this behavior. You could also specify a CommandParameter which you then could use if OnShowParticles had an object parameter defined. When you click the plus sign next to Triggers again, you can specify another EventTrigger to be called when idolPath is clicked. Repeat this for all the paths.

Run the game now and click on an item. You should see lots of particles flowing from that location. Find another object and click it and there will be a second stream of particles. The ParticleControl currently doesn’t have a way to stop the particles from flowing. We will take care of that in a minute. But let’s first try to figure out what is happening between the triggers and the behavior. The command defined on the behavior is an ICommand, it knows nothing about triggers. And triggers just know when something is going to happen. How does it know to call a command on the behavior? The answer is the InvokeCommandAction that is created behind the scenes. This can be seen in a diagram from the Expression Blend SDK User Guide:

It might be a good learning opportunity to intercept the trigger before it calls InvokeCommandAction and see it doing the work of connecting the trigger to the behavior. Let’s create a trigger that we can use and debug. In a new folder under Interactivity called DebugEventTrigger, create a class called DebugEventTrigger.cs:

public class DebugEventTrigger : System.Windows.Interactivity.EventTrigger
{
    public DebugEventTrigger() : base() {}

    public DebugEventTrigger(string eventName) : base(eventName) {}

    protected override void OnEvent(EventArgs eventArgs)
    {
        base.OnEvent(eventArgs);
    }
}

We derive the class from EventTrigger so we can use it just list the triggers we have already used. Compile the project and in Blend, add this trigger to the triggers collection on the ParticlesBehavior and select the SourceName and EventName. Switch back to Visual Studio and set a breakpoint in the OnEvent method. Run the app in debug mode from Visual Studio. When the breakpoint is hit, look at the Locals window and you will see that the trigger has a single action, InvokeCommandAction, the CommandName is “ShowParticles” and the AssociatedObject is ParticlesBehavior.

Back to the issue of the never-ending particles. To fix this, we need just one set of changes to ParticleControl. The UpdateParticles method is the heart of the control and is called each time the Silverlight rendering process renders a frame. It updates existing particles, creates new particles, and removes expired particles. The particles are created 10 at a time until the particles collection reaches the MaxParticleCount. But because particles are also being removed from this collection the result is a never-ending flow of particles. Change this.particles.Count to totalParticlesCreated and define it as a private field at the top of the class definition as type int. At the end of the SpawnParticle method, increment the totalParticlesCreated value by one. Now the control will create particles until MaxParticleCount is reach and stop.

We have a fully-functioning ParticlesBehavior.

The last thing I did was add the dependency properties from ParticleControl (StartColor, EndColor, Fuzziness, MaxParticleCount, OriginVariance, Speed, ParticleSize, ParticleSizeVariance, Life, and LifeVariance) to ParticlesBehavior and set the values for each created instance of the ParticleControl. This gives a nice design-time experience to configure the particles. I changed a few values to better time the particles with the length of the sound played when an item is clicked:

There is one final thing we need to do when an item is clicked. Right now you can click on an item multiple times and it will play a sound and generate particles each time. To solve this, we can use the RemoveElementAction. This action removes the targeted element from its parent control. Drag the RemoveElementAction onto the staplerPath object. Since the trigger defaults to EventTrigger and MouseLeftButtonDown and the TargetName defaults to the AssociatedObject then we don’t have to change anything:

When we click on the staplerPath then it will:

  • Change the opacity of its corresponding TextBlock (ChangePropertyAction)
  • Play a sound (PlaySoundAction)
  • Spray particles (ParticlesBehavior)
  • Be removed from its parent control (RemoveElementAction)

Download the source for this episode.

In our next episode, we will use a magnifer pixel shader effect wrapped in a behavior to allow the user to zoom in on portions of the image.

Hidden Object: Episode 3 – Marking Items off the List

September 17, 2009 in Hidden Object Game, Silverlight

In this episode of Creating a Hidden Object Game in Silverlight 3 we overlay the items in the image with paths and associate the ChangePropertyAction to change the Opacity of the TextBlock to indicate that the item has been found.

In order to detect when an item is clicked in the office image, we need a hotspot. A Path works great for this and odd-shaped items can be outlined using the Pen tool. Press the “P” key and the Pen tool will be selected:

Center the mouse cursor over the stapler and use the mouse wheel to zoom in. Now click various points to outline the shape of the stapler:

I started with a point in the bottom left corner and clicked a total of 7 points to outline the shape. When I got to point 5 in the top left, the path’s fill started obstructing my view of the rest of the object. Simply move over to the Appearance group and set the Opacity below 50%. Continue to draw the remaining points. When you get back to the first point, the mouse cursor will change to the pen with a loop indicating that clicking will close the path. Give the path a name (staplerPath) and set the Opacity to 0%. It is time to add the ChangePropertyAction to the path. On the Assets tab, click Behaviors and find the ChangePropertyAction.

 

 

Drag the action from the Assets tab to the Object tree and drop it on staplerPath:

 

 

This action is now associated with staplerPath object. Adding this action to our object tree caused two assemblies to be added to the References folder:

  • Microsoft.Expression.Interactions.dll
  • System.Windows.Interactivity.dll

Looking at the Properties panel you will see the properties for this action and notice that there is already an EventTrigger selected to fire when the staplerPath object gets a MouseLeftButtonDown event. That is exactly what we want. In the Common Properties group you will see TargetName and PropertyName. The TargetName is the name of the object that will have its property changed and PropertyName is the name of the property. Since we want the staplerText TextBlock to be dimmed when the staperPath object is clicked, then we set the TargetName to staplerText. An easy way to do that is to grab the bullseye (called the artboard element picker) and drag it to staplerText on either the canvas or the object tree. The Properties for this action should now look like this:

 

Press F5 to run the project and click on the stapler. The text for stapler is now dimmed.

 

You can play with the Animation Properties by setting duration for the property change and even set an easing value if you don’t want a linear change in the Opacity from 100% to 40%. Let’s make this even better by adding a sound effect when an item is clicked.  I found a nice magic wand sound on SoundBible.com and added it to the project in an Audio folder. Now drag the PlaySoundAction from the Assets list onto staplerPath and set the Properties to use the audio file:

 

 

Now repeat this same process for the other 12 items. We have the core of our very own hidden object game!

 

In our next episode, we will write a custom behavior that will shoot a spray of particles when an item is clicked.

Hidden Object: Episode 2 – Create the Item list

September 17, 2009 in Hidden Object Game, Silverlight

This is the second episode in the series: Creating a Hidden Object Game in Silverlight 3

In this episode, we will add the image for the item list and create a wrap panel with a text blocks for each item in the list. If you have not already installed the Silverlight Toolkit, you will need to download and install it. This project is using the Silverlight 3 Toolkit July 2009 Installer.

I found an image of a whiteboard and did some cropping, removed the background, and saved it as a PNG. This was added to the Images folder.

Drag the image to the bottom center of the canvas and resize it to 328×205 and position it at 202, 395.

Now it is time to add the WrapPanel and position it on top of the whiteboard image:

  • Go to the Assets tab
  • Select Controls
  • Scroll down until you find the WrapPanel control and select it

Now you can draw the WrapPanel over the whiteboard image. If you go to the Projects tab and open the References folder you will notice that the following references have been added:

  • System.Windows.Controls.dll
  • System.Windows.Controls.Toolkit.dll

Before we start adding TextBlock controls to the WrapPanel, lets create a style called WhiteboardText and add it to the Application.Resources section of App.xaml:

<Style x:Key="WhiteboardText" TargetType="TextBlock">
 <Setter Property="Foreground" Value="#FFB11111"/>
 <Setter Property="FontFamily" Value="Comic Sans MS"/>
 <Setter Property="FontSize" Value="16"/>
 <Setter Property="Margin" Value="10,15,10,0"/>
</Style>

To keep the Image and WrapPanel together, select both in the tree and from the context menu pick Group Into > Canvas:

Select the WrapPanel in the tree, select the TextBlock tool in the Tools panel and double-click the canvas. This will add a TextBlock control as a child of the WrapPanel. Make sure the Width and Height properties are set to Auto. In the Miscellaneous group click the peg to the right of the textbox and pick Local Resources > WhiteboardText. We will now copy that TextBlock and paste it 12 more times so that the WrapPanel has a total of 13 items. For each TextBlock, change the Text and Name properties based on this list of items:

  • Stapler
  • Idol
  • Candies
  • Ball
  • Tape
  • Eeyore
  • Pencil
  • Tigger
  • Business Cards
  • Learning WCF
  • Rock
  • Dog
  • Post Its

So for the first TextBlock the Text is “Stapler” and the Name is staplerText. When I copied and pasted the TextBlock in the tree, it added a Margin property to the 12 items pasted. Since that property is contained in WhiteboardText I did a quick manual clean-up of the xaml. The hidden object game now looks like this:

In the next episode, we will use the ChangePropertyAction to mark items off the list when we select them as well as the PlaySoundAction.

Hidden Object: Episode 1 – Create the Project & Picture

September 17, 2009 in Hidden Object Game, Silverlight

This is the first episode in the series: Creating a Hidden Object Game in Silverlight 3

In this episode, we will get the project started and layout the UI.

Before we begin, make sure that you have the following installed:

  • Silverlight 3 Tools for Visual Studio 2008 SP1
  • Blend 3
  • Silverlight Toolkit
  • Expression Blend 3 SDK

The first 3 can be downloaded from the Get Started tab at Silverlight.net. You can download the Expression Blend 3 SDK from here.

First we launch Blend 3 and create a new Silverlight project:

The project is called ClutteredCube as that will be the main image for the hidden object game. The two things that we are going to do is change the LayoutRoot so that its LayoutType is Canvas and change the UserControl’s dimension to 800×600:

One day I brought a digital camera to work and took a photo of my cube. This is really how it looks with just a few items added to make the game more interesting:

You can use your own images as you follow this tutorial or download these.

The image has been resized and cropped to 800×500. Create an Images folder in the project and add the file to it.

Now drag-n-drop the office image onto the canvas and position it at the top. Select the LayoutRoot again, select Background under Brushes and use the color eyedropper to pick a dark color from the shelf or drawer. The page should now look like this:

In the next episode, we will create the list of items.

Creating a Hidden Object Game in Silverlight 3

September 16, 2009 in Hidden Object Game, Silverlight

According to the Casual Games Association, there are over 200 million casual game players worldwide.  One genre of casual game is the hidden object game. Mystery Case Files: Return to Ravenhearst by Big Fish Games is one example.  You are presented with a picture full of images and a list of items you must find.  As you click on the item in the picture, it is crossed off the list.  When you find the last item, you progress in the story to the next location or puzzle.

In this series, we will create a hidden object game using Silverlight 3 and behaviors, actions, and triggers.