rem --------------------------------------------
rem -------------  thanks to   -----------------
rem ------  CVirus for the Menu system  --------
rem ---   JSM for the High Score Library  ------
rem -- and of course MOPZ for creating NaaLaa --
rem ------  and sharing it with us -------------
rem ------  visit www.NaaLaa.com  -------------- 
rem --------------------------------------------
rem -----  any questions can be answered on ----
rem --   http://forum.retrogamecoding.org/ -----
rem --------------------------------------------
rem ----- sound files from freesound.org -------
rem --------------------------------------------

rem ---------import required libraries----------
rem --------------------------------------------

 import "Speed.lib"
 import "Keycodes.lib"
 import "SimpleParticle.lib"
 import "HighScore.lib"


 constant:
	BASE_IMAGE  0
	EXP_IMAGE 1
	HEIGHT 240
	WIDTH 320
	CROSS_HAIRS_IMAGE 2
	CITY_IMAGE 3
	MISSILE_IMAGE 5
	BG_IMAGE 6

	MISSILE_SOUND 0
	EXP_SOUND 1

	MISSILE_FIRED 2
	MISSILE_TARGET_X 3
	MISSILE_TARGET_Y 4
	MISSILE_NEW_X 5
	MISSILE_NEW_X_INC 6
	MISSILE_NEW_Y 10
	MISSILE_NEW_Y_INC 11
	MISSILE_TIMER 7
	MISSILE_TIMER2 8
	
	MISSILE_EXP_TIMER 12


   ENEMY_MISSILE_FIRED 14
	ENEMY_MISSILE_START_X 24
	ENEMY_MISSILE_START_Y 25


	ENEMY_MISSILE_TARGET_X 15
	ENEMY_MISSILE_TARGET_Y 16
	ENEMY_MISSILE_NEW_X 17
	ENEMY_MISSILE_NEW_X_INC 18
	ENEMY_MISSILE_NEW_Y 19
	ENEMY_MISSILE_NEW_Y_INC 20
	ENEMY_MISSILE_TIMER 21
	ENEMY_MISSILE_TIMER2 22
	
	ENEMY_MISSILE_EXP_TIMER 23


	CITY_X 26
	CITY_Y 27
	CITY_OK 28
	CITY_MAX 6
	


 hidden:

 	set window 0,0, WIDTH,HEIGHT	,true,2
 	set redraw off

rem select the .png image if Java is running
if javac()
	img$ = ".png"
else
	img$ = ".bmp"
endif

	rem mopz.
	if javac() then load font 0, "data/courier.txt", "data/courier.png"
	set color 0, 0, 0
	cls
	set color 255, 255, 255
	set caret 160, 100
	center "Loading ..."
	redraw


rem ------------   load images/soundfiles .....--------------------
	rem base is 36w 18h
	load image BASE_IMAGE, "data/base" + img$
	set image colorkey BASE_IMAGE,0,0,0

	load image 	EXP_IMAGE , "data/fuzz" + img$
	set image grid 	EXP_IMAGE , 3, 1

	rem cross hairs are 15h 15w
	load image CROSS_HAIRS_IMAGE, "data/crosshairs" + img$
	set image colorkey CROSS_HAIRS_IMAGE,0,0,0

	rem city are 30h 15w
	load image CITY_IMAGE, "data/city" + img$
	set image colorkey CITY_IMAGE,0,0,0
	
	load image MISSILE_IMAGE,"data/missile" + img$
	set image colorkey MISSILE_IMAGE,0,0,0

   load image BG_IMAGE,"data/example_screen" + img$

rem sound files
	load sound MISSILE_SOUND,"data/missile_sound2.wav"
	load sound EXP_SOUND,"data/explosion_sound2.wav"
rem =======================================================

rem create a high score list if necessary
if not HS_Load("hs_list.txt")
	proc HS_Init 10, "Michael", 800
endif


 visible:
randomize time()


rem -----------  base  variables  ------------------
base2x= 160
base2y = 200 + height(BASE_IMAGE)


rem  ---------------  missile variables  -----------

missiles_left = 25

MISSILE_MAX = 100

MISSILE#[MISSILE_MAX][20]

for a = 0 to MISSILE_MAX - 1
 rem 0.0 if false .......1.0 is true
	MISSILE[a][MISSILE_FIRED]  = 0.0
	MISSILE[a][MISSILE_TARGET_X] = -50.0
	MISSILE[a][MISSILE_TARGET_Y] = -50.0
	MISSILE[a][MISSILE_NEW_X] = 132.0
	MISSILE[a][MISSILE_NEW_X_INC] = -6.0
	MISSILE[a][MISSILE_NEW_Y] = 200.0 + float(height(BASE_IMAGE))
	MISSILE[a][MISSILE_NEW_Y_INC] = -6.0
	MISSILE[a][MISSILE_TIMER] = 1.0
	MISSILE[a][MISSILE_TIMER2] = 100.0
	MISSILE[a][MISSILE_EXP_TIMER] = 1.0
next

