'                                Henry's House 
'
'                           LEVEL THREE SOURCE CODE  
'
'                          by : Gurmita & Sucha Singh
'
'----------------------------------------------------------------------------- 
' NOTE:This source code can not be Run through the AMOS editor as some files 
' need to be copied into memory which is done in the startup-sequence of the 
' full Game(see Intro source code).
'
' The following source code reseaves the Main variables LIVES,SC,EXSC and
' LEVEL.It then asks you to 'Press Fire' on the loading screen and then starts 
' the Level. 
'
' Memory Banks Used: 
'  1 - Sprites - Sprites 
'  5 - Sound - Main samples
'  6 - Sound - In Game samples 
'  9 - Pac Pic - Control panel 
' 10 - Pac Pic - Background picture
' 11 - Pac Pic - Bonus screen background 
'
' Bobs and Sprites Used: 
' Bob  1 - Tank
' Bob  2 - Train 
' Bob  3 - Jack-in-the-box 
' Bob  4 to 9 - Asteroids
' Bob 10 - Player Ship Explosion 
' Bob 10 - Henry Crying
' Bob 11 - Asteroid Explosion
' Bob 15 - Laser 
' Bob 26 - Henry Hit 
' Sprite  8 - Player 
' Sprite 10 - Bomb 
' Sprite 12 - Cork 
' Sprite 13 to 19 - Items
' Sprite 22 - Bonus Entrance 
' Sprite 24 - Exit 
'
' Variables Used:
' X     - Player sprites X coordinates.
' Y     - Player sprites Y coordinates.
' I     - Player sprite Image. 
' D     - Start direction image for player.
' A     - The number of items you have collected.
' F     - The platform colour which you collide with.
' N     - The current height of Henry when jumping.
' SC    - Your current score.
' EXSC  - Increases with your score but gets set to 0 if it equals 2000 when 
'         you get an extra life. 
' LEVEL - The level number which effects the images of the doors.
' LIVES - Number of lives Henry has. 
' BONUS - BONUS=1 when you're in the normal level or BONUS=0 when you enter
'         the bonus stage. 
' BLINK - Time taken for player to start blinking if left alone. 
' TIME  - The level time limit.
' COUNT - Used to slow down the time.
' GONG  - Timer for the Gong sfx.
' SPEED - Gradgually increases the speed of the asteroids. 
' AX()  - Asteroids X coordinates. 
' AY()  - Asteroids Y coordinates. 
' ADY() - The speed of the asteroids.
' SX    - Player ship X coordinates. 
' SY    - Player ship Y coordinates. 
' SHIP  - Player ship's image number.
' MISSILE - Activates ship's laser.
' MX    - Laser X coordinates. 
' MY    - Laser Y coordinates. 
' Z     - Asteriod's array number. 
' DEAD  - Used to exit out of bonus stage if your ship explodes. 
' C     - Collision value for asteroids. 
' DELAY - Used as a timer to wait until the player ship has finished exploding.
'
'----------------------------------------------------------------------------- 
'
' Increase sprite buffer.
Set Sprite Buffer 256
'
' Close the AMOS editor to save memory.
Close Editor 
'
' Make arrays for the asteroids. 
Dim AX(9),AY(9),ADY(9)
'
' Make the variables global so the whole program can access them.
Global X,Y,I,D,A,F,N,SC,EXSC,LEVEL,LIVES,BONUS,BLINK,TIME,COUNT,GONG
Global SPEED,AX(),AY(),ADY()
Global SX,SY,SHIP,MISSILE,MX,MY,Z,DEAD,C,DELAY
'
' Set the variables. 
X=320 : Y=218 : I=1 : D=0 : A=0 : BONUS=1 : BLINK=0 : TIME=80 : COUNT=0 : GONG=80
SX=294 : SY=190 : SHIP=67 : MISSILE=0 : MX=260 : MY=200 : DEAD=0 : DELAY=0
SPEED=100^10
'
'----------------------------------------------------------------------------- 
' Calls the SETUP procedure. 
_SETUP
'
' Creates a mask for the sprites,sets Autoback to manual,turns off the 
' automatic updating and updates every 2 vertical blank periods to make the
' Amal animations smoother.
Make Mask : Autoback 0 : Update Off : Update Every 2
'
'----------------------------------------------------------------------------- 
' Calls the BLINK procedure. 
_BLINK
'
' Calls the MAINLOOP procedure.
_MAINLOOP
'----------------------------------------------------------------------------- 
'
' Uses Screen Swap which updates everything at once so making the Game run 
' faster.Updates the timer,checks for collisions,the joystic,the platforms and 
' the soundfx. 
Procedure _MAINLOOP
   '
   ' Manualy updates all the bobs so there is no flicker at the start.
   Bob Clear : Bob Draw 
   '
   ' The loop is divided into two parts. The first is used to perform the 
   ' various instructions in your program and the secound part repeats the  
   ' above contents.This keeps the loop in sync.
   Do 
      ' Swaps between the Phisical and Logical screens then clears the screen  
      ' of bobs. 
      Screen Swap 0
      Wait Vbl 
      Bob Clear 
      '
      ' Calls the TIME and EXTRA LIFE procedures and sets the Joystic. 
      _TIME
      _EXTRA_LIFE
      J=Joy(1)
      '
      ' Checks the collision between the player sprite and other bobs and sprites. 
      If Spritebob Col(8,1 To 3) Then _DEAD
      If I Sprite(10)<>58 and Sprite Col(8,10 To 10) Then _DEAD
      If I Sprite(12)<>58 and Sprite Col(8,12 To 12) Then _DEAD
      If Sprite Col(8,13 To 19) Then _GET_ITEM
      If Sprite Col(8,22 To 22) Then _BONUS
      If Sprite Col(8,24 To 24) Then _EXIT
      '
      ' Places the next wave of itmes. 
      If A=7 Then MORE_ITEMS
      '
      ' Places the Exit on screen when you have collected all the items. 
      If A=15 : Sprite 24,170,232,65 : A=16 : End If 
      '
      ' If all the items are collected within the timelimit then the Bonus 
      ' entrance appears.
      If A=16 and TIME>0 : Sprite 22,148,167,64 : TIME=0 : End If 
      '
      ' Checks for the joystic and floor,so if Henry is left alone on a  
      ' platform the variable 'BLINK' increases or else it equals 0. 
      If J=0 and F=2 Then Inc BLINK Else BLINK=0
      If BLINK>20 Then _BLINK
      '
      ' Checks Left & Right joystic directions.If the joystic has been moved 
      ' and Henry is away from the edge then he will either move left or right.  
      ' The variable D sets the starting frame depending on the joystic direction    
      ' you have moved.It then adds the frames to make Henry look like he's walking.     
      If J=4 and X>138 Then Dec X : _TIME : D=6 : Inc C : If C mod 4=0 Then Add I,1,1 To 6
      If J=8 and X<438 Then Inc X : _TIME : D=0 : Inc C : If C mod 4=0 Then Add I,1,1 To 6
      '
      ' Checks for Up,Up left and Up right directions.If the joystic has been  
      ' moved in either of the up directions and Henry is on the platform then   
      ' the relevant JUMP procedure is called. 
      If J=1 and F=2 Then JUMP_UP
      If J=5 and F=2 Then D=6 : JUMP_LEFT
      If J=9 and F=2 Then D=0 : JUMP_RIGHT
      '
      ' Plays the various soundfx for the enemy depending on their images. 
      If I Bob(3)=48 Then Sam Bank 6 : Sam Play %11,1
      If I Bob(1)=16 Then Sam Bank 6 : Sam Play %11,2
      If I Sprite(10)=27 Then Sam Bank 6 : Sam Play %11,3
      If X Sprite(12)=151 Then Sam Bank 6 : Sam Play %11,4
      ' Plays the Gong sfx when the Gong timer equals 0. 
      If GONG=0 Then Sam Bank 5 : Sam Play %1110,3 : GONG=80
      '
      ' Checks for a collision between a colour on screen and the player sprite. 
      F=Point(X Screen(X Sprite(8)),Y Screen(Y Sprite(8)))
      '
      ' If the colour is not equal to 2(the platform colour) then Henry will fall.   
      If F<>2 Then Inc Y
      '
      ' Updates the player sprite and draws all the bobs on screen.
      Sprite 8,X,Y,I+D
      Sprite Update 
      Bob Draw 
      '
      '----------------------------------------------------------------------- 
      ' Swaps between the Phisical and Logical screens then clears the screen  
      ' of bobs. 
      Screen Swap 0
      Wait Vbl 
      Bob Clear 
      J=Joy(1)
      '
      ' Checks for the joystic.By repeating it again it makes Henry walk faster. 
      If J=4 and X>138 Then Dec X : D=6 : Inc C : If C mod 4=0 Then Add I,1,1 To 6
      If J=8 and X<438 Then Inc X : D=0 : Inc C : If C mod 4=0 Then Add I,1,1 To 6
      '
      ' Checks for the collision between Henry and the platforms.
      F=Point(X Screen(X Sprite(8)),Y Screen(Y Sprite(8)))
      If F<>2 Then Inc Y
      '
      ' Updates the player sprite and draws the bobs on screen.
      Sprite 8,X,Y,I+D
      Sprite Update 
      Bob Draw 
   Loop 
   '
