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.
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:
Sub moveFly()
Image5.Left = Image5.Left + 50 + Int(Rnd * 100 - 50)
If Image5.Left > 18000 Then
Image5.Left = 0
End If
If Image5.Left < 0 Then
Image5.Left = 18000
End If
Image5.Top = Image5.Top + 20
End Sub
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:
Sub checkOverlap()
x = Image3.Left - Image5.Left
y = Image3.Top - Image5.Top
If x < 10 And y < 10 Then
MsgBox "GULP!"
Image5.Visible = False
End If
End Sub
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:
Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
Function KeyDown(code As Integer)
KeyDown = (GetKeyState(code) < 0)
End Function
This must be placed inside a module - like you did with variables in the adventure game.
Now your program can be written this way:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyDown(65) Then
Image2.Left = Image2.Left - 100
If Image2.Left < 0 Then
Image2.Left = 18000
End If
End If
If KeyDown(68) Then
Image1.Left = Image1.Left + 100
If Image1.Left > 18000 Then
Image1.Left = 0
End If
End If
moveFly
End Sub
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.