Atari BASIC Learning by Using
Atari BASIC Learning by Using
Atari BASIC Learning by Using
Rowley
Thomas E. Rowley
ISBN 0-936200-35-9
(previously) ISBN-3-92-1682-86-X
10 9 8 7 6 5 4 3 2 1
Copyright
1983 by IJG
Inc.
Credits
Preface
_.
CO
:l
en
_.
c:
'<
CO
:l
:l
CD
rm
rn
o
l>
::Jl
-m
l>
Table of Contents
Introduction ...... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Chapter 1
Atari .. .. . .... .............. .... .. ............ . . .. . . . . . . . .... 3
3
3
4
6
7
8
9
Chapter 2
Special Sounds . . . .... .. . ...... . . . ..... ... . .. .. ... . .. ....... 12
12
14
16
18
Chapter 3
Keys, Paddles and Joysticks . . .. .. ... . . ... . .... . .............. 2 1
22
23
25
26
27
28
Chapter 4
Specialized Screen Routines . ..... . . . ........... ......... . ... . 30
30
33
35
39
Chapter 5
Graphics and Sound Application . .. . ...... ... .... .. . .. . ... . . .. . 42
43
44
45
47
49
Chapter 6
PEEKS, POKES and Special Stuff . ... .. . . . ..................... 53
Appendix 4
Introduction
This is an "ACTION" book. You do more than read it. You use it,
discover with it and create with it. Learn Atari BASIC through the short
programs provided. The examples challenge you to change and write
programs on your own. They are a great source of instruction for any
teacher or student.
Enough words, let's have some "ACTION." Sit down to your Atari
computer and "Learn by Using."
ATARI400
1
ATARI 800
Atari
Screen Drawings
This set of programs for "Screen Drawings" provides a good starting
point to learn Atari graphics.
Try to become proficient in the use of the following Atari statements
used in this section.
COLOR
SETCOLOR
GRAPHICS
PLOT
DRAWTO
German Flag
This routine provides a short example of using COLOR, PLOT and
DRA WTO in the GRAPHICS 3 MODE. It's simp le because the German
flag is so easy to draw.
1.0.0
11.0
12.0
13.0
14.0
15.0
16.0
17.0
18.0
19.0
2.0.0
21.0
22.0
1.
2.
3.
4.
Figure 1.1
German Flag
Design
Take a close look at this one. Colors are produced in GRAPHICS 8 by
selectively drawing lines on the screen. By taking advantage of the
physical separation between color dots on the television screen, the three
primary colors can be individually turned ON and OFF.
The line shown in the diagram would be drawn (red). A series of lines
drawn adjacent to one another would turn on all of the color dots to
produce (white) .
Figure 1.2
8888888
8 888888
888 8888
8888 888
R=red, B=blue, G=Green
Suggestion:
1.
Circle
Plot a circle. You choose the radius (size). The trigonometric functions
sine (SIN) and cosine (COS) are used in line 60 to calculate a position on
the circle.
Here is a quick definition of how sine and cosine relate to a position on a
circle.
--
Figure 1 .3
Plot a Circle
ANGLE
Suggestions:
1.
70 PLOT X+60,Y+40
2. You can move the Y axis by changing line 40, try it.
3. How could you fill in between the dots to make the circle a
continuous curve?
Star
This program draws a Star of Solomon. Simple changes have some
interesting effects on the size and shape. See the program "CIRCLE" for
a definition of the SIN and COS functions.
10
15
20
22
25
30
40
50
55
60
70
Figure 1.4
Star of Solomon
Suggestions:
1.
Title Page
Put a title page on the front of your programs. It can make a good
program look even better. Try this one for practice with large characters
in different colors.
10
20
30
40
50
60
70
Figure 1.5
Title Page
Brown background
COVER
PAGE ~_-I--_
Green let ters
By Don Smith
Suggestions:
1.
25 SETCOLOR 2,13,2
65 POKE 752,1
90 GOTO 90
2. Make the title a different color changing the program title
in lines 40 and 50 to upper-case or inverse video.
3. Try making a Litle page in GRAPHICS l.
4. Make a screen likE' the one to the right. You can change
the color of a character with the SETCOLOR statement.
Symbols in GRAPHICS 2
Make a graphics design on the screen. You can use special symbols in
GRAPHIC MODES 1 and 2. Just change the value in the character base
to 226 (POKE 756,226).
Figure 1.6
Change the color by using inverse video characters. Add 128 to the
numbers inside the CHR$ commands in line 50.
Suggestions:
1.
Change line 50 to
50 7#6;CHR$(8);CHR$(138);CHR$(10);
2. Try it in GRAPHICS 1.
3. If you liked that, try a new design with some other graphic
symbols.
10
Special Sounds
Figure 2.1
Screen Drawings
Sou nd Effects
A variety of sound effects can be created using the SOUND statement.
The changing patterns of pitch and volume determine the special quality
of the sound. Here are some simple sound effects. First, try the "Dropped
Coin."
12
1~~
1~5 P=6~
11~ FOR J=l~ TO ~ STEP -~.2
115 REM INCREASE VOLUME
12~ FOR V=l TO J :SOUND .P.1~.V:NEXT V
122 REM DECREASE VOLUME
125 FOR V=2*J TO 1 STEP -l:SOUND ~.p.10. V:NEXT V
13~ NEXT J
15~ END
The volume goes UP and DOWN while the length of each cycle
decreases.
Here is a good imitation of a European police siren.
13
1~
2~ P=INT(RND(~)*6)+25~
3~ V=INT(RND(~)*4)+6
4~
5~
6~
SOUND ~,P,14,V
FOR X=1 TO 1~:NEXT X
GOTO 1~
You can call this one random noise, or perhaps space age music.
1~
2~ P=INT(RND(~) *256)
3~ V=INT (R ND(~)*16)
4~
5~
SOUND ~,P,I~,V
GOTO 1~
Suggestions:
Musical Tune
Listen to the musical tune generated by this routine. A musical note
such as G is assigned a number (G=162) which sets the pitch in the
14
SOUND statement. The data in lines 300 through 320 gives the musical
note and the duration. Run it, and see if you recognize the tune.
1.
130 T=T*10
145 SOUND I,PITCH(X) +2,10,8
2. Try another song such as Row, Row, Row Your Boat.
Change the notes and the duration in the DATA
statements.
15
Figure 2.2
Musical Tune
ANGLE
90
180
210
270
360
Figure 2.3
SIN (ANGLE)
0.000
1.000
0.000
-0 .500
-1. 000
0.000
90
270
16
100
200
205
207
210
215
220
230
240
Suggestions:
17
Audible Joystick
Make your programs communicate with both "sight and sound." Listen
as you make the colored square move horizontally. Before using this
program, you need to plug a joystick into the left hand jack of the
computer (STICK (0)).
Figure 2.4
Joystick Positions
STICK (0) = 14
~------~H-r---------
STICK (0
=13
18
Suggestions:
1.
Figure 2.5
Joysticks
19
Keys
Paddles and Joysticks
Figure 3.1
Screen Drawing
STICK
PEEK
21
Figure 3.2
Cartoon
10
20
30
40
50
70
22
Figure 3.3
Binary Numbers
De ci mal
Number
811
181
118
III
bit 2
Wx2
[Dx2
OJx2
ITJx2
bit I
+
+
+
+
ITIx2
[]]x2
OJx2
OJx2
bit 0
[Ox
ITIx2
+ []]x2
+ OJx2
+
PEEK (53279)
OPTION
~t'LECf
S TA~T
No Switch pressed
Suggestions:
1.
Paddle Motion
Use the paddle to move a colored square horizontally across the
screen. By turning the paddle, a value from 1 to 228 is produced. For this
program the paddle must be plugged into the left hand jack of the
computer.
23
Figure 3.4
Paddle Movement
PADDLE (.0)
10
20
30
40
50
55
60
65
70
80
90
Suggestion:
1.
Experiment
with
the
color.
Change
line
70
to
30
55
60
70
X=0 :C=0
IF PTRIG(0) =0 THEN C=C+l
COLOR C+l :PLOT X,10
COLOR C:PLOT Xl,10
24
Joystick Drawing
Use the joystick to draw on the screen. Here is a diagram ofthe values
the computer returns to STICK(O).
Figure 3.5
Values of STICK(O)
14
)-------7
11--------------;
9
13
25
140
150
160
170
180
190
X=X-Z=10)+(Z=11)+(Z=9
IF X<0 OR X>39 THEN X=39*(X>39)
Y=Y-Z=5)+(Z=9)+Z=13
Y=Y-Z=6)+(Z=14)+(Z=10
IF Y<0 OR 7>23 THEN Y=23*(Y>23)
GOTO 80
The stick position is read in line 110. Lines 130 to 180 change the
cursor position.
The cursor position is calculated using a series of Boolean Algebra
statements. A boolean statement is either true or false. Here is a simple
example of a Boolean statement. The statement B=(Z=7) will return a 1
for the value of B if Z is equal to 7, and will return a 0 for the value of B if Z
is not eq ual to 7.
Add several of these together as in line 130 and you have a powerful
statement.
Suggestions:
1.
TAB
Use the TAB key just as you would on a typewriter. Use it in immediate
mode or build it into your programs. You can clear the old tabs and set
new ones.
26
".
I
To clear a tab press TAB, then control TAB. Repeat this for as many
tabs as you wish to clear. In program mode you can PRINT
CHR$(127);CHR$(158) .
To set a tab press SHIFT TAB after you have moved the cursor to
desired position on the screen. In program mode the CHR$ function can
be used (ie. PRINT" ";CHR$(159).)
Suggestions:
1.
Change line 45
45 PRINT"
";CHR$(l59);"
";CHR$(l59)
Key Control
This subroutine is used to convert all key entries to upper-case normal
video. This demonstration is designed to accept one character at a time
by a GET statement. The variable A holds the Atari ASCII value of the
key pressed. This subroutine is especially useful when only normal
upper-case input is desired. As an example, enter a letter between A and
J. Even if the reverse VIDEO key or CAPS lower key is pressed, the
computer will check and reset itself for upper-case normal video.
Pick a Key
If you are looking for a method to get input from the keyboard while not
stopping the execution of your program, a PEEK to the keyboard might
be your answer. Address 764 holds the keycode of the last key pressed.
The keycode is not the same as the Atal'i ASCII code. Here is a program to
help you find the keycode values.
10
15
20
30
35
40
50
60
70
Suggestions:
28
Specialized
S'c reen Routines
Give your screen displays something extra. Add more colors, change
the Atari character set or perhaps mix several graphic modes.
Figure 4.1
Graphics
Player-Missile Graphics
An entirely new dimension in graphics is possible by using players and
missiles. A player, or a missile is a special graphics design which can be
easily moved from one part of the screen to another. Try it.
30
Figure 4.2
1~~~
1~1~
1~15
1~2~
1~25
1~3~
1~4~
1~42
1~45
Graphic Missile
1~5~ I=PEEK(1~6)-8
1~6~
1~7~
1~9~
1~95
11~~
111~
12~~
121~
122~
123~
14~~
149~
15~~
151~
152~
16~~
31
Line 120 determines the resolution (in this case, single line) . The top
part of memory is used to store the shape of a player. Line 1050 calculates
the beginning of this memory section. A memory map is shown in
Appendix 2. Also, refer to Appendix 1 for a description of "Memory
Addresses. "
Line 1060 is needed to access the player. Lines 1090 to 1110 read the
data and store the player shape in memory. The vertical position on the
screen depends on this data's position in memory. A single POKE in line
1500 changes the horizontal position.
Suggestions:
1.
2.
FOR
N=5~
TO 78
POKE
7~4,134
Figure 4.3
128
Player
64
32
16
32
German Font
This routine is used to modify the Atari character set. Four graphic
characters are changed to represent ii, 0, U, and B .
By pressing CONTROL A, 0, U and S respectively these special German
characters can be generated.
1~
2~~ GERMFONT=28~~~
GOSUB GERMFONT
FOR X=97 TO 122 :? CHR$(X); :NEXT X
520 ? CHR$(1);CHR$(15);CHR$(21);CHR$(19)
6~~ END
28~~~ REM GERMAN FONT . . . . . . . .
280~5 REM CONTROL -a,o,u,s PRODUCES
28~~6 REM GERMAN UMLAUT a,o,u &ss
28007 DIM B$(80)
28~1~ POKE 756,224
28~4~ MEMEND=PEEK(1~6)-4:CHSET-MEMEND:256
5~~
51~
28~65
33
o
u
In line 28060 four pages of memory are reserved for a new character
set. This section of memory is determined by PEEKing the top of
memory, PEEK(106) and subtracting 4 from it. A description of
"Memory Addresses" can be found in Appendix 1.
The machine language routine that executes in line 28090 copies the
Atari character setfrom ROM to the new memory location in RAM. Now
the characters can be modified. Line 28120 is used to read new data, and
change a particular character in the new character set. The data can be
interpreted like this.
Figure 4.4
32
96
128
16~
224
Operation
Value + 64
Value - 32
None
Value + 64
Value - 32
None
31
95
127
159
223
255
The next 8 numbers in the data statement form the character. Each
number represents the decimal equivalent of a byte. Each character is
formed on a 8 x 8 matrix.
Parts of a character are turned either ON or OFF depending on the
number POKEd into the memory address representing that character.
After the character set has been modified, change the character set
pointer by POKE 756,MEMEND in line 28700. A graphics command or
the SYSTEM RESET key will change the pointer back, and return the
characters to their original state. Before executing the routine a second
time you must press SYSTEM RESET or POKE 106, MEMEND+4.
This new character set (font), can be used in a running program or in the
immediate mode.
34
Figure 4.5
128 64
Character 79 - 0
32 16
Note:
64+32+4+2=102
o
60
102
102
60
o
Each line in the character takes 1 byte.
Suggestions:
1.
2.
3.
4.
27000
27010
27020
27025
27027
27030
27035
27040
27045
27050
27055
27060
27065
27070
27072
27073
27075
27080
27082
27084
27090
27095
27100
27105
27110
27115
27120
27122
27125
27130
27135
27140
27150
27160
27200
The computer generates 192 scan lines on the TV screen. A single line
of a particular graphics mode (mode line), is made up of a number of scan
lines. For example, in GRAPHICS MODE 3 eight scan lines are required
to produce 1 mode line. In GRAPHICS MODE 8, only one scan line is
required to produce a mode line.
The computer uses a short sequence of code called the" display list" to
generate these mode lines. To build a custom display list, you need to
modify the existing display list. First, decide on the GRAPHICS MODE S
and their positions on the screen. Then change the mode lines in the
display list to correspond to your screen. This procedure requires several
well planned steps. Refer to Appendix 3 "Building a Display List" for
more details.
Suggestions:
1.
36
Figure 4.6
THE LETTER X
Graphics 7 diagram
Hints:
37
38
1.
39
Figure 4.7
Graphics Screen
H
E
I
G
H
T
A
B C 0
40
41
Graphics and
Sound Applications
This is the section that let's you tie all of your programming techniques
together. Playa game like "DUEL" or "SLOT MACHINE." Watch
"VIDEO ART" or plot an equation with "LINEAR X- Y PLOT."
Figure 5.1
Slot Machine
MAC H
S LOT
YOU NOW HAVE 7 DOLLARS
TO PLA Y
PRESS
mIDI
.:::::.
42
Duel
Use the joystick to duel with an opponent. Eat up your opponents
squares faster than yours are eaten and you win. This game takes two
players. Put the joysticks into the first two jacks.
1~
12
13
14
15
16
17
18
19
2~
21
22
23
25
27
43
317
320
322
325
330
340
350
360
370
390
Suggestions:
1.
Video Art
Sit back and enjoy this one. Letitrun for several minutes and watch the
screen pattern develop.
44
Figure 5.2
Video Art
Suggestions:
1.
120 GO TO 65
2.
GueSSing Game
This is a classic computer game. The computer g~nerates a random
number for you to guess. For a flashy screen, the large characters of
GRAPHICS MODE 2 are used.
46
2.
Slot Machine
This game will not cost you a penny to play. You start with 10 do ll ars.
Each time you press the SELECT button a new set of figures appear. If
all three match, you 're a winner. You will, however find it hard to put your
winnings in the bank.
100
200
210
220
400
405
410
412
413
415
417
420
500
540
600
610
620
710
715
720
730
750
800
805
810
812
815
816
820
840
842
47
1.
48
Linear X- Y Plot
This math program draws lines on an X-Y axis. A full understanding
requires you to be familiar with simple linear equation. Here is a brief
introduction to the equation of a straight line. Y=mX + b is a linear
equation in slope intercept form.
Figure 5.3
i}
value of b is
I
I
iot''''Pt~
chang e in Y
valu e
-------------~
change in X
valu e
__~~----~~-------------------- X
m = s lope
= change in Y
change in X
The line is a set of X and Y values that fit the equation Y=mX +b. In
line 1072, the values of m and b are entered. In line 1080 Y values are
calculated for the set of X values ranging from-80 to +80. In line 1090 the
point is plotted on the screen.
1000
1014
1017
1018
1020
1030
1035
1038
50
Suggestions:
1.
51
PEEKS, POKES
and Special Stuff
PEEK and POKE let you unleash a power not available through other
BASIC commands. With these two key words, it is possible to look
directly into a memory address and change the content of many of the
addresses. PEEK is used to "read" the contents of a memory address.
POKE is used to "write" to an address. Because each address contains a
single byte, the decimal value of the contents will always be between 0
and 255.
Refer to Appendix 1 "Description of Memory Addresses" and the
Atari Basic Reference Manual as you go through this section.
Margins 82,83
POKE 82,20
Change the right margin.
POKE 83,22
POKE things back to normal or press SYSTEM RESET.
Inverse Video 694
10
20
30
40
50
60
DIM Q$(40)
POKE 694,128
INPUT Q$
POKE 694,0
INPUT Q$
END
POKE 752,1
Get it back.
POKE 752,0
53
POKE 755,4
I can't either.
POKE 755,0
Loudspeaker 53279
PRINT PEEK(B4)
What column are you in?
PRINT PEEK(B5)
Recorder 5401 9
POKE 710,12
Change the character luminance.
POKE 709,0
Characters 702
5 DIM Q$(40)
10 POKE 702,0
20 INPUT Q~
30 POKE 70; ,~28
________
40 INPUT Q$--------__=:::; Press some keys
50POKE702~
60 INPUT Q$
70 END
54
10 PRINT PEEK(18)*256*256+PEEK(10)*256+PEEK(20)
20 GOTO 10
Can you make the time print in seconds?
Try this to RESET the CLOCK to O.
5 POKE18,0:POKE19,0:POKE20 ,0
55
56
Appendix 1
Description
18,19,20
65
77
82,83
84
85,86
87
88,89
106
201
559
560,561
649
702
704
705
706
707
708
709
710
711
712
57
752
Cursor inhibit:
O=visible,
1 =invisible
Character mode:
755
1 =Blank, 2=Normal, 3=Flip
Character base register:
756
226= lower-case
224=upper-case
Last key pressed (internal code)
764
Horizontal position of Player 0
53248
Horizontal position of Player 1
53249
53250
Horizontal position of Player 2
Horizontal position of Player 3
53251
Horizontal position of Missile 0
53252
Horizontal position of Missile 1
53253
53254
Horizontal position of Missile 2
Horizontal position of Missile 3
53255
53256-53259 Players 0-3 Size:
o or 2 = Normal size
1 = twice normal size
3 = 4x normal size
Missiles 0-3 sizes
53260
Player Missile enable, 3=on
53277
Recorder motor control line:
54018
52=On
60=Off
54279
Holds page number of Player Missile data
58
59
Append ix 2
Figu re 6. 1
MemoryMap
Single li ne
resolution
Double line
reso lut ion
(POKE 559
~6)
2B~8
Top of lellory
Player 3
1792
Player 2
1536
Player I
128B
Player
e
Player 3
H3
HI
H2
HB
896
Player 2
768
Player I
6~B
Play er B
512
H3
H2
HI
HB
38~
Unused
Unused
Ab so lut e sta rt in g
8 bitswide . .
8 bits wide . . .
60
Notes:
61
Appendix 3
Figure 6.2
Display List
(Mode 0 is graphics 0)
Mode
Mode
Mode
Mode
0 - 2 mode lines
I - 2 mode lines
2 - 2 mode lines
3 - 4 mode lines
63
Table 1.
2
3
4
5
6
7
8
sca n li nes
per mode
lin e
mode
byte
RAM per
lin e
8
8
2
6
16
7
8
9
40
20
20
10
10
20
20
40
40
4
4
2
10
11
13
15
Procedure:
Figure 6.3
0
1
2
3
7
# of scan
lines/ mode
line
# of mode
lines
2
2
2
4
48
x
x
x
x
x
Total #
of scan
lines
8
8
16
16
16
32
32
8
2
96
Total
64
192
Figure 6.4
Mode
2
6
7
8
13
2
3
7
LMS byte = 64
2 = 66
65
Figure 6.5
Graphics Mode
Mode
REM required
8+16
8
7+ 16
7
6+16
6
5+16
5
4+16
4
3+16
3
2+16
2
1+16
8138 bytes
8112
4200
4190
2184
2174
1176
1174
696
694
432
434
420
424
672
674
992
5.
o
1
2
3
POKE OLIST+2,2
POKE OLIST+3,6 :POKE OLIST+4,6
POKE OLIST+5,7:POKE OLIST+6,7
FOR X=OLIST+7 TO OLIST+10:POKE X,8:NEXT X
POKE OLIST+59,65
POKE OLIST+60,PEEK(56)
POKE OLIST+61,PEEK(561)
9.
66
Figure 6.6
Display List
DLIST
-I
66
+~
LO
+1
+2
+3
+4
+5
+6
+7
+8
+9
HI
2
6
6
7
7
8
8
8
8
13
+I~
+11
~
+58
DLIST
Mode
Mode 1
Mode 2
Mode 3
Mode 7
+59
13
65
+6~
LO
+61
HI
Dump Instruction
To write to the screen, first POKE the graphics mode being used into
address 87. For example, if you want to print in GRAPHICS MODE 2
Poke 87 ,2
67
Appendix 4
a) T0=PEEK(560)+PEEK(561)*256
Tl=PEEK (T0+4)+PEEK(T0+5)*256
b) Tl=PEEK(88)+PEEK(89)*256
2. Calculate the amount of screen memory between the
upper-left hand corner and the target position on the screen.
Call this SCRMEM.
To calculate SCRMEM, add up the total memory requirements for
each mode line above the target position and add the horizontal position
of the preceeding value. You will need to know the amount of memory
required for each mode line above the target line. Refer to Table 1 in
Appendix 3.
Example, suppose the entire screen were made of GRAPHICS 2
MODE lines. Suppose you want to print a character 4 lines down, and 2
spaces to the right of the upper-left hand corner. The value for SCRMEM
would be:
In our example:
Tl+62
69
Example:
5
10
20
30
40
50
60
70
DIM Q$(l)
GRAPHICS 0:REM 40 BYTES/ LINE
T1=PEEK(88)+PEEK(89)*256
PRINT"VERTICAL PDSITIDN" :INPUT V
PRINT"HORIZONTAL POSITION" :INPUT H
PRINT"LETTER TO BE POKED" : INPUT Q$
POKE T1+40*V+H,ASC(Q$)-32
GOTO 30
Figure 6.7
o - 31
32
96
128
160
224
Operation
Value + 64
Value - 32
None
Value + 64
Value - 32
None
95
127
159
223
255
70
--- - - -- - - ---,--,..------------
.-
-~~--
v-coa~
_r
prov1dss
cassette
CABDUP~
tapes
available