10 Minute Silverlight Game Programming Tutorial - Shootorial Conversion #4 (C#)

Another day, another shootorial! Every good game has a villian and in this fourth installment, we will add several villians at random intervals. OO-ifying the game in the last tutorial will make adding enemy ships almost embarrassingly easy.

So, let’s get started!

Prerequisites

QuickStart

If you want to see the end result of this tutorial and you have installed all the prerequisites, then please download the ZIP file below, unzip it and open the shootorial.html file in your browser. Now, you should see enemy ships flying across the screen. Like before, you will need to click on the Silverlight control to give it focus first.

You can also see it in action, here.

Lesson

Step 1: Create the EnemyShip class

The last tutorial migrated all game entities into classes, so the EnemyShip will need a class as well. Create a file named “EnemyShip.cs” and put this code in it:


namespace ShootorialApplication
{
  using System;
  using System.Windows;
  using System.Windows.Input;
  using System.Windows.Media;
  using System.Windows.Media.Imaging;
  using System.Windows.Controls;
  using System.Windows.Shapes;
 
  public class EnemyShip : ContentControl, IGameEntity
  {
  }
}

As highlighted, this class derives from the ContentControl class and IGameEntity interface. The previous tutorial explains the hows, whats and whys of this derivation in greater detail. I just wanted to remind the reader, here.

Step 2: Add the enemy ship image

You can download the emeny ship image from Kongregate’s website. During construction, the EnemyShip class loads this image using a BitmapImage object, and being a ContentControl, sets it to its inherited Content property:


...
public class EnemyShip : ContentControl, IGameEntity
{
  public EnemyShip()
  {
    Image enemyShipImage = new Image();
    enemyShipImage.Source = new BitmapImage(new Uri("/enemyShip.png", UriKind.RelativeOrAbsolute));
 
    this.Content = enemyShipImage;
  }
}
...
Step 3: Set the enemy ships’s initial position

Just like Kongregate’s tutorial, the ship should start off screen and gradually come into view from the right at a random altitude. Again, this code will go in the constructor:


...
public class EnemyShip : ContentControl, IGameEntity
{
  private int speed = 0;
 
  public EnemyShip()
  {
    Image enemyShipImage = new Image();
    enemyShipImage.Source = new BitmapImage(new Uri("/enemyShip.png", UriKind.RelativeOrAbsolute));
 
    this.Content = enemyShipImage;
 
    Random random = new Random();
 
    Canvas.SetLeft(this, 700);
    Canvas.SetTop(this, random.Next(50, 250));
 
    speed = random.Next(5, 10);

  }
}
...

The first line highlighted shows the declaration of a new variable that holds the “speed” of the enemy ship. The highlighted and now very familiar Canvas.SetLeft and Canvas.SetTop functions set the enemy ship’s initial starting position. The call to Canvas.SetTop uses the Next function of the Random class. The Random class provides easy functions for generating random numbers and this particular call returns a random number between 50 and 250. This makes ships appear at different altitudes. The same call to Random.Next sets the emeny ship’s speed, so it moves anywhere between 5 to 10 units on each frame.

Speaking of making the ship move…

Step 4: Move the enemy ship

The movement code for EnemyShip will look very similar to the movement code for the Missle class from the last tutorial:


...
public class EnemyShip : ContentControl, IGameEntity
{
...
  public void Update(Canvas theCanvas)
  {
    Move(Direction.Left);
 
    if( Canvas.GetLeft(this) < -100 )
    {
      theCanvas.Children.Remove(this);
    }
  }
 
  public void Move(Direction direction)
  {
    Canvas.SetLeft(this, Canvas.GetLeft(this) - speed);
  }
...
}
...

Working from the bottom up, the Move function simply moves the position of the enemy ship to the left by the number of units set in the “speed” variable (ignoring the direction parameter, for now). The Update function first calls the Move function and then, determines if the enemy ship has moved off screen, and if so, removes it from the Canvas.

That’s it! No let’s put this bad boy to use!

Step 5: Add enemy ships

To add ships on the fly, “ShootorialControl.xaml.cs” will need a few modifications:


...
public partial class ShootorialControl : UserControl
{
  private int enemyTimer = 0;
  ...
 
  private void ShootorialControl_Rendering(object sender, EventArgs e)
  {
    if( shootLimiter < 8 )
      shootLimiter += 1;
 
    enemyTimer += 1;
 
    if( enemyTimer > 60 )
    {
      enemyTimer = 0;
 
      theCanvas.Children.Add(new EnemyShip());
    }

 
    for( int elementIndex = 0; elementIndex < theCanvas.Children.Count; elementIndex++ )
    {
      IGameEntity gameObject = theCanvas.Children[elementIndex] as IGameEntity;
 
      if( gameObject != null )
      {
        gameObject.Update(theCanvas);
      }
    }
  }
}