End Proc
'
' Opens up a screen,sets the fonts,reseaves the variables from the last
' program then loads the bobs and sfx and waits until you have pressed fire
' on the loading screen to start the game. 
Procedure _SETUP
   '
   ' Hides mouse pointer,opens screen 0 and screen 2 then hides them both.
   Hide On 
   Screen Open 0,320,200,32,Lowres : Screen Hide 0
   Curs Off : Flash Off : Cls 0
   Screen Open 2,320,30,16,Lowres : Screen Hide 2
   Curs Off : Flash Off : Cls 0
   '
   ' Loads fonts from Disk2 then checks all fonts to make sure they have loaded.
   Get Disc Fonts 
   Screen 2
   For F=0 To 6
      Set Font F : T$="Amos 12345:"+Str$(F)
      Text 0,0,T$
   Next F
   Cls 0
   '
   ' Unpacks the Control Panel to screen 2 hides it,fades it to black then
   ' sets its display.
   Unpack 9 To 2 : Screen Hide 2 : Fade 1 : Wait 15
   Screen Display 2,130,252,320,30
   '
   ' Reseaves the command line$ then separates its contents into the 4 strings. 
   Set Font 6
   B$=Left$(Command Line$,3)
   C$=Mid$(Command Line$,4,6)
   D$=Mid$(Command Line$,10,5)
   E$=Right$(Command Line$,1)
   '
   ' Converts the 4 strings into variables. 
   LIVES=Val(B$) : SC=Val(C$) : EXSC=Val(D$) : LEVEL=Val(E$)
   '
   ' Prints the Lives,Score and Time onto the control panel.
   If LIVES<10 Then Ink 2,0 : Text 39,20,B$ : Text 144,20,C$
   If LIVES>9 Then Ink 2,0 : Text 27,20,B$ : Text 144,20,C$
   Ink 2,0 : Text 278,20,"80"
   '
   ' Loads the powerpacked bobs and sfx.
   Screen 0
   'Ppload "Henry's_HouseD2:Bobs/SpritesLev3" 
   'Ppload "Henry's_HouseD2:Sound/Mainsfx",5
   '
   ' Display message 'Press Fire To Start'. 
   Screen 1 : Paste Bob 115,180,97
   '
   ' Small loop which keeps you at the loading screen until you press Fire. 
   Do 
      J=Joy(1)
      If Fire(1)=-1 Then Exit 
      Paste Bob 115,180,97 : Bob Update 
      Wait Vbl 
   Loop 
   ' Small loop which repeats until you have released the fire button then
   ' fades the screen and closes it.
   Repeat 
   Until Fire(1)=0
   Screen 1 : Fade 1 : Wait 15 : Screen Close 1
   '
   ' Unpacks the background to screen 0,hides it then gets the sprite palette 
   ' and Double Buffers the screen for no flicker.
   Screen 0 : Unpack 10 To 0 : Screen Hide 0
   Double Buffer 
   '
   ' Sets the hot spot to bottom centre for the Player images,calls the 
   ' AMAL and ITEMS procedures and pastes the Soilder with cannon on screen.
   For N=1 To 12 : Hot Spot N,$12 : Next 
   _AMAL
   _ITEMS
   Paste Bob -5,22,41
   '
   ' Fades background to black then fades the Control Panel and Background
   ' picture into view. 
   Fade 1 : Wait 15
   Screen 2
   Screen Show 2
   Fade 1,$0,$BBB,$FFF,$EB0,$E90,$FD0,$B70,$840,$520,$CF,$6E,$F7E,$F0,$A01,$D01,$EC8
   Wait 15
   Screen 0
   Screen Show 0
   Fade 1,$0,$BBB,$903,$484,$5B6,$886,$A97,$CB9,$652,$863,$A85,$555,$DDD,$5BE,$363,$B60,$34,$0,$888,$FFF,$520,$840,$B70,$FB0,$486,$5A8,$5CA,$8E,$8,$A01,$D01,$EC8
   Wait 15
   '
   ' Sets the volume for all channels to maximum. 
   Volume %1111,63
   '
End Proc
'
' Updates the Time.
Procedure _TIME
   '
   ' As long as the time is greater than 0 then the variable,to slow down the 
   ' timer,can keep on increasing.
   If TIME>0 Then Inc COUNT
   '
   ' Adds 1 to the TIME variable and decreases the GONG variable whenever COUNT=50. 
   If COUNT>50
      TIME=TIME-1 : Dec GONG : COUNT=0
      ' Convert TIME variable to a string so it can be printed using 'Text'. 
      A$=Str$(TIME)
      Screen 2 : L=Text Length(A$) : Text 304-L,20,A$
   End If 
   '
   ' Returns program back to relevant screen. 
   If BONUS=1 Then Screen 0
   If BONUS=0 Then Screen 1
   '
End Proc
'
' Updates the lives,sets the death animation then plays it until its finished. 
Procedure _DEAD
   '
   ' Decrease the LIVES by 1,convert variable into a string to display your lives.  
   Screen 2
   LIVES=LIVES-1
   B$=Str$(LIVES) : LD=Text Length(B$) : Text 64-LD,20,B$
   '
   Screen 0
   ' Match the Bob starting position with the Player sprite position. 
   DX=X Screen(X Sprite(8)) : DY=Y Screen(Y Sprite(8))
   Bob 26,DX,DY,94
   ' Set Amal animation.
   Channel 14 To Bob 26
   I$="A 0,(94,0); M 0,-20,20; M 0,-4,4; M 0,-3,3; M 0,-3,4; M 0,2,4; M 0,3,3; M 0,15,5; M 0,250,46"
   Amal 14,I$ : Amal On 14
   ' Turn off player sprite,play Ouch sfx and update bobs.
   Sprite Off 8
   Sam Bank 5 : Sam Play %1110,2
   Bob Clear : Bob Draw 
   '    
   ' Small loop to allow Henry to finish death sequence without causing any   
   ' problems to the main loop eg,colliding while still playing the death anim. 
   Repeat 
      Screen Swap 0
      Wait Vbl 
      Bob Clear 
      _TIME
      '      
      If I Bob(3)=48 Then Sam Bank 6 : Sam Play %11,1
      If I Bob(1)=16 Then Sam Bank 6 : Sam Play %11,2
      If I Sprite(10)=27 Then Sam Bank 6 : Sam Play %11,3
      If X Sprite(12)=151 Then Sam Bank 6 : Sam Play %11,4
      If GONG=0 Then Sam Bank 5 : Sam Play %1110,3 : GONG=80
      '
      Sprite Update 
      Bob Draw 
      '
      Screen Swap 0
      Wait Vbl 
      Bob Clear 
      '
      Sprite Update 
      Bob Draw 
   Until Y Bob(26)>249
   '
   ' Check to see if the player has lost all their lives if so then call the
   ' GAMEOVER procedure.
   If LIVES=0 Then _GAMEOVER
   '
   ' Turn off Death animation,reset player starting position,'N' variable and 
   ' image then swap screen and clear bobs to help stay in sync with mainloop.  
   Amal Off 14 : X=320 : Y=216 : N=34 : I=2 : Sprite Update : Wait Vbl 
   Screen Swap 0 : Wait Vbl : Bob Clear 
   '
End Proc
'
' Makes Henry Jump up. 
Procedure JUMP_UP
   '
   ' Change sample bank then play the Jumping sfx.
   Sam Bank 5 : Sam Play %1100,5
   '
   ' First 'for next' loop makes Henry move up,tests for collisions and plays   
   ' the soundfx. 
   For N=1 To 34
      Dec Y
      If BONUS=1 Then _TIME : Rem Call TIME proc when your not in the Bonus stage. 
      _EXTRA_LIFE
      If Spritebob Col(8,1 To 3) Then N=34 : _DEAD
      If I Sprite(10)<>58 and Sprite Col(8,10 To 10) Then N=34 : _DEAD
      If I Sprite(12)<>58 and Sprite Col(8,12 To 12) Then N=34 : _DEAD
      If Sprite Col(8,13 To 19) Then _GET_ITEM
      If Y Sprite(8)<60 Then N=34 : Rem So Henry dos'nt go off screen. 
      '
      If I Bob(3)=48 Then Sam Bank 6 : Sam Play %11,1
      If I Bob(1)=16 Then Sam Bank 6 : Sam Play %11,2
      If I Sprite(10)=27 Then Sam Bank 6 : Sam Play %11,3
      If X Sprite(12)=151 Then Sam Bank 6 : Sam Play %11,4
      If GONG=0 Then Sam Bank 5 : Sam Play %1110,3 : GONG=80
      Sprite 8,X,Y,I+D
      Update 
      Wait Vbl 
   Next 
   '
   ' Secound loop makes Henry come down and checks for collisions with any    
   ' platforms and other Sprites & Bobs and plays the soundfx.
   For N=1 To 34
      If BONUS=1 Then _TIME
      _EXTRA_LIFE
      F=Point(X Screen(X Sprite(8)),Y Screen(Y Sprite(8)))
      If F<>2 Then Inc Y
      ' If Henry touches a platform before N=34 then N=34 and exits out of loop.   
      If F=2 Then N=34
      '
      If Spritebob Col(8,1 To 3) Then N=34 : _DEAD
      If I Sprite(10)<>58 and Sprite Col(8,10 To 10) Then N=34 : _DEAD
      If I Sprite(12)<>58 and Sprite Col(8,12 To 12) Then N=34 : _DEAD
      If Sprite Col(8,13 To 19) Then _GET_ITEM
      '
      If I Bob(3)=48 Then Sam Bank 6 : Sam Play %11,1
      If I Bob(1)=16 Then Sam Bank 6 : Sam Play %11,2
      If I Sprite(10)=27 Then Sam Bank 6 : Sam Play %11,3
      If X Sprite(12)=151 Then Sam Bank 6 : Sam Play %11,4
      If GONG=0 Then Sam Bank 5 : Sam Play %1110,3 : GONG=80
      Sprite 8,X,Y,I+D
      Update 
      Wait Vbl 
   Next 
   '
   ' Reset 'N' variable and clear the bobs to keep in sync with mainloop. 
   N=1
   Bob Clear 
   '
End Proc
'
' Makes Henry Jump up and left.
Procedure JUMP_LEFT
   '
   ' Change sample bank,play the Jumping sfx and call the EXTRA LIFE procedure. 
   Sam Bank 5 : Sam Play %1100,5
   _EXTRA_LIFE
   '
   ' First 'for next' loop makes Henry move up and left,tests for collisions  
   ' and plays the soundfx. 
   For N=1 To 34
      If BONUS=1 Then _TIME : Rem Call TIME proc when your not in the Bonus Stage. 
      Add I,1,1 To 6 : Dec Y : If X>140 Then Dec X
      '
      If Spritebob Col(8,1 To 3) Then N=34 : _DEAD
      If I Sprite(10)<>58 and Sprite Col(8,10 To 10) Then N=34 : _DEAD
      If I Sprite(12)<>58 and Sprite Col(8,12 To 12) Then N=34 : _DEAD
      If Sprite Col(8,22 To 22) Then _BONUS
      If Y Sprite(8)<60 Then N=34 : Rem So Henry dos'nt go off screen. 
      '
      If I Bob(3)=48 Then Sam Bank 6 : Sam Play %11,1
      If I Bob(1)=16 Then Sam Bank 6 : Sam Play %11,2
      If I Sprite(10)=27 Then Sam Bank 6 : Sam Play %11,3
      If X Sprite(12)=151 Then Sam Bank 6 : Sam Play %11,4
      If GONG=0 Then Sam Bank 5 : Sam Play %1110,3 : GONG=80
      Sprite 8,X,Y,I+D
      Update 
      Wait Vbl 
   Next N
   '
   ' Secound loop makes Henry come down and move left checks for collisions   
   ' with any platforms and other Sprites & Bobs and plays the soundfx. 
   For N=1 To 34
      If BONUS=1 Then _TIME
      F=Point(X Screen(X Sprite(8)),Y Screen(Y Sprite(8)))
      Add I,1,1 To 6 : If X>140 Then Dec X
      If F<>2 Then Inc Y
      ' If Henry touches a platform before N=34 then N=34 and exits out of loop.   
      If F=2 Then N=34
      '
      If Spritebob Col(8,1 To 3) Then N=34 : _DEAD
      If I Sprite(10)<>58 and Sprite Col(8,10 To 10) Then N=34 : _DEAD
      If I Sprite(12)<>58 and Sprite Col(8,12 To 12) Then N=34 : _DEAD
      If Sprite Col(8,22 To 22) Then _BONUS
      '
      If I Bob(3)=48 Then Sam Bank 6 : Sam Play %11,1
      If I Bob(1)=16 Then Sam Bank 6 : Sam Play %11,2
      If I Sprite(10)=27 Then Sam Bank 6 : Sam Play %11,3
      If X Sprite(12)=151 Then Sam Bank 6 : Sam Play %11,4
      If GONG=0 Then Sam Bank 5 : Sam Play %1110,3 : GONG=80
      Sprite 8,X,Y,I+D
      Update 
      Wait Vbl 
   Next N
   '
   ' Reset 'N' variable and clear the bobs to keep in sync with mainloop. 
   N=1
   Bob Clear 
   '
End Proc
'
' Makes Henry Jump up and right. 
Procedure JUMP_RIGHT
   '
   ' Change sample bank,play the Jumping sfx and calls the EXTRA LIFE procedure.. 
   Sam Bank 5 : Sam Play %1100,5
   _EXTRA_LIFE
   '
   ' First 'for next' loop makes Henry move up and right,tests for collisions   
   ' and plays the soundfx. 
   For N=1 To 34
      If BONUS=1 Then _TIME : Rem Call TIME proc when your not in the Bonus Stage.   
      Add I,1,1 To 6 : Dec Y : If X<436 Then Inc X
      '
      If Spritebob Col(8,1 To 3) Then N=34 : _DEAD
      If I Sprite(10)<>58 and Sprite Col(8,10 To 10) Then N=34 : _DEAD
      If I Sprite(12)<>58 and Sprite Col(8,12 To 12) Then N=34 : _DEAD
      If Y Sprite(8)<60 Then N=34 : Rem So Henry dos'nt go off screen. 
      '
      If I Bob(3)=48 Then Sam Bank 6 : Sam Play %11,1
      If I Bob(1)=16 Then Sam Bank 6 : Sam Play %11,2
      If I Sprite(10)=27 Then Sam Bank 6 : Sam Play %11,3
      If X Sprite(12)=151 Then Sam Bank 6 : Sam Play %11,4
      If GONG=0 Then Sam Bank 5 : Sam Play %1110,3 : GONG=80
      Sprite 8,X,Y,I+D
      Update 
      Wait Vbl 
   Next 
   '
   ' Secound loop makes Henry come down and move right checks for collisions  
   ' with any platforms and other Sprites & Bobs and plays the soundfx. 
   For N=1 To 34
      If BONUS=1 Then _TIME
      F=Point(X Screen(X Sprite(8)),Y Screen(Y Sprite(8)))
      Add I,1,1 To 6 : If X<436 Then Inc X
      If F<>2 Then Inc Y
      ' If Henry touches a platform before N=34 then N=34 and exits out of loop.   
      If F=2 Then N=34
      '
      If Spritebob Col(8,1 To 3) Then N=34 : _DEAD
      If I Sprite(10)<>58 and Sprite Col(8,10 To 10) Then N=34 : _DEAD
      If I Sprite(12)<>58 and Sprite Col(8,12 To 12) Then N=34 : _DEAD
      '
      If I Bob(3)=48 Then Sam Bank 6 : Sam Play %1,1
      If I Bob(1)=16 Then Sam Bank 6 : Sam Play %11,2
      If I Sprite(10)=27 Then Sam Bank 6 : Sam Play %11,3
      If X Sprite(12)=151 Then Sam Bank 6 : Sam Play %11,4
      If GONG=0 Then Sam Bank 5 : Sam Play %1110,3 : GONG=80
      Sprite 8,X,Y,I+D
      Update 
      Wait Vbl 
   Next 
   '
   ' Reset 'N' variable and clear the bobs to keep in sync with mainloop. 
   N=1
   Bob Clear 
   '
