3-Lib banner

The Absolute Beginners Guide to OPL

Part 16 - Simple animation


We return to OPL's graphic capabilities this week, with an attempt to jazz up our MiLo splash screen with a simple animation. Let's make the pile of money on the left of our picture 'dance' around a little!

Let me give you the code snippet first, so that you can type it in, translate it and see what I'm talking about. Find the bit at the start of the program which refers to gCOPYing the picture and amend it as follows:

gCOPY image%,0,0,480,160,0
i%=1
WHILE i%<=50
  gAT rnd*10,rnd*10
  gCOPY image%,0,0,100,120,3
  i%=i%+1
ENDWH
gCLOSE image%

Screen shotTranslate and run and watch the opening graphic carefully. The money should jiggle about randomly for a second or so. Now that you believe me(!), let me unpack the extra lines of code and what they mean.

i% is used (as usual) as a general-purpose dogsbody of a variable, just counting up to 50. Each time through the loop its value is incremented by 1 until the WHILE condition finally fails and the loop stops.

This simple animation is achieved by simply overlaying a portion of the left-hand side of our image% 'drawable' (i.e. bitmap in memory) fifty times, each time with a slightly different random offset on the screen. Because the clipart image has quite a bit of 'white space' around it, there's no need for us to blank off the previous drawing before overlaying the next - the image is only 'jiggled' by up to 10 pixels in the horizontal and vertical and in each case no portion of the previous drawing is left on-screen to look ugly.

The program asks the Psion for a random number between 0 and 1 each time it gets to the RND command. I've simply multiplied each number by 10 so that the result is in the right sort of magnitude for us to use for the jiggling of our graphic. Note, obviously, that the 'jiggling' will be different every time you run the program as the numbers really are extremely random.

The gCOPY is most important to note. As before, the first 2 parameters it takes are just the starting point in the image% bitmap, i.e. 0,0 (the top-left). The second two tell the program how much of the image% bitmap to copy onto the screen. The numbers 100 and 120 were measured by me in my painting program to give me the correct bit of the picture - you may find you need to alter them slightly for your own. Finally, the last parameter is vital. Let me explain.

Every graphics operation in OPL operates in one of 4 modes, and it's very important to understand the differences between them: