                        
                         DESKTOP INVADER
    
                    Written by  Ian Wilson



The following description assumes a working knowledge of the RISC OS 
desktop.



Playing the Game

To load Desktop Invader, double click on its icon in the directory 
window and the Invader will appear on the icon bar. Click here to open 
the first screen. You will see forty aliens lined up waiting to 
attack. Your defence is a turret near the bottom of the screen. With 
this you must save the world. You have an unlimited supply of 
ammunition, but three direct hits will destroy your turret. To give you 
some cover there are three houses but these may be destroyed by 15 
direct hits either by bombs dropped by the aliens or by your own 
rockets. 

Press Menu in the Invader's window and select "play." The aliens will 
start moving across and down the screen dropping bombs. To move 
your turret, move the pointer to the left or right icon and to fire a 
rocket, press Select. Every so often, the alien Mother Ship will fly 
across the top of the screen. This is quite difficult to hit with a 
rocket. 

If you allow an alien to reach the ground, you will loose a life. If 
you succeed in destroying all forty aliens, a new wave of attack will 
begin. With each successive wave the aliens start a little lower and 
they drop bombs with greater frequency and greater accuracy.

One point is scored for hitting a green alien, three points for a blue, 
and twenty points for a Mother Ship. The game finishes when you have 
lost three lives. A new game begins when "new game" is selected from 
the menu and this may be done at any time.  

During the game you may return to your more boring desktop applications 
if you must, but the attack will continue unless you select "stop" from 
the menu in the meantime. Selecting "play" will subsequently restart 
the game where you left off.Desktop Invader is quite hungry for 
processor time and you will not get a very good game if you have, say, 
!Maestro running at the same time. But most applications will happily 
sit beside it on the icon bar. 



Typing It In

CreateApp is a BASIC program which when run will create an application 
directory named "!Invader" in the currently selected directory and will 
load into this directory three files: "Sprites","!Sprites" and 
"Templates." If you are horrified at the sight of all those DATA lines, 
use !Paint to make your own sprites (the required sprites are described 
below) and omit from the program lines 90 to 290 and 420 to 2560.

!Boot and !Run files are created as Obey files in !Edit:

     | !Boot file for Invader - Ian Wilson
     IconSprites <Obey$Dir>.!Sprites

     | !Run file for invader - Ian Wilson 1990
     IconSprites <Obey$Dir>.!Sprites
     Set Invader$Dir <Obey$Dir>
     WimpSlot -min 36K -max 36K
     Run <Invader$Dir>.!RunImage

and should be saved in the directory "!Invader."

Finally, type in the program !RunImage and save it in the 
application directory.



The Sprites

The file "!Sprites" contains a single sprite which represents Desktop 
Invader. Mine looks like an eight legged monster with tentacles and a 
nasty grimace. The sprite bears the name "!invader," is 40 pixels wide 
and 20 high in mode 12. It has a mask, and the background is 
transparent.


The file "Sprites" contains 14 sprites, all in mode 12 and without any 
masks. They all have black backgrounds, the importance of which will be 
explained. The sprites are described below, each one after its name:

Gun: Width 32, height 8. A turret shaped object drawn with eight pixels 
of black on either side. See fig. 1. Mine is coloured blue and yellow.

           fig. 1.       XXXXXXXXXXXXXXX  XXXXXXXXXXXXXXX
                         XXXXXXXXXXXXXXX  XXXXXXXXXXXXXXX
           X=black       XXXXXXXXXXXXXX    XXXXXXXXXXXXXX
                         XXXXXXXXXX            XXXXXXXXXX
                         XXXXXXXXXX            XXXXXXXXXX
                         XXXXXXXX                XXXXXXXX
                         XXXXXXXX                XXXXXXXX
                         XXXXXXXX                XXXXXXXX

                                        gun

Alien1: Width 32, height 16. The alien must have four pixels of black 
above and below it, and eight pixels of black on each side. See fig. 2.
Use any other colours you like.
Alien2: Like Alien1, but coloured differently.

           fig. 2.       XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
                         XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
                         XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
           X=Black       XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
                         XXXXXXXX  XXXXXXXXXXXX  XXXXXXXX
                         XXXXXXXXX  XXXXXXXXXX  XXXXXXXXX
                         XXXXXXXXX              XXXXXXXXX
                         XXXXXXXX    X      X    XXXXXXXX
                         XXXXXXXX                XXXXXXXX
                         XXXXXXXXX              XXXXXXXXX
                         XXXXXXXX  X  XX  XX  X  XXXXXXXX
                         XXXXXXXX  X  XX  XX  X  XXXXXXXX
                         XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
                         XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
                         XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
                         XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

                                      alien1

Ship: Similar to Gun but make it saucer shaped. Eight black pixels 
either side 

Bang1: A 32 by 16 black rectangle with a small blob of firey colours in 
the middle. I suggest using !Paint's spray gun.

Bang2: A slightly larger blob of firey colours.

Bang3: A larger still blob of firey colours.

