6

h_rule.jpg

Making Your Player
Dance



Next you will learn how to add life to your player by making his or her legs move. You'll also be able to create other kinds of movements as desired. In addition, you'll learn how to take care of those troublesome errors that occur when you move a player too far off the screen.

Let's start by animating those legs.

DEFINING IMAGES

To make our player's legs move, we need to define three player images, which we will call "LEGS1$, LEGS2$, and LEGS3$."

Here's the first image for our player along with the data needed to create her. (Notice that we have omitted the leading and trailing zeros.)

ch6_p1.gif

And here's the second image with accompanying data.

ch6_p2.gif
ch6_p4.gif

ANSWER

 

28,28,8,28,58,89,24,56,72,132,130

 

INITIALIZING THE IMAGE STRINGS

Now that we've designed the images, we need to initialize our player image strings. Remember how we do it? To initialize our first image, we use a line like this:

11060 FOR I=1 TO 17:READ A:LEGS1$(I,I)=CHR$(A):NEXT I
ch6_p3.gif
ANSWER

 

So the player will erase herself when we move her up or down. By using three leading and trailing zeros we can bump the player up three bytes and still get a clean erase.

 

Now to initialize our second image we simply insert line 11065, which is almost identical to line 11060:

11065 FOR I=1 TO 17:READ A:LEGS2$(I,I)=CHR$(A):NEXT I

Of course we also need to add the data for LEGS2$. We do that on line 11310:

11310 DATA 0,0,0,28,28,8,28,58,89,24,40,76,68,68,0,0,0

11067

11320

ANSWER

 

11067 FOR I=1 to 17:READ A:LEGS3$(I,I)=CHR$(A):NEXT I

11320 DATA 0,0,0,28,28,8,28,58,89,24,56,72,132,130,0,0,0

 

Oh yes, another thing. We need to dimension our new strings. We can easily do this on line 11050:

11050 DIM LEGS1$(17),LEGS2$(17),LEGS3$(17)
ANSWER

 

We'll want to move the data into Player0$, which is the PM memory area for PLAYER0.

 

AN EXPERIMENT

First, enter the last program from the previous chapter into memory. Then make these changes:

1. Delete lines 4000 through 4020 and remove GOSUB 4000 from line 1.

2. Change line 11050 so it reads:

11050 DIM LEGS1$(17),LEGS2$(17),LEGS3$ (17)

3. Add lines 11060, 11065, and 11067:

11060 FOR I=1 to 17:READ A:LEGS1$(I,I)=CHR$(A):NEXT I

11065 FOR I=1 to 17:READ A:LEGS2$(I,I)=CHR$(A):NEXT I

11067 FOR I=1 to 17:READ A:LEGS3$(I,I)=CHR$(A):NEXT I

4. Add these data lines:

11300 DATA 0,0,0,28,28,8,28,58,89,24,60,36,36,102,0,0,0

11310 DATA 0,0,0,28,28,8,28,58,89,24,40,76,68,68,0,0,0

11320 DATA 0,0,0,28,28,8,28,58,89,24,56,72,132,130,0,0,0

5. Change line 200 so it reads like this:

GOSUB PEEK(JOYSTICK):GOSUB MOVELEGS:GOTO 200

6. Insert at line 10050:

MOVELEGS=30:SP=1:PR=8

7. Add a subroutine at line 30 as follows:

30 POKE HPOSP0,X0:PLAYERO$(Y0)=LEGS1$:PLAYERO$(Y0)=LEGS2$:

31 PLAYER0$(Y0)=LEGS3$:RETURN

8. Change line 13040 so it reads:

PLAYER0$(Y0)=LEGS1$

Notice that we are now moving each of the three images, in turn, into the PM memory area.

ANSWER

 

(I trust you have tried the experiment by now. If you haven't you might want to do that now. You'll learn more that way, honest.) Well, the player's legs are moving all right, but they're moving too fast! We need to slow them down. First, delete lines 30 and 31 and I'll show you how.

 

One way is to set up a counter. We'll use the variable Z for this. To increment the counter we simply put the statement "Z=Z+1" at the beginning of line 30.

Remember that our animation routine is based on a loop. Each time we pass through the loop, variable Z will be increased by one. Also, our player will be moved to a new position based on the new horizontal and vertical coordinates. Suppose we want the first image to be displayed during the first three times through the animation loop. Then we might use an IF-statement at the end of line 30. Line 30 would then look like this:

