Using Graphics: Making a Lander Game
It may cost many zillions of dollars to get a space ship into space, but all it takes is a simple API call to get our space ship onto the screen. If fact, it is only for speed that we use the API, as the lowly PaintPicture method would suffice. However, since the space ship will be zipping around, we will be using the BitBlt (pronounced 'bitblit') API call. It really does nothing fancy except merging two images to form one. Using a few clever tricks we will be putting the space ship onto a starry background and let it float around a bit.
Well, enough of the introduction, lets get our teeth into the API call:
Declare Function BitBlt Lib "gdi32" _ (ByVal hDestDC As Long, ByVal x As Long, _ ByVal y As Long, ByVal nWidth As Long, _ ByVal nHeight As Long, ByVal hSrcDC As _ Long, ByVal xSrc As Long, ByVal ySrc As _ Long, ByVal dwRop As Long) As Long
Phew! There's plenty of it, but it's pretty easy really.
At any given time when you are drawing under Windows, the area being drawn to has a number of specific characteristics, including the size, the color and the type of device. All this information is encapsulated by an object called a Device Context. Anything that you can draw to has a Device Context (referred to from now on as a DC), including printers, picture boxes and forms, although VB does its best to hide these away since they are only used with the API. All the aforementioned objects provide a handle to the DC, accessible through the hDC property. It is this property that we will be using the the hDescDC and hSrcDC parameters of the BitBlt API call.
The x and y parameters specify where the image will be painted on the destination. The nWidth and nHeight parameters specify how big the source image is. The xSrc and ySrc parameters specify whereabouts in the destination to grab the image from. Since we are using the API, you must work in pixels, so it is best to set the scalemode property to 3 - Pixels as soon as you draw any picture boxes.
The last parameter, dwRop is where the fun stuff comes in. It allows you to define the bit-wise operation that combines the pixels from the source and destination bitmaps. It uses the same constants as the PaintPicture method, which can be found in the help file. These include vbSrcCopy to make a direct copy of the source on the destination and vbSrcInvert, which we will be using later to put the ship onto the starry background.
So much for the techno-babble, lets have a look at using this call.
Page 2 of 8