Skip to main content

Past Blast

Featured Products

Windows Mobile Developer Controls
Windows Mobile Developer Controls
Stay in touch using the DEVBUSS RSS feeds.
 

News

Windows Mobile Developer Controls
Sapphire Soltuions

Creating Pocket PC Games in eVB Using ASpriteCE

Written by Andy Beaulieu  [author's bio]  [read 54485 times]
Edited by Derek

Download the code

Page 1  Page 2  Page 3 

To define our sprites, first add the following constant declarations at the top of Form1's code window:

Const SPRITE_SHIP = 0
Const SPRITE_BULLET = 1
Const SPRITE_BUZZARD_FIRST = 2
Const NUM_BUZZARDS = 3

Using constants like this for sprite identifiers will help keep the game code more readable. Now we'll use the SpriteDefine method to create our sprites. SpriteDefine has the following signature:

SpriteDefine(lSpriteNum As Long, sFileName As String, _
iXPosBMP As Long, iYPosBMP As Long, _
iFrameWidth As Long, iFrameHeight As Long, _
iFrameCount As Long, iFramesPerRow As Long)

Let's discuss these parameters a bit, as SpriteDefine is a core method in ASpriteCE: lSpriteNum is a distinct identifier for the sprite. You should start at zero for your first sprite and increment from there. sFileName is the absolute path to the image. iXPosBMP and iYPosBMP are the coordinates in the image file that the sprite frames begin at. This is useful only if your image file contains multiple sprites, otherwise simply pass zeros for these. iFrameWidth and iFrameHeight define the dimensions of each frame for the sprite. iFrameCount is the number of frames the sprite has. Finally, iFramesPerRow is equal to the number of frames on each "row" in the image file; this is equal to iFrameCount unless you create your image file by breaking frames into multiple rows (useful for sprites with a lot of frames).

OK, now let's implement SpriteDefine for our sprites:

Game1.SpriteDefine SPRITE_SHIP, _
App.Path & "\ship.gif", 0, 0, 28, 35, 1, 1
Game1.SpriteDefine SPRITE_BULLET, _
App.Path & "\bullet.gif", 0, 0, 5, 8, 1, 1
Game1.SpriteDefine SPRITE_BUZZARD_1, _
App.Path & "\buzzard.gif", 0, 0, 36, 16, 5, 5

In DevBuzzards, we will want to have multiple buzzard sprites that have identical source frames. If we were to use SpriteDefine to create all of the buzzards, we would be wasting memory by duplicating the frame data for each one. Instead, we can use the SpriteCopy method which efficiently copies sprites by sharing their frame data:

' Make copies of buzzard sprite
Dim i As Integer
For i = SPRITE_BUZZARD_1 + 1 To _
SPRITE_BUZZARD_1 + NUM_BUZZARDS - 1
Game1.SpriteCopy SPRITE_BUZZARD_1, i
Next

Next we'll position the sprites with a call to SpriteSetPosition - this is pretty straightforward. The SpriteFrameRate property will be used to make the buzzards animate one sprite frame for every three game frames. We also want to make the bullet sprite initially inactive by using the SpriteActive property.

' Position sprites
Game1.SpriteSetPosition SPRITE_SHIP, 0, 150
For i = SPRITE_BUZZARD_1 To _
SPRITE_BUZZARD_1 + NUM_BUZZARDS - 1
Game1.SpriteSetPosition i, _
(i - SPRITE_BUZZARD_1) * 40, 10
Game1.SpriteFrameRate(i) = 3
Next
Game1.SpriteActive(SPRITE_BULLET) = False

The last part of our "setup" code in Form_Load will start the game timer with a call to StartTimer. The timer determines the frame rate of our game, and in DevBuzzards we'll aim for 20 frames per second. We pass StartTimer a value of the number of milliseconds between game frames - 50 milliseconds will give us 20 frames per second.

' Start Timer
Game1.TimerStartTimer 50

At this point, we've only written about a dozen lines of code, but it's interesting to see how much this code does! Go ahead and make the VB file by selecting File/Make Project1.vb from the eVB menu. Then, copy the Project1.vb file along with the four image files to a directory on your device and run Project1.vb. You should see the animated buzzard sprites at the top and the deVBuzz PDA logo at the bottom, with the starfield image in the background.

Previous Page  Next Page