End Proc
'
' Makes Henry start blinking.
Procedure _BLINK
   '
   ' Set the Amal animation of Henry blinking and turn the automatic updating on.   
   Channel 15 To Sprite 8
   Sprite 8,X,Y,
   J$="A 0,(95,30)(96,10)(95,10)(96,10);"
   Amal 15,J$ : Amal On 15 : Update On 
   '
   ' Small loop which repeats itself until you move Henry.
   Do 
      ' Call TIME procedure,set joystic,check for collisions between the 
      ' player and enemy and play the soundfx. 
      _TIME
      J=Joy(1)
      If Spritebob Col(8,1 To 3) Then Update Off : _DEAD : Exit 
      If I Sprite(10)<>58 and Sprite Col(8,10 To 10) Then Update Off : _DEAD : Exit 
      If I Sprite(12)<>58 and Sprite Col(8,12 To 12) Then Update Off : _DEAD : Exit 
      '
      If I Bob(3)=48 Then Sam Bank 6 : Sam Play %11,1
      If I Bob(1)=16 Then Sam Bank 6 : Sam Play %11,2
      If I Sprite(10)=27 Then Sam Bank 6 : Sam Play %11,3
      If X Sprite(12)=151 Then Sam Bank 6 : Sam Play %11,4
      If GONG=0 Then Sam Bank 5 : Sam Play %1110,3 : GONG=80
      '
      ' Check for any joystic movement then exits out of loop. 
      If J>0 Then Exit 
      ' Wait for a vertical blank. 
      Wait Vbl 
   Loop 
   '
   ' Turns off blinking animation,automatic updateing and reset the BLINK timer.    
   Amal Off 15 : Update Off : BLINK=0
   '
   ' Update Bobs,swap screen then clear the bobs on screen to help stay in  
   ' sync with the Main loop. 
   Bob Clear : Bob Draw : Screen Swap 0 : Wait Vbl : Bob Clear 
   '
End Proc
'
' Reverses the player images to make Henry's left facing images. 
Procedure _REVERSESPRITES
   '
   ' Goes through the image of player,gets the image,sets the hot spot then 
   ' pastes the fliped image. 
   For N=1 To 6
      Bob 1,16,27,N : Wait Vbl : Get Bob N+6,9,1 To 25,28
      Hot Spot N+6,$12 : Paste Bob 500,500,Hrev(N+6)
   Next : Bob Off 1 : Wait Vbl 
   '
End Proc
'
' Sets the Amal animations for the enemy.
Procedure _AMAL
   '
   ' Set the Channels for each enemy and their starting positions.
   Channel 1 To Bob 1
   Channel 2 To Sprite 10
   Channel 3 To Bob 2
   Channel 4 To Sprite 12
   Channel 5 To Bob 3
   Bob 1,304,116,13
   Sprite 10,354,160,58
   Bob 2,340,184,33
   Sprite 12,150,96,45
   Bob 3,160,82,46
   '
   ' Set Amal animation strings for each enemy. 
   A$="Tank:A 0,(13,5)(14,5)(15,5); M -60,0,170; A 1,(13,5); M 0,0,10; A 1,(16,2)(17,5)(18,5)(13,5); M 0,0,17; A 1,(19,5)(20,5)(21,5)(22,5); M 0,0,20;"
   A$=A$+"A 0,(23,5)(24,5)(25,5); M 60,0,170; A 1,(23,5); M 0,0,10; A 1,(22,5)(21,5)(20,5)(19,5)(13,5); M 0,0,25; Jump Tank"
   '
   B$="Bomb:A 0,(58,5); M 0,0,180; A 0,(26,5); M -25,-10,15; M -25,-5,15; M -25,5,15; M -25,10,15; M -25,15,15; A 1,(27,2)(28,5)(29,5)(30,5)(31,5)(32,5); M 0,0,27; A 0,(58,5); M 125,-15,5; M 0,0,135; Jump Bomb"
   '
   C$="Train:M 0,0,40; A 0,(33,5)(34,5)(35,5)(36,5); M-360,0,230; M 0,0,40; A 0,(37,5)(38,5)(39,5)(40,5); M 360,0,230; Jump Train"
   '
   D$="Cork:A 0,(45,5); M 0,0,55; M 1,0,1; A 0,(46,5); M 301,0,80; A 0,(58,5); M -302,0,5; Jump Cork"
   '
   E$="A 0,(47,5); M 0,0,40;"
   E$=E$+"A 0,(47,5)(48,2)(49,4)(50,4)(51,4)(52,4)(49,4)(53,4)(54,4)(55,4)(56,4)(57,4)(47,60);"
   '
   ' Set the Channels to the strings then turn on the Amal animations.  
   Amal 1,A$ : Amal 2,B$ : Amal 3,C$ : Amal 4,D$ : Amal 5,E$
   Amal On 
   '
End Proc
'
' Sets the positions for the first wave of items.
Procedure _ITEMS
   '
   Sprite 13,170,170,59
   Sprite 14,436,136,60
   Sprite 15,220,74,62
   Sprite 16,388,95,63
   Sprite 17,410,238,61
   Sprite 18,276,190,60
   Sprite 19,180,238,61
   '
