Snippets of Basic Code

These originally appeared on my old Dragon blog - some contributions are attributed to the username Zephyr - you know who you are and thanks again!

Putting Some Words On The Screen

The easiest way is to get something on the Dragon's text screen is the PRINT command:
10 PRINT "DRAGON 32 FOREVER"
You can also add a margin
10 PRINT TAB(5) + "DRAGON 32 FOREVER"
For most control, use the PRINT @ command
10 PRINT @200, "DRAGON 32 FOREVER"
The number after the @ sign is the position on screen. Top left is 0 and the last character on the screen is 511. Try printing your name around the screen.

Graphics - Colourful Big Blocks

The Dragon 32 text screen is the most colourful of the machine's display modes. It provides a grid of 32 columns and 16 rows. Text and blocks can be printed on this screen in the same manner - the graphical blocks are just members of the character set.

This little snippet gives a confetti display:
10 CLS
20 G=RND(512)
30 POKE 1023+G,127+RND(128)
40 GOTO 20

Keyboard Input


The following program shows reading the keyboard using a simple method. Each key press is discrete and you have to continually repress the key to move the asterisk character around.
10 CLS
20 X=0:Y=0
30 POKE 1024+((Y*32)+X),ASC("*")
40 A$ = INKEY$
50 IF A$ = CHR$(10) THEN GOSUB1000:Y = Y + 1
60 IF A$ = CHR$(94) THEN GOSUB1000:Y = Y - 1
70 IF A$ = CHR$(9) THEN GOSUB1000:X = X + 1
80 IF A$ = CHR$(8) THEN GOSUB1000:X = X - 1
110 IF Y>15 THEN Y=15
130 IF X>31 THEN X=31
200 GOTO 30
1000 REM HIDE IT!
1010 POKE 1024+((Y*32)+X),ASC(".")
1020 RETURN
More useful in some situations is to allow the user to press and hold the button for continuous movement. To do this we rely on some PEEK commands which read the keyboard activity straight from the Dragons memory.
10 CLS
20 X=0:Y=0
30 POKE 1024+((Y*32)+X),ASC("*")
40 PE = PEEK(135) : PR = PEEK(337) : IF PR = 255 THEN PE = 0
50 IF PE = 10 THEN GOSUB1000:Y = Y + 1
60 IF PE = 94 THEN GOSUB1000:Y = Y - 1
70 IF PE = 9 THEN GOSUB1000:X = X + 1
80 IF PE = 8 THEN GOSUB1000:X = X - 1
110 IF Y>15 THEN Y=15
130 IF X>31 THEN X=31
200 GOTO 30
1000 REM HIDE IT!
1010 POKE 1024+((Y*32)+X),ASC(".")
1020 RETURN

Sound and Music

Make your Dragon Roar! Sound Command
10 SOUND 255,1
The first value is pitch and the second is duration. This is most useful for notifications and small sound effects. Play Command
10 PLAY "ABCDEFG"
The play command acts more like a regular instrument with the standard notes. You can even change octave:
10 PLAY "O1ABCDEFG"
20 PLAY "O4ABCDEFG"
You can also change the tempo.
10 PLAY "T255ABCDEFGABCDEFG"
Advanced Sound

For more sophisticated music, DAMS and Composer were software packages that gave the effect of four different notes being played at the same time.

Joysticks

Joysticks are probably the most important input device for the Dragon for a number of applications from graphics to games. The Dragon supports two joysticks and they are named the left and right joysticks according to the port they are plugged into.

The Dragon's BASIC has functions for reading the joystick position. Oddly we are left peeking memory directly to decipher whether the button has been pressed.


10 CLS
20 PRINT @1, "right joystick"
30 PRINT @32, JOYSTK(0)
40 PRINT @64, JOYSTK(1)
50 RB=1:IF PEEK(65280)AND2 THEN RB=0
60 PRINT @97, "button";RB
70 REM
80 PRINT @161, "left joystick"
90 PRINT @192, JOYSTK(2)
100 PRINT @224, JOYSTK(3)
110 LB=1:IF PEEK(65280)AND1 THEN LB=0
120 PRINT @257, "button";LB
130 GOTO 20
The JOYSTK function returns a value from 0 to 63. 0 and 1 cover the right joystick position horizontal and vertical respectively. Similarly 2 and 3 the left. Value 31 is the centred position. Please note the above screenshot was from an emulator and my XBox controller does not quite match a regular Dragon 32 joystick in the values it returns! Thanks to zephyr for the simplified button code!

HiRes Graphics


To start with we have a demo of how to draw CIRCLEs and how to PAINT them. This program uses PMODE 3 which has a resolution of 256 columns and 192 rows. In this mode (selected by PMODE in line 20) you can use four different colours including the background colour. This mode is often used in arcade games.
10 X=RND(-TIMER):Z=0
20 PMODE 3,1:SCREEN 1,Z:PCLS
30 TIMER=0
40 X=RND(256):Y=RND(192):R=RND(20):C=RND(4)
50 CIRCLE(X,Y),R,C
60 PAINT(X,Y),C,C
70 IF TIMER/50 < 300 THEN 40
80 IF Z=0 THEN Z=1 ELSE Z=0
90 GOTO 20
A few other commands are worth noting. SCREEN switches between screens such as the text screen and the graphics screen. PCLS clears the current high resolution screen.