Similar to step #6 of Kongregate’s tutorial, the “enemyTimer” variable increments every frame until it reaches 60. When this occurs, a new EnemyShip gets added to the Children collection of the Canvas, making new ship images appear. All very simple.

Step 6: Build it

With so many tutorials as prolog I doubt these build file additions need explaination:


<ItemGroup>
  <Compile Include="IGameEntity.cs">
  </Compile>
  <Compile Include="Missle.cs">
  </Compile>
  <Compile Include="Ship.cs">
  </Compile>
  <Compile Include="ScrollingBackground.cs">
  </Compile>
  <Compile Include="EnemyShip.cs">
  </Compile>

</ItemGroup>
...
<ItemGroup>
   <None Include="enemyShip.png">
     <CopyToOutputDirectory>Always</CopyToOutputDirectory>
   </None>
</ItemGroup>

...

You can build this with the usual command line:


"C:\WINDOWS\Microsoft.NET\Framework\v3.5\msbuild.exe" ShootorialApplication.csproj
Step 7: Run it!

To run it, simply open the shootorial.html file in your browser. You should only have to wait a moment before enemy ships start flying at you.

Conclusion

…but unfortunately, no matter how many shots you fire, your bullets won’t connect :). That’s for next tutorial ;).

Share and Enjoy:
These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • StumbleUpon
  • Reddit
  • del.icio.us
Related Posts:
10 Minute Silverlight Game Programming Tutorial - Shootorial Conversion #2 (C#)
10 Minute Silverlight Game Programming Tutorial - Shootorial Conversion #3 (C#)
10 Minute Silverlight Game Programming Tutorial - Shootorial Conversion #5 (C#)
10 Minute Game Programming Tutorial: Silverlight - Shootorial #1 Conversion (C#)

Comments

15 Responses to “10 Minute Silverlight Game Programming Tutorial - Shootorial Conversion #4 (C#)”

  1. jeff (Game Talk) on February 23rd, 2009 8:16 am

    Thanks for sharing this news.

  2. Hoschbel on March 31st, 2009 3:42 pm

    Hey, that’s fun!
    I’m a professional developer building complex business systems for several years now. But I never tried game programming (don’t know why)… I should have started earlier :-)

  3. JARED on July 15th, 2010 6:51 pm

    < blockquote >< a href=”http://pillspot.org/”>Pillspot.org. Canadian Health&Care.Best quality drugs.Special Internet Prices.No prescription online pharmacy. Online Pharmacy. Buy drugs online< /a >…

    Buy:Cialis Super Active+.Cialis Soft Tabs.Viagra Super Force.Tramadol.Cialis.Levitra.Propecia.Cialis Professional.Super Active ED Pack.Soma.Maxaman.Viagra Professional.Viagra Super Active+.Viagra.Viagra Soft Tabs.Zithromax.VPXL….

  4. 31a 1038 genelec/ on August 29th, 2010 3:08 am

    10 http://04FORDPARTS.US/tag/10 : 10…

    10…

  5. 00a rw NEC/ on August 29th, 2010 3:32 am

    10 http://04FORDPARTS.US/tag/DVD-RW 10 : DVD-RW…

    DVD-RW…

  6. 1 on August 29th, 2010 6:01 am

    1 http://04FORDPARTS.US/tag/1 : 18 Accessories Cars 118/…

    1…

  7. uk MacBook Apple/ on August 29th, 2010 6:01 am

    uk MacBook Apple/ http://AWESOMEBABYCLOTHES.INFO/tag/r\x3dh : uk MacBook Apple/…

    r\x3dh…

  8. Center on August 29th, 2010 6:13 am

    Center http://cdiscounty-rv.AUTOPARTSVILLE.INFO/tag/lowes Center Unit/ : lowes…

    Center…

  9. BioGS on August 29th, 2010 6:18 am

    Fresh http://icoventryrvb0pbq.bestpartsstore.info/tag/Bio Fresh BioGS fresh/ : fresh…

    Fresh…

  10. Phone on August 29th, 2010 6:29 am
  11. Home on August 29th, 2010 5:21 pm
  12. cooktop on August 29th, 2010 5:24 pm
  13. Lights on August 29th, 2010 5:32 pm
  14. ATampT on August 29th, 2010 5:38 pm
  15. Marker on August 29th, 2010 10:01 pm