End Proc
'
' Sets the positions for the secound wave of items.
Procedure MORE_ITEMS
   '
   A=8 : Rem Sets number of items so the Mainloop only calls this procedure once.   
   Sprite 13,202,116,60
   Sprite 14,350,116,61
   Sprite 15,400,150,63
   Sprite 16,420,200,62
   Sprite 17,224,168,59
   Sprite 18,430,70,59
   Sprite 19,220,220,63
   '
End Proc
'
' Updates your score and turns off a sprite when you collect an item.
Procedure _GET_ITEM
   '
   ' Turns off the sprite you have collided with,plays the Collect sfx and  
   ' updates the EXSC variable. 
   Sprite Off Col(-1) : Sam Bank 5 : Sam Play %1100,4 : A=A+1 : EXSC=EXSC+50
   '
   Screen 2
   ' Updates your score variable. 
   SC=SC+50
   ' Turn score variable to string so it can be printed using 'Text'. 
   C$=Str$(SC) : Text 144,20,C$
   ' Return to screen 0.
   Screen 0
   '
End Proc
'
' Restarts the game after finishing the Bonus stage. 
Procedure _RESTART
   '
   ' Fades the bonus screen,turns off all sprites,bobs,Amal animations and hides screen.
   Screen 1 : Fade 1 : Wait 15 : Sprite Off : Bob Off : Amal Off : Screen Hide 1
   '
   ' Resets the A,BONUS,TIME,X and Y variables and turns on the enemy animations. 
   Screen 0 : A=0 : BONUS=1 : TIME=0 : X=320 : Y=216 : Sprite Update : _AMAL
   ' Resets the timer on the panel. 
   Screen 2 : Ink 2,0 : Text 278,20,"00"
   Screen 0 : Screen Show 0
   '
   ' Place Exit back on screen and fade screen into view. 
   Sprite 24,170,232,65
   Fade 1,$0,$BBB,$903,$484,$5B6,$886,$A97,$CB9,$652,$863,$A85,$555,$DDD,$5BE,$363,$B60,$34,$0,$888,$FFF,$520,$840,$B70,$FB0,$486,$5A8,$5CA,$8E,$8,$A01,$D01,$EC8
   Wait 15
   '
End Proc
'
' Exits out of game once you have finished the level.
Procedure _EXIT
   '
   ' Play Door sfx,make door open then turn off player sprite.
   Sam Bank 5 : Sam Play %1110,1
   Sprite 24,170,232,66
   Sprite Off 8
   Sprite Update 
   Wait 30
   ' Fade screen,close it then reset player position. 
   Screen 0 : Fade 2 : Wait 30 : Screen Close 0
   X=320 : Y=215
   '
   '
   ' Create 4 Main strings to hold the 4 main variables(LIVES,SC,EXSC,LEVEL). 
   A$="   " : B$="      " : C$="     " : D$="  "
   '
   ' Convert the 4 variables into strings.
   L$=Str$(LIVES) : S$=Str$(SC) : E$=Str$(EXSC) : LEV$=Str$(LEVEL)
   '
   ' Then place those strings into the above Main strings.
   Right$(A$,3)=L$ : Left$(B$,6)=S$ : Right$(C$,5)=E$ : Right$(D$,2)=LEV$
   '
   ' Lastly place the final 4 strings into the Command Line$ to be passed   
   ' through to the next program. 
   Command Line$=A$+B$+C$+D$
   '
   ' Fade control panel,close it,turn off all Amal animations,erase memory  
   ' banks then load the Select Level program.
   Screen 2 : Fade 2 : Wait 30 : Screen Close 2
   Amal Off 
   For B=0 To 15 : Erase B : Next B
   Run "HenryGame/Selectlevel"
   '
End Proc
'
' Updates your lives every 2000 points.
Procedure _EXTRA_LIFE
   '
   If EXSC=2000
      Screen 2
      ' Play Extra life sfx. 
      Sam Bank 5 : Sam Play %1110,6
      '
      ' Update LIVES variable by 1 and convert it to a string. 
      LIVES=LIVES+1 : B$=Str$(LIVES)
      ' Print the score string and depending on whether its a single or  
      ' double number offset the text position.  
      If LIVES>9 : Text 27,20,B$ : End If 
      If LIVES<10 : Text 40,20,B$ : End If 
      '
      ' Reset EXSC variable to 0 and then return to the relevant screen. 
      EXSC=0
      If BONUS=1 : Screen 0 : End If 
      If BONUS=0 : Screen 1 : End If 
   End If 
   '
End Proc
'
' Plays the Game Over animation then loads the Title program.
Procedure _GAMEOVER
   '
   ' Close the game and control panel screens.  
   If BONUS=1
      Screen 0 : Fade 2 : Wait 30 : Cls 0 : Sprite Off : Sprite Update : Wait Vbl : Bob Off : Amal Off : Screen Close 0
      Screen 2 : Fade 2 : Wait 30 : Cls 0 : Screen Close 2
   End If 
   '
   ' Open up new screen for animation,get the sprite palette,Double Buffer
   ' screen for no flicker and turn update on.  
   Screen Open 0,320,200,32,Lowres : Screen Hide 0
   Curs Off : Flash Off : Cls 0
   Get Sprite Palette 
   Double Buffer : Update On 
   '
   ' Set Amal animation.  
   Channel 15 To Bob 10
   Bob 10,160,110,100
   F$="A 0,(100,5)(101,5)(102,5)(103,5)(104,5)(105,5)"
   Amal 15,F$ : Amal On 
   '
   ' Paste the 'Game Over' words on screen,fade screen to black then fade the 
   ' screen into view.  
   Paste Bob 70,10,98 : Paste Bob 164,10,99
   Fade 1 : Wait 15
   Screen Show 0
   Fade 1,$0,$BBB,$903,$484,$5B6,$886,$A97,$CB9,$652,$863,$A85,$555,$DDD,$5BE,$363,$B60,$34,$0,$888,$FFF,$520,$840,$B70,$FB0,$486,$5A8,$5CA,$8E,$8,$A01,$D01,$EC8
   Wait 15
   '
   ' Small loop which exits when the GO variable equals 120 or you press Fire.
   Do 
      Bob 10,160,110,
      If Fire(1)=-1 Then Exit 
      Inc GO
      If GO>120 Then Exit 
      Wait Vbl 
   Loop 
   ' Small loop which exits once you have released the Fire button. 
   Repeat 
      Bob 10,160,110,
      Wait Vbl 
   Until Fire(1)=0
   Fade 2 : Wait 30
   '
   ' Prepare a string to hold the score string. 
   A$="      "
   ' Convert the score to a string. 
   S$=Str$(SC)
   ' Place score string into 'A$' then place it in the command line$. 
   Left$(A$,6)=S$
   Command Line$=A$
   '
   ' Turn off Amal animations,close screen,erase memory banks then run the
   ' Title program. 
   Amal Off 
   Screen Close 0
   For B=0 To 15 : Erase B : Next B
   Run "HenryGame/Title"
   '
