Mouse In A Hole, the platform game the course builds in Xtal BASIC 4.2, running on the Tatung Einstein: the mouse mid-jump between platforms, two cats and the cheese
← Back to Courses
module
29

Home

Introduction

The mouse can reach the hole already, and nothing happens. This section makes the hole the end of the game - but only once every piece of cheese is eaten, because a hole you can run straight to is not much of a game.

Then, win or lose, the game offers another go.

Counting The Cheese

The program needs to know when all five pieces are gone. The score would do - five pieces are 50 points - but a count says what it means. Add one to GOT whenever a piece is eaten:

420 I=C:EATEN(I)=-1:PTS=PTS+10:GOT=GOT+1

Three Things At Once

The mouse is home when three things are all true: every piece is eaten; it is level with the middle of the hole; and it is standing on the top platform.

185 IF GOT=5 AND ABS(MX-167)<6 AND ABS(MY-138)<4 THEN 210

Each comparison is worth -1 when it holds and 0 when it does not, as S11 showed, and AND gives -1 only when all three are -1.

The numbers come from the field. The hole's middle is at x=175, and the mouse's middle is 8 pixels in from its x, so the mouse is centred on the hole when MX is 167. The top platform's surface is at y=121, and from S22 a creature standing on a surface is at that height plus 17: 138. ABS from S7 gives a little room either side, because the mouse moves six pixels a step and might never land on 167 exactly.

Home

210 SPRITE OFF 0:PRINT @17,4;"HOME!"

SPRITE OFF 0 hides the mouse - it has gone down the hole.

Another Go

Win or lose, the game ends at the same question:

200 PRINT @15,4;"GAME OVER":GOTO 220
210 SPRITE OFF 0:PRINT @17,4;"HOME!"
220 GOSUB 1810
225 PRINT @11,6;"ANOTHER GO? Y OR N"
230 A=INCH:IF A=89 THEN 60
235 IF A<>78 THEN 230
238 END

Both ways in arrive at line 220, which empties the key queue from S24 before the question - otherwise the Xs queued up while you played would be the first keys INCH saw. Line 200 no longer needs its own GOSUB 1810.

INCH, from S10, waits for one key and gives its code - 89 for Y and 78 for N, with the capitals on as BASIC starts. Any other key is ignored and it waits again.

Y goes back to line 60, not to the start of the program. The start makes the mouse and cat shapes, which takes a long while and which there is no need to do twice.

Going back does mean every line from 60 on must put the game back as it was at the beginning. Most of it already does: line 60 clears the screen, the field and the cheese are drawn again, the cats go back to their places. Three things did not, and now do:

60 CLS:MAG 2:SPRITE OFF
65 PTS=0:GOT=0:I=0

The score and the count go back to 0; LIVES=3 on line 85 was already below line 60.

And one thing has to move out of the way. Line 65 used to be the DIM for the cheese arrays. BASIC will not DIM the same array twice - the second time is a Dimension Error - so it goes up to line 35, before the restart point, where it happens only once:

35 DIM CHX(5),CHY(5),EATEN(5)

The Code

Add lines 35, 185, 220, 225, 230, 235 and 238, and change lines 60, 65, 200, 210 and 420. Line 210 used to be END; END is now line 238.

35 DIM CHX(5),CHY(5),EATEN(5)
60 CLS:MAG 2:SPRITE OFF
65 PTS=0:GOT=0:I=0
185 IF GOT=5 AND ABS(MX-167)<6 AND ABS(MY-138)<4 THEN 210
200 PRINT @15,4;"GAME OVER":GOTO 220
210 SPRITE OFF 0:PRINT @17,4;"HOME!"
220 GOSUB 1810
225 PRINT @11,6;"ANOTHER GO? Y OR N"
230 A=INCH:IF A=89 THEN 60
235 IF A<>78 THEN 230
238 END
420 I=C:EATEN(I)=-1:PTS=PTS+10:GOT=GOT+1

Starting from

LOAD "MOUSE11" from S28.

What you should see

A complete game. Collect all five pieces of cheese, climb to the hole, and the mouse goes down it:

Home: the mouse has gone down the hole

Press Y and the field is set out again, with the cheese back and three lives:

Another go

Save this. SAVE "MOUSE12".

Change One Thing

  • Move the DIM back: change line 35 to 35 REM and put the DIM at the start of line 65. Win or lose, then press Y. What happens?
  • Change GOT=5 in line 185 to GOT=3. Is the game better or worse?
  • Take SPRITE OFF 0 out of line 210. Where is the mouse when the game says HOME!?

Exercises

29.1 Give a bonus of 10 points for every life left when the mouse gets home.

29.2 Draw the hole in a different colour once all the cheese is eaten, so the player can see it is open. (S18: drawing it again over itself.)

29.3 Keep a best score: after each game, if PTS is higher than the best so far, remember it, and show it on the score line. It must survive "another go" - which line must not reset it?

Worked solutions are in Appendix II.

When It Goes Wrong

SymptomCause
Dimension Error in 65 on the second goA DIM is below the restart point. Move it to line 35.
The mouse sits on the hole and nothing happensSome cheese is still out. Or the mouse is not quite level: check the numbers in line 185.
Another go starts with the old scoreLine 65 does not reset PTS.
The question ignores the keyOnly Y and N are taken; anything else is ignored until one of them is pressed.

Summary

AND joins conditions, and the whole is true only when every part is. A count, not the score, says when all the cheese is gone. INCH waits for an answer. Starting again from the middle of a program means every line from there on must put things back as they were at the beginning - and nothing there may be a DIM, which can only happen once.

Next

S30, Noise - a squeak for the cheese, a sound for the jump, and a jingle for getting home.

Get the Newsletter

New guides, disk images and community finds, roughly once a quarter. No spam, we promise, this isn't Tatung's marketing department.
Your subscription could not be saved. Please try again.
Your subscription has been successful.

Newsletter

Subscribe to our newsletter and stay updated.