Z=Z+1:POKE HPOSP0,X0:IF Z<4 THEN PLAYER0$(Y0)=LEGS1$:RETURN

Notice the "RETURN" statement that sends control back to the main loop at line 200.


ANSWER

 

31 IF Z<7 THEN PLAYEROS(Y0)=LEGS2$:RETURN

 

Following the same pattern, we might want to display image three during loops 7 through 9. We don't need an IF-statement this time. If Z is 7 or greater, control will simply pass to line 32. At line 32 we simply set PLAYER0$ to the third image. However, when X=9. we will want to reset Z to 0 so that next time image one will be displayed.


ANSWER

 

Here's how I did it:

32 PLAYER0$(Y0)=LEGS3$:IF Z=9 THEN Z=0:RETURN

 

ANSWER

 

Since Z is not equal to 9, control will pass to the next statement, whatever it may happen to be, and we will never return from the subroutine. This will lead to problems! Consequently, line 33 needs to be "RETURN" so that control will return to the main loop.

 
Try it. Make sure lines 30 through 33 read like this:
30 Z=Z+1:POKE HPOSP0,X0:IF Z<4 THEN PLAYER0$(Y0)=LEGS1$:RETURN
31 IF Z<7 THEN PLAYER0$(Y0)=LEGS2$:RETURN
32 PLAYER0$(Y0)=LEGS3$:IF X=9 THEN X=0:RETURN
33 RETURN

ANSWER

 

The player's legs move even when he is standing still! Fortunately, this is easy to fix. Just change line 15 to read:

15 POP:PLAYER0$(Y0)=LEGS1$:GOTO 200

 

Control is passed to line 15 when the joystick is not moved--when the player is stationary. So we can use this line to display a single image and then jump back to line 200. The POP command serves to cancel the GOSUB that passed control to line 15. By using "POP" we make it "legal" to use the "GOTO 200" statement. Without the POP statement, you would eventually end up with an error message as a result of "GOTO 200." That's because BASIC is set up to expect a RETURN statement at the end of a subroutine.

Try adding line 15. If everything goes well, you will find that the player's legs move only when you move the joystick! If you want, you can experiment with a different "standing still" image of the player. For example, you might code line 15 like this:

15 POP:PLAYER0$(Y0)=LEGS2$:GOTO 200

With this statement, image 2 will be displayed when you leave the joystick in the upright position. The program will now work fine. If yours doesn't, you may wish to compare it with the listing at the end of this chapter.

TRAPPING ERRORS

I promised you I'd explain how to handle those error messages that pop up when you move the player too far off the screen. Here's one simple way:

1. Change line 1 so it reads like this:
1 TRAP 3000:GOSUB 2000:GOTO 200
2. Add line 3000:
3000 PLAYER0$=BUFFER$:Y0=128:X0=80:Z=0:TRAP 3000:GOTO 200

The TRAP at line 1 says. "Trap any error you find. Don't print an error message. Instead, just GOTO line 3000.


ANSWER

 

Erase the player image from the screen. (BUFFER$ is set to zeros, so setting PLAYER0$ equal to BUFFER$ is the same as setting PLAYER0 to zeros. That, in effect, erases the PLAYERS image.)

 

After that, the variables Y0 and X0 are set to some specific values. The value of 128 for Y0 hides the player off the bottom of the screen. The value 80 for X0 puts the PLAYER$ at the left of the center of the screen. The result is that if you move the player too far off the screen in any direction, she will be automatically repositioned off the bottom of the screen on the left side. To bring her back,just push forward on the joystick and she will scoot up onto the screen.

The idea here is that the error condition is caused by a bad value in either X0 or Y0. To fix that, we simply trap the error and reset X0 and Y0 to any desired "legal" values. In addition, we reset the leg movement counter (Z) to zero.

Also, notice that we repeat the TRAP command at line 3000, so that if another error occurs, we will again branch to line 3000. (A TRAP statement must be reset after an error occurs.)

If you haven't yet got your player's legs to move, now's the time. You may wish to refer to the following program, which is a complete listing of all the lines needed to make your player dance!

Once you have it working, you may wish to animate your player in other ways. For example, you might want to add some additional images to make the legs move more smoothly--or you may wish to have the player move her arms or other body parts. Have fun!

LEGS.BAS
Download (Saved BASIC)
Download / View (Listed BASIC)


Return to Table of Contents | Previous Chapter | Next Chapter