End Proc
'
'----------------------------------------------------------------------------- 
'              *******  Procedures for the Bonus Stage. *******
'
' Bonus stage mainloop which repeats until the timer runs out or your ship 
' hits an asteroid.
Procedure _BONUS
   '
   ' Set the collect variable 'A',BONUS variable,TS(Timer used for the wait 
   ' before the asteroids come on screen) and the new Time limit.   
   A=16
   BONUS=0 : TS=1 : TIME=99
   ' Fade screen,turn off all sprites,bobs and animations then hide screen. 
   Screen 0 : Fade 1 : Wait 15 : Sprite Off : Sprite Update : Bob Off : Amal Off : Screen Hide 0
   '
   ' Call the BSETUP procedure. 
   _BSETUP
   '
   ' Loop handled in the same way as Mainloop(using screen swap).By using   
   ' another mainloop for the Bonus stage the Game runs faster and smoother.
   ' Exits out of Loop when the timer has reached 0 or the player ship hits 
   ' an asteroid.   
   Repeat 
      '
      ' Swaps between the Phisical and Logical screens then clears the screen  
      ' of bobs. 
      Screen Swap 1
      Wait Vbl 
      Bob Clear 
      '
      ' Sets the joystic,Calls the TIME and EXTRA LIFE procedures. 
      _TIME
      _EXTRA_LIFE
      J=Joy(1)
      ' Small timer set for the wait before the asteroids come on screen for 
      ' the first time.  
      TI=TI+TS
      If TI=40 Then ALIENS : TS=0
      '
      ' Checks for the joystic and moves your Ship in the relevant direction.
      If J=0 Then SHIP=67
      If Jleft(1) and SX>194 Then SX=SX-4 : SHIP=68
      If Jleft(1) and SX=194 Then SHIP=68
      If Jright(1) and SX<400 Then SX=SX+4 : SHIP=69 : If SX>400 Then SX=SX-4
      If Jup(1) and SY>60 Then SY=SY-4
      If Jdown(1) and SY<248 Then SY=SY+4 : If SY>240 Then SY=SY-4
      '
      'Calls the relevant procedures.
      _MOVEMISSILE
      _MISSCOLLISION
      _SHIPCOLLISION
      '
      ' Updates the player sprite and draws all the bobs on screen.
      Sprite 8,SX,SY,SHIP
      Bob Draw 
      Sprite Update 
      '
      '----------------------------------------------------------------------- 
      ' Swaps between the Phisical and Logical screens then clears the screen  
      ' of bobs. 
      Screen Swap 1
      Wait Vbl 
      Bob Clear 
      ' Wait before the asteroids come on screen.  
      TI=TI+TS
      If TI=40 Then ALIENS : TS=0
      '
      ' Checks for the joystic and moves your Ship in the relevant direction.
      If J=0 Then SHIP=67
      If Jleft(1) and SX>194 Then SX=SX-4 : SHIP=68
      If Jleft(1) and SX=194 Then SHIP=68
      If Jright(1) and SX<400 Then SX=SX+4 : SHIP=69 : If SX>400 Then SX=SX-4
      If Jup(1) and SY>60 Then SY=SY-4
      If Jdown(1) and SY<248 Then SY=SY+4 : If SY>240 Then SY=SY-4
      '
      ' Calls the MOVEMISSILE procedure. 
      _MOVEMISSILE
      '
      ' Updates the player sprite and draws all the bobs on screen.
      Sprite 8,SX,SY,SHIP
      Sprite Update 
      Bob Draw 
   Until DEAD=1 or TIME=0
   '
   ' Calls the RESTART procedure. 
   _RESTART
   '