missile_alpha = 65
missile_alpha_inc = 8
missile_available

rem =====================================

rem ------------   enemy missile variables   ---------------

ENEMY_MISSILE_MAX = 100

ENEMY_MISSILE#[ENEMY_MISSILE_MAX][30]

for a = 0 to ENEMY_MISSILE_MAX - 1
 rem 0.0 if false .......1.0 is true
	ENEMY_MISSILE[a][ENEMY_MISSILE_FIRED]  = 0.0

	ENEMY_MISSILE[a][ENEMY_MISSILE_START_X] = float(rnd(320))
	ENEMY_MISSILE[a][ENEMY_MISSILE_START_Y] = - 2.0


	ENEMY_MISSILE[a][ENEMY_MISSILE_TARGET_X] = float(rnd(320))
	ENEMY_MISSILE[a][ENEMY_MISSILE_TARGET_Y] = 200.0 + float(height(BASE_IMAGE))

	ENEMY_MISSILE[a][ENEMY_MISSILE_TIMER] = 1.0
	ENEMY_MISSILE[a][ENEMY_MISSILE_TIMER2] = 100.0
	ENEMY_MISSILE[a][ENEMY_MISSILE_EXP_TIMER] = 1.0


	ENEMY_MISSILE[a][ENEMY_MISSILE_NEW_X] = ENEMY_MISSILE[a][ENEMY_MISSILE_START_X]
	ENEMY_MISSILE[a][ENEMY_MISSILE_NEW_X_INC] = (ENEMY_MISSILE[a][ENEMY_MISSILE_TARGET_X] - ENEMY_MISSILE[a][ENEMY_MISSILE_START_X]) / ENEMY_MISSILE[a][ENEMY_MISSILE_TIMER2]

	ENEMY_MISSILE[a][ENEMY_MISSILE_NEW_Y] = ENEMY_MISSILE[a][ENEMY_MISSILE_START_Y]
	ENEMY_MISSILE[a][ENEMY_MISSILE_NEW_Y_INC] = (ENEMY_MISSILE[a][ENEMY_MISSILE_TARGET_Y] - ENEMY_MISSILE[a][ENEMY_MISSILE_START_Y]) / ENEMY_MISSILE[a][ENEMY_MISSILE_TIMER2]
next

	
enemy_missile_alpha = 65
enemy_missile_alpha_inc = 8

enemy_missile_release_timer = 0
enemy_missile_released = 0
enemy_missile_allowed = 10
enemy_missile_fired = 0
enemy_missile_alive = 0

enemy_missile_drawing_timer = 0

rem decrease the speed variable to increase speed
enemy_missile_speed = 20

rem decrease the frequency variable to increase release speed
enemy_missile_frequency = 200

rem ---  this is the radius that explosions check for ------
rem ------ a larger figure makes it easier ----------
rem ----- was 10 in v3---------------------------
en_missile_radius# 
rem =========================================================

rem  ----------------------   city variables   ---------------

cities[CITY_MAX][60]
cityx[] = [10,55,100,190,235,280]
cityy[]=[203,203,203,203,203,203]

for a = 0 to CITY_MAX - 1
	cities[a][CITY_X] = cityx[a]
	cities[a][CITY_Y] = cityy[a]
	cities[a][CITY_OK] = true
next
surviving_cities = 0

rem ==========================================================

score = 0
level = 1
start_level = true
firstrun = true
alpha = 65
alpha_timer = 0
alpha_inc = 1

name$
proc SP_Init 100

shouldDraw = true

rem ====================================================================================================
rem =====================================   start of game loop   =======================================
rem ====================================================================================================
 do
rem ------------------------------------------------------------------------------------------
rem ---------------------------   game logic   -----------------------------------------------
rem ------------------------------------------------------------------------------------------
	
if firstrun = true
	proc MenuScreen
endif

firstrun = false

