January 27, 2021
Hot Topics:

Using Graphics: Making a Lander Game

  • By John Percival
  • Send Email »
  • More Articles »

Well, we've now got the basics of getting the space craft into space, it's now time to add that extra dimension of reality...the stars. I considered making 3D-rendered burning balls of fire, but that seemed a little overkill for our Lander game, so we will just stick with yellow dots for now.

At the start of the game, we need to draw the stars onto the picture box that will eventually host the space craft. Therefore, we must loop through all of the pixels in the box and decide if we want a star at that particular point or not. We can do this with a For...Next loop, as you will see in a moment. VB provides its own function for setting a pixel's colour, PSet, but this is extremely slow, so we will be using another API, SetPixelV.

Here's the declaration

Declare Function SetPixelV Lib "gdi32" Alias _
   "SetPixelV" (ByVal hdc As Long, _
   ByVal x As Long, ByVal y As Long, _
   ByVal crColor As Long) As Long

This function is virtually identical to SetPixel, except that it is faster since it does not return the actual colour set to the point. As you can see, we again must use the hDC property of the picture box. x and y are the coordinates of the point, in pixels, and crColor is the RGB colour to be set.

Getting Random Numbers

The Rnd function will return a random number between 0 and 1 inclusive. However, before we start getting random numbers, it is recommended to issue the 'Randomize Timer' command. The timer function returns the number of seconds elapsed since midnight. The Randomize command sets the 'seed' for the random number generator. Without going too deep into the complicated stuff behind it, this command effectively means that we get a different set of random numbers each time, therefore a different star pattern. To demonstrate what I mean, try replacing the 'Timer' with an actual number, then study the star patterns. They will be the same each time round now!

Let's try drawing some starts now. Start a new project, and place a picture box and command button on the form. In the click event of the command button, put this code:

Private Sub Command1_Click()
Randomize Timer
Picture1.ScaleMode = vbPixels
For starx = 0 To Picture1.ScaleWidth
  For stary = 0 To Picture1.ScaleHeight
    If Rnd < 0.005 Then
      SetPixelV Picture1.hdc, starx, stary, vbYellow
    End If
End Sub

Now when you run your program and click on the button, you will get a lovely star pattern. Ahhh!

The last thing that we need now is a surface for our lander craft to land on. We'll just stick with a bog-standard VB function to do this, rather than any fancy APIs. Its name is Line, which is a bit strange considering we want to draw a box, but never mind, those guys at Microsoft have done it again!

Its anatomy, according to the help file is:

object.Line [Step] (x1, y1)-[Step] (x2, y2),
   [color], [B[F]]

It looks rather complicated, but just think of it as drawing a line from 'x1,y1' to 'x2,y2' in 'color'. If 'B' is specified, that a box instead of a line, and if 'F' is specified additionally, then the box is filled. No prob!

Our planet is going to be just a boring flat surface for now, but you can add extra challenges if you wish. Here is the code:

Picture1.Line (0, Picture1.ScaleHeight - 30)- _
  (Picture1.ScaleWidth, Picture1.ScaleHeight), _
  vbWhite, BF

The results are not very spectacular, but you can add it to the star program if you would like to see them.

Page 4 of 8

This article was originally published on November 20, 2002

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date