Bang4: The firey colours are fading

Blank: A plain black rectangle 40 pixels wide and 16 high.

House: Width 40, height 16. A house with a black background. No 
significant black margin is required for this one.

House1: Like house but with a few cracks.

House2: Like house but now distinctly tumbledown.

Bomb: Width 8, height 16. It must have a black tail 10 pixels long. See 
fig. 3.

Rocket: Like bomb, but upside down. See fig. 3.

           Fig. 3        XXXXXXXX         XXX  XXX
                         XXXXXXXX         XX    XX
                         XXXXXXXX         X      X
           X=black       XXXXXXXX         !      !
                         XXXXXXXX         XXX  XXX
                         XXXXXXXX         X      X
                         XXXXXXXX         XXXXXXXX
                         XXXXXXXX         XXXXXXXX
                         XXXXXXXX         XXXXXXXX
                         XXXXXXXX         XXXXXXXX
                         X      X         XXXXXXXX
                         XXX  XXX         XXXXXXXX
                         !      !         XXXXXXXX
                         !      !         XXXXXXXX
                         !      !         XXXXXXXX
                         XX....XX         XXXXXXXX

                           Bomb            Rocket



How It Works

Animated screen displays are of necessity very greedy for processor 
time, but it is the duty of any multi-tasking application to call 
Wimp-Poll as frequently as it can to allow other programs to run. 
Therefore some compromises have to be made. For example, although there 
may be up to 46 objects moving in the game at any one time, the program 
only updates a maximum of 5 in any one polling loop; and it is updating 
the screen which takes a lot of time. To avoid too many POKEs each time 
an object is moved, the data for each object is stored in RAM in a form 
useable by SYS "Wimp_PlotIcon" and only the changes to that object are 
POKEd on each movement. To save time, a moving object is not deleted 
before moving, but is simply redrawn in its new position. For this 
reason, several sprites have a black margin which obscures the object 
in its old position. 

The program starts by dimensioning all the blocks required to store the 
data for all the moving objects. Then all the variables involved in 
gameplay are set. PROCinit installs Desktop Invader on the icon bar, 
and also puts several SWI numbers into variables to speed up their use 
during animation. There is a procedure to set up parameters for each 
of the objects used in the game. For example, PROCstartaliens POKEs 
into a forty byte block for each alien its position and which sprite to 
use. Alien?38 holds certain information about the alien, ie. whether 
it is alive or dead. A similar address holds useful information for 
other objects, for instance whether it is moving or not.

Most of the action takes place in PROCplay which is called each time a 
reason code of 0 is returned from Wimp-Poll. This reason code is masked 
when a game is not in play. This procedure begins by getting the mouse 
position and moving the turret if the mouse is over the "left" or 
"right" icon. Next, a variable "tbomb%" decides which, if any, of 
the projectiles needs updating in this polling loop (there are three 
bombs and one rocket, each occupying 40 bytes so "tbomb%" is reset when 
it reaches 160). The variables time% and t% control the overall speed of 
the game and govern the rate at which the aliens themselves are moved. 
TIME cannot be reset until all the aliens have been moved and t% is 
greater than 41. The next 2 loops with t%=40 and t%=41 look for the 
edge of the squadron of aliens and alter their direction if they are 
near the edge of the play area. Finally, if an alien is dying, its 
sprite is updated to the next in the sequence of explosion sprites.

Dead aliens are flagged (alien%?(x+38)=0) and are not continuously 
updated. This is true except in the case of the top left one 
(alien%+0). These coordinates are required for the collision detection 
routines. As a rocket travels upward, it checks if it is in a row, and 
then checks the coordinates of any live aliens in that row. The rows 
are calculated from the position of the first alien. 

Rockets are launched in response to a mouse click (reason code 6). Only 
one rocket is available. PROClaunchrocket sets its initial 
coordinates and flags it as moving (bomb%?(120+38)=1). Bombs are 
dropped in PROCmovealiens. Three bombs are available. Three variables 
govern their use: "bombcounter%" decides which bomb will go; 
"bombrate%" is compared with a random number; "accuracy%" is compared 
with the difference between the x coordinate of the alien and that of 
the turret. The latter two variables are altered as the game progresses. 
If the right conditions are met, PROCdropbomb is called which sets the 
bomb's coordinates and flags it as moving (bomb%?(x+38)=1).

As more things are moving at once, each one is updated less often. To 
allow for this, each procedure which moves an object has a local 
variable "inc%" which is calculated according to whatever else is 
moving in that polling loop. The result is that although if many 
things are moving at once their movements are a little jerky, their 
progress across the screen in real time is roughly constant. 

Houses are created as true icons and their sprite is updated as they 
are hit, and are finally deleted when they have been hit 15 times.

PROCsoftreset alters the play variables and moves the squadron of aliens 
a little closer when they have all been shot (deadaliens%=40). 
PROCreset starts a new game. The variables score%, hiscore% and lives% 
are displayed and updated as necessary in icons at the top of the 
screen.