rem ------------  set up variables based on level   ----------
if start_level = true
	rem ************
	if level = 1
	rem ************
		missiles_left = 20
		ENEMY_MISSILE_MAX = 15
		MISSILE_MAX = 25
		rem decrease the speed variable to increase speed
		enemy_missile_speed = 15
		rem decrease the frequency variable to increase release speed
		enemy_missile_frequency = 150
		enemy_missile_allowed = 10
	rem ************
	elseif level = 2
	rem ************
		missiles_left = 20
		ENEMY_MISSILE_MAX = 25
		MISSILE_MAX = 20
		enemy_missile_speed = 12
		enemy_missile_frequency = 125
		enemy_missile_allowed = 17
	rem ************
	elseif level = 3
	rem ************
		missiles_left = 17
		ENEMY_MISSILE_MAX = 35
		MISSILE_MAX = 17
		enemy_missile_speed = 9
		enemy_missile_frequency = 100
		enemy_missile_allowed = 15
	rem ************
	elseif level = 4
	rem ************
		missiles_left = 15
		ENEMY_MISSILE_MAX = 45
		MISSILE_MAX = 15
		enemy_missile_speed = 6
		enemy_missile_frequency = 75
		enemy_missile_allowed = 15
	rem ************
	elseif level = 5
	rem ************
		missiles_left = 20
		ENEMY_MISSILE_MAX = 45
		MISSILE_MAX = 15
		enemy_missile_speed = 6
		enemy_missile_frequency = 70
		enemy_missile_allowed = 20
	rem ************
	elseif level = 6
	rem ************
		missiles_left = 25
		ENEMY_MISSILE_MAX = 45
		MISSILE_MAX = 15
		enemy_missile_speed = 5
		enemy_missile_frequency = 65
		enemy_missile_allowed = 25
	rem ************
	elseif level = 7
	rem ************
		missiles_left = 40
		ENEMY_MISSILE_MAX = 45
		MISSILE_MAX = 15
		enemy_missile_speed = 4
		enemy_missile_frequency = 60
		enemy_missile_allowed = 38
	rem ************
	elseif level = 8
	rem ************
		missiles_left = 40
		ENEMY_MISSILE_MAX = 45
		MISSILE_MAX = 15
		enemy_missile_speed = 4
		enemy_missile_frequency = 60
		enemy_missile_allowed = 35
	rem ************
	elseif level > 8
	rem ************
		missiles_left = 30
		ENEMY_MISSILE_MAX = 45
		MISSILE_MAX = 15
		enemy_missile_speed = 4
		enemy_missile_frequency = 60
		enemy_missile_allowed = 20
	endif
		start_level = false
endif
rem ----  picks up the mouse coordinates ----
rem ----    and hides the mouse pointer  ----
rem -- these are used later for the cross hair sight --
	
	set mouse off
	x=mousex()
	y=mousey()

rem =========================================


rem  -----   changes display transparency   ------
alpha = alpha + alpha_inc
if alpha > 192 or alpha < 64 then alpha_inc = -alpha_inc

rem  -----   timers   ------
missile_timer = missile_timer + 1
rem ---- this tests whether the missile has reached the target height yet
rem ---- it then initiates an explosion and tests for collision 
for a = 0 to MISSILE_MAX - 1
	if MISSILE[a][MISSILE_NEW_Y] <= MISSILE[a][MISSILE_TARGET_Y]
		MISSILE[a][MISSILE_EXP_TIMER] = MISSILE[a][MISSILE_EXP_TIMER] + 1.0
		if MISSILE[a][MISSILE_EXP_TIMER] < 150.0
			if MISSILE[a][MISSILE_EXP_TIMER] = 10.0 then play sound EXP_SOUND
			proc SP_AddPoof EXP_IMAGE , -2, int(MISSILE[a][MISSILE_TARGET_X]) , int(MISSILE[a][MISSILE_TARGET_Y]) , 8, 4.0, 1.5, 0.045, false,true
		else 
			MISSILE[a][MISSILE_TARGET_Y] = -50.0
	endif

rem =============================================================================================
rem ================================   collision test   =========================================
rem =============================================================================================

for b = 0 to ENEMY_MISSILE_MAX - 1
	if MISSILE[a][MISSILE_TARGET_X] > ENEMY_MISSILE[b][ENEMY_MISSILE_NEW_X] - en_missile_radius and MISSILE[a][MISSILE_NEW_Y] <= MISSILE[a][MISSILE_TARGET_Y]
		if MISSILE[a][MISSILE_TARGET_X] < ENEMY_MISSILE[b][ENEMY_MISSILE_NEW_X] + en_missile_radius and MISSILE[a][MISSILE_NEW_Y] <= MISSILE[a][MISSILE_TARGET_Y]
			if MISSILE[a][MISSILE_TARGET_Y] > ENEMY_MISSILE[b][ENEMY_MISSILE_NEW_Y] - en_missile_radius and MISSILE[a][MISSILE_NEW_Y] <= MISSILE[a][MISSILE_TARGET_Y]
				if MISSILE[a][MISSILE_TARGET_Y] < ENEMY_MISSILE[b][ENEMY_MISSILE_NEW_Y] + en_missile_radius and MISSILE[a][MISSILE_NEW_Y] <= MISSILE[a][MISSILE_TARGET_Y]
					score = score + 10
					rem ----   the following 9 lines reset the enemy missile, ready to fire again
					ENEMY_MISSILE[b][ENEMY_MISSILE_TARGET_X] = float(rnd(320))
					ENEMY_MISSILE[b][ENEMY_MISSILE_TARGET_Y] = 200.0 + float(height(BASE_IMAGE))

					ENEMY_MISSILE[b][ENEMY_MISSILE_START_X] = float(rnd(320))
					ENEMY_MISSILE[b][ENEMY_MISSILE_NEW_X] = ENEMY_MISSILE[b][ENEMY_MISSILE_START_X]
					ENEMY_MISSILE[b][ENEMY_MISSILE_NEW_X_INC] = (ENEMY_MISSILE[b][ENEMY_MISSILE_TARGET_X] - ENEMY_MISSILE[b][ENEMY_MISSILE_START_X]) / ENEMY_MISSILE[b][ENEMY_MISSILE_TIMER2]

					ENEMY_MISSILE[b][ENEMY_MISSILE_START_Y] = - 2.0
					ENEMY_MISSILE[b][ENEMY_MISSILE_NEW_Y] = ENEMY_MISSILE[b][ENEMY_MISSILE_START_Y]
					ENEMY_MISSILE[b][ENEMY_MISSILE_NEW_Y_INC] = (ENEMY_MISSILE[b][ENEMY_MISSILE_TARGET_Y] - ENEMY_MISSILE[b][ENEMY_MISSILE_START_Y]) / ENEMY_MISSILE[b][ENEMY_MISSILE_TIMER2]

					ENEMY_MISSILE[b][ENEMY_MISSILE_FIRED] = 0.0
   
				endif
			endif
		endif
	endif
