A complete game using machine code.
Let's write a complete arcade game... a typical sideways scrolling shoot-'em-up. Before starting to write this there are several things to think about first. We can't just pick up the Psion and program it straight away, we need to plan out the game as much as possible before even touching the organiser.
Is it worth writing partly in machine code? If so, which parts?
The main reason for writing in machine code is speed. Is there anything in this game that
would tax an OPL program too much? There will be many moving objects in such a game (you,
your missiles, aliens, stars). An OPL program would be slow if there are too many. Also
the speed would vary depending on how many objects there are unless it is slowed down even
more when there are few objects. Therefore it will be worth programming the movement of
most of the objects in machine code.
How will I store the data of the game?
The number of objects on the screen will vary. We could have an array of object
coordinates, and keep track of what each object is and where. The difficulty of this is
that detecting collisions is quite awkward.
Another way is to have a string containing the screen contents, and simply have the MC
search through that to find and move objects. Each time the string has been updated, it
can be printed on the screen to replace the previous game position. This is the method we
will use.
Here are all possible objects on the screen, and their appearance.
You: > Your missile: - Alien: < Star: * Empty space:
Now lets consider what movements must be done at each loop in the main program:
1) Your missiles move to the right.
2) The whole screen except for you and your missiles scrolls one character to the left.
3) You move and fire, depending on the key presses.
For each of these we could write a short MC routine, but this is not necessary. Part 3, the up and down movement of your ship (and shooting off the missiles) can be left to the OPL program. In fact we may simplify the other routines by not including our ship in the screen string at all, and write all its movement and collision detection in OPL.
Actually, we can simplify things even further. The second part, the scrolling of the screen can also be managed by first scrolling everything to the left, and then moving your missiles to the right. The movements needed are now:
1) Your missiles move to the right.
2a) The whole screen one character to the left.
2b) Your missiles move to the right, as in step 1.
The scrolling of the screen can be done from OPL using ordinary string functions. This leaves only one machine code routine, used to move missiles to the right.
Now comes the tough job of writing the code. Before doing so, we must think
about what information must be passed to and from the missile routine. It
needs the string containing the screen info, the dimensions of the screen,
and the characters used for each object. By passing these we can use the same
code for all types of Psion, and change the shape of the objects later (for
example replacing them by UDG's), without having to change the code at all.
The routine must return how much the score is changed, by hitting aliens.
Therefore it will also need to know how much the score changes for a hit.
Lets put all this information in an array A%()
A%(1)=ADDR(A$) 'A$ is the screen string A%(2)=20 'Length of a screen line on a Psion LZ. 'For a Psion CM or XP, use 16 instead. 'The height of the screen is not needed. A%(2)=A%(2)+256*10 'The score for a hit. A%(3)=256*%-+%< 'Missile and alien shape.
The shape of a star isn't needed, because any object that a missile hits that isn't an alien will have to be a star.
The value of the USR(ADDR(MC$)+1,ADDR(A%())) function will be the total change in score.
We are now ready to write the routine - in Part 4 we'll do just this!