Hopping Froggies

img1.gif

This program demonstrates how to move pictures using the keyboard.  

Every key on the keyboard has a number - an ASCII code.  Here area some examples:

    a = 97     b = 98    c = 99    .....  y = 121    z = 122

For other keys, you need to count to figure out the code.

  1. Create a new VB project.
  2. Select a small picture, like the frogs in the picture above.  Use Paint Shop Pro
    (or some other graphics tool) to rotate the picture 90 degrees,
    save it, rotate again, etc - until you have 4 copies pointing in 4 directions.
    Save them as .BMP files.
  3. Copy the 4 pictures into image-boxes in VB.
  4. Create a KeyPress subroutine.
  5. Type the code shown in the picture.
  6. Run the program, and use the A, W, D, X keys to move the pictures.
  7. Two of the keys move the wrong pictures.  Fix this mistake.
  8. Change the program to use the H, K, U and M keys.
  9. Make the frogs move faster.
  10. Change the program so that the pictures wrap around at the edges of the screen.
    The teacher will show you how to do this.

Another Program

Make a new program that works differently.  It should have 2 pictures, representing 2 players.
It should use A, S, D, X to move one picture, but use H, K, U, M to move lthe other picture.
You can do this with another student.  Test whether this works when 2 players are pressing
their control keys at the same time.


Add Food

Add a fly (or some other target) that the frogs (or your pictures) might want to catch.
Use code like the following, to make the target move every time a player moves:

You must also add a command - moveFly - at the end of the keyPress subroutine,
so that every time a key is pressed, the fly moves as well.

Eat the Food - First Try

You will want to check whether the frog has touched the fly.  I looks something like this:

This is just a first try - it is not "correct".  The teacher will show you how to do this better.

Eat the Food - the RIGHT Way

The code above attempts to calculate the distance between a frog and the fly.
It doesn't work for several reasons:

Now you can give it another try.  And remember that EITHER frog should be allowed to eat the fly,
not just one frog.  So there is a lot more to check.

What?  Your 2-Player Game Doesn't Work?

Yes, that is indeed unfortunate.  Visual Basic does not manage the keyboard very well, so you cannot
have both players typing keys at the same time, and have both frogs moving.  Now what?

Rather than responding to a key being pressed (down and up), we need to check whether a specific key
is being held down, and then check again - lots and lots of times.  In fact, we need to check lots of
different keys and respond to each one.  This process is called keyboard polling - like asking each
individual person what they think.

Windows can poll the keyboard, but VB doesn't know how.  So we need to add a Windows API call
to ask Windows to inspect the keyboard for us.  It looks like this:

This must be placed inside a module - like you did with variables in the adventure game.

Now your program can be written this way:

Notice that this has been changed to a KeyDown subroutine, rather than KeyPressed.  
Notice further that this has different parameters (KeyCode rather than KeyAscii, etc)
Notice further that you must use different KeyCodes than the ASCII codes mentioned above.
65 is the code for a CAPITAL A, rather than 97 for a small a.
68 is the code for CAPITAL D.  You can count on your fingers to figure out others.

Of course you need to add more keys - probably 4 keys for each frog.  
Add another ... If KeyDown(num) then ...  for each key you want to check.

You need to throw away your old sub Form_KeyPress, as it will interfere with  KeyDown .

And if you want to use the arrow keys, the numbers are 37, 38, 39, 40.

You will notice that every time a new key is pressed, the game pauses.  
This is fixable, but requires ANOTHER NEW IDEA - next week.

Now download FROGGER2.ZIP
and do the problems listed at the bottom of the program