Moving Your UFO on the Screen in Phrogram
- Using Sprites for Game Graphics
- Variables Let You Control Your Game Over Time
- Loops Tell Phrogram to Do Something Over and Over Again
Using Sprites for Game Graphics
In the preceding section, we explored how (x, y) coordinates are used for displaying graphics. Graphics are usually loaded from a picture file, such as the image of the UFO in the file "ufo.gif". We also learned that Phrogram uses the sprite system class to make all of this very easy to do!
Now let's explore how to move graphical objects on the screen. Just like when placing an object on the screen, moving one around uses the coordinate system. We'll start by showing the finished example, and then we'll explain how the program works in more detail. Here is the full program. The six new instructions we have added to your last example tell Phrogram to move the UFO slowly down the screen, and to play an eerie sound effect while it does. Go ahead and change your program from the last section, as shown in Figure 3.2. Note that we changed the name of the program on line 1, and we added six new instructions in Method Main().
Figure 3.2 Moving your UFO, and playing a sound effect
Once you have changed the program, it's best to save it with a new name so that your preceding example is still saved separately in the file system, in addition to this modified version. To do that, click the File menu and then select Save As New Program. When the dialog comes up, save this new program with the filename UFOMovingWithSound. Once you save it, you should see the new file appear in Phrogram's File Explorer.
Now, let's recap briefly what we learned in Section 2, looking only at those instructions, which are also included in the new example we're doing in this section:
Define myUFO As Sprite myUFO.Load( "ufo.gif" ) myUFO.MoveTo( 50, 50 ) myUFO.Show()
We told Phrogram to load the image for a sprite named myUFO from the file "ufo.gif" with this instruction: myUFO.Load( "ufo.gif" ). We told Phrogram where to place the sprite on the screen with the instruction myUFO.MoveTo( 50, 50 ). Notice here that there are quotation marks around the words "ufo.gif", but not around the numeric values 50 and 50. In general, Phrogram requires quotation marks around values that are words (also known as string values). Phrogram does not require quotation marks around values that are numbers (also known as integer and decimal values). Other programming languages use the same or a similar convention.
Now let's look at the new instructions we are adding:
PlaySound("eerie.wav") Define ufoY As Integer For ufoY = 51 To 150 Delay(49) myUFO.MoveTo(50, ufoY) Next
The first instruction is the easiest, and it is pretty obvious: PlaySound("eerie.wav") tells Phrogram to play the sound that is stored in the sound file "eerie.wav" through the computer's speakers. Note that the same rule applies for this filename: It must have double quotes around it, as shown. As the name of the sound file suggests, it is indeed an eerie kind of sound, like from a scary, old, science fiction movie. Many additional sounds are available in Phrogram, under the Media Files folder in the Sounds subfolder. Also, hundreds of additional sounds are available for you to download for free, from the Phrogram community Web site. You can also record your own sounds and add them to the same Sounds subfolder. But this eerie one seemed very appropriate as our UFO flies down for a landing.