Putting it all together.
Now we have to put this machine code routine in an OPL framework.
The machine code is only 73 bytes long, so we can easily put this in a single
string variable.
SHOOT: LOCAL MC$(73),A%(3),A$(80) LOCAL X%,Y%,P%,B%,C%,E%,F%,K%,S% LOCAL H%,W%,L%,D% H%=4 W%=20 REM Screen dimensions. Use 2 and 16 for a CM or XP Psion. MC$=CONV$:("18EC02DD41EC04DD437F00457F0046EE00E60027353A37A600") MC$=MC$+CONV$:("914326278620A700174C904222FC271BA6018120271191442611") MC$=MC$+CONV$:("8620A701D6414FD345DD4520049643A70133095A26CDDE4539") REM Initialise machine code. A%(1)=ADDR(A$) A%(2)=256*10+W% A%(3)=256*%-+%< L%=H%*W% A$=REPT$(" ",L%) REM Screen is empty at first. D%=4 S%=0 X%=1 Y%=1 P%=A%(1)+1 REM Your ship is at coordinates X%,Y%. P% is its position in A$. D% is REM the number of characters between each object. S% is your score. F%=0 E%=0 DO REM If it's time, put in a star or alien at right of random line. IF E% E%=E%-1 ELSE E%=D% B%=(1+INT(RND*H%))*W%+A%(1) IF RND>.5 POKEB B%,%* ELSE POKE B%,%< ENDIF REM Poking is quicker than A$=LEFT$(A$,..-1)+"*"+RIGHT$(A$,L%-..) ENDIF REM Put in your ship POKEB P%,%> REM Put in a missile if you pressed fire IF F% IF PEEKB(P%+1)=32 POKEB P%+1,%- ENDIF F%=0 ENDIF REM display AT 1,1 PRINT A$; REM Remove your ship from the string. POKEB P%,32 REM Clear left column of screen, score -100 if alien slipped by. B%=H% C%=A%(1)+1 DO IF PEEKB(C%)=%< S%=S%-100 ENDIF POKEB C%,32 C%=C%+W% B%=B%-1 UNTIL B%=0 REM Scroll whole screen left A$=RIGHT$(A$,L%-1)+" " REM Move missiles right twice, adding hits to your score S%=S%+USR(ADDR(MC$)+1,ADDR(A%())) S%=S%+USR(ADDR(MC$)+1,ADDR(A%())) REM The program is actually a bit too fast! PAUSE 2 REM Change coordinates if movement key pressed K%=KEY IF K%=3 IF Y%>1 Y%=Y%-1 P%=P%-W% ENDIF ELSEIF K%=4 IF Y%<H% Y%=Y%+1 P%=P%+W% ENDIF ELSEIF K%=5 IF X%>1 X%=X%-1 P%=P%-1 ENDIF ELSEIF K%=6 IF X%<W%-3 X%=X%+1 P%=P%+1 ENDIF ELSEIF K%=%S F%=-1 ENDIF REM Loop until you hit something UNTIL PEEKB(P%)<>32 WHILE KEY :ENDWH AT 4,2 PRINT "SCORE:";S% GET
If you wish you can now put more frills on the game like better graphics, sound effects, an option to play again etcetera.
If you are really ambitious, you might try the following:
Define the UDG's to show one object in the top half, and another in the bottom
half. There are not enough UDG's to make the stars and the aliens look
different, but you can differentiate between those and missiles.
You only need 7 UDG's for this, because there can only be one star or one
alien per column. The eighth UDG can then be redefined during the game to
display your ship. To display the screen you will have to write a small
machine code routine to convert the string of characters in A$ used in the
program above, to a string to be printed on screen. This would allow you to
have eight lines to play the game on (or 4 lines if you can manage it on a CM
or XP).
You could write further code to move objects around. Maybe the aliens could move up and down, shoot missiles of their own (which would have to be a different character to '-') etc.
I hope you have found it useful to see how to program machine code, and much more importantly, how to plan such a program. I'd also like to wish you the best of luck in your own future programming projects.
Jaap Scherphuis.