next
		MISSILE[a][MISSILE_FIRED]  = 0.0
		MISSILE[a][MISSILE_TIMER] = 1.0
		MISSILE[a][MISSILE_NEW_X] = -20.0
	endif
next

rem =============================================================================================
rem ================================  end of  collision test   ==================================
rem =============================================================================================

rem ---  release one of the enemy missiles periodically
rem ---  using a timer
enemy_missile_drawing_timer = (enemy_missile_drawing_timer + 1 ) % enemy_missile_speed
enemy_missile_release_timer = (enemy_missile_release_timer + 1 ) % enemy_missile_frequency
if enemy_missile_release_timer = 1 and enemy_missile_fired < enemy_missile_allowed
	ENEMY_MISSILE[enemy_missile_released][ENEMY_MISSILE_FIRED] = 1.0
	enemy_missile_released = enemy_missile_released + 1
	enemy_missile_fired = enemy_missile_fired + 1
	if	enemy_missile_released > ENEMY_MISSILE_MAX - 1
		enemy_missile_released = 0
	endif
endif
rem --------   makes the missile trails "shimmer"   ------------
rem -----   by altering the transparency of them    ------------
	missile_alpha = missile_alpha + missile_alpha_inc
if missile_alpha > 255 or missile_alpha < 64
	missile_alpha_inc = - missile_alpha_inc
