Prove It

advertisement
Cat and Mouse
The game of Cat and Mouse is a bit more difficult than previous challenges. Since you made it this far, we
know you can write this program that has a cat chasing a mouse.
Use the template which has a form that looks like this:
Assume the screen is 600 pixels wide and 300 pixels tall.
Move the cat and mouse in somewhat random patterns by changing the location of the picture boxes
containing the cat and mouse pictures by adding a positive or negative number to the current X and Y
coordinates.
There is a Timer event that executes every 1/4 of a second. Inside the timer event you move the cat and
mouse. You cannot do it directly. To move a picture box, you must calculate an X and a Y coordinate (number)
then use this line of code:
picMouse.Location = New Point(X, Y)
After moving the cat and mouse, check to see if:




The mouse escaped
The cat caught the mouse
The mouse needs to change direction because it is close to the cat
The cat or mouse is near the edge of the screen.
The cat is in a picture box called picCat.
The mouse is in a picture box called picMouse.
If the cat is within 20 pixels of the mouse as measured from the top, left corner of the picture box, the mouse
is caught. If the mouse is within 20 pixels of either black text box as measured from the top, left corner of the
text box and mouse, the mouse escapes. In either case, stop the timer.
If the mouse is within 40 pixels of the cat, the mouse changes direction.
Hole one is located at (50, 150). Hole two is located at (530, 150).
There are several things in the template that help you write this program.
Function HowFar returns the distance between two points if you supply the X and Y coordinates of the points.
For example,
Distance = HowFar(Cat.Location.X, Cat.Location.Y, Mouse.Location.X,_ Mouse.Location.Y)
Function NewChg returns a random number that may be positive or negative. It represents the amount the
mouse or cat should move on each turn. For example,
CatXChg = NewChg()
In the new game button click event code, assign an initial random location for the cat and the mouse. Assign
an X and a Y increment for each. Start the timer.
Hint: Many programmers find it best to write a little code then test, write some more then test again. You
should write this program as follows:








Place the cat and mouse
Move the cat
Check for cat boundaries
Move the mouse
Check for mouse boundaries
Check for mouse escape
Check for cat catches mouse
Check for mouse getting close to cat
After each step, test your code
When your program works correctly, show it to your instructor.
Extension / Side Trip
Keep track of which direction the image points. Reverse the direction if the cat or mouse change direction. Use
this or something like it:
Cat.Image.RotateFlip(RotateFlipType.Rotate180FlipX)
Figure out in which direction the mouse is, then change the cat increments to keep the cat going in the
direction of the mouse. For example, if the cat had an X coordinate of 450 and the mouse was at 250 then the
cat needs to be moving in a negative direction.
Add a dog to chase the cat.
Download