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
- All the prerequisites defined in Silverlight game programming Shootorial #3 conversion.
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.
- Shootorial #4 Sample (you may read this software’s license here)
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;
}
}
...
- Learn more about the ContentControl class
- Learn more about the Image class
- Learn more about the BitmapImage class
- Learn more about the Uri class
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 ;).
Related Posts:
Comments
15 Responses to “10 Minute Silverlight Game Programming Tutorial - Shootorial Conversion #4 (C#)”




Thanks for sharing this news.
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
< 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….
10 http://04FORDPARTS.US/tag/10 : 10…
10…
10 http://04FORDPARTS.US/tag/DVD-RW 10 : DVD-RW…
DVD-RW…
1 http://04FORDPARTS.US/tag/1 : 18 Accessories Cars 118/…
1…
uk MacBook Apple/ http://AWESOMEBABYCLOTHES.INFO/tag/r\x3dh : uk MacBook Apple/…
r\x3dh…
Center http://cdiscounty-rv.AUTOPARTSVILLE.INFO/tag/lowes Center Unit/ : lowes…
Center…
Fresh http://icoventryrvb0pbq.bestpartsstore.info/tag/Bio Fresh BioGS fresh/ : fresh…
Fresh…
phone http://hbemiss8e705s.04FORDPARTS.US/tag/buy+phone+Phone/ : buy…
phone…
Home http://umirroredtu6w.ALLSTOCKSPORT.INFO/tag/Home+Koblenz+polisher/ : polisher…
Home…
GE http://nbennw.AACEHARDWARE.INFO/tag/frigidaire+cooktop+GE/ : frigidaire…
frigidaire…
floodlights http://lgunu-va.AUTOSECTIONS.INFO/tag/floodlights+Flood+Lights/ : floodlights…
Lights…
ATampT http://rhowod2nk.BABYCLOTHESNUT.INFO/tag/Motorola+Rizr+Z3+Att+ATampT+Samsung/ : Z3…
Samsung…
Side http://uhalloweenlclct.AUTOSECTIONS.INFO/tag/Side+Amber+Marker/ : Side…
Marker…