endif 
rem ============================================================
rem -------------------------------   call   procedures   -----------------------------------------------
rem mopz.
proc fire shouldDraw
proc hit_base
proc SP_UpdateParticles
rem ======================  end of p[rocedure calls  =============================	
rem ---  this tests whether the level has been completed
rem ---  first by checking the number of enemy missiles on screen
	enemy_missile_alive = 0

	for a = 0 to ENEMY_MISSILE_MAX - 1
		if ENEMY_MISSILE[a][ENEMY_MISSILE_FIRED] = 1.0
			enemy_missile_alive = enemy_missile_alive + 1
		endif
	next
rem --- if all enemy missiles have expired, the level is complete
	if  enemy_missile_fired >= enemy_missile_allowed and enemy_missile_alive = 0
		wait 1000
		proc next_level
	endif
rem  ================================================================================================
rem  ---------  missiles - calculate the missile trail  ---------------

for a = 0 to MISSILE_MAX - 1
	 rem 0.0 if false .......1.0 is true
	if MISSILE[a][MISSILE_FIRED]  = 1.0
		MISSILE[a][MISSILE_EXP_TIMER] = 0.0
		
		MISSILE[a][MISSILE_NEW_X] = MISSILE[a][MISSILE_NEW_X] - MISSILE[a][MISSILE_NEW_X_INC] 
		MISSILE[a][MISSILE_NEW_Y] = MISSILE[a][MISSILE_NEW_Y] - MISSILE[a][MISSILE_NEW_Y_INC]
	endif
next

rem  =========================================================
rem -----   logic for releasing enemy missiles  -----------------
for a = 0 to ENEMY_MISSILE_MAX - 1
	if ENEMY_MISSILE[a][ENEMY_MISSILE_FIRED] = 1.0
		if enemy_missile_drawing_timer = 0
			ENEMY_MISSILE[a][ENEMY_MISSILE_NEW_X] = ENEMY_MISSILE[a][ENEMY_MISSILE_NEW_X] + ENEMY_MISSILE[a][ENEMY_MISSILE_NEW_X_INC]  
			ENEMY_MISSILE[a][ENEMY_MISSILE_NEW_Y] = ENEMY_MISSILE[a][ENEMY_MISSILE_NEW_Y] + ENEMY_MISSILE[a][ENEMY_MISSILE_NEW_Y_INC] 
		endif

			if ENEMY_MISSILE[a][ENEMY_MISSILE_NEW_Y] > float(HEIGHT)
				ENEMY_MISSILE[a][ENEMY_MISSILE_TARGET_X] = float(rnd(320))
				ENEMY_MISSILE[a][ENEMY_MISSILE_TARGET_Y] = 200.0 + float(height(BASE_IMAGE))

				ENEMY_MISSILE[a][ENEMY_MISSILE_START_X] = float(rnd(320))
				ENEMY_MISSILE[a][ENEMY_MISSILE_NEW_X] = ENEMY_MISSILE[a][ENEMY_MISSILE_START_X]
				ENEMY_MISSILE[a][ENEMY_MISSILE_NEW_X_INC] = (ENEMY_MISSILE[a][ENEMY_MISSILE_TARGET_X] - ENEMY_MISSILE[a][ENEMY_MISSILE_START_X]) / ENEMY_MISSILE[a][ENEMY_MISSILE_TIMER2]


				ENEMY_MISSILE[a][ENEMY_MISSILE_START_Y] = - 2.0
				ENEMY_MISSILE[a][ENEMY_MISSILE_NEW_Y] = ENEMY_MISSILE[a][ENEMY_MISSILE_START_Y]
				ENEMY_MISSILE[a][ENEMY_MISSILE_NEW_Y_INC] = (ENEMY_MISSILE[a][ENEMY_MISSILE_TARGET_Y] - ENEMY_MISSILE[a][ENEMY_MISSILE_START_Y]) / ENEMY_MISSILE[a][ENEMY_MISSILE_TIMER2]

				ENEMY_MISSILE[a][ENEMY_MISSILE_FIRED] = 0.0
			endif
	endif
next




rem ----------------------------------------------------------------------------
rem ----------------------------------------------------------------------------
rem -------------------   drawing routines   -----------------------------------
rem ----------------------------------------------------------------------------
rem ----------------------------------------------------------------------------
if shouldDraw	
		set color 0,0,32
 			cls
		set color 255,255,255
	
rem  ---------  missiles - draw the missile trail  ---------------

for a = 0 to MISSILE_MAX - 1
	if MISSILE[a][MISSILE_FIRED]  = 1.0
		MISSILE[a][MISSILE_EXP_TIMER] = 0.0
			set color 255,0,0,missile_alpha
			draw line base2x , base2y-1 ,int(MISSILE[a][MISSILE_NEW_X]) ,int(MISSILE[a][MISSILE_NEW_Y]) 
	endif
next

rem  =========================================================

	set color 255,255,255
	draw image BASE_IMAGE,160-width(BASE_IMAGE)/2,200
	draw image CROSS_HAIRS_IMAGE,x - 7,y - 7

rem -----   draw enemy missiles  -----------------
for a = 0 to ENEMY_MISSILE_MAX - 1
	draw line int(ENEMY_MISSILE[a][ENEMY_MISSILE_START_X]),int(ENEMY_MISSILE[a][ENEMY_MISSILE_START_Y]),int(ENEMY_MISSILE[a][ENEMY_MISSILE_NEW_X]),int(ENEMY_MISSILE[a][ENEMY_MISSILE_NEW_Y])
	draw line int(ENEMY_MISSILE[a][ENEMY_MISSILE_START_X])+2,int(ENEMY_MISSILE[a][ENEMY_MISSILE_START_Y]),int(ENEMY_MISSILE[a][ENEMY_MISSILE_NEW_X])+2,int(ENEMY_MISSILE[a][ENEMY_MISSILE_NEW_Y])
next

rem ----   tests if Cities are still "alive"
rem ----   and draws them if so.....
if shouldDraw
	for a = 0 to CITY_MAX - 1
		if cities[a][CITY_OK] = true
			draw image CITY_IMAGE,cities[a][CITY_X] ,cities[a][CITY_Y]
		endif
	next
endif

	rem -- shows missile count
	rem -- in the red base

	set caret WIDTH/2,203
	center "",missiles_left

rem -----  warning message when missiles are running low ------
if missiles_left < 6 and missiles_left > 0
		set caret WIDTH/2,HEIGHT/3
		set color 128,128,0,alpha
		center "YOU'RE RUNNING OUT OF MISSILES...."
endif

if missiles_left = 0
		set caret WIDTH/2,HEIGHT/2 
		set color 128,0,0,alpha
	center "YOU'VE NOW RUN OUT OF MISSILES...."
endif


	rem -- shows score and enemy missiles left at the bottom of the screen 

	set caret 160,222
	center "SCORE    ",score,"          ENEMIES LEFT   ",enemy_missile_allowed - enemy_missile_fired 
	rem ======================================================================

	rem --  show high score and level at top of screen
	set caret 80,0
	set color 0,128,0,255
	z = HS_GetScore(0)
	center "HIGH SCORE"
	center z

	set caret 260,0
	center "LEVEL"
	center level
	set color 255,255,255

	proc SP_DrawParticles

	 redraw
endif
	 shouldDraw = SPD_HoldFrameDraw(60)
	 until keydown(27) or not running()

rem procedures  ------------------------------    procedures   ------
rem ----------------------------------
rem mopz
procedure fire(sd)

missile_available = 0

if missiles_left > 0
	for a = 0 to MISSILE_MAX - 1
 		rem 0.0 if false .......1.0 is true
		if MISSILE[a][MISSILE_FIRED]  = 0.0 and mousebutton(0,true)
			MISSILE[a][MISSILE_TARGET_X] = float(x)
			MISSILE[a][MISSILE_TARGET_Y] = float(y)
			missiles_left = missiles_left - 1
			MISSILE[a][MISSILE_NEW_X] = float(base2x) 
			MISSILE[a][MISSILE_NEW_X_INC] = (float(base2x) - MISSILE[a][MISSILE_TARGET_X]) / MISSILE[a][MISSILE_TIMER2] 
			MISSILE[a][MISSILE_NEW_Y] = float(base2y) 
			MISSILE[a][MISSILE_NEW_Y_INC] = (float(base2y) - MISSILE[a][MISSILE_TARGET_Y]) / MISSILE[a][MISSILE_TIMER2] 
			MISSILE[a][MISSILE_FIRED]  = 1.0
			play sound MISSILE_SOUND
		endif
	next
endif

rem ---    warning when missiles are low
rem ---        count them first 
for a = 0 to MISSILE_MAX - 1
 rem 0.0 if false .......1.0 is true
	if MISSILE[a][MISSILE_FIRED]  = 0.0
		missile_available = missile_available + 1
	endif
next

endproc
rem -------------------------------------
procedure hit_base()
	rem test for cities destruction
for a = 0 to ENEMY_MISSILE_MAX - 1
for b = 0 to CITY_MAX - 1
	if cities[b][CITY_OK] = true
		if ENEMY_MISSILE[a][ENEMY_MISSILE_NEW_X] > float( cities[b][CITY_X])
			if ENEMY_MISSILE[a][ENEMY_MISSILE_NEW_X] < float( cities[b][CITY_X] + width(CITY_IMAGE))
				if ENEMY_MISSILE[a][ENEMY_MISSILE_NEW_Y] > float( cities[b][CITY_Y] )
					if ENEMY_MISSILE[a][ENEMY_MISSILE_NEW_Y] < float( cities[b][CITY_Y] + height(CITY_IMAGE)) + 2.0
						play sound EXP_SOUND

						cities[b][CITY_OK] = false
						proc SP_AddPoof EXP_IMAGE , -2, cities[b][CITY_X] + width(CITY_IMAGE) / 2,  cities[b][CITY_Y] + height(CITY_IMAGE) / 2 , 8, 4.0, 1.5, 0.025, false,true						
					endif
				endif
			endif
		endif
	endif
next
next


rem test whether end of game has been reached
surviving_cities = 0
rem --- count the cities left
for a = 0 to CITY_MAX - 1
	if cities[a][CITY_OK] = true
		surviving_cities = surviving_cities + 1
	endif
next
if surviving_cities = 0
	set color 0,0,128
						cls
						set color 255,255,255	
						set caret 160,90
						center "All of your Cities"
						center "have been destroyed ....."
                  redraw
                  wait 4000
	proc high_score
	proc end_game
endif

rem ----- test for base destruction
for a = 0 to ENEMY_MISSILE_MAX - 1
		if ENEMY_MISSILE[a][ENEMY_MISSILE_NEW_X] > 160.0 - float(width(BASE_IMAGE)/2)
			if ENEMY_MISSILE[a][ENEMY_MISSILE_NEW_X] < float(160 + width(BASE_IMAGE)/2)
				rem the y level for the base is higher as much of the top of the image is transparent
				if ENEMY_MISSILE[a][ENEMY_MISSILE_NEW_Y] > 205.0
					if ENEMY_MISSILE[a][ENEMY_MISSILE_NEW_Y] < float(200 + height(BASE_IMAGE))
                  play sound EXP_SOUND

						set color 0,0,128
						cls
						set color 255,255,255	
						set caret 160,90
						center "Your Missile Base"
						center "has been destroyed ....."
                  redraw
                  wait 4000
						proc high_score
	               proc end_game		
					endif
				endif
			endif
		endif
next

endproc
rem --------------------------------------------------------------
procedure next_level()

set color 0,0,128
cls
set color 255,255,255

level = level + 1
enemy_missile_fired = 0
rem ----- as levels increase the missiles have to be a little more accurate
if en_missile_radius > 12.0 then en_missile_radius = en_missile_radius - 1.0
rem ----  bonus calculation =----------------

surviving_cities = 0

for a = 0 to CITY_MAX - 1
	if cities[a][CITY_OK] = true
		surviving_cities = surviving_cities + 1
	endif
next
score = score + (surviving_cities * 100) + (missiles_left * 20)

set caret WIDTH/2,40
if missiles_left = 1
	center "You have ",missiles_left," missile left"
else
	center "You have ",missiles_left," missiles left"
endif
center ""

xpos2 = 163 - (missiles_left * 6)
for a = 0 to missiles_left - 1
	draw image MISSILE_IMAGE,xpos2,60
		xpos2 = xpos2 + 12
next
	center "giving you a bonus of ",missiles_left * 20
	center "--------------------------------- "

if surviving_cities = 1
	center "You also have ",surviving_cities ," city left"
else
	center "You also have ",surviving_cities ," cities left"
endif

xpos = 165 - (surviving_cities * 20) 

for a = 0 to surviving_cities - 1
	draw image CITY_IMAGE,xpos,125
	xpos = xpos + 40
next
center " "
center " "
center "giving you a bonus of ",surviving_cities * 100
center "--------------------------------- "
center " "                              
center "Next level is level ", level
redraw


rem - kills off any on-going explosions
for a = 0 to MISSILE_MAX - 1
	MISSILE[a][MISSILE_EXP_TIMER] = 151.0
next


wait 8000
missiles_left = MISSILE_MAX
start_level = true
endproc
rem ------------------------------------------------------------------------------------------
procedure end_game()
set color 0,0,128
cls
set color 255,255,255

set caret 160,90
center "Game Over"
redraw

rem ---  reset the variables, ready for the next game
for a = 0 to ENEMY_MISSILE_MAX - 1
	ENEMY_MISSILE[a][ENEMY_MISSILE_TARGET_X] = float(rnd(320))
	ENEMY_MISSILE[a][ENEMY_MISSILE_TARGET_Y] = 200.0 + float(height(BASE_IMAGE))


	ENEMY_MISSILE[a][ENEMY_MISSILE_START_X] = float(rnd(320))
	ENEMY_MISSILE[a][ENEMY_MISSILE_NEW_X] = ENEMY_MISSILE[a][ENEMY_MISSILE_START_X]
	ENEMY_MISSILE[a][ENEMY_MISSILE_NEW_X_INC] = (ENEMY_MISSILE[a][ENEMY_MISSILE_TARGET_X] - ENEMY_MISSILE[a][ENEMY_MISSILE_START_X]) / ENEMY_MISSILE[a][ENEMY_MISSILE_TIMER2]

	ENEMY_MISSILE[a][ENEMY_MISSILE_START_Y] = - 2.0
	ENEMY_MISSILE[a][ENEMY_MISSILE_NEW_Y] = ENEMY_MISSILE[a][ENEMY_MISSILE_START_Y]
	ENEMY_MISSILE[a][ENEMY_MISSILE_NEW_Y_INC] = (ENEMY_MISSILE[a][ENEMY_MISSILE_TARGET_Y] - ENEMY_MISSILE[a][ENEMY_MISSILE_START_Y]) / ENEMY_MISSILE[a][ENEMY_MISSILE_TIMER2]

	ENEMY_MISSILE[a][ENEMY_MISSILE_FIRED] = 0.0
next

for a = 0 to CITY_MAX - 1
	cities[a][CITY_OK] = true
next

start_level = true
score = 0
missiles_left = MISSILE_MAX
enemy_missile_allowed = ENEMY_MISSILE_MAX
enemy_missile_fired = 0
level = 1


wait 4000
proc MenuScreen
endproc
rem -------------------------------------------------
procedure high_score()

rem check to see if High score qualifies for table
didQualify = HS_IsQualified(score)

set caret 160,0
center "YOUR SCORE: ",score
if didQualify
	proc get_name
endif

if HS_AddEntry(name, score)
						set color 0,0,128
						cls
						set color 255,255,255	
						set caret 160,110

		
		center "Congratulations........... "
		center " you qualified as one of"
		center " the highest scorers..."
else
						set color 0,0,128
						cls
						set color 255,255,255	
						set caret 160,110

		center "Sorry, you did not qualify for "
		center "the high score list......."
endif
redraw

set color 0, 0, 128
cls
set color 255, 255, 255

set caret 160,0
center "YOUR SCORE: ",score
set caret 85, 30
center "Name"
set caret 225, 30
set justification right
write "Score"

	for i = 0 to 9

		set caret 70, i*16 + 50
		set justification left
		write HS_GetName(i)

		set caret 220, i*16 + 50
		set justification right
		write HS_GetScore(i)
	next

redraw
	wait 4000

if not HS_Save("hs_list.txt")
	rem could not save list for some reason.
endif

rem --- reset variables ready for next game
for a = 0 to ENEMY_MISSILE_MAX - 1
	ENEMY_MISSILE[a][ENEMY_MISSILE_TARGET_X] = float(rnd(320))
	ENEMY_MISSILE[a][ENEMY_MISSILE_TARGET_Y] = 200.0 + float(height(BASE_IMAGE))


	ENEMY_MISSILE[a][ENEMY_MISSILE_START_X] = float(rnd(320))
	ENEMY_MISSILE[a][ENEMY_MISSILE_NEW_X] = ENEMY_MISSILE[a][ENEMY_MISSILE_START_X]
	ENEMY_MISSILE[a][ENEMY_MISSILE_NEW_X_INC] = (ENEMY_MISSILE[a][ENEMY_MISSILE_TARGET_X] - ENEMY_MISSILE[a][ENEMY_MISSILE_START_X]) / ENEMY_MISSILE[a][ENEMY_MISSILE_TIMER2]

	ENEMY_MISSILE[a][ENEMY_MISSILE_START_Y] = - 2.0
	ENEMY_MISSILE[a][ENEMY_MISSILE_NEW_Y] = ENEMY_MISSILE[a][ENEMY_MISSILE_START_Y]
	ENEMY_MISSILE[a][ENEMY_MISSILE_NEW_Y_INC] = (ENEMY_MISSILE[a][ENEMY_MISSILE_TARGET_Y] - ENEMY_MISSILE[a][ENEMY_MISSILE_START_Y]) / ENEMY_MISSILE[a][ENEMY_MISSILE_TIMER2]

	ENEMY_MISSILE[a][ENEMY_MISSILE_FIRED] = 0.0
next

for a = 0 to CITY_MAX - 1
	cities[a][CITY_OK] = true
next


start_level = true
score = 0
missiles_left = MISSILE_MAX
enemy_missile_allowed = ENEMY_MISSILE_MAX
enemy_missile_fired = 0
level = 1

rem proc end_game

endproc

rem -----------------------------------------------------
procedure get_name()

rem Clear screen and display list.
	set color 0, 0, 128
	cls
	set color 255, 255, 255
	
	rem Get name.
set caret 160,0
center "YOUR SCORE: ",score

set redraw on
	set caret 160, 40
	center
	center "Well done - you are one"
	center "of the top 10 scorers"
	center "Enter your name below.."
	center ""
	name$ = rln$(10)
	
set redraw off

endproc
rem ----------------------------    menu   ---------------------------------------------
rem ------------------------------------------------------------------------------------
rem ------------------------------------------------------------------------------------
rem --------------------------  nothing else is below this    --------------------------
rem ------------------------------------------------------------------------------------
rem ------------------------------------------------------------------------------------
rem ------------------------------------------------------------------------------------
rem ------------------------------------------------------------------------------------
procedure MenuScreen()

rem ---- reset all variables so that the option to "Start Game" will work ....
rem ---- ....if not, that option will just trigger the EndGame menu again ..


do


menuItem = 0
do
rem mopz.
if not running() then end
if keydown(VK_DOWN,true) and menuItem < 6 then menuItem = menuItem + 1
if keydown(VK_UP,true) and menuItem > 0 then menuItem = menuItem - 1
set color 0,0,0
cls
set color 255,255,255,64
draw image BG_IMAGE,0,0
rem draw image BANNER, 320 - width(BANNER)/2,50
set caret 160, 25
set color 0,255,255
center "Use Keys To Move in The Menu"
center "Press Space to Select an Item"
set caret 160, 80
rem --------- 
rem -----------------------  start of menu display   ---------------------------------
rem ---------

rem if menuItem = 0; set color 0,255,255; else; set color 0,96,128;
rem level = 1
rem endif
rem center "START A FULL GAME"
rem --------- 

if menuItem = 1; set color 0,255,255; else; set color 0,96,128;
endif
center "HOW TO PLAY THE GAME"
rem --------- 

if menuItem = 2; set color 0,255,255; else; set color 0,96,128;
rem level=2
endif
center "PLAY AN EASY GAME"


rem --------------
if menuItem = 3; set color 0,255,255; else; set color 0,96,128;
rem level=4
endif
center "PLAY A HARD GAME"

rem --------- 
if menuItem = 4; set color 0,255,255; else; set color 0,96,128;
endif
center"SEE THE HIGH SCORES"
rem --------- 
if menuItem = 5; set color 0,255,255; else; set color 0,96,128;
endif
center"QUIT THE GAME"


rem --------- 
if menuItem = 0; set color 0,255,255; else; set color 0,96,128;
endif
redraw
wait 10
until keydown(VK_SPACE,true)

if menuItem = 1
set color 0,0,0
cls
set color 255,0,255
set caret 160,10
rem center "++++++++++++++++++++++++++++++++++++"
center "***** HOW TO PLAY *****"
rem center "++++++++++++++++++++++++++++++++++++"
center ""
set color 0,255,255
center "Your Mission is to Destroy" 
center "all alien missiles"
center ""
center "Use the mouse to move the target" 

center "Sight and Left click to fire"
center""
center "The game ends when all cities" 
center "are destroyed or a missile" 
center "hits your missile base"
center""
set color 255,255,255
center "Press any key to continue..."
redraw
wait keydown

rem ------------------------
elseif menuItem = 4
set color 0, 0, 128
cls
set color 255, 255, 255

set caret 160,0
set caret 85, 10
center "Name"
set caret 225, 10
set justification right
write "Score"

	for i = 0 to 9

		set caret 70, i*16 + 30
		set justification left
		write HS_GetName(i)

		set caret 220, i*16 + 30
		set justification right
		write HS_GetScore(i)
	next

set caret 160,200

center "Press any key to continue..."
redraw
wait keydown



rem ------------------------
rem elseif menuItem = 0
rem level = 1
rem donotreturn = true
elseif menuItem = 2
en_missile_radius# = 22.0 
donotreturn = true
elseif menuItem = 3
en_missile_radius# = 14.0 
donotreturn = true
rem ------------
elseif menuItem = 5
end

endif

rem until menuItem = 0 or menuItem = 2 or menuItem = 3
until menuItem = 2 or menuItem = 3

endproc
