Creative Programming for Young Minds ...ontheTI 99/4A
Volume VII
by Leonard Storm
•^^-•v:'.. I
'
CZ7"
'-' S
i.1.n'Vi',1. r
Zi - • \!z7
'y^
«v V '.fi ?•'..''.
czr
n—./^ay--n-
Jit
»
^r^ r ^
© 1982, CREATIVE Programming, Inc., Charleston. IL 61920 A Subsidiary of RV Weotherford Co.
® A registered trade mark of Texas Instruments, Inc.
CREATIVE PROGRAMMING FOR YOUNG MINDS
...ON THE TI-99/4A VOLUME VII
TABLE
LESSON #24
LESSON #26
THE COLORED
VIOLET
CONTENTS
SPRITES CALL SPRITE CALL DELSPRITE
294
CALL MAGNIFY (2) CALL MAGNIFY (1)
294 294
CALL POSITION CALL MOTION
298 298
::
LESSON #25
OF
(double colon)
290
290
298
CALL COINC CALL LOCATE CALL PATTERN
300 305 308
CALL DISTANCE
310
CALL MAGNIFY (3) CALL MAGNIFY (4)
313 313
DISPLAY DISPLAY AT
315 315
BEEP ERASE ALL SIZE USING IMAGE ACCEPT AT VALIDATE
316 316 316 318 319 321 322
SUB
327
CALL subprogram
327
SUBEND SUBEXIT ON ERROR RETURN number RETURN NEXT
327 331 334 334 335
PAGES
PROJECTS
290
LESSON #24:
SPRITES
CONGRATULATIONS, TI-99/4A MASTER PROGRAMMER! VOLUME VI
YOU HAVE COMPLETED
AND ARE
READY
SPRITELY ADVENTURES
FOR THE
AHEAD.
IN
THIS VOLUME, WE WILL EXPLORE SOME CAPABILITIES OF THE TI
EXTENDED
BASIC COMMAND MODULE.
TO BEGIN,
INSERT THE EXTENDED BASIC MODULE INTO THE ON
COMPUTER AND
THEN TURN
YOUR COMPUTER.
In this lesson, you will learn how to create smoothly mov
ing graphics characters called sprites.
These sprites are
defined using the CALL SPRITE subprogram.
The format of
the CALL SPRITE subprogram is shown below:
CALL SPRITE
(^number, character, color, dotrow, dotcol ,rowvel, col vel)
The first variable, number, is a numeric expression from
1 to 28. one time.
That is, up to 28 sprites can be defined at any The # sign must be present in front of the
numeric expression.
The second variable, character, is the character code which
defines the shape of the sprite. j^v
integer from 32 to 143.
This number may be any
291
The third variable, color, defines the foreground color of
the sprite.
The background color of a sprite is always
transparent. to
COLOR may be any numeric expression from 1
16.
The next two variables determine the starting position
Dotrow is a number from 1 to 256 (but 193
of the sprite.
through 256 are off the bottom of the screen) and dotcol is a number from 1 to 256.
The D0TC0L=1 ,D0TR0W=1 position
is the upper left-hand corner of the screen.
DOTROW and
DOTCOL actually specify the position of the upper left-hand corner of the character or characters which define the sprite.
The last two variables of the CALL SPRITE subprogram are
optional.
If present, they specify the row and column
velocity of the sprite.
If these variables are not present,
then the sprite will be stationary.
Rowvel and colvel may
be any number from -128 to 127.
Now type the following program into the computer and RUN
it:
10
CALL CLEAR
20
INPUT
"CHARACTER ":Z
30 CALL SPRITE (#1, Z,16,80,80,10,10) 40
GOTO
10
292
Input the following numbers and record the sprite shape
CHARACTER
SHAPE
65
34
98 72
Notice that once the sprite is defined in statement 30, it continues to move with the specified velocity until the sprite parameters are changed.
Also, notice that every time you run the CALL SPRITE
subprogram, the new character starts again at dot row 80
and dot column 80.
Next, change statement 30 to:
30 CALL SPRITE(#1,66,16,80,80,X,Y) and change statement 20 to: 20 INPUT "XVEL,YVEL":X,Y
RUN the program.
On the next page, INPUT the values listed for XVEL and YVEL.
293
XVEL
YVEL
0
0
10
0
-10
0
0
10
0
-10
-30
-30
DIRECTION OF MOTION (Draw an arrow.)
Now change statement 20 to: 20 INPUT
"SPRITE NUMBER":S
and change statement 30 to: 30 CALL SPRITE(#S,66,16,80,80,10,10) jfPN
Next, RUN the program.
Input the following numbers (one
at a time):
S = l, 1, 1, 2, 3, 28, 15, 4, 5, 9, 8, 7
Note that entering the same number several times redefines
the same character and starts its motion at the position DOTROW=80 and DOTCOL=80.
After you enter the numbers above, you should have 10 sprites moving diagonally across the screen.
What happens if you input a number outside the range from 1
#^v
to 28?
294
Now change statement 30 to:
30 CALL SPRITE(#S,66+S,16,80,80,0,10)
RUN the program and input the following numbers: S =1,
3, 9, 21
When you can see all the sprites on the screen at one
time, enter another sprite, say S = ll.
Notice that when you entered the last sprite, #11, one of the other sprites disappears.
The sprite that has
disappeared still exists, it's just invisible.
The rule
is:
#^
ONLY 4 SPRITES WILL BE VISIBLE ON ANY ONE ROW OF THE SCREEN.
THE LOWEST NUMBERED 4 WILL BE VISIBLE.
BERED
SPRITES.
HIGHER NUM
SPRITES WILL BE INVISIBLE
Now add the following program statements: 25 IF S=0 THEN 45
45 INPUT "DELETE SPRITE":M
50 CALL DELSPRITE(#M) 55
GOTO
10
RUN the program again and input: S = 21,
3, 9, 1.
Then input S =11 and observe that sprite #21, the W dis appears .
295
Next, input S = 0 and when you get the DELETE SPRITE
message, input a 1.
This causes sprite #1 to be undefined.
It disappears because it no longer exists.
Now, the W
reappears since only four sprites exist on that row.
THE CALL DELSPRITE COMMAND IS USED TO DELETE CURRENTLY DEFINED
SPRITES.
Now change the program again.
Change statement 30 to:
30 CALL SPRITE (#S,66+S,16,80,80,-10,0)
Input as many sprites numbers as you wish. none of the sprites disappear.
Notice that
All 28 sprites can exist
and be visible in any one column. Now add the following program statement: 1 CALL MAGNIFY(2)
RUN the program again and input any numbers that you wish. Notice that the CALL MAGNIFY(2)
statement causes all the
sprites to have twice the size that they had before. Finally, change statement 1 to: 1 CALL MAGNIFY(1)
RUN the program again and notice the size of the sprites.
r
296
Instead of using the standard character set, let's define
a sprite of our own design.
It's really quite easy to do.
We just use the CALL CHAR subprogram A program example is shown below.
Type it into the com
puter and then RUN it. 10 CALL CLEAR
20 CALL CHAR(96,"3C7EE7DBDBE77E3C")
30 CALL SPRITE(#1,96,7,60,80,16,15) 40 GOTO 40
Now add the following program lines: 40 CALL CHAR(40,"FFFFFFFFFFFFFFFF") 50 CALL C0L0R(2,11,11) 60 CALL HCHAR(10,1,40,32) 70 GOTO
70
Rl)N the program. Here's how the program works:
Statement 20 defines the sprite character shape. Statement 30 defines the sprite characteristics.
The
sprite is to be red, start at DOTROW 60 and DOTCOL 80, and
move with speed 16 downward and 15 to the right. Statement 40 defines character 40 to be a solid square.
Statement 50 causes set 2 (which contains character 40) to be colored dark yellow.
Statement 60 prints 32 of the yellow squares in a horizontal line beginning at row 10 and column 1.
297
Notice that the sprite that was defined appears to move
over the dark yellow band.
What change would you have to make to cause the sprite to have twice the size it has now?
Include a
line in
your program which will make the sprite twice as large.
Write your program line: Now change statement 30 to: 30 CALL SPRITE (#1,96,7,60,80,16,15,#2,96,7,62,100,16,15)
RUN the program again. Notice that statement 30 defines two sprites at once.
See if you can alter statement 30 so that sprite #1 is black and sprite #2 is red. on the line below.
Show the revised statement 30
Then RUN the program to check it out.
Now let's write a bouncing ball program.
Later we will
add a paddle and make a game out of it. Type the following program code into your computer: 10 CALL CLEAR
20 A$="3C7EFFFFFFFF7E3C"
30 CALL COLOR(2,5,8) 40 CALL SCREEN(8)
50 B$="FFFFFFFFFFFFFFFF"
60 CALL CHAR(40,A$)
70 CALL COLOR(3,2,8) 80 CALL CHAR(48,B$)
298
Statement 20,
30, and 60 define the ball character.
Statement 50,
70, and 80 define the character which will
be used as a
border.
Also type in the following lines: 90 CALL HCHAR(1,1,48,64) 100 CALL HCHAR(23,1,48,64)
110 CALL VCHAR(1,1,48,48) 120 CALL VCHAR(1,31,48,48) 130 CALL MAGNIFY(2)
140 CALL SPRITE(#5,40,7,80,80,8,10) Statements 90 through 140 print a solid border around the screen.
Statement 130 causes sprites to be double sized and statement 140 prints the ball sprite on the screen.
The
sprite is started at position DOTROW =80 and DOTCOL =80.
Its speed is 8 units downward and 10 units to the right. Now type in the following code: 150 AX=8::AY=10::VX=8::VY=10
160 CALL POSITION(#5,X,Y) 170 IF X>160 THEN VX=-AX ELSE IF X220 THEN VY=-AY ELSE IF Y160,
then the
sprite has hit the screen border on the bottom.
then the sprite has run into the top border.
If X