Controlling The Bullets
Click here to load the completed tutorial directly into the editor
SWARM-19.DBA. If you want to step through and make the modifications, click here to load SWARM-18.DBA.
Both the aliens and the player can fire bullets. The player fires a bullet in response to the spacebar being pressed, and the aliens fire bullets at random. The first thing we need to do is create the behaviour of the bullet.
Replace the line REM * HERE A * to read:
rem Control aliens
gosub _control_aliens
We shall use the subroutine _control_bullets to handle our bullet behaviour. We will assume that bullet 1 belongs to the player and the remaining 39 bullets belong to the aliens.
Replace the line REM * HERE D * to read:
rem Is the bullet visible
if object visible(objid)=1
We should only handle bullets that have actually been fired. To determine this, we shall make sure all active bullets are visible and all inactive bullets are invisible.
Replace the line REM * HERE E * to read:
rem Move bullet up if it belongs to player
if b=1 then bullet#(b,3)=bullet#(b,3)+10.0
Replace the line REM * HERE F * to read:
rem Move bullet down if it belongs to alien
if b>1 then bullet#(b,3)=bullet#(b,3)-5.0
Replace the line REM * HERE G * to read:
rem Position bullet
position object objid,bullet#(b,2),bullet#(b,3),-10
The player bullet should always move up when active. To move the bullet up, we are changing the BULLET array data item 3 representing the vertical position of the bullet object. The aliens’ bullets should always move down, and so we deduct 5.0 from data item 3 in our BULLET array. We can see from the last addition that data items 2 and 3 in our BULLET array are used to update the horizontal and vertical positions of the bullet object. Now our bullet behaviour has been established, we can proceed to give our player the means to fire.
Replace the line REM * HERE C * to read:
rem Player fires bullet with spacebar
objid=bullet#(1,1)
if spacekey()=1 and object visible(objid)=0
bullet#(1,2)=shipx#
bullet#(1,3)=-90.0
show object objid
endif
The OBJID obtains the object number of the first bullet in our BULLET array. We use this to check whether the bullet object is visible or not. If the bullet is invisible and the player presses the Spacebar, we can fire the bullet. Inside the IF statement, we position bullet one at the current location of our ship and then show the bullet.
Press F5 to test fire your bullet. Likewise, we should allow the aliens to fire their own salvo of bullets too.
Replace the line REM * HERE B * to read:
rem Aliens randomly fire bullets
b=2+rnd(37)
objid=bullet#(b,1)
if rnd(200)=1 and object visible(objid)=0
bullet#(b,2)=ax#
bullet#(b,3)=ay#
show object objid
endif
The variable B is used to pick a random bullet from the BULLET array. The OBJID variable us used to hold the object number for the chosen bullet.
The IF statement will only become true if the computer randomly selects 1 from a choice of 200 possible values, and only then if the chosen bullet is invisible. When the IF condition is true, the bullet data is positioned near the alien coordinates AX and AY, and then the bullet is made visible.
Pressing F5 will reveal both the player and the aliens able to fire.
You will notice however, that the player only has one bullet to fire and the aliens soon run out of bullets themselves. This is because the bullets remain visible, even when they have flown off screen. In order to re-use the bullet, we must make them invisible when they leave the screen.
Replace the line REM * HERE H * to read:
rem Destroy bullet if it leaves screen
if bullet#(b,3)<-130.0 then hide object objid
if bullet#(b,3)>130.0 then hide object objid
As the player bullet leaves via the top of the screen and the alien bullets leave via the bottom, we can hide the bullet objects when their vertical positions exceed the range of the screen area.
Click Here For The Next Tutorial
Detecting Collisions .