End Proc
'
' Shows the Bonus level title screen then sets up the Bonus background and 
' enemy animations.
Procedure _BSETUP
   '
   'Opens a screen then prints the information on the Title screen. 
   Screen Open 1,320,200,32,Lowres : Screen Hide 1
   Get Sprite Palette 
   Curs Off : Flash Off : Colour 0,$0 : Cls 0 : Paper 0
   Pen 23 : Locate 0,4 : Centre "STARBURST"
   Pen 26 : Locate 0,7 : Centre "Bonus Game"
   Pen 27 : Locate 0,10 : Centre "Get as many points as you can"
   Locate 0,11 : Centre "by destroying the asteroids."
   Locate 0,13 : Centre "Do this within the time limit and"
   Locate 0,14 : Centre "one life."
   Pen 30 : Locate 0,20 : Centre "Press Fire to start"
   ' Prints the new Time limit. 
   Screen 2 : Ink 2,0 : Text 278,20,"99"
   '
   ' Sets the Anal animation for the Ship on the title screen.
   Screen 1
   Channel 14 To Sprite 8
   Sprite 8,294,190,
   I$="A 0,(67,8)(68,8)(67,8)(69,8);"
   Amal 14,I$ : Amal On 14
   Screen Show 1
   '
   ' Small loop which exits when you press Fire.
   Do 
      F=Fire(1)
      If F=-1 Then Exit 
      Sprite 8,294,190, : Sprite Update : Wait Vbl 
   Loop 
   ' Small loop which exits once you have released the Fire button. 
   Repeat 
      Sprite 8,294,190, : Sprite Update : Wait Vbl 
   Until Fire(1)=0
   ' Fade title screen then turn off Amal animation and Ship. 
   Fade 1 : Amal Off 14 : Sprite Off 8 : Wait 15
   '
   ' Unpack the Bonus Background to screen 1 then hide it.
   Unpack 11 To 1 : Screen Hide 1
   Screen 1
   ' Double Buffer screen for no flicker,turn Autoback to manual,turn Update  
   ' off then Update every 2 for smoother animation.
   Double Buffer 
   Autoback 0 : Update Off : Update Every 2
   '
   ' Set the Asteroids starting positions.
   For Z=4 To 9
      AX(Z)=Rnd(272) : AY(Z)=-30 : ADY(Z)=Rnd(230)+250
      Limit Bob Z,48,0 To 272,200
      Bob Z,AX(Z),AY(Z),
   Next Z
   '
   ' Set the Amal channels for the 6 Asteroids.   
   Channel 6 To Bob 4
   Channel 7 To Bob 5
   Channel 8 To Bob 6
   Channel 9 To Bob 7
   Channel 10 To Bob 8
   Channel 11 To Bob 9
   '
   ' Set Amal animation string for an asteroid.   
   F$=F$+"A 0,(71,4)(72,4)(73,4)(74,4)(75,4)(76,4)(77,4)(78,4)(79,4)(80,4)(81,4)(82,4)(83,4)"
   Amal 6,F$ : Amal 7,F$ : Amal 8,F$ : Amal 9,F$ : Amal 10,F$ : Amal 11,F$
   Amal On 
   '
   ' Show Bonus screen. 
   Screen Show 1
   '
End Proc
'
' Fires the Player ship's lasers.
Procedure _MOVEMISSILE
   '
   If MISSILE=1
      ' Moves the laser upwards. 
      MY=MY-6
      ' Makes player fire one laser at a time. 
      If MY<0
         MISSILE=0
         Bob Off 15
      End If 
      Bob 15,MX-135,MY-60,70
   End If 
   '
   If Joy(1) and 16
      ' Delay when you can next fire a laser.
      If MSLDELAY>20 or MISSILE=0
         ' Plays the Laser sfx. 
         Sam Bank 6 : Sam Play %1100,5
         MISSILE=1
         ' Sets the laser's starting position to that of the ship.  
         MX=SX : MY=SY
         ' Resets the Delay.
         MSLDELAY=0
      End If 
   End If 
   ' Counting the delay before you can next fire the laser. 
   MSLDELAY=MSLDELAY+1
   '
End Proc
'
' Moves the Asteroids. 
Procedure ALIENS
   '
   ' For..Next loop goes through each asteroid updating it. 
   For Z=4 To 9
      If BONUS=0 Then Inc COUNT
      ' Moves the asteroid.
      AY(Z)=AY(Z)+ADY(Z)
      ' Resets asteroid if it reaches the bottom of the screen and increases 
      ' its speed. 
      If AY(Z)>230 : AY(Z)=-30 : AX(Z)=Rnd(272) : ADY(Z)=Rnd(Z)+1+(Timer/SPEED) : End If 
      If AX(Z)<56 : AX(Z)=Rnd(272) : End If 
      ' Limit Asteroid to the playing area.
      Limit Bob Z,48,0 To 272,200
      Bob Z,AX(Z),AY(Z),
   Next Z
   '
End Proc
'
' Checks collision between laser and Asteroids.
Procedure _MISSCOLLISION
   '
   If Bob Col(15,4 To 9)=-1
      Screen 2
      ' Update SC and EXSC then convert score to a string to be printed. 
      SC=SC+25 : EXSC=EXSC+25
      C$=Str$(SC) : Text 144,20,C$
      '
      Screen 1
      For C=4 To 9
         ' Checks which Asteroid is hit.
         If Col(C)=-1
            ' Turn off laser.
            Bob Off 15
            MISSILE=0
            ' Call EXPLODE procedure.
            _EXPLODE
         End If 
      Next C
   End If 
   '
End Proc
'
' Checks collision between the Player's ship and Asteroids.
Procedure _SHIPCOLLISION
   '
   If Spritebob Col(8,4 To 9)
      For C=4 To 9
         ' Checks which asteroid is hit.
         If Col(C)=-1
            DELAY=1
            ' Calls the relevant procedures. 
            _EXPLODE
            _SHIPDEATH
            ' Turn off laser.  
            MISSILE=0
            Bob Off 15
         End If 
      Next C
   End If 
   ' Timer to allow your ship to finish exploding before you exit out of Bonus level.   
   If DELAY>0 : DELAY=DELAY+1 : End If 
   If DELAY>12 Then DEAD=1
   '
End Proc
'
' Player's Ship explosion animation. 
Procedure _SHIPDEATH
   '
   ' Set explosion bob position to that of the Player's ship. 
   SDX=X Screen(X Sprite(8)) : SDY=Y Screen(Y Sprite(8))
   Bob 10,SDX,SDY,
   Limit Bob 10,48,0 To 272,200
   ' Set the Amal animation.
   Channel 13 To Bob 10
   H$=H$+"A 1,(84,2)(85,2)(86,2)(87,2)(88,2)(89,2)(90,2)(91,2)(92,2)(93,2)(58,2);"
   Amal 13,H$ : Amal On 13
   ' Send player ship off screen.   
   SX=0 : SY=0
   Sprite Update 
   Wait Vbl 
   '
End Proc
'
' Asteroid explosion animation.
Procedure _EXPLODE
   '
   ' Play Explosion sfx.
   Sam Bank 6 : Sam Play %11,6
   ' Sends Asteroid off screen. 
   AY(C)=240
   ' Set explosion bob position to that of the Asteroid.  
   EXX=X Bob(C) : EXY=Y Bob(C)
   Bob 11,EXX,EXY,
   Limit Bob 11,48,0 To 272,200
   ' Set Amal animation.
   Channel 12 To Bob 11
   G$=G$+"A 1,(84,2)(85,2)(86,2)(87,2)(88,2)(89,2)(90,2)(91,2)(92,2)(93,2)(58,2);"
   Amal 12,G$ : Amal On 12
   '
End Proc
