' Mighty Line source code by Lachie D. (Aug, 2007)
' Compiler: FreeBASIC v.0.18

' Reading this code may cause blindness and impotence.
' Highly un-optimized, full of bad code and patches in
' order to complete the game in time. So I don't care!

#include "fbgfx.bi"
Using FB
#define ri(x) (int(rnd*x))
' FMOD declarations.
#include "fmod.bi"

const FALSE = 0
const TRUE = 1
const fpslimit = 42
const numofmisctraces = 780
const numofexplosions = 20
const numofpowerups = 8
const numofparticles = 90
CONST linecenterx = 320
CONST linecentery = 220

const SCR_WIDTH = 640
const SCR_HEIGHT = 480
const PI = 3.141593
const XMID = SCR_WIDTH \ 2
const YMID = SCR_HEIGHT \ 2

' Sound/music subs/functions.
DECLARE SUB setSoundVolume
DECLARE SUB playsample (plsnd as integer ptr)
DECLARE SUB playStreamMusic (musc As String)

DECLARE SUB stopStreamMusic ()
DECLARE SUB InitiateLineTrace ()
DECLARE SUB DrawLineTrace ()
DECLARE SUB InitiateProjectile (ptyp AS INTEGER, pangle AS DOUBLE, side AS INTEGER)
DECLARE SUB ResetProjectiles ()
DECLARE SUB DrawProjectiles ()
DECLARE SUB InitiateMiscTrace (ttyp AS INTEGER, tx AS SINGLE, ty AS SINGLE)
DECLARE SUB InitiateMiscTrace2 (ttyp AS INTEGER, tx AS SINGLE, ty AS SINGLE)
DECLARE SUB InitParticle (partx AS SINGLE, party AS SINGLE, parttyp AS INTEGER)
DECLARE SUB DrawMoveParticles ()
DECLARE SUB DrawMiscTraces ()
DECLARE SUB DrawMiscTraces2 ()
DECLARE SUB DrawMoveBalls ()
DECLARE SUB DrawClock ()
DECLARE SUB DrawLine ()
DECLARE SUB InitPhase (phase AS INTEGER)
DECLARE SUB RandomizeBall (rndball AS INTEGER, mode AS INTEGER)
DECLARE SUB CheckForCollision (colproj AS INTEGER)
DECLARE SUB InitiateExplosion (etyp AS INTEGER, ex AS SINGLE, ey AS SINGLE)
DECLARE SUB DrawExplosions ()
DECLARE SUB InitiatePowerUp (powx AS SINGLE, powy AS SINGLE, powtyp AS INTEGER)
DECLARE SUB DrawPowerUps ()
DECLARE SUB ResetVariables ()
DECLARE SUB TitleScreen ()
DECLARE SUB DrawMoveShips ()
DECLARE SUB RandomizeShip (rndship AS INTEGER)
DECLARE SUB InitiateScrollText (istring AS STRING)
DECLARE SUB DrawScrollingText ()
DECLARE SUB MainLoop ()
DECLARE SUB GenerateBackground ()
DECLARE SUB FadeInScreen ()
DECLARE SUB FadeOutScreen ()
DECLARE SUB Options ()
DECLARE SUB WriteHighScore ()
DECLARE SUB ReadHighScore ()
DECLARE SUB InputName () 
DECLARE SUB Instructions ()
DECLARE SUB PhaseControl ()
DECLARE SUB CalculateScore ()
DECLARE SUB neoENCpass (Offset AS ANY PTR, Length AS INTEGER, password AS STRING)
DECLARE SUB neoENCdepass (Offsett AS ANY PTR, Lengthh AS INTEGER, passsword AS STRING) 

declare function dist (byval x as single,byval  y as single, xc() as single,  yc() as single) as single
declare sub Rel_Tri_F( Byval x1 As Integer, Byval y1 As Integer,_
               Byval x2 As Integer, Byval y2 As Integer, Byval x3 As Integer,_
               Byval y3 As Integer, Byval r As integer, Byval g As integer, Byval b As integer,_
               Byval a As integer)

declare sub draw_line_width( byval x1 as single, byval y1 as single, byval x2 as single, byval y2 as single,_
					 byval lwidth as single, byval r as integer, byval g as integer, byval b as integer,_
					 byval alpha as integer)
                     
declare Sub GenPlasma(byval w     as integer, byval h     as integer, _
              byval crnr1 as integer, byval crnr2 as integer, _
              byval crnr3 as integer, byval crnr4 as integer, _
              byval rough as integer, byval iter  as integer, _
              byval prs   as double)
declare sub Rainbow (bluecol AS INTEGER, greencol AS INTEGER, redcol AS INTEGER)

declare sub DrawFlare (fposx AS SINGLE, fposy AS SINGLE, flradius AS INTEGER)
 
dim shared as integer Grad(0 To 255)

dim shared as integer flx, fly, fldx, fldy, flcr, flcg, flcb
dim shared as single flr, flc

TYPE SpriteType
X AS SINGLE
Y AS SINGLE
DTime AS INTEGER
Angle AS DOUBLE
Speed AS SINGLE
Active AS INTEGER
Typ AS INTEGER
Energy AS INTEGER
LoopPass AS INTEGER
RotationSpeed AS INTEGER
AngleDeg AS INTEGER
Alive AS INTEGER
END TYPE

TYPE PowerUpType
X AS SINGLE
Y AS SINGLE
DTime AS INTEGER
Active AS INTEGER
Angle AS SINGLE
Typ AS INTEGER
Speed AS SINGLE
END TYPE

TYPE ScrollTextType
X AS SINGLE
Y AS SINGLE
SText AS STRING*50
Active AS INTEGER
ABlender AS INTEGER
END TYPE

DIM SHARED workpage AS INTEGER
DIM SHARED st AS DOUBLE
DIM SHARED mx AS INTEGER, my AS INTEGER, mbutton AS INTEGER
DIM SHARED FullScreen AS INTEGER, FPS2 AS INTEGER
DIM SHARED lineangle AS INTEGER, taranglerad AS SINGLE, tarangledeg AS SINGLE, lineradangle AS DOUBLE, lineradangle2 AS SINGLE, lineradangle3 AS SINGLE, lineradangle4 AS SINGLE
DIM SHARED FPS AS INTEGER, StartTime AS DOUBLE
DIM SHARED seconds AS INTEGER, rndangle AS INTEGER
DIM SHARED sinang AS DOUBLE, cosang AS DOUBLE, costang AS DOUBLE, sintang AS DOUBLE
DIM SHARED linecol AS UINTEGER, numofballs AS INTEGER, bsection AS INTEGER
DIM SHARED Frame1 AS INTEGER, Frame2 AS INTEGER, zeross AS STRING, ar2 AS INTEGER, score AS INTEGER, sbonus AS INTEGER
DIM SHARED highname AS STRING, playername AS STRING
DIM SHARED startwrite AS INTEGER, playl AS INTEGER, countlet AS INTEGER, br AS INTEGER, m AS INTEGER, n AS INTEGER
DIM SHARED scores(4) AS SINGLE
DIM SHARED textarray(9) AS STRING
DIM SHARED playerletter(8) AS STRING
DIM SHARED blinker AS STRING
DIM SHARED inputted_letter  AS STRING
DIM SHARED cpoint(3) AS UINTEGER, ballcol(3) AS UINTEGER
DIM SHARED cloop1 AS INTEGER, cloop1direc AS INTEGER
dim shared distbuffer( SCR_WIDTH - 1, SCR_HEIGHT - 1 ) as single
DIM shared xcoords1(120) as single
DIM shared ycoords1(120) as single
DIM shared xcoords2(40) as single
DIM shared ycoords2(40) as single
DIM SHARED ballspr AS INTEGER
DIM SHARED endline AS INTEGER, startline AS INTEGER
DIM SHARED rndnum1 AS INTEGER
DIM SHARED DoSwing AS INTEGER
DIM SHARED MenuPos AS INTEGER
DIM SHARED MenuY AS INTEGER
DIM SHARED MenuPosBlend(5) AS INTEGER
DIM SHARED LineTrace(90) AS SpriteType
DIM SHARED Ball(40) AS SpriteType
DIM SHARED Ship(40) AS SpriteType
DIM SHARED Particle (numofparticles) AS SpriteType
DIM SHARED MiscTrace (numofmisctraces) AS SpriteType
DIM SHARED MiscTrace2 (30) AS SpriteType
DIM SHARED Projectile(90) AS SpriteType
DIM SHARED ScrollText(5) AS ScrollTextType
DIM SHARED Explosion(numofexplosions) AS SpriteType
DIM SHARED speedup AS SINGLE
DIM SHARED bimageblend AS INTEGER
DIM SHARED sfxv AS STRING, musicv AS STRING, grmode AS STRING
DIM SHARED CurrentWeapon AS INTEGER, Weapon2Ammo AS INTEGER, Weapon3Ammo AS INTEGER
Dim SHARED frameintvl As Double = 1.0/fpslimit
Dim SHARED sleepintvl As Integer
DIM SHARED reload AS INTEGER
DIM SHARED buffer AS UINTEGER PTR
Dim SHARED image_buffer(4) As Any Ptr
Dim SHARED fadeimg_buffer(2) As Any Ptr
Dim SHARED pball_buffer(10) As Any Ptr
Dim SHARED nball_buffer(10) As Any Ptr
DIM SHARED addx AS INTEGER, addy AS INTEGER
DIM SHARED cmisct as integer
DIM SHARED KeyFree AS INTEGER
DIM SHARED Energy AS SINGLE, encol AS UINTEGER
DIM SHARED wblend(1) AS INTEGER
DIM SHARED PowerUp(numofpowerups) AS PowerupType
DIM SHARED countkill AS INTEGER
DIM SHARED lineanglespeed AS INTEGER
DIM SHARED EndGame AS INTEGER
DIM SHARED EndGameCount AS INTEGER
DIM SHARED numofships AS INTEGER
DIM SHARED resultAngle AS SINGLE, resultangledeg AS INTEGER
DIM SHARED MAXPOINTS AS INTEGER
DIM SHARED currentbackg AS INTEGER
DIM SHARED CountPhase AS INTEGER
DIM SHARED nextpowerup AS INTEGER
DIM SHARED dont_respawn AS INTEGER
DIM SHARED rndpowtyp AS INTEGER
DIM SHARED changebackground AS INTEGER
DIM SHARED oldbackground AS INTEGER
DIM SHARED backchangeblend AS INTEGER
dim shared checkforalive as integer
DIM shared checkifhit AS INTEGER
DIM SHARED weaponlock AS INTEGER
DIM SHARED targetmode AS INTEGER
DIM SHARED count_left_energy AS INTEGER
DIM SHARED count_finished AS INTEGER
DIM SHARED count_scoringscreen AS INTEGER
DIM SHARED ShowFPS AS INTEGER
' Sound related variables.
DIM SHARED FMODStatus AS INTEGER
DIM SHARED PlaySound AS INTEGER
DIM SHARED sample(18) AS INTEGER PTR
DIM SHARED music_handle AS INTEGER PTR
DIM SHARED SFXVolume AS INTEGER, MusicVolume AS INTEGER, SetVol AS INTEGER, GraphicMode AS INTEGER

' Powerup variables
DIM SHARED ShortLineTime AS INTEGER
DIM SHARED LongLineTime AS INTEGER
DIM SHARED KillerLineTime AS INTEGER
DIM SHARED DoubleGunTime AS INTEGER
DIM SHARED NoHoleTime AS INTEGER
DIM SHARED SlowLineTime AS INTEGER
DIM SHARED HomingTime AS INTEGER
DIM SHARED KillerLineWidth AS INTEGER
DIM SHARED raiseenergy AS INTEGER

DIM SHARED setvolold AS INTEGER
DIM SHARED volumedirection AS INTEGER
DIM SHARED shifting_volume AS INTEGER

SCREENRES 640, 480, 32, 2, GFX_ALPHA_PRIMITIVES+GFX_WINDOWED
SCREENSET 1, 0
SETMOUSE 50,50,0

image_buffer(1) = ImageCreate(640, 480)
image_buffer(2) = ImageCreate(640, 480)
image_buffer(3) = ImageCreate(640, 480)
image_buffer(4) = ImageCreate(640, 480)
fadeimg_buffer(1) = ImageCreate(640, 480)

FOR cballbuff AS INTEGER = 1 TO 10
   pball_buffer(cballbuff) = ImageCreate(18, 18)
   nball_buffer(cballbuff) = ImageCreate(18, 18) 
NEXT cballbuff

SFXVolume = 3
MusicVolume = 3
GraphicMode = 1

MenuPos = 1
MenuY = MenuPos*10

speedup = 1.2 ' Used to increase the pixel
              ' pixel speeds of game objects
              ' due the reduce of FPS during
              ' development.

FOR inittrace AS INTEGER = 1 TO 90
LineTrace(inittrace).DTime = 0 
NEXT inittrace

PlaySound = TRUE

FSOUND_Init(44100, 16, 0) 
IF FSOUND_Init(44100, 16, 0) = 0 THEN PlaySound = FALSE

' Load all the samples.
IF PlaySound = TRUE THEN
sample(1)=FSOUND_SAMPLE_Load(FSOUND_FREE,"Sounds/main_laser.ogg",0,0,0)
sample(2)=FSOUND_SAMPLE_Load(FSOUND_FREE,"Sounds/explosion1.ogg",0,0,0)
sample(3)=FSOUND_SAMPLE_Load(FSOUND_FREE,"Sounds/explosion2.ogg",0,0,0)
sample(4)=FSOUND_SAMPLE_Load(FSOUND_FREE,"Sounds/swing.ogg",0,0,0)
sample(5)=FSOUND_SAMPLE_Load(FSOUND_FREE,"Sounds/bzzz.ogg",0,0,0)
sample(6)=FSOUND_SAMPLE_Load(FSOUND_FREE,"Sounds/blasters.ogg",0,0,0)
sample(7)=FSOUND_SAMPLE_Load(FSOUND_FREE,"Sounds/looppass.ogg",0,0,0)
sample(8)=FSOUND_SAMPLE_Load(FSOUND_FREE,"Sounds/goodpickup.ogg",0,0,0)
sample(9)=FSOUND_SAMPLE_Load(FSOUND_FREE,"Sounds/badpickup.ogg",0,0,0)
sample(10)=FSOUND_SAMPLE_Load(FSOUND_FREE,"Sounds/linehit.ogg",0,0,0)
sample(11)=FSOUND_SAMPLE_Load(FSOUND_FREE,"Sounds/addscore.ogg",0,0,0)
sample(12)=FSOUND_SAMPLE_Load(FSOUND_FREE,"Sounds/misceffect.ogg",0,0,0)
sample(13)=FSOUND_SAMPLE_Load(FSOUND_FREE,"Sounds/tagged_hit.ogg",0,0,0)
END IF

GenerateBackground
titlescrn:
bimageblend = 0
stopStreamMusic
playStreamMusic("Sounds/LineMenu.mp3")
TitleScreen
'InitPhase 1
DoSwing = FALSE
stopStreamMusic
playStreamMusic("Sounds/MightyLine.mp3")
MainLoop
WHILE MULTIKEY(&h01)
WEND
GOTO titlescrn:

SUB MainLoop ()
    
DO
    
IF PlaySound = TRUE THEN FSOUND_Update

Frame1 = (Frame1 MOD 5) + 1

st = Timer

screenlock
screenset workpage, workpage xor 1

FPS = FPS + 1                     ' Used to count FPS.
if StartTime + 1 < timer then
 FPS2 = FPS
 FPS = 0
 seconds = seconds + 1
 StartTime = timer
end if

lineradangle = (lineangle*3.14159)/180

sinang = SIN(lineradangle)
cosang = COS(lineradangle)

IF MULTIKEY(SC_1) THEN CurrentWeapon = 1
IF MULTIKEY(SC_2) THEN CurrentWeapon = 2
IF MULTIKEY(SC_3) THEN CurrentWeapon = 3
IF (CurrentWeapon = 2 AND Weapon2Ammo = 0) THEN CurrentWeapon = 1
IF (CurrentWeapon = 3 AND Weapon3Ammo = 0) THEN CurrentWeapon = 1

'IF MULTIKEY(SC_F1) THEN currentbackg = 1
'IF MULTIKEY(SC_F2) THEN currentbackg = 2
'IF MULTIKEY(SC_F3) THEN currentbackg = 3
'IF MULTIKEY(SC_F4) THEN currentbackg = 4

IF weaponLock = TRUE THEN reload = 1
IF MULTIKEY(&h39) OR MULTIKEY(SC_UP) OR MULTIKEY(SC_X) THEN
IF reload = 0 THEN
IF CurrentWeapon = 1 THEN reload = 7
IF CurrentWeapon = 2 THEN reload = 3
IF CurrentWeapon = 3 THEN reload = 12
IF CurrentWeapon = 1 THEN 
    InitiateProjectile CurrentWeapon, lineradangle,1
    playsample (sample(1))
    IF DoubleGunTime > 0 THEN InitiateProjectile CurrentWeapon, lineradangle-PI,2
END IF
IF CurrentWeapon = 2 AND Weapon2Ammo > 0 THEN 
    'Weapon2Ammo = Weapon2Ammo - 1
    InitiateProjectile CurrentWeapon, lineradangle,1
    'playsample (sample(5))
    'IF DoubleGunTime > 0 THEN InitiateProjectile CurrentWeapon, lineradangle-PI,2
END IF
IF CurrentWeapon = 3 AND Weapon3Ammo > 0 THEN 
    Weapon3Ammo = Weapon3Ammo - 1
    InitiateProjectile CurrentWeapon, lineradangle,1
    playsample (sample(6))
    IF DoubleGunTime > 0 THEN InitiateProjectile CurrentWeapon, lineradangle-PI,2
END IF
END IF
END IF

IF reload>0 THEN reload=reload-1
IF Energy = 0 THEN reload = 1
IF raiseenergy > 0 THEN
Energy = Energy + 1
IF Energy > 100 THEN Energy = 100
raiseenergy = raiseenergy - 1
END IF

lineanglespeed = 4
IF SlowLineTime > 0 THEN lineanglespeed = 2

IF Energy > 0 THEN
IF MULTIKEY(&h4D) THEN
    IF DoSwing <> 3 THEN playsample (sample(4))
    DoSwing = 3
    lineangle=lineangle-lineanglespeed*speedup
    IF lineangle<0 THEN lineangle=lineangle+360
    lineradangle = (lineangle*3.14159)/180
    lineradangle2 = ((lineangle+1)*3.14159)/180
    lineradangle3 = ((lineangle+2)*3.14159)/180
    lineradangle4 = ((lineangle+3)*3.14159)/180
    InitiateLineTrace
END IF
IF MULTIKEY(&h4B) THEN 
    IF DoSwing <> 2 THEN playsample (sample(4))
    DoSwing = 2
    lineangle=lineangle+lineanglespeed*speedup
    IF lineangle>359 THEN lineangle=lineangle-360
    lineradangle = (lineangle*3.14159)/180
    lineradangle2 = ((lineangle-1)*3.14159)/180
    lineradangle3 = ((lineangle-2)*3.14159)/180
    lineradangle4 = ((lineangle-3)*3.14159)/180
    InitiateLineTrace
END IF
END IF

IF ShortLineTime > 0 THEN ShortLineTime = ShortLineTime - 1
IF LongLineTime > 0 THEN LongLineTime = LongLineTime - 1
IF KillerLineTime > 0 THEN KillerLineTime = KillerLineTime - 1
IF DoubleGunTime > 0 THEN DoubleGunTime = DoubleGunTime - 1
IF NoHoleTime > 0 THEN NoHoleTime = NoHoleTime - 1
IF SlowLineTime > 0 THEN SlowLineTime = SlowLineTime - 1
IF HomingTime > 0 THEN HomingTime = HomingTime - 1

'LINE (0,0)-(639,479), RGBA(0, 0, 0, 255), BF
IF changebackground = FALSE THEN PUT (0,0), image_buffer(currentbackg), PSET
IF changebackground = TRUE THEN
backchangeblend = backchangeblend + 4
IF backchangeblend > 255 THEN backchangeblend = 255
IF backchangeblend = 255 THEN changebackground = FALSE
PUT (0,0), image_buffer(oldbackground), ALPHA, 255-backchangeblend
PUT (0,0), image_buffer(currentbackg), ALPHA, backchangeblend
END IF

'Draw String (10,443), "Seconds:"+STR$(seconds), RGBA(2,117, 190, 255)

cmisct = 0
FOR cmt AS INTEGER = 1 TO numofmisctraces
IF MiscTrace(cmt).DTime>0 THEN cmisct = cmisct + 1    
NEXT cmt

'Draw String (10,453), "MiscTraces:"+STR$(cmisct), RGBA(2,117, 190, 255)

checkforalive = FALSE

DrawLineTrace      ' Draws line traces
DrawMiscTraces     ' Draws misc traces (ball traces, etc.)
DrawMiscTraces2    ' Draws secondary misc traces (an ugly patch to enable better collision with the laser!!!!)
DrawLine           ' Draws player's line
DrawMoveBalls      ' Draws/moves balls
DrawMoveShips      ' Draws/move ships
DrawProjectiles    ' Draws projectiles
DrawExplosions     ' Draws explosions
DrawMoveParticles
DrawPowerups

DrawScrollingText  ' Draws scrolling text
DrawClock          ' Draws the hourglass

LINE (429,6)-(631,6), RGBA(255, 255, 255, 255), BF
LINE (631,6)-(631,11), RGBA(255, 255, 255, 255), BF
LINE (429,6)-(429,11), RGBA(255, 255, 255, 255), BF
LINE (429,11)-(631,11), RGBA(255, 255, 255, 255), BF

FOR countenergy AS INTEGER = 1 TO Energy
encol = RGB(192,0,0)
IF Energy > 20 THEN encol = RGBA(254,155,2,190)
IF Energy > 40 THEN encol = RGBA(254,168,2,190)
IF Energy > 60 THEN encol = RGBA(233,254,2,190)
IF Energy > 80 THEN encol = RGBA(85,254,2,190)
LINE (430,7)-(430+Energy*2,10), encol, BF    
NEXT countenergy

IF EndGame = TRUE THEN
EndGameCount = EndGameCount + 6
IF EndGameCount > 2 AND volumedirection = 1 THEN
shifting_volume = shifting_volume - setvolold/60
IF shifting_volume <= 0 THEN
shifting_volume = 0
volumedirection = 2
    if PlaySound = TRUE then
        stopStreamMusic
        music_handle = FSOUND_Stream_Open("Sounds/JooGotPwned.ogg", FSOUND_LOOP_NORMAL, 0, 0 ) 
        FSOUND_Stream_SetMode(music_handle,FSOUND_LOOP_NORMAL) 
        FSOUND_Stream_Play(1, music_handle)
        FSOUND_SetVolumeAbsolute(1, shifting_volume)
    END IF	
END IF
if PlaySound = TRUE then FSOUND_SetVolumeAbsolute(1, shifting_volume)
END IF
IF volumedirection = 2 AND shifting_volume < setvolold THEN
shifting_volume = shifting_volume + setvolold/60
IF shifting_volume >= setvolold THEN
shifting_volume = setvolold
END IF
if PlaySound = TRUE then FSOUND_SetVolumeAbsolute(1, shifting_volume)
END IF

IF EndGameCount < 370 THEN
Draw String (EndGameCount-100,210), "G A M E", RGBA(187,249, 255, 200)
Draw String (740-EndGameCount,210), "O V E R", RGBA(187,249, 255, 200)
END IF
IF EndGameCount > 369 THEN
Draw String (370-100,210), "G A M E", RGBA(187,249, 255, 200)
Draw String (720-370,210), "O V E R", RGBA(187,249, 255, 200)
END IF
END IF

IF CountPhase = 8 THEN
EndGameCount = EndGameCount + 5
IF EndGameCount > 2 AND volumedirection = 1 THEN
shifting_volume = shifting_volume - setvolold/60
IF shifting_volume <= 0 THEN
shifting_volume = 0
volumedirection = 2
    if PlaySound = TRUE then
        stopStreamMusic
        music_handle = FSOUND_Stream_Open("Sounds/LineWin.ogg", FSOUND_LOOP_NORMAL, 0, 0 ) 
        FSOUND_Stream_SetMode(music_handle,FSOUND_LOOP_NORMAL) 
        FSOUND_Stream_Play(1, music_handle)
        FSOUND_SetVolumeAbsolute(1, shifting_volume)
    END IF	
END IF
if PlaySound = TRUE then FSOUND_SetVolumeAbsolute(1, shifting_volume)
END IF
IF volumedirection = 2 AND shifting_volume < setvolold THEN
shifting_volume = shifting_volume + setvolold/60
IF shifting_volume >= setvolold THEN
shifting_volume = setvolold
END IF
if PlaySound = TRUE then FSOUND_SetVolumeAbsolute(1, shifting_volume)
END IF
IF EndGameCount < 360 THEN
Draw String (EndGameCount-100,210), "R O U N D", RGBA(187,249, 255, 200)
Draw String (740-EndGameCount,210), "O V E R", RGBA(187,249, 255, 200)
END IF
IF EndGameCount > 359 THEN
Draw String (360-100,210), "R O U N D", RGBA(187,249, 255, 200)
Draw String (720-360,210), "O V E R", RGBA(187,249, 255, 200)
END IF
END IF

'IF EndGameCount = 36 OR EndGameCount = 35 THEN stopstreammusic

FOR cwblend AS INTEGER = 1 TO 3
    wblend(cwblend) = 200
NEXT cwblend
wblend(CurrentWeapon) = 255
IF weaponlock = FALSE THEN
Draw String (12,5), "Cannon: INF", RGBA(2,117, 190, wblend(1))
Draw String (140,5), "Laser:"+STR$(Weapon2Ammo), RGBA(2,117, 190, wblend(2))
Draw String (250,5), "Blasters:"+STR$(Weapon3Ammo), RGBA(2,117, 190, wblend(3))
END IF
IF weaponlock = TRUE THEN
Draw String (92,5), "W E A P O N S  L O C K E D", RGBA(234,33, 0, 240)
END IF

IF ShowFPS = TRUE THEN Draw String (13,456), "FPS:"+STR$(FPS2), RGBA(2,117, 190, 255)
zeross = ""
IF ABS(Score)<100000 THEN zeross = "0"
IF ABS(Score)<10000 THEN zeross = "00"
IF ABS(Score)<1000 THEN zeross = "000"
IF ABS(Score)<100 THEN zeross = "0000"
IF ABS(Score)<10 THEN zeross = "00000"
Draw String (13,466), "Score:"+STR$(zeross)+STR$(Score), RGBA(2,117, 190, 255)


Draw String (390,5), "Life:", RGBA(30,127, 200, 255)

FadeInScreen

workpage xor = 1
screenunlock

IF MULTIKEY(SC_F) AND ShowFPS = FALSE THEN
ShowFPS = TRUE
WHILE MULTIKEY(SC_F)
SLEEP 1
WEND
END IF
IF MULTIKEY(SC_F) AND ShowFPS = TRUE THEN
ShowFPS = FALSE
WHILE MULTIKEY(SC_F)
SLEEP 1
WEND
END IF

IF MULTIKEY(SC_ESCAPE) AND EndGame = FALSE THEN 
GET (0,0)-(639,479), fadeimg_buffer(1)
IF PlaySound = TRUE THEN FSOUND_SetPaused(1, 1)
GOTO abortscreen:
END IF

sleepintvl = Cint((st + frameintvl - Timer)*1000.0)
If sleepintvl>1 Then 
    Sleep sleepintvl
else 
    sleep 1
end if


IF MULTIKEY(&h4B) OR MULTIKEY(&h4D) THEN GOTO skipswingreset:
DoSwing = 1
skipswingreset:

IF EndGameCount > 1200 THEN 
GET (0,0)-(639,479), fadeimg_buffer(1)
GOTO endgame:
END IF

PhaseControl


gobackinmloop:
LOOP

abortscreen: ' abort screen loops
'-------------------------------

' Fade out the battle screen partialy.
bimageblend = 255
DO
st = Timer

screenlock
screenset workpage, workpage xor 1

LINE (0,0)-(639,479), RGBA(0, 0, 0, 255), BF
PUT (0,0), fadeimg_buffer(1), ALPHA, bimageblend
bimageblend = bimageblend - 15

workpage xor = 1

screenunlock

sleepintvl = Cint((st + frameintvl - Timer)*1000.0)
If sleepintvl>1 Then 
    Sleep sleepintvl
else 
    sleep 1
end if

LOOP UNTIL bimageblend < 150

' Partialy faded out screen with
' question loop.
DO

st = Timer
screenlock
screenset workpage, workpage xor 1

IF PlaySound = TRUE THEN FSOUND_Update 

LINE (0,0)-(639,479), RGBA(0, 0, 0, 255), BF
PUT (0,0), fadeimg_buffer(1), ALPHA, bimageblend
Draw String (280,225), "Abort(Y/N)?", RGBA(2,117, 190, 255)

workpage xor = 1
screenunlock

sleepintvl = Cint((st + frameintvl - Timer)*1000.0)
If sleepintvl>1 Then 
    Sleep sleepintvl
else 
    sleep 1
end if

IF MULTIKEY(SC_N) THEN
' Fade in the battle screen.
LINE (0,0)-(639,479), RGBA(0, 0, 0, 255), BF

DO

st = Timer
screenlock
screenset workpage, workpage xor 1

LINE (0,0)-(639,479), RGBA(0, 0, 0, 255), BF
PUT (0,0), fadeimg_buffer(1), ALPHA, bimageblend
bimageblend = bimageblend + 15
IF bimageblend > 255 THEN bimageblend = 255

workpage xor = 1
screenunlock

sleepintvl = Cint((st + frameintvl - Timer)*1000.0)
If sleepintvl>1 Then 
    Sleep sleepintvl
else 
    sleep 1
end if

LOOP UNTIL bimageblend = 255
IF PlaySound = TRUE THEN FSOUND_SetPaused(1, 0)
GOTO gobackinmloop:
END IF

LOOP UNTIL MULTIKEY(SC_Y)
stopstreammusic
endgame:
' Fade out the game screen completely.
DO

st = Timer
screenlock
screenset workpage, workpage xor 1

LINE (0,0)-(639,479), RGBA(0, 0, 0, 255), BF
PUT (0,0), fadeimg_buffer(1), ALPHA, bimageblend
bimageblend = bimageblend - 15
IF bimageblend < 0 THEN bimageblend = 0
workpage xor = 1

screenunlock

sleepintvl = Cint((st + frameintvl - Timer)*1000.0)
If sleepintvl>1 Then 
    Sleep sleepintvl
else 
    sleep 1
end if
LOOP UNTIL bimageblend = 0

endgamelabel:

IF EndGame = TRUE OR CountPhase = 8 THEN 
CalculateScore
WriteHighScore
END IF

END SUB


SUB InitiateLineTrace ()
    
FOR inittrace AS INTEGER = 1 TO 90
IF LineTrace(inittrace).DTime = 0 THEN
LineTrace(inittrace).DTime = 16
LineTrace(inittrace).Angle = lineradangle
GOTO nextline1:
END IF
NEXT inittrace

nextline1:
FOR inittrace AS INTEGER = 1 TO 90
IF LineTrace(inittrace).DTime = 0 THEN
LineTrace(inittrace).DTime = 14
LineTrace(inittrace).Angle = lineradangle2
GOTO nextline2:
END IF
NEXT inittrace

nextline2:
FOR inittrace AS INTEGER = 1 TO 90
IF LineTrace(inittrace).DTime = 0 THEN
LineTrace(inittrace).DTime = 12
LineTrace(inittrace).Angle = lineradangle3
GOTO nextline3:
END IF
NEXT inittrace

nextline3:
FOR inittrace AS INTEGER = 1 TO 90
IF LineTrace(inittrace).DTime = 0 THEN
LineTrace(inittrace).DTime = 10
LineTrace(inittrace).Angle = lineradangle4
EXIT SUB
END IF
NEXT inittrace

END SUB

SUB DrawLineTrace
    
FOR counttrace AS INTEGER = 1 TO 90
    
  IF LineTrace(counttrace).DTime > 0 THEN
    LineTrace(counttrace).DTime = LineTrace(counttrace).DTime - 1  
    sintang=SIN(LineTrace(counttrace).Angle)
    costang=COS(LineTrace(counttrace).Angle)
    LINE (linecenterx+sintang*startline,linecentery+costang*startline)-(linecenterx+sintang*endline,linecentery+costang*endline), RGBA(5,105,255, 21+LineTrace(counttrace).DTime*5)
    LINE (linecenterx-sintang*startline,linecentery-costang*startline)-(linecenterx-sintang*endline,linecentery-costang*endline), RGBA(5,105,255, 21+LineTrace(counttrace).DTime*5)

  END IF

NEXT counttrace
    
END SUB

SUB InitiateProjectile (ptyp AS INTEGER, pangle AS DOUBLE, side AS INTEGER)
  
  
  FOR initproj AS INTEGER = 1 TO 90
      
    IF Projectile(initproj).Active = FALSE THEN  
         Projectile(initproj).Angle = pangle
         Projectile(initproj).Speed = 4*speedup
         Projectile(initproj).Typ = ptyp
         Projectile(initproj).Active = TRUE
         Projectile(initproj).DTime = 70
         Projectile(initproj).RotationSpeed = 6
         IF Projectile(initproj).Typ = 3 THEN 
             Projectile(initproj).Speed = 3.4*speedup
             Projectile(initproj).DTime = 120
         END IF
         IF side = 1 THEN
         Projectile(initproj).X = linecenterx+SIN(lineradangle)*endline
         Projectile(initproj).Y = linecentery+COS(lineradangle)*endline
         END IF
         IF side = 2 THEN
         Projectile(initproj).X = linecenterx-SIN(lineradangle)*endline
         Projectile(initproj).Y = linecentery-COS(lineradangle)*endline
         END IF
         EXIT SUB
    END IF
    
  NEXT initproj
  
END SUB

SUB DrawProjectiles ()
    
FOR countproj AS INTEGER = 1 TO 90
      
    IF Projectile(countproj).Active = TRUE THEN
        IF Projectile(countproj).Typ = 2 THEN
             InitiateMiscTrace2 9, Projectile(countproj).X, Projectile(countproj).Y
             Projectile(countproj).Active = FALSE
             GOTO nextproj:
         END IF
         Projectile(countproj).DTime = Projectile(countproj).DTime - 1
         IF Projectile(countproj).DTime = 0 THEN Projectile(countproj).Active = FALSE
         IF Projectile(countproj).X > 645 OR Projectile(countproj).X < -5 OR Projectile(countproj).Y > 485 OR Projectile(countproj).Y < - 5 THEN Projectile(countproj).Active = FALSE
         CheckForCollision countproj
         Projectile(countproj).X = Projectile(countproj).X + SIN(Projectile(countproj).Angle)*Projectile(countproj).Speed
         Projectile(countproj).Y = Projectile(countproj).Y + COS(Projectile(countproj).Angle)*Projectile(countproj).Speed
         IF Projectile(countproj).Typ = 3 THEN
             InitiateMiscTrace 9, Projectile(countproj).X, Projectile(countproj).Y
             CIRCLE (Projectile(countproj).X, Projectile(countproj).Y), 4, RGBA(0,168,198,145), , , , F
             CIRCLE (Projectile(countproj).X, Projectile(countproj).Y), 3, RGBA(15,219,255,145), , , , F
         END IF
         IF Projectile(countproj).Typ = 1 THEN
         InitiateMiscTrace 1, Projectile(countproj).X, Projectile(countproj).Y
         LINE (Projectile(countproj).X, Projectile(countproj).Y)-(Projectile(countproj).X+SIN(Projectile(countproj).Angle)*3,Projectile(countproj).Y+COS(Projectile(countproj).Angle)*3), RGBA(175,175,175,255)
         LINE (Projectile(countproj).X, Projectile(countproj).Y)-(Projectile(countproj).X+SIN(Projectile(countproj).Angle)*2,Projectile(countproj).Y+COS(Projectile(countproj).Angle)*2), RGBA(128,128,128,255)
         LINE (Projectile(countproj).X, Projectile(countproj).Y)-(Projectile(countproj).X+SIN(Projectile(countproj).Angle)*1,Projectile(countproj).Y+COS(Projectile(countproj).Angle)*1), RGBA(243,196,53,255)
         END IF
         Projectile(countproj).AngleDeg = (Projectile(countproj).Angle*180)/PI
         
    END IF
nextproj:    
NEXT countproj
  
END SUB


SUB InitiateMiscTrace (ttyp AS INTEGER, tx AS SINGLE, ty AS SINGLE)
    
FOR inittrace AS INTEGER = 1 TO numofmisctraces
   IF MiscTrace(inittrace).DTime = 0 THEN
    MiscTrace(inittrace).DTime = 40
      MiscTrace(inittrace).X = tx
      MiscTrace(inittrace).Y = ty
      MiscTrace(inittrace).Typ = ttyp
      IF MiscTrace(inittrace).Typ = 1 THEN
         MiscTrace(inittrace).DTime = 35
      END IF
      IF MiscTrace(inittrace).Typ > 1 AND MiscTrace(inittrace).Typ > 6 THEN
         MiscTrace(inittrace).DTime = 40
      END IF
      IF MiscTrace(inittrace).Typ = 6 THEN
         MiscTrace(inittrace).DTime = 37
      END IF
      IF MiscTrace(inittrace).Typ = 9 THEN
         MiscTrace(inittrace).DTime = 32
      END IF
      IF MiscTrace(inittrace).Typ = 10 THEN
         MiscTrace(inittrace).DTime = 32
      END IF
      IF MiscTrace(inittrace).Typ > 11 THEN
         MiscTrace(inittrace).DTime = 37
      END IF
      EXIT SUB
   END IF
NEXT inittrace   

END SUB

SUB InitiateMiscTrace2 (ttyp AS INTEGER, tx AS SINGLE, ty AS SINGLE)
    
FOR inittrace AS INTEGER = 1 TO 30
   IF MiscTrace2(inittrace).DTime = 0 THEN
      MiscTrace2(inittrace).X = tx
      MiscTrace2(inittrace).Y = ty
      MiscTrace2(inittrace).Typ = ttyp
      IF MiscTrace2(inittrace).Typ = 9 THEN MiscTrace2(inittrace).DTime = 6
      EXIT SUB
   END IF
NEXT inittrace   

END SUB

SUB DrawMiscTraces ()
    
FOR counttrace AS INTEGER = 1 TO numofmisctraces
    
  IF MiscTrace(counttrace).DTime > 0 THEN
      MiscTrace(counttrace).DTime = MiscTrace(counttrace).DTime - 1 
      SELECT CASE MiscTrace(counttrace).Typ
      CASE 1
          PSET (MiscTrace(counttrace).X, MiscTrace(counttrace).Y), RGBA(243,174,53,30+MiscTrace(counttrace).DTime*2)
      
      CASE 2
          CIRCLE (MiscTrace(counttrace).X, MiscTrace(counttrace).Y), 1+MiscTrace(counttrace).DTime/5, RGBA(0,0,145, 50+MiscTrace(counttrace).DTime*2), , , , F
      CASE 3
          CIRCLE (MiscTrace(counttrace).X, MiscTrace(counttrace).Y), 1+MiscTrace(counttrace).DTime/5, RGBA(11,218,169, 10+MiscTrace(counttrace).DTime*2), , , , F
      CASE 4
          CIRCLE (MiscTrace(counttrace).X, MiscTrace(counttrace).Y), 1+MiscTrace(counttrace).DTime/5, RGBA(176,0,0, 70+MiscTrace(counttrace).DTime*2), , , , F
      CASE 5
          CIRCLE (MiscTrace(counttrace).X, MiscTrace(counttrace).Y), 1+MiscTrace(counttrace).DTime/5, RGBA(232,244,0, 70+MiscTrace(counttrace).DTime*2), , , , F
      CASE 6
          CIRCLE (MiscTrace(counttrace).X, MiscTrace(counttrace).Y), 3+MiscTrace(counttrace).DTime/5, RGBA(255,182,41, 70+MiscTrace(counttrace).DTime*2), , , , F
      CASE 7
          CIRCLE (MiscTrace(counttrace).X, MiscTrace(counttrace).Y), 1+MiscTrace(counttrace).DTime/5, RGBA(243,53,192, 20+MiscTrace(counttrace).DTime*2), , , , F
      CASE 9
          CIRCLE (MiscTrace(counttrace).X, MiscTrace(counttrace).Y), 1+MiscTrace(counttrace).DTime/7, RGBA(0,168,198, 5+MiscTrace(counttrace).DTime*2), , , , F
      CASE 10
          CIRCLE (MiscTrace(counttrace).X, MiscTrace(counttrace).Y), 1+MiscTrace(counttrace).DTime/7, RGBA(190,0,190, 25+MiscTrace(counttrace).DTime*2), , , , F
      CASE 11
          CIRCLE (MiscTrace(counttrace).X, MiscTrace(counttrace).Y), 1+MiscTrace(counttrace).DTime/7, RGBA(61,0,172, 25+MiscTrace(counttrace).DTime*2), , , , F
      CASE 12
          CIRCLE (MiscTrace(counttrace).X, MiscTrace(counttrace).Y), 1+MiscTrace(counttrace).DTime/6, RGBA(243,132,2, 20+MiscTrace(counttrace).DTime*2), , , , F
      CASE 13
          CIRCLE (MiscTrace(counttrace).X, MiscTrace(counttrace).Y), 1+MiscTrace(counttrace).DTime/6, RGBA(0,140,142, 20+MiscTrace(counttrace).DTime*2), , , , F
      
      END SELECT
  
  END IF
    
NEXT counttrace

END SUB

SUB DrawMiscTraces2 ()
    
FOR counttrace AS INTEGER = 1 TO 30
    
  IF MiscTrace2(counttrace).DTime > 0 THEN
      MiscTrace2(counttrace).DTime = MiscTrace(counttrace).DTime - 1 
      SELECT CASE MiscTrace2(counttrace).Typ
      CASE 9
          Weapon2Ammo = Weapon2Ammo - 1
          IF Weapon2Ammo < 0 THEN Weapon2Ammo = 0
          IF Frame1 = 5 THEN playsample (sample(5))
          FOR countlaser AS INTEGER = (endline + 5) TO 350
          PUT (linecenterx-13+sinang*countlaser, linecentery-13+cosang*countlaser), nball_buffer(10), ADD, 132    
          PSET (linecenterx+sinang*countlaser,linecentery+cosang*countlaser), RGBA(255, 255, 255, 255)
          IF DoubleGunTime > 0 THEN
          PUT (linecenterx-13-sinang*countlaser, linecentery-13-cosang*countlaser), nball_buffer(10), ADD, 132    
          PSET (linecenterx-sinang*countlaser,linecentery-cosang*countlaser), RGBA(255, 255, 255, 255)          
          END IF
          NEXT countlaser
      END SELECT
  
  END IF
    
NEXT counttrace

END SUB

SUB InitPhase (phase AS INTEGER)
    
    SELECT CASE phase
    CASE 1
       numofballs = 10
       numofships = 0
       FOR initball AS INTEGER = 1 TO numofballs
           Ball(initball).Typ = 1
           IF initball > 8 THEN Ball(initball).Typ = 2
       NEXT initball
    CASE 2
       numofballs = 20
       FOR initball AS INTEGER = 1 TO numofballs
           Ball(initball).Typ = 1
           IF initball > 9 THEN Ball(initball).Typ = 2
           IF initball > 15 THEN Ball(initball).Typ = 3
       NEXT initball
    CASE 3
       numofballs = 25
       FOR initball AS INTEGER = 1 TO numofballs
           Ball(initball).Typ = 1
           IF initball > 8 THEN Ball(initball).Typ = 2
           IF initball > 16 THEN Ball(initball).Typ = 3
           IF initball > 21 THEN Ball(initball).Typ = 4
       NEXT initball
    CASE 4
       numofballs = 15
       FOR initball AS INTEGER = 1 TO numofballs
           Ball(initball).Typ = 2
           IF initball > 4 THEN Ball(initball).Typ = 3
           IF initball > 6 THEN Ball(initball).Typ = 4
       NEXT initball
    CASE 5
       numofballs = 28
       FOR initball AS INTEGER = 1 TO numofballs
           Ball(initball).Typ = 2
           IF initball > 12 THEN Ball(initball).Typ = 3
           IF initball > 19 THEN Ball(initball).Typ = 4
           IF initball > 24 THEN Ball(initball).Typ = 5
       NEXT initball
       
    CASE 6
       numofballs = 0
       numofships = 8
       FOR initship AS INTEGER = 1 TO numofships
           Ship(initship).Typ = 1
           IF initship > 5 THEN Ship(initship).Typ = 2
       NEXT initship
    
    END SELECT
    
    FOR initball2 AS INTEGER = 1 TO numofballs
    RandomizeBall initball2, 2
    NEXT initball2
    FOR initship2 AS INTEGER = 1 TO numofships
    RandomizeShip initship2
    NEXT initship2
    
    
END SUB

SUB DrawMoveBalls ()

    FOR countball AS INTEGER = 1 TO numofballs
    IF Ball(countball).Alive = TRUE THEN 
            checkforalive = TRUE
            Ball(countball).X = Ball(countball).X + SIN(Ball(countball).Angle)*Ball(countball).Speed
            Ball(countball).Y = Ball(countball).Y - COS(Ball(countball).Angle)*Ball(countball).Speed
            
            IF Energy > 0 THEN
            FOR colcheck AS INTEGER = 1 TO 2
            IF colcheck = 1 THEN linecol = RGBA(231, 231, 231, 255)
            IF colcheck = 2 THEN linecol = RGBA(192, 0, 0, 255)
            FOR loopb AS INTEGER = 0 TO 4
            IF POINT (Ball(countball).X-loopb, Ball(countball).Y+loopb) = linecol OR POINT (Ball(countball).X+loopb, Ball(countball).Y-loopb) = linecol OR POINT (Ball(countball).X-loopb, Ball(countball).Y-loopb) = linecol OR POINT (Ball(countball).X+loopb, Ball(countball).Y+loopb) = linecol THEN 
                 IF KillerLineTime = 0 THEN
                 score = score - 30
                 playsample (sample(10))
                 checkifhit = TRUE
                 Energy = Energy - 1*Ball(countball).Typ
                 IF Ball(countball).Typ = 4 THEN Energy = Energy + 0.7 ' gameplay balancing
                 IF Ball(countball).Typ = 5 THEN Energy = Energy + 1.7 ' gameplay balancing
                 IF Energy < 0 THEN Energy = 0
                 END IF
                 IF KillerLineTime > 0 THEN
                     sbonus = 1
                 IF Ball(countball).LoopPass = TRUE THEN sbonus = 2
                     IF sbonus = 1 THEN score = score + (50 + Ball(countball).Typ * 15)
                     IF sbonus = 2 THEN
                     playsample (sample(13))
                     InitiateScrollText "Tagged Ball Bonus! + "+ STR$(1000+200*Ball(countball).Typ)+" Pts"
                     score = score + (1000 + 200*Ball(countball).Typ)
                     END IF
                     countkill = countkill + 1
                     IF countkill = nextpowerup THEN
                     nextpowerup = INT(RND * 4) + 20
                     countkill = 0
                     InitiatePowerup Ball(countball).X, Ball(countball).Y, 1
                     END IF
                 END IF
                 IF score < 0 THEN score = 0
                 InitParticle Ball(countball).X, Ball(countball).Y, Ball(countball).Typ
                 InitParticle Ball(countball).X-3, Ball(countball).Y, Ball(countball).Typ
                 InitParticle Ball(countball).X, Ball(countball).Y+3, Ball(countball).Typ
                 InitParticle Ball(countball).X-1, Ball(countball).Y, Ball(countball).Typ
                 InitiateExplosion 1, Ball(countball).X, Ball(countball).Y
                 InitiateExplosion 2, Ball(countball).X, Ball(countball).Y
                 InitiateExplosion 3, Ball(countball).X, Ball(countball).Y
                 InitiateExplosion 4, Ball(countball).X, Ball(countball).Y
                 InitiateExplosion 5, Ball(countball).X, Ball(countball).Y
                 RandomizeBall countball,1
                 GOTO nextball:
            END IF
            NEXT loopb
            NEXT colcheck
            END IF
         
            IF Ball(countball).X>690 OR Ball(countball).X < - 50 THEN RandomizeBall countball,1
            IF Ball(countball).Y>530 OR Ball(countball).Y < - 50 THEN RandomizeBall countball,1
            IF Frame1 = 4 AND Ball(countball).LoopPass = FALSE THEN InitiateMiscTrace Ball(countball).Typ+1, Ball(countball).X, Ball(countball).Y
            IF Frame1 = 4 AND Ball(countball).LoopPass = TRUE THEN InitiateMiscTrace 7, Ball(countball).X, Ball(countball).Y
            
            IF NoHoleTime = 0 AND Energy > 0 THEN
            PSET (linecenterx,linecentery), RGB(0, 0, 0)
            PSET (linecenterx+sinang*10,linecentery+cosang*10), RGB(0, 0, 0)
            PSET (linecenterx-sinang*10,linecentery-cosang*10), RGB(0, 0, 0)
            END IF

' Laser line collision.
FOR countpoint2 AS INTEGER = 1 TO 17
        
        addx = 0
        addy = 0
    SELECT CASE countpoint2
    CASE 1
        addx = 3
        addy = 3
    CASE 2
        addx = -3
        addy = 3    
    CASE 3
        addx = -3
        addy = -3    
    CASE 4
        addx = 3
        addy = -3
    CASE 5
        addx = 2
        addy = 2
    CASE 6
        addx = 2
        addy = -2
    CASE 7
        addx = -2
        addy = -2
    CASE 8
        addx = -2
        addy = 2
    CASE 9
        addx = 4
        addy = 4
    CASE 10
        addx = 4
        addy = -4
    CASE 11
        addx = -4
        addy = -4
    CASE 12
        addx = -4
        addy = 4
     CASE 13
        addx = 1
        addy = 1
    CASE 14
        addx = 1
        addy = -1
    CASE 15
        addx = -1
        addy = -1
    CASE 16
        addx = -1
        addy = 1        
    END SELECT
        
        cpoint(3) = POINT (Ball(countball).X+addx,Ball(countball).Y+addy)
            
        IF cpoint(3) = RGBA(255,245, 255, 255) OR cpoint(3) = RGBA(255,251, 255, 255) OR cpoint(3) = RGBA(255, 240, 255, 255) OR cpoint(3) = RGBA(254,254, 254, 255) OR cpoint(3) = RGBA(255,255, 255, 255) OR cpoint(3) = RGBA(254,255,255, 255) THEN
        IF (Ball(countball).X > 8 AND Ball(countball).X < 632) AND (Ball(countball).Y > 8 AND Ball(countball).Y < 471) THEN    
             Ball(countball).Energy = Ball(countball).Energy - 6
             InitiateExplosion 6, Ball(countball).X+addx, Ball(countball).Y+addy
             IF Ball(countball).Energy > 0 THEN goto nextball:
             IF Ball(countball).Energy <= 0 THEN
                 countkill = countkill + 1
                 IF countkill = nextpowerup THEN
                 nextpowerup = INT(RND * 4) + 20
                 countkill = 0
                 InitiatePowerup Ball(countball).X, Ball(countball).Y, 1
                 END IF
                 sbonus = 1
                 IF Ball(countball).LoopPass = TRUE THEN sbonus = 2
                 IF sbonus = 1 THEN score = score + (50 + Ball(countball).Typ * 15)
                 IF sbonus = 2 THEN
                 playsample (sample(13))
                 InitiateScrollText "Tagged Ball Bonus! + "+ STR$(1000+200*Ball(countball).Typ)+ " Pts"
                 score = score + (1000 + 200*Ball(countball).Typ)
                 END IF
                 InitiateExplosion 1, Ball(countball).X, Ball(countball).Y
                 InitParticle Ball(countball).X, Ball(countball).Y, Ball(countball).Typ
                 InitParticle Ball(countball).X-3, Ball(countball).Y, Ball(countball).Typ
                 InitParticle Ball(countball).X, Ball(countball).Y+3, Ball(countball).Typ
                 InitParticle Ball(countball).X-1, Ball(countball).Y, Ball(countball).Typ
                 InitiateExplosion 2, Ball(countball).X, Ball(countball).Y
                 InitiateExplosion 3, Ball(countball).X, Ball(countball).Y
                 InitiateExplosion 4, Ball(countball).X, Ball(countball).Y
                 InitiateExplosion 5, Ball(countball).X, Ball(countball).Y
                 RandomizeBall countball,1
             END IF
        END IF
        END IF

NEXT countpoint2

ballspr = Ball(countball).Typ
IF Ball(countball).LoopPass = TRUE THEN ballspr = 7
PUT (Ball(countball).X-8, Ball(countball).Y-8), nball_buffer(ballspr), TRANS
'PSET (Ball(countball).X,Ball(countball).Y), RGBA(255, 0, 0, 255)

cpoint(1) = POINT (linecenterx+sinang*10,linecentery+cosang*10)
cpoint(2) = POINT (linecenterx-sinang*10,linecentery-cosang*10)
cpoint(3) = POINT (linecenterx,linecentery)
            
FOR countpoint AS INTEGER = 1 TO 3
                
        IF Energy > 0 AND Ball(countball).LoopPass = FALSE AND cpoint(countpoint) <> RGB(0, 0, 0) AND NoHoleTime = 0 THEN
            Ball(countball).LoopPass = TRUE
            playsample (sample(7))
            Ball(countball).Energy = 1
            InitiateScrollText "Ball Tagged! + "+ STR$(50+15*Ball(countball).Typ)+" Pts"
            score = score + 50 + 15*Ball(countball).Typ
        END IF

NEXT countpoint
            
    nextball:
    END IF
    NEXT countballs
    
END SUB

SUB RandomizeBall (rndball AS INTEGER, mode AS INTEGER)
             
    Ball(rndball).Alive = FALSE
    bsection = 1 + INT(RND * 4)
    
    IF Mode = 1 THEN
    IF bsection = 1 THEN 
            Ball(rndball).X = -80 + INT(RND * 40) 
            Ball(rndball).Y = -20 + INT(RND * 500)
    END IF
    IF bsection = 2 THEN 
            Ball(rndball).X = 680 + INT(RND * 40) 
            Ball(rndball).Y = -20 + INT(RND * 500)
    END IF
    IF bsection = 3 THEN 
            Ball(rndball).X = -20 + INT(RND * 660) 
            Ball(rndball).Y = -80 + INT(RND * 40)
    END IF
    IF bsection = 4 THEN 
            Ball(rndball).X = -20 + INT(RND * 660) 
            Ball(rndball).Y = 490 + INT(RND * 40)
    END IF
    END IF

    IF Mode = 2 THEN
    IF bsection = 1 THEN 
            Ball(rndball).X = -120 + INT(RND * 80) 
            Ball(rndball).Y = -30 + INT(RND * 510)
    END IF
    IF bsection = 2 THEN 
            Ball(rndball).X = 680 + INT(RND * 80) 
            Ball(rndball).Y = -30 + INT(RND * 510)
    END IF
    IF bsection = 3 THEN 
            Ball(rndball).X = -30 + INT(RND * 670) 
            Ball(rndball).Y = -120 + INT(RND * 80)
    END IF
    IF bsection = 4 THEN 
            Ball(rndball).X = -30 + INT(RND * 670) 
            Ball(rndball).Y = 490 + INT(RND * 80)
    END IF
    END IF

    taranglerad = ATAN2((Ball(rndball).Y-linecentery),(Ball(rndball).X-linecenterx))
    taranglerad = taranglerad - PI/2 
    IF taranglerad<0 THEN taranglerad = taranglerad + 2*PI 
    tarangledeg = (taranglerad*180)/PI
    IF targetmode = 1 THEN rndangle = 1 + INT(RND * 70) - 35
    IF targetmode = 2 THEN rndangle = 1 + INT(RND * 32) - 16
    tarangledeg = tarangledeg + rndangle
    IF tarangledeg<0 THEN tarangledeg=tarangledeg+360
    IF tarangledeg>359 THEN tarangledeg=tarangledeg-360
    
    IF dont_respawn = FALSE THEN Ball(rndball).Alive = TRUE
    Ball(rndball).Angle = (tarangledeg*PI)/180
    Ball(rndball).LoopPass = FALSE
    SELECT CASE Ball(rndball).Typ
        CASE 1
         Ball(rndball).Speed = 1*speedup
         Ball(rndball).Energy = 1
        CASE 2
         Ball(rndball).Speed = 1.3*speedup
         Ball(rndball).Energy = 1
        CASE 3
         Ball(rndball).Speed = 1.5*speedup
         Ball(rndball).Energy = 2
        CASE 4
         Ball(rndball).Speed = 1.5*speedup
         Ball(rndball).Energy = 3
        CASE 5
         Ball(rndball).Speed = 1.6*speedup
         Ball(rndball).Energy = 4
    END SELECT
        
END SUB

SUB CheckForCollision (colproj AS INTEGER)
    
    FOR countcball AS INTEGER = 1 TO numofballs
        
        IF HomingTime > 0 AND Projectile(colproj).Typ <> 2 THEN
            
             taranglerad = ATAN2(Ball(countcball).X-Projectile(colproj).X, Ball(countcball).Y-Projectile(colproj).Y)
             IF taranglerad<0 THEN taranglerad = taranglerad + 2*PI 
             tarangledeg = (taranglerad*180)/PI
             
             IF Ball(countcball).Alive = TRUE AND Projectile(colproj).Active = TRUE AND ABS(Projectile(colproj).X-Ball(countcball).X)<60 AND ABS(Projectile(colproj).Y-Ball(countcball).Y)<60 THEN
                 
             IF Projectile(colproj).AngleDeg > tarangledeg THEN
                  IF (360-Projectile(colproj).AngleDeg+tarangledeg) >= (Projectile(colproj).AngleDeg-tarangledeg) THEN Projectile(colproj).AngleDeg = Projectile(colproj).AngleDeg - Projectile(colproj).RotationSpeed
                  IF (360-Projectile(colproj).AngleDeg+tarangledeg) < (Projectile(colproj).AngleDeg-tarangledeg) THEN Projectile(colproj).AngleDeg = Projectile(colproj).AngleDeg + Projectile(colproj).RotationSpeed
             END IF

             IF Projectile(colproj).AngleDeg < tarangledeg THEN
                  IF (360-tarangledeg+Projectile(colproj).AngleDeg) >= (tarangledeg-Projectile(colproj).AngleDeg) THEN Projectile(colproj).AngleDeg = Projectile(colproj).AngleDeg + Projectile(colproj).RotationSpeed
                  IF (360-tarangledeg+Projectile(colproj).AngleDeg) < (tarangledeg-Projectile(colproj).AngleDeg) THEN Projectile(colproj).AngleDeg = Projectile(colproj).AngleDeg - Projectile(colproj).RotationSpeed
             END IF

             IF Projectile(colproj).AngleDeg<0 THEN Projectile(colproj).AngleDeg=Projectile(colproj).AngleDeg+360
             IF Projectile(colproj).AngleDeg>359 THEN Projectile(colproj).AngleDeg=Projectile(colproj).AngleDeg-360

             Projectile(colproj).Angle = (Projectile(colproj).AngleDeg*PI)/180

             END IF
         
         END IF
         
        IF Ball(countcball).Alive = TRUE AND CurrentWeapon = 3 AND Projectile(colproj).Active = TRUE AND ABS(Projectile(colproj).X-Ball(countcball).X)<30 AND ABS(Projectile(colproj).Y-Ball(countcball).Y)<30 THEN
        
        
            Projectile(colproj).Active = FALSE
            InitiateExplosion 7, Projectile(colproj).X, Projectile(colproj).Y

            InitParticle Projectile(colproj).X, Projectile(colproj).Y, 11
            InitParticle Projectile(colproj).X-3, Projectile(colproj).Y, 11
            InitParticle Projectile(colproj).X, Projectile(colproj).Y+3, 11
            InitParticle Projectile(colproj).X-1, Projectile(colproj).Y, 11
            InitiateExplosion 8, Projectile(colproj).X, Projectile(colproj).Y
            InitiateExplosion 9, Projectile(colproj).X, Projectile(colproj).Y
            InitiateExplosion 10, Projectile(colproj).X, Projectile(colproj).Y
            InitiateExplosion 11, Projectile(colproj).X, Projectile(colproj).Y
        
        FOR countcball_plasma AS INTEGER = 1 TO numofballs
        IF Ball(countcball_plasma).Alive = TRUE AND ABS(Projectile(colproj).X-Ball(countcball_plasma).X)<40 AND ABS(Projectile(colproj).Y-Ball(countcball_plasma).Y)<40 THEN
             
             Ball(countcball_plasma).Energy = Ball(countcball_plasma).Energy - 6
             InitiateExplosion 6, Projectile(colproj).X, Projectile(colproj).Y
             IF Ball(countcball_plasma).Energy > 0 THEN EXIT SUB
             IF Ball(countcball_plasma).Energy <= 0 THEN 
                 countkill = countkill + 1
                 IF countkill = nextpowerup THEN
                 nextpowerup = INT(RND * 4) + 20
                 countkill = 0
                 InitiatePowerup Ball(countcball).X, Ball(countcball).Y, 1
                 END IF
                 sbonus = 1
                 IF Ball(countcball_plasma).LoopPass = TRUE THEN sbonus = 2
                 IF sbonus = 1 THEN score = score + (50 + Ball(countcball_plasma).Typ * 15)
                 IF sbonus = 2 THEN
                 playsample (sample(13))
                 InitiateScrollText "Tagged Ball Bonus! + "+ STR$(1000+200*Ball(countcball_plasma).Typ)+" Pts"
                 score = score + (1000 + 200*Ball(countcball_plasma).Typ)
                 END IF
                 InitiateExplosion 1, Ball(countcball_plasma).X, Ball(countcball_plasma).Y
                 InitParticle Ball(countcball_plasma).X, Ball(countcball_plasma).Y, Ball(countcball_plasma).Typ
                 InitParticle Ball(countcball_plasma).X-3, Ball(countcball_plasma).Y, Ball(countcball_plasma).Typ
                 InitParticle Ball(countcball_plasma).X, Ball(countcball_plasma).Y+3, Ball(countcball_plasma).Typ
                 InitParticle Ball(countcball_plasma).X-1, Ball(countcball_plasma).Y, Ball(countcball_plasma).Typ
                 InitiateExplosion 2, Ball(countcball_plasma).X, Ball(countcball_plasma).Y
                 InitiateExplosion 3, Ball(countcball_plasma).X, Ball(countcball_plasma).Y
                 InitiateExplosion 4, Ball(countcball_plasma).X, Ball(countcball_plasma).Y
                 InitiateExplosion 5, Ball(countcball_plasma).X, Ball(countcball_plasma).Y
                 RandomizeBall countcball_plasma,1
             END IF
        END IF
        NEXT countcball_plasma
        
        END IF ' end if current weapon is 3
        
        IF Ball(countcball).Alive = TRUE AND CurrentWeapon = 1 AND Projectile(colproj).Active = TRUE AND ABS(Projectile(colproj).X-Ball(countcball).X)<6 AND ABS(Projectile(colproj).Y-Ball(countcball).Y)<6 THEN
             Ball(countcball).Energy = Ball(countcball).Energy - 1
             InitiateExplosion 6, Projectile(colproj).X, Projectile(colproj).Y
             Projectile(colproj).Active = FALSE
             IF Ball(countcball).Energy > 0 THEN EXIT SUB
             IF Ball(countcball).Energy <= 0 THEN 
                 countkill = countkill + 1
                 IF countkill = nextpowerup THEN
                 nextpowerup = INT(RND * 4) + 20
                 countkill = 0
                 InitiatePowerup Ball(countcball).X, Ball(countcball).Y, 1
                 END IF
                 sbonus = 1
                 IF Ball(countcball).LoopPass = TRUE THEN sbonus = 2
                 IF sbonus = 1 THEN score = score + (50 + Ball(countcball).Typ * 15)
                 IF sbonus = 2 THEN
                 playsample (sample(13))
                 InitiateScrollText "Tagged Ball Bonus! + "+ STR$(1000+200*Ball(countcball).Typ)+ " Pts"
                 score = score + (1000 + 200*Ball(countcball).Typ)
                 END IF
                 InitiateExplosion 1, Ball(countcball).X, Ball(countcball).Y
                 InitParticle Ball(countcball).X, Ball(countcball).Y, Ball(countcball).Typ
                 InitParticle Ball(countcball).X-3, Ball(countcball).Y, Ball(countcball).Typ
                 InitParticle Ball(countcball).X, Ball(countcball).Y+3, Ball(countcball).Typ
                 InitParticle Ball(countcball).X-1, Ball(countcball).Y, Ball(countcball).Typ
                 InitiateExplosion 2, Ball(countcball).X, Ball(countcball).Y
                 InitiateExplosion 3, Ball(countcball).X, Ball(countcball).Y
                 InitiateExplosion 4, Ball(countcball).X, Ball(countcball).Y
                 InitiateExplosion 5, Ball(countcball).X, Ball(countcball).Y
                 RandomizeBall countcball,1
             END IF
        END IF 

    NEXT countcball
    
FOR countcship AS INTEGER = 1 TO numofships
        
        IF HomingTime > 0 AND Projectile(colproj).Typ <> 2 THEN
            
             taranglerad = ATAN2(Ship(countcship).X-Projectile(colproj).X, Ship(countcship).Y-Projectile(colproj).Y)
             IF taranglerad<0 THEN taranglerad = taranglerad + 2*PI 
             tarangledeg = (taranglerad*180)/PI
             
             IF Ship(countcship).Alive = TRUE AND Projectile(colproj).Active = TRUE AND ABS(Projectile(colproj).X-Ship(countcship).X)<60 AND ABS(Projectile(colproj).Y-Ship(countcship).Y)<60 THEN
                 
             IF Projectile(colproj).AngleDeg > tarangledeg THEN
                  IF (360-Projectile(colproj).AngleDeg+tarangledeg) >= (Projectile(colproj).AngleDeg-tarangledeg) THEN Projectile(colproj).AngleDeg = Projectile(colproj).AngleDeg - Projectile(colproj).RotationSpeed
                  IF (360-Projectile(colproj).AngleDeg+tarangledeg) < (Projectile(colproj).AngleDeg-tarangledeg) THEN Projectile(colproj).AngleDeg = Projectile(colproj).AngleDeg + Projectile(colproj).RotationSpeed
             END IF

             IF Projectile(colproj).AngleDeg < tarangledeg THEN
                  IF (360-tarangledeg+Projectile(colproj).AngleDeg) >= (tarangledeg-Projectile(colproj).AngleDeg) THEN Projectile(colproj).AngleDeg = Projectile(colproj).AngleDeg + Projectile(colproj).RotationSpeed
                  IF (360-tarangledeg+Projectile(colproj).AngleDeg) < (tarangledeg-Projectile(colproj).AngleDeg) THEN Projectile(colproj).AngleDeg = Projectile(colproj).AngleDeg - Projectile(colproj).RotationSpeed
             END IF

             IF Projectile(colproj).AngleDeg<0 THEN Projectile(colproj).AngleDeg=Projectile(colproj).AngleDeg+360
             IF Projectile(colproj).AngleDeg>359 THEN Projectile(colproj).AngleDeg=Projectile(colproj).AngleDeg-360

             Projectile(colproj).Angle = (Projectile(colproj).AngleDeg*PI)/180

             END IF
         
         END IF
         
        IF Ship(countcship).Alive = TRUE AND CurrentWeapon = 3 AND Projectile(colproj).Active = TRUE AND ABS(Projectile(colproj).X-Ship(countcship).X+sin(Ship(countcship).Angle*14))<20 AND ABS(Projectile(colproj).Y-Ship(countcship).Y-cos(Ship(countcship).Angle*14))<20 THEN
            
            Projectile(colproj).Active = FALSE
            InitiateExplosion 7, Projectile(colproj).X, Projectile(colproj).Y

            InitParticle Projectile(colproj).X, Projectile(colproj).Y, 11
            InitParticle Projectile(colproj).X-3, Projectile(colproj).Y, 11
            InitParticle Projectile(colproj).X, Projectile(colproj).Y+3, 11
            InitParticle Projectile(colproj).X-1, Projectile(colproj).Y, 11
            InitiateExplosion 8, Projectile(colproj).X, Projectile(colproj).Y
            InitiateExplosion 9, Projectile(colproj).X, Projectile(colproj).Y
            InitiateExplosion 10, Projectile(colproj).X, Projectile(colproj).Y
            InitiateExplosion 11, Projectile(colproj).X, Projectile(colproj).Y
        
        FOR countcship_plasma AS INTEGER = 1 TO numofships
        IF Ship(countcship_plasma).Alive = TRUE AND ABS(Projectile(colproj).X-Ship(countcship_plasma).X+sin(Ship(countcship).Angle*14))<30 AND ABS(Projectile(colproj).Y-Ship(countcship_plasma).Y-cos(Ship(countcship).Angle*14))<30 THEN
             
             Ship(countcship_plasma).Energy = Ship(countcship_plasma).Energy - 6
             InitiateExplosion 6, Projectile(colproj).X, Projectile(colproj).Y
             IF Ship(countcship_plasma).Energy > 0 THEN EXIT SUB
             IF Ship(countcship_plasma).Energy <= 0 THEN 
                 countkill = countkill + 1
                 IF countkill = nextpowerup THEN
                 nextpowerup = INT(RND * 4) + 20
                 countkill = 0
                 InitiatePowerup Ship(countcship).X, Ship(countcship).Y, 1
                 END IF
                 sbonus = 1
                 score = score + (50 + Ship(countcship_plasma).Typ * 25)*sbonus
                 InitiateExplosion 1, Ship(countcship_plasma).X, Ship(countcship_plasma).Y
                 InitParticle Ship(countcship_plasma).X, Ship(countcship_plasma).Y, Ship(countcship_plasma).Typ
                 InitParticle Ship(countcship_plasma).X-3, Ship(countcship_plasma).Y, Ship(countcship_plasma).Typ
                 InitParticle Ship(countcship_plasma).X, Ship(countcship_plasma).Y+3, Ship(countcship_plasma).Typ
                 InitParticle Ship(countcship_plasma).X-1, Ship(countcship_plasma).Y, Ship(countcship_plasma).Typ
                 InitiateExplosion 2, Ship(countcship_plasma).X, Ship(countcship_plasma).Y
                 InitiateExplosion 3, Ship(countcship_plasma).X, Ship(countcship_plasma).Y
                 InitiateExplosion 4, Ship(countcship_plasma).X, Ship(countcship_plasma).Y
                 InitiateExplosion 5, Ship(countcship_plasma).X, Ship(countcship_plasma).Y
                 RandomizeShip countcship_plasma
             END IF
        END IF
        NEXT countcship_plasma
        
        END IF ' end if current weapon is 3
        
        IF POINT (Projectile(colproj).X,Projectile(colproj).Y) = RGB(240, 0, 240) OR POINT (Projectile(colproj).X,Projectile(colproj).Y) = RGB(93, 0, 255) THEN
        IF Ship(countcship).Alive = TRUE AND CurrentWeapon = 1 AND Projectile(colproj).Active = TRUE AND Projectile(colproj).Active = TRUE AND ABS(Projectile(colproj).X-Ship(countcship).X+sin(Ship(countcship).Angle*14))<14 AND ABS(Projectile(colproj).Y-Ship(countcship).Y-cos(Ship(countcship).Angle*14))<14 THEN
             Ship(countcship).Energy = Ship(countcship).Energy - 1
             InitiateExplosion 6, Projectile(colproj).X, Projectile(colproj).Y
             Projectile(colproj).Active = FALSE
             IF Ship(countcship).Energy > 0 THEN EXIT SUB
             IF Ship(countcship).Energy <= 0 THEN 
                 countkill = countkill + 1
                 IF countkill = nextpowerup THEN
                 nextpowerup = INT(RND * 4) + 20
                 countkill = 0
                 InitiatePowerup Ship(countcship).X, Ship(countcship).Y, 1
                 END IF
                 sbonus = 1
                 score = score + (50 + Ship(countcship).Typ * 25)*sbonus
                 InitiateExplosion 1, Ship(countcship).X, Ship(countcship).Y
                 InitParticle Ship(countcship).X, Ship(countcship).Y, Ship(countcship).Typ
                 InitParticle Ship(countcship).X-3, Ship(countcship).Y, Ship(countcship).Typ
                 InitParticle Ship(countcship).X, Ship(countcship).Y+3, Ship(countcship).Typ
                 InitParticle Ship(countcship).X-1, Ship(countcship).Y, Ship(countcship).Typ
                 InitiateExplosion 2, Ship(countcship).X, Ship(countcship).Y
                 InitiateExplosion 3, Ship(countcship).X, Ship(countcship).Y
                 InitiateExplosion 4, Ship(countcship).X, Ship(countcship).Y
                 InitiateExplosion 5, Ship(countcship).X, Ship(countcship).Y
                 RandomizeShip countcship
             END IF
        END IF 
        END IF

NEXT countcship


END SUB

SUB DrawExplosions ()
    
FOR countexpl AS INTEGER = 1 TO numofexplosions
  IF Explosion(countexpl).DTime > 0 THEN
      Explosion(countexpl).DTime = Explosion(countexpl).DTime - 1
      SELECT CASE Explosion(countexpl).Typ
      CASE 1
          CIRCLE (Explosion(countexpl).X, Explosion(countexpl).Y), 1+Explosion(countexpl).DTime/2, RGBA(245,140,5,80+Explosion(countexpl).DTime*3), , , , F
      CASE 2
          Explosion(countexpl).X = Explosion(countexpl).X + 1
          Explosion(countexpl).Y = Explosion(countexpl).Y + 1
          CIRCLE (Explosion(countexpl).X, Explosion(countexpl).Y), 1+Explosion(countexpl).DTime/3, RGBA(245,140,5,60+Explosion(countexpl).DTime*3), , , , F
      CASE 3
          Explosion(countexpl).X = Explosion(countexpl).X + 1
          Explosion(countexpl).Y = Explosion(countexpl).Y - 1
          CIRCLE (Explosion(countexpl).X, Explosion(countexpl).Y), 1+Explosion(countexpl).DTime/3, RGBA(245,140,5,60+Explosion(countexpl).DTime*3), , , , F
      CASE 4
          Explosion(countexpl).X = Explosion(countexpl).X - 1
          Explosion(countexpl).Y = Explosion(countexpl).Y + 1
          CIRCLE (Explosion(countexpl).X, Explosion(countexpl).Y), 1+Explosion(countexpl).DTime/3, RGBA(245,140,5,60+Explosion(countexpl).DTime*3), , , , F
       CASE 5
          Explosion(countexpl).X = Explosion(countexpl).X - 1
          Explosion(countexpl).Y = Explosion(countexpl).Y - 1
          CIRCLE (Explosion(countexpl).X, Explosion(countexpl).Y), 1+Explosion(countexpl).DTime/3, RGBA(245,140,5,60+Explosion(countexpl).DTime*3), , , , F
       CASE 6
          CIRCLE (Explosion(countexpl).X, Explosion(countexpl).Y), 1+Explosion(countexpl).DTime/2, RGBA(245,140,5,80+Explosion(countexpl).DTime*3), , , , F
       CASE 7
          CIRCLE (Explosion(countexpl).X, Explosion(countexpl).Y), 1+Explosion(countexpl).DTime/2, RGBA(0,149,176,80+Explosion(countexpl).DTime*3), , , , F
       CASE 8
          Explosion(countexpl).X = Explosion(countexpl).X + 1
          Explosion(countexpl).Y = Explosion(countexpl).Y + 1
          CIRCLE (Explosion(countexpl).X, Explosion(countexpl).Y), 1+Explosion(countexpl).DTime/3, RGBA(0,149,176,60+Explosion(countexpl).DTime*3), , , , F
       CASE 9
          Explosion(countexpl).X = Explosion(countexpl).X + 1
          Explosion(countexpl).Y = Explosion(countexpl).Y - 1
          CIRCLE (Explosion(countexpl).X, Explosion(countexpl).Y), 1+Explosion(countexpl).DTime/3, RGBA(0,149,176,60+Explosion(countexpl).DTime*3), , , , F
       CASE 10
          Explosion(countexpl).X = Explosion(countexpl).X - 1
          Explosion(countexpl).Y = Explosion(countexpl).Y + 1
          CIRCLE (Explosion(countexpl).X, Explosion(countexpl).Y), 1+Explosion(countexpl).DTime/3, RGBA(0,149,176,60+Explosion(countexpl).DTime*3), , , , F
       CASE 11
          Explosion(countexpl).X = Explosion(countexpl).X - 1
          Explosion(countexpl).Y = Explosion(countexpl).Y - 1
          CIRCLE (Explosion(countexpl).X, Explosion(countexpl).Y), 1+Explosion(countexpl).DTime/3, RGBA(0,149,176,60+Explosion(countexpl).DTime*3), , , , F
      
      END SELECT
  END IF
  
NEXT countexpl

END SUB

SUB InitiateExplosion (etyp AS INTEGER, ex AS SINGLE, ey AS SINGLE)
    
FOR initexp AS INTEGER = 1 TO numofexplosions
    
   IF Explosion(initexp).DTime = 0 THEN
      Explosion(initexp).DTime = 40
      Explosion(initexp).X = ex
      Explosion(initexp).Y = ey
      Explosion(initexp).Typ = etyp
      rndnum1 = 1 + INT(RND * 2)
      IF rndnum1 = 1 THEN playSample (sample(2))
      IF rndnum1 = 2 THEN playSample (sample(3))
      IF Explosion(initexp).Typ = 1 THEN
         Explosion(initexp).DTime = 30
      END IF
      IF Explosion(initexp).Typ = 2 THEN
         Explosion(initexp).DTime = 28
      END IF
      IF Explosion(initexp).Typ = 3 THEN
         Explosion(initexp).DTime = 28
      END IF
      IF Explosion(initexp).Typ = 4 THEN
         Explosion(initexp).DTime = 28
      END IF
      IF Explosion(initexp).Typ = 5 THEN
         Explosion(initexp).DTime = 28
      END IF
      IF Explosion(initexp).Typ = 7 THEN
         Explosion(initexp).DTime = 30
      END IF
      IF Explosion(initexp).Typ = 8 THEN
         Explosion(initexp).DTime = 28
      END IF
      IF Explosion(initexp).Typ = 9 THEN
         Explosion(initexp).DTime = 28
      END IF
      IF Explosion(initexp).Typ = 10 THEN
         Explosion(initexp).DTime = 28
      END IF
      IF Explosion(initexp).Typ = 11 THEN
         Explosion(initexp).DTime = 28
      END IF
       IF Explosion(initexp).Typ = 6 THEN
         Explosion(initexp).DTime = 16
      END IF
      EXIT SUB
   END IF
NEXT initexp   

END SUB

SUB TitleScreen ()
  
  
cloop1 = 250
cloop1direc = 1
CLS
workpage xor = 1

startloop:
DO

GETMOUSE mx, my, mbutton

Frame1 = (Frame1 MOD 4) + 1

st = Timer

screenlock
screenset workpage, workpage xor 1

FPS = FPS + 1                     ' Used to count FPS.
if StartTime + 1 < timer then
 FPS2 = FPS
 FPS = 0
 seconds = seconds + 1
 StartTime = timer
end if

LINE (0,0)-(639,479), RGBA(0, 0, 0, 255), BF

PUT (0,0), image_buffer(1), ALPHA, 100+cloop1/2

IF cloop1direc = 1 THEN cloop1 = cloop1 + 2
IF cloop1>255 THEN cloop1direc = 2
IF cloop1direc = 2 THEN cloop1 = cloop1 - 2
IF cloop1 < 150 THEN cloop1direc = 1
IF cloop1 > 255 THEN cloop1 = 255
IF cloop1 < 0 THEN cloop1 = 0

LINE (190,0)-(82,224), RGBA(231, 231, 231, 255)
LINE (82,224)-(201,224), RGBA(231, 231, 231, 255)
LINE (201,224)-(260,0), RGBA(231, 231, 231, 255)
PAINT (188,56), RGBA(231, 231, 231, 255), RGBA(231, 231, 231, 255)

LINE (82,224)-(33,325), RGBA(225, 0, 0, 255)
LINE (33,325)-(174,325), RGBA(225, 0, 0, 255)
LINE (82,224)-(201,224), RGBA(225, 0, 0, 255)
LINE (174,325)-(201,224), RGBA(225, 0, 0, 255)
PAINT (140,274), RGBA(225, 0, 0, 255), RGBA(225, 0, 0, 255)

LINE (33,326)-(174,326), RGB(185, 0, 0)
LINE (33,326)-(33,365), RGB(185, 0, 0)
LINE (33,365)-(174,365), RGB(185, 0, 0)
LINE (174,365)-(174,326), RGB(185, 0, 0)
PAINT (124,346), RGB(185, 0, 0), RGB(185, 0, 0)

LINE (201,224)-(201,260), RGB(145, 0, 0)
LINE (174,365)-(201,260), RGB(145, 0, 0)
LINE (174,365)-(174,324), RGB(145, 0, 0)
LINE (201,224)-(174,324), RGB(145, 0, 0)
PAINT (186,295), RGB(145, 0, 0), RGB(145, 0, 0)

LINE (202,258)-(268,0), RGB(160, 160, 160)
LINE (261,0)-(268,0), RGB(160, 160, 160)
LINE (261,0)-(202,224), RGB(160, 160, 160)
LINE (202,258)-(202,224), RGB(160, 160, 160)
PAINT (214,188), RGB(160, 160, 160), RGB(160, 160, 160)

Draw String (5,5), "L.D. Jul 07", RGBA(2,117, 190, 200)
Draw String (580,5), "ver.1.1", RGBA(2,117, 190, 200)

Draw String (380,87+cloop1/10), "M I G H T Y  L I N E", RGBA(255,255,255, cloop1)

FOR countmenuposblend AS INTEGER = 1 TO 5
MenuPosBlend(countmenuposblend) = 200
NEXT countmenuposblend
IF MenuY = MenuPos*10 THEN MenuPosBlend(MenuPos) = 255

Draw String (400,170), "Start New Game", RGBA(145,145, 255, MenuPosBlend(1))
Draw String (400,180), "Options", RGBA(145,145, 255, MenuPosBlend(2))
Draw String (400,190), "Instructions", RGBA(145,145, 255, MenuPosBlend(3))
Draw String (400,200), "High Scores", RGBA(145,145, 255, MenuPosBlend(4))
Draw String (400,210), "Exit", RGBA(145,145, 255, MenuPosBlend(5))

Draw String (380,160+MenuY), "->", RGBA(145,145, 255, 255)

IF MULTIKEY(SC_DOWN) AND KeyFree = TRUE THEN 
MenuPos = MenuPos + 1
KeyFree = FALSE
END IF
IF MULTIKEY(SC_UP) AND KeyFree = TRUE THEN 
MenuPos = MenuPos - 1
KeyFree = FALSE
END IF
IF MenuPos > 5 THEN MenuPos = 5
IF MenuPos < 1 THEN MenuPos = 1
IF MenuY < MenuPos * 10 THEN MenuY = MenuY + 2
IF MenuY > MenuPos * 10 THEN MenuY = MenuY - 2

IF MULTIKEY(SC_ENTER) THEN
SELECT CASE MenuPos
CASE 1
ResetVariables
workpage xor = 1
FadeOutScreen
EXIT SUB
CASE 2
MenuPos = 1
MenuY = MenuPos*10
workpage xor = 1
'screenunlock
Options
MenuPos = 2
MenuY = MenuPos*10
GOTO startloop:
CASE 3
workpage xor = 1
Instructions
MenuPos = 3
MenuY = MenuPos*10
GOTO startloop:
CASE 4
workpage xor = 1
'WriteHighScore
ReadHighScore
'CalculateScore
GOTO startloop:
CASE 5
workpage xor = 1
'screenunlock    
FadeOutScreen
IMAGEDESTROY fadeimg_buffer(1)
IMAGEDESTROY image_buffer(1)
IMAGEDESTROY image_buffer(2)
IMAGEDESTROY image_buffer(3)
IMAGEDESTROY image_buffer(4)
FOR cballbuff AS INTEGER = 1 TO 10
   IMAGEDESTROY pball_buffer(cballbuff)
   IMAGEDESTROY nball_buffer(cballbuff) 
NEXT cballbuff
END    
END SELECT
END IF

IF MULTIKEY(SC_DOWN) OR MULTIKEY(SC_UP) THEN GOTO skipkeyrest1:
KeyFree = TRUE
skipkeyrest1:

'Draw String (570,10), STR$(mx) + " , " + STR$(my), RGB(192,81, 0)

FadeInScreen

workpage xor = 1
screenunlock

sleepintvl = Cint((st + frameintvl - Timer)*1000.0)
If sleepintvl>1 Then 
    Sleep sleepintvl
else 
    sleep 1
end if

LOOP

END SUB

SUB InitiateScrollText (istring AS STRING)
   FOR inittext AS INTEGER = 1 TO 5
       IF ScrollText(inittext).Active = FALSE THEN
           ScrollText(inittext).Active = TRUE
           ScrollText(inittext).Stext = istring
           ScrollText(inittext).ABlender = 0
           ScrollText(inittext).X = 645
           FOR inittext2 AS INTEGER = 1 TO 5
              IF (ScrollText(inittext2).Active = TRUE AND inittext2<>inittext AND ScrollText(inittext2).X>400) THEN                  
              ScrollText(inittext).X = ScrollText(inittext).X + Len(istring)*10
              END IF
           NEXT inittext2  
           EXIT SUB
       END IF
   NEXT inittext
   
END SUB

SUB DrawScrollingText ()
    
    FOR countstext AS INTEGER = 1 TO 5
        IF ScrollText(countstext).Active = TRUE THEN
        ScrollText(countstext).X = ScrollText(countstext).X - 4
        IF ScrollText(countstext).X>320 AND ScrollText(countstext).X<580 THEN ScrollText(countstext).ABlender = ScrollText(countstext).ABlender + 4
        IF ScrollText(countstext).X<260 THEN ScrollText(countstext).ABlender = ScrollText(countstext).ABlender - 4
        IF ScrollText(countstext).ABlender > 255 THEN ScrollText(countstext).ABlender = 255
        IF ScrollText(countstext).ABlender < 0 THEN ScrollText(countstext).ABlender = 0
        IF ScrollText(countstext).X < -60 THEN ScrollText(countstext).Active = FALSE
        Draw String (ScrollText(countstext).X,470),ScrollText(countstext).Stext, RGBA(255,255,255, ScrollText(countstext).ABlender)
        END IF
    NEXT countstext
    
END SUB

SUB GenerateBackground ()
    
screenlock
screenset workpage, workpage xor 1

LINE (0,0)-(80,80), RGB (255,0,255), BF

FOR countbbuff AS INTEGER = 1 TO 5
    IF countbbuff = 1 THEN
        ballcol(1) = RGBA(0, 20, 208, 255)
        ballcol(2) = RGBA(1, 25, 255, 255)
        ballcol(3) = RGBA(79, 96, 255, 255)
        ballcol(4) = RGBA(135, 146, 255, 255)
    END IF
    IF countbbuff = 2 THEN
        ballcol(1) = RGBA(0, 150, 115, 255)
        ballcol(2) = RGBA(0, 188, 146, 255)
        ballcol(3) = RGBA(0, 228, 177, 255)
        ballcol(4) = RGBA(11, 255, 200, 255)
    END IF
    IF countbbuff = 3 THEN
        ballcol(1) = RGBA(176, 0, 0, 255)
        ballcol(2) = RGBA(218, 0, 0, 255)
        ballcol(3) = RGBA(240, 0, 0, 255)
        ballcol(4) = RGBA(255, 120, 120, 255)
    END IF
    IF countbbuff = 4 THEN
        ballcol(1) = RGBA(186, 198, 0, 255)
        ballcol(2) = RGBA(222, 236, 0, 255)
        ballcol(3) = RGBA(241, 255, 19, 255)
        ballcol(4) = RGBA(248, 255, 141, 255)
    END IF
    IF countbbuff = 5 THEN
        ballcol(1) = RGBA(78, 78, 78, 255)
        ballcol(2) = RGBA(99, 99, 99, 255)
        ballcol(3) = RGBA(99, 99, 99, 255)
        ballcol(4) = RGBA(117, 117, 117, 255)
    END IF

    CIRCLE (40, 40), 7, ballcol(1), , , , F
    CIRCLE (40, 40), 5, ballcol(2), , , , F
    CIRCLE (40, 40), 3, ballcol(3), , , , F
    IF countbbuff < 3 THEN CIRCLE (40+3,40+3), 1, ballcol(4), , , , F
    IF countbbuff = 3 THEN CIRCLE (40+2,40+1), 1, ballcol(4), , , , F
    IF countbbuff = 4 THEN CIRCLE (40+1,40+1), 1, ballcol(4), , , , F
    
    IF countbbuff = 5 THEN
         CIRCLE (40-3,40+2), 1, ballcol(4), , , , F
         CIRCLE (40+1,40-3), 1, ballcol(4), , , , F
         CIRCLE (40-2,40+1), 1, ballcol(4), , , , F
    END IF
    GET (32,32)-(48,48), nball_buffer(countbbuff)

NEXT countbbuff

ballcol(1) = RGBA(161, 0, 152, 255)
ballcol(2) = RGBA(212, 2, 202, 255)
ballcol(3) = RGBA(245, 32, 242, 255)
ballcol(4) = RGBA(255, 135, 248, 255)
CIRCLE (40, 40), 7, ballcol(1), , , , F
CIRCLE (40, 40), 5, ballcol(2), , , , F
CIRCLE (40, 40), 3, ballcol(3), , , , F
CIRCLE (40+3,40+3), 1, ballcol(4), , , , F
GET (32,32)-(48,48), nball_buffer(7)

FOR countbbuff AS INTEGER = 1 TO 6
    ballcol(1) = RGBA(0, 118, 150, 255)
    ballcol(2) = RGBA(0, 176, 214, 255)
    ballcol(3) = RGBA(1, 214, 255, 255)
    CIRCLE (40, 40), 7, ballcol(1), , , , F
    CIRCLE (40, 40), 5, ballcol(2), , , , F
    CIRCLE (40, 40), 3, ballcol(3), , , , F
    IF countbbuff = 1 THEN Draw String (37,37), "D", RGB(255,255, 255)
    IF countbbuff = 2 THEN Draw String (37,37), "S", RGB(255,255, 255)
    IF countbbuff = 3 THEN Draw String (37,37), "K", RGB(255,255, 255)
    IF countbbuff = 4 THEN Draw String (37,37), "B", RGB(255,255, 255)
    IF countbbuff = 5 THEN Draw String (37,37), "H", RGB(255,255, 255)
    IF countbbuff = 6 THEN Draw String (37,37), "L", RGB(255,255, 255)
    GET (32,32)-(48,48), pball_buffer(countbbuff)
NEXT countbbuff

FOR countbbuff AS INTEGER = 7 TO 9
    ballcol(1) = RGBA(128, 0, 0, 255)
    ballcol(2) = RGBA(189, 0, 0, 255)
    ballcol(3) = RGBA(210, 0, 0, 255)
    CIRCLE (40, 40), 7, ballcol(1), , , , F
    CIRCLE (40, 40), 5, ballcol(2), , , , F
    CIRCLE (40, 40), 3, ballcol(3), , , , F
    IF countbbuff = 7 THEN Draw String (37,37), "L", RGB(255,187,187)
    IF countbbuff = 8 THEN Draw String (37,37), "N", RGB(255,187,187)
    IF countbbuff = 9 THEN Draw String (37,37), "S", RGB(255,187,187)
    GET (32,32)-(48,48), pball_buffer(countbbuff)
NEXT countbbuff

CLS
DrawFlare 40, 40, 5
GET (32,32)-(48,48), nball_buffer(10)

' good powerups
' D(1) - double gun
' S(2) - short line
' K(3) - killer line
' B(4) - blazers!
' H(5) - homing missiles!
' L(6) - lasers!

' bad powerups
' L (7) - long line!
' N - no hole!
' S - slow line!

CLS

workpage xor = 1
screenunlock

'sleep
'end

screenlock
screenset workpage, workpage xor 1

Draw String (200,220), "Please Wait - Generating Backgrounds", RGB(192,81, 0)

workpage xor = 1
screenunlock

screenset workpage, workpage xor 1

	randomize 12345

    FOR i1 as integer = 0 TO 108 - 1
        xcoords1(i1) = rnd * SCR_WIDTH
        ycoords1(i1) = rnd * SCR_HEIGHT
    NEXT i1

          dim as single mindist = 1D+16
          dim as single maxdist = 0

          FOR y as integer = 0 TO SCR_HEIGHT - 1
          FOR x as integer = 0 TO SCR_WIDTH  - 1
              dim as single tx = x
              dim as single ty = y
              dim as single distance = dist(tx, ty, xcoords1(), ycoords1())
              distbuffer(x, y) = distance
              IF distance < mindist THEN mindist = distance
              IF distance > maxdist THEN maxdist = distance
          NEXT x
          NEXT y


          FOR y as integer = 0 TO SCR_HEIGHT - 1
          FOR x as integer = 0 TO SCR_WIDTH - 1
              dim as single c = (distbuffer(x, y) - mindist) / (maxdist - mindist)
              dim as single r = cint(c * 205)
              dim as single g = cint(c * 155)
              dim as single b = 205 - cint(c * 155)
              pset (x, y), rgba(r,g,b,115)
          NEXT x
          NEXT y

GET (0,0)-(639,479), image_buffer(1)
LINE (0,0)-(639,479), RGBA(0, 0, 0, 255), BF

randomize 2345

    FOR i2 as integer = 0 TO 38 - 1
        xcoords2(i2) = rnd * SCR_WIDTH
        ycoords2(i2) = rnd * SCR_HEIGHT
    NEXT i2


          FOR y as integer = 0 TO SCR_HEIGHT - 1
          FOR x as integer = 0 TO SCR_WIDTH  - 1
              dim as single tx = x
              dim as single ty = y
              dim as single distance = dist(tx, ty, xcoords2(), ycoords2())
              distbuffer(x, y) = distance
              IF distance < mindist THEN mindist = distance
              IF distance > maxdist THEN maxdist = distance
          NEXT x
          NEXT y


          FOR y as integer = 0 TO SCR_HEIGHT - 1
          FOR x as integer = 0 TO SCR_WIDTH - 1
              dim as single c = (distbuffer(x, y) - mindist) / (maxdist - mindist)
              dim as single r = 255-cint(c * 205)
              dim as single g = cint(c * 105)
              dim as single b = cint(c * 205)
              pset (x, y), rgba(r,g,b,85)
          NEXT x
          NEXT y

GET (0,0)-(639,479), image_buffer(2)
LINE (0,0)-(639,479), RGBA(0, 0, 0, 255), BF

randomize 16569
Rainbow (150, 215, 216)
GenPlasma 640,480,ri(256),ri(256),ri(256),ri(256),300,8,0.5

GET (0,0)-(639,479), image_buffer(3)
LINE (0,0)-(639,479), RGBA(0, 0, 0, 255), BF
PUT (0,0), image_buffer(3), ALPHA, 74
GET (0,0)-(639,479), image_buffer(3)

LINE (0,0)-(639,479), RGBA(0, 0, 0, 255), BF

randomize 5155
Rainbow (233, 240, 113)
GenPlasma 640,480,ri(256),ri(256),ri(256),ri(256),300,8,0.5

GET (0,0)-(639,479), image_buffer(4)
LINE (0,0)-(639,479), RGBA(0, 0, 0, 255), BF
PUT (0,0), image_buffer(4), ALPHA, 85
GET (0,0)-(639,479), image_buffer(4)

END SUB

function dist (byval x as single,byval  y as single, xc() as single,  yc() as single) as single

    dim as single mindist = 1D+16
    dim as integer max = UBOUND(xc)
    FOR i as integer = 0 TO max
        dim as single a = (xc(i) - x) * (xc(i) - x)
        dim as single b = (yc(i) - y) * (yc(i) - y)
        dim as single d = SQR(a + b)
        IF d < mindist THEN mindist = d
    NEXT i
    return mindist
    
END function

SUB DrawClock ()
    
LINE (610,475)-(635,430), RGBA(231, 231, 231, 255)
LINE (610,430)-(635,475), RGBA(231, 231, 231, 255)
LINE (607,430)-(638,430), RGBA(231, 231, 231, 255)
LINE (607,427)-(638,427), RGBA(231, 231, 231, 255)
LINE (607,475)-(638,475), RGBA(231, 231, 231, 255)
LINE (607,478)-(638,478), RGBA(231, 231, 231, 255)
LINE (607,478)-(607,475), RGBA(231, 231, 231, 255)
LINE (638,475)-(638,478), RGBA(231, 231, 231, 255)
LINE (607,430)-(607,427), RGBA(231, 231, 231, 255)
LINE (638,430)-(638,427), RGBA(231, 231, 231, 255)

IF seconds>50 THEN
LINE (611,474)-(634,474), RGBA(224, 142, 0, 255)
LINE (612,473)-(633,473), RGBA(224, 142, 0, 255)
END IF
IF seconds>100 THEN
LINE (613,472)-(632,472), RGBA(224, 142, 0, 255)
LINE (613,471)-(632,471), RGBA(224, 142, 0, 255)
END IF
IF seconds>150 THEN
LINE (614,470)-(631,470), RGBA(224, 142, 0, 255)
LINE (614,469)-(631,469), RGBA(224, 142, 0, 255)
END IF
IF seconds>200 THEN
LINE (615,468)-(630,468), RGBA(224, 142, 0, 255)
LINE (615,467)-(630,467), RGBA(224, 142, 0, 255)
END IF
IF seconds>250 THEN
LINE (616,466)-(629,466), RGBA(224, 142, 0, 255)
LINE (617,465)-(628,465), RGBA(224, 142, 0, 255)
END IF
IF seconds>300 THEN
LINE (617,464)-(628,464), RGBA(224, 142, 0, 255)
LINE (618,463)-(627,463), RGBA(224, 142, 0, 255)
END IF
IF seconds>350 THEN
LINE (618,462)-(627,462), RGBA(224, 142, 0, 255)
LINE (619,461)-(626,461), RGBA(224, 142, 0, 255)
END IF
IF seconds>400 THEN
LINE (619,460)-(626,460), RGBA(224, 142, 0, 255)
LINE (620,459)-(625,459), RGBA(224, 142, 0, 255)
END IF
IF seconds>450 THEN
LINE (620,458)-(625,458), RGBA(224, 142, 0, 255)
LINE (621,457)-(624,457), RGBA(224, 142, 0, 255)
END IF
IF seconds>470 THEN
LINE (622,456)-(623,456), RGBA(224, 142, 0, 255)
LINE (622,455)-(623,455), RGBA(224, 142, 0, 255)
END IF

IF seconds<471 THEN
LINE (622,450)-(623,450), RGBA(224, 142, 0, 255)
END IF
IF seconds<451 THEN
LINE (622,449)-(623,449), RGBA(224, 142, 0, 255)
LINE (621,448)-(624,448), RGBA(224, 142, 0, 255)
END IF
IF seconds<401 THEN
LINE (620,447)-(625,447), RGBA(224, 142, 0, 255)
LINE (620,446)-(625,446), RGBA(224, 142, 0, 255)
END IF
IF seconds<351 THEN
LINE (619,445)-(626,445), RGBA(224, 142, 0, 255)
LINE (619,444)-(626,444), RGBA(224, 142, 0, 255)
END IF
IF seconds<301 THEN
LINE (618,443)-(627,443), RGBA(224, 142, 0, 255)
LINE (618,442)-(627,442), RGBA(224, 142, 0, 255)
END IF
IF seconds<251 THEN
LINE (617,441)-(628,441), RGBA(224, 142, 0, 255)
LINE (617,440)-(628,440), RGBA(224, 142, 0, 255)
END IF
IF seconds<201 THEN
LINE (616,439)-(629,439), RGBA(224, 142, 0, 255)
LINE (615,438)-(630,438), RGBA(224, 142, 0, 255)
END IF
IF seconds<151 THEN
LINE (615,437)-(630,437), RGBA(224, 142, 0, 255)
LINE (614,436)-(631,436), RGBA(224, 142, 0, 255)
END IF
IF seconds<101 THEN
LINE (614,435)-(631,435), RGBA(224, 142, 0, 255)
LINE (613,434)-(632,434), RGBA(224, 142, 0, 255)
END IF
IF seconds<51 THEN
LINE (613,433)-(632,433), RGBA(224, 142, 0, 255)
LINE (612,432)-(633,432), RGBA(224, 142, 0, 255)
LINE (611,431)-(634,431), RGBA(224, 142, 0, 255)
END IF

END SUB

SUB DrawLine ()


'LINE (linecenterx-cosang*2+sinang*50,linecentery+sinang*2+cosang*50)-(linecenterx-cosang*2+sinang*55,linecentery+sinang*2+cosang*55),RGBA(192, 0, 0, 255)
'LINE (linecenterx+cosang*2+sinang*50,linecentery-sinang*2+cosang*50)-(linecenterx+cosang*2+sinang*55,linecentery-sinang*2+cosang*55),RGBA(192, 0, 0, 255)
'LINE (linecenterx+cosang*2+sinang*50,linecentery-sinang*2+cosang*50)-(linecenterx-cosang*2+sinang*50,linecentery+sinang*2+cosang*50),RGBA(192, 0, 0, 255)
'LINE (linecenterx+cosang*2+sinang*55,linecentery-sinang*2+cosang*55)-(linecenterx-cosang*2+sinang*55,linecentery+sinang*2+cosang*55),RGBA(192, 0, 0, 255)
'PAINT (linecenterx+sinang*52,linecentery+cosang*52),RGBA(192, 0, 0, 255),RGBA(192, 0, 0, 255)

'LINE (linecenterx+cosang*2-sinang*20,linecentery-sinang*2-cosang*20)-(linecenterx+cosang*2-sinang*55,linecentery-sinang*2-cosang*55),RGBA(231, 231, 231, 255)
'LINE (linecenterx-cosang*2-sinang*20,linecentery+sinang*2-cosang*20)-(linecenterx-cosang*2-sinang*55,linecentery+sinang*2-cosang*55),RGBA(231, 231, 231, 255)
'LINE (linecenterx-cosang*2-sinang*20,linecentery+sinang*2-cosang*20)-(linecenterx+cosang*2-sinang*20,linecentery-sinang*2-cosang*20),RGBA(231, 231, 231, 255)
'LINE (linecenterx-cosang*2-sinang*55,linecentery+sinang*2-cosang*55)-(linecenterx+cosang*2-sinang*55,linecentery-sinang*2-cosang*55),RGBA(231, 231, 231, 255)
'PAINT (linecenterx-sinang*40,linecentery-cosang*40),RGBA(231, 231, 231, 255),RGBA(231, 231, 231, 255)

'LINE (linecenterx-cosang*2+sinang*20,linecentery+sinang*2+cosang*20)-(linecenterx-cosang*2+sinang*49,linecentery+sinang*2+cosang*49),RGBA(231, 231, 231, 255)
'LINE (linecenterx+cosang*2+sinang*20,linecentery-sinang*2+cosang*20)-(linecenterx+cosang*2+sinang*49,linecentery-sinang*2+cosang*49),RGBA(231, 231, 231, 255)
'LINE (linecenterx+cosang*2+sinang*20,linecentery-sinang*2+cosang*20)-(linecenterx-cosang*2+sinang*20,linecentery+sinang*2+cosang*20),RGBA(231, 231, 231, 255)
'LINE (linecenterx+cosang*2+sinang*49,linecentery-sinang*2+cosang*49)-(linecenterx-cosang*2+sinang*49,linecentery+sinang*2+cosang*49),RGBA(231, 231, 231, 255)
'PAINT (linecenterx+sinang*40,linecentery+cosang*40),RGBA(231, 231, 231, 255),RGBA(231, 231, 231, 255)

IF Energy > 0 THEN
IF ShortLineTime = 0 AND LongLineTime = 0 THEN
IF endline < 55 THEN endline = endline + 1
IF endline > 55 THEN endline = endline - 1
END IF
IF ShortLineTime > 0 THEN
IF endline > 30 THEN endline = endline - 1
END IF
IF LongLineTime > 0 THEN
IF endline < 75 THEN endline = endline + 1
END IF
IF NoHoleTime > 0 AND startline > 0 THEN startline = startline - 1    
IF NoHoleTime = 0 AND startline < 20 THEN startline = startline + 1    
END IF

IF Energy = 0 THEN 
IF endline > 5 THEN endline = endline - 1
IF startline > 0 THEN startline = startline - 1
IF endline = 5 AND EndGame = FALSE THEN
InitParticle linecenterx, linecentery, 1
InitParticle linecenterx-3, linecentery, 1
InitParticle linecenterx, linecentery+3, 1
InitParticle linecenterx-1, linecentery, 1
InitiateExplosion 1, linecenterx, linecentery
InitiateExplosion 2, linecenterx, linecentery
InitiateExplosion 3, linecenterx, linecentery
InitiateExplosion 4, linecenterx, linecentery
InitiateExplosion 5, linecenterx, linecentery
EndGame = TRUE
END IF
END IF

IF endline > 5 THEN
IF KillerLineTime > 0 THEN
IF KillerLineWidth < 8 AND KillerLineTime > 8 THEN KillerLineWidth = KillerLineWidth + 1
IF KillerLineTime < 9 AND KilleRLineWidth > 1 THEN KillerLineWidth = KillerLineWidth - 1
IF cloop1direc = 1 THEN cloop1 = cloop1 + 2
IF cloop1>100 THEN cloop1direc = 2
IF cloop1direc = 2 THEN cloop1 = cloop1 - 2
IF cloop1 < 1 THEN cloop1direc = 1
IF cloop1 > 100 THEN cloop1 = 100
IF cloop1 < 0 THEN cloop1 = 0
draw_line_width (linecenterx+sinang*(startline-4),linecentery+cosang*(startline-4),linecenterx+sinang*(endline+4),linecentery+cosang*(endline+4),KillerLineWidth,0,234,88,100+cloop1)
draw_line_width (linecenterx-sinang*(startline-4),linecentery-cosang*(startline-4),linecenterx-sinang*(endline+4),linecentery-cosang*(endline+4),KillerLineWidth,0,234,88,100+cloop1)
END IF
IF endline > 8 THEN draw_line_width (linecenterx+sinang*(endline-6),linecentery+cosang*(endline-6),linecenterx+sinang*endline,linecentery+cosang*endline,4,192,0,0,255)
draw_line_width (linecenterx+sinang*startline,linecentery+cosang*startline,linecenterx+sinang*(endline-5),linecentery+cosang*(endline-5),4,231,231,231,255)
IF DoubleGunTime = 0 THEN draw_line_width (linecenterx-sinang*startline,linecentery-cosang*startline,linecenterx-sinang*endline,linecentery-cosang*endline,4,231,231,231,255)
IF DoubleGunTime > 0 THEN
IF endline > 8 THEN draw_line_width (linecenterx-sinang*(endline-6),linecentery-cosang*(endline-6),linecenterx-sinang*endline,linecentery-cosang*endline,4,192,0,0,255)
draw_line_width (linecenterx-sinang*startline,linecentery-cosang*startline,linecenterx-sinang*(endline-5),linecentery-cosang*(endline-5),4,231,231,231,255)
END IF
END IF

END SUB

SUB InitParticle (partx AS SINGLE, party AS SINGLE, parttyp AS INTEGER)
    
FOR initpart AS INTEGER = 1 TO numofparticles
   IF Particle(initpart).DTime = 0 THEN
      Particle(initpart).DTime = 10
      Particle(initpart).X = partx
      Particle(initpart).Y = party
      Particle(initpart).Angle = 1 + INT(RND * 359)
      Particle(initpart).Angle = (Particle(initpart).Angle*PI)/180
      Particle(initpart).Typ = parttyp
      Particle(initpart).Speed = 4
      EXIT SUB
    END IF
NEXT initpart 

END SUB

SUB DrawMoveParticles ()
    
 FOR countpart AS INTEGER = 1 TO numofparticles
   IF Particle(countpart).DTime > 0 THEN
      Particle(countpart).DTime = Particle(countpart).DTime - 1
      ballcol(1) = RGBA(0, 69,224,255)
      ballcol(2) = RGBA(68, 125,224,255)
    
      Particle(countpart).X = Particle(countpart).X + SIN(Particle(countpart).Angle)*Particle(countpart).Speed
      Particle(countpart).Y = Particle(countpart).Y - COS(Particle(countpart).Angle)*Particle(countpart).Speed

      IF Frame1 = 1 OR Frame1 = 3 THEN 
          IF Particle(countpart).Typ < 10 THEN InitiateMiscTrace 12, Particle(countpart).X, Particle(countpart).Y
          IF Particle(countpart).Typ = 11 THEN InitiateMiscTrace 13, Particle(countpart).X, Particle(countpart).Y
      END IF
      
      'CIRCLE (Particle(countpart).X, Particle(countpart).Y), 1, ballcol(1), , , , F
     
   END IF
NEXT initpart 

END SUB


sub draw_line_width( byval x1 as single, byval y1 as single, byval x2 as single, byval y2 as single,_
					 byval lwidth as single, byval r as integer, byval g as integer, byval b as integer,_
					 byval alpha as integer)



	'' get the normal of the line
	dim as single nx,ny	
	nx = -(y2-y1)
	ny =  (x2-x1)
	
	'' normalize the normal
	dim leng as single
    leng = sqr(nx * nx + ny * ny )
    nx = nx / leng
    ny = ny / leng

	'' divide by 2 since we only need the half-width
	nx *= lwidth/2
	ny *= lwidth/2
	
	'' varialbles for temp coords 	
	dim as single lx1, ly1, lx2, ly2, lx3, ly3, lx4, ly4
	
	'' calculate the vertices of the quads using the normal
	'' negate the normal for the "left" side
	lx1 = x2+nx
    ly1 = y2+ny
    lx2 = x2-nx
    ly2 = y2-ny                            
    lx3 = x1-nx
    ly3 = y1-ny
    lx4 = x1+nx
    ly4 = y1+ny 

	'' Draw the quad using 2 triangles
	Rel_Tri_F( lx1, ly1, lx2, ly2, lx3, ly3, r, g, b, alpha )
	Rel_Tri_F( lx3, ly3, lx1, ly1, lx4, ly4, r, g, b, alpha ) 
               	
	
end sub

'' Draws a triangle

Sub Rel_Tri_F( Byval x1 As Integer, Byval y1 As Integer,_
               Byval x2 As Integer, Byval y2 As Integer, Byval x3 As Integer,_
               Byval y3 As Integer, Byval r As integer, Byval g As integer, Byval b As integer,_
               Byval a As integer)



    Dim dx1 As Integer, dy1 As Integer
    Dim dx2 As Integer, dy2 As Integer
    Dim dx3 As Integer, dy3 As Integer
   

    Dim delta1 As Integer, delta2 As Integer, delta3 As Integer
    Dim Lx As Integer, Rx  As Integer
       
   
   
    If y2 < y1 Then
        Swap y1, y2
        Swap x1, x2
    End If
    If y3 < y1 Then
        Swap y3, y1
        Swap x3, x1
    End If
   
    If y3 < y2 Then
        Swap y3, y2
        Swap x3, x2
    End If
   
    dx1 = x2 - x1
    dy1 = y2 - y1
    If dy1 <> 0 Then
        delta1 = (dx1 Shl 16) \ dy1
    Else
        delta1 = 0
    End If
   
    dx2 = x3 - x2
    dy2 = y3 - y2
    If dy2 <> 0 Then
        delta2 = (dx2 Shl 16) \ dy2
    Else
        delta2 = 0
    End If


    dx3 = x1 - x3
    dy3 = y1 - y3
    If dy3 <> 0 Then
        delta3 = (dx3 Shl 16) \ dy3
    Else
        delta3 = 0
    End If
   
   
    'Flat bottom
    'Top part of triangle

    Dim y As Integer
    Dim Lxo As Integer, Rxo As Integer

    Lx = x1 Shl 16
    Rx = Lx
   
    For y = y1 To y2 - 1
        Lxo = Lx Shr 16
        Rxo = Rx Shr 16                
        line (Lxo, y)-(Rxo,y), rgba(r,g,b,a) 
        Lx = Lx + delta1
        Rx = Rx + delta3
    Next y

    'Flat top
    'Lower part of triangle

    Lx = x2 Shl 16
    For y = y2 To y3
        Lxo = Lx Shr 16
        Rxo = Rx Shr 16        
        line (Lxo, y)-(Rxo,y), rgba(r,g,b,a) 
        Lx = Lx + delta2
        Rx = Rx + delta3
    Next y

End Sub

Sub setSoundVolume
IF PlaySound = TRUE THEN
IF SFXVolume = 0 THEN SetVol = 0
IF SFXVolume = 1 THEN SetVol = 63
IF SFXVolume = 2 THEN SetVol = 126
IF SFXVolume = 3 THEN SetVol = 190
IF SFXVolume = 4 THEN SetVol = 255
FSOUND_SetSFXMasterVolume(SetVol)
IF shifting_volume <> setvolold THEN FSOUND_SetVolumeAbsolute(1, shifting_volume)
IF MusicVolume = 0 THEN SetVol = 0
IF MusicVolume = 1 THEN SetVol = 63
IF MusicVolume = 2 THEN SetVol = 126
IF MusicVolume = 3 THEN SetVol = 190
IF MusicVolume = 4 THEN SetVol = 255
FSOUND_SetVolumeAbsolute(1, SetVol)
END IF
End Sub

sub playsample(plsnd as integer ptr)
    IF SFXVolume = 0 THEN EXIT SUB
    If PlaySound = TRUE Then 
        'setSoundVolume
        FSOUND_PlaySound(FSOUND_FREE, plsnd)
        setSoundVolume
    END IF
end sub

SUB playStreamMusic(musc As String)
    IF MusicVolume = 0 THEN EXIT SUB
    if PlaySound = TRUE then
        music_handle = FSOUND_Stream_Open(musc, FSOUND_LOOP_NORMAL, 0, 0 ) 
        FSOUND_Stream_SetMode(music_handle,FSOUND_LOOP_NORMAL) 
        FSOUND_Stream_Play(1, music_handle)
        IF MusicVolume = 0 THEN SetVol = 0
        IF MusicVolume = 1 THEN SetVol = 63
        IF MusicVolume = 2 THEN SetVol = 126
        IF MusicVolume = 3 THEN SetVol = 190
        IF MusicVolume = 4 THEN SetVol = 255
        setvolold = SetVol
        shifting_volume = SetVol
        FSOUND_SetVolumeAbsolute(1, SetVol)
    END IF	
END SUB

Sub stopStreamMusic()
    if PlaySound = TRUE then
        FSOUND_Stream_Stop music_handle
        FSOUND_Stream_Close music_handle
    end if
end sub


Sub GenPlasma(byval w     as integer, byval h     as integer, _
              byval crnr1 as integer, byval crnr2 as integer, _
              byval crnr3 as integer, byval crnr4 as integer, _
              byval rough as integer, byval iter  as integer, _
              byval prs   as double)
    Dim as double prex, prey, d1, d2, d3, d4, hr, fv, Image(0 to w,0 to h)
    hr = rough * 2
    prex = w / 2: prey = h / 2
    Image(0,0)=crnr1
    Image(w,0)=crnr2: d1 = (crnr1+crnr2) / 2: Image(prex,0)=d1
    Image(w,h)=crnr3: d2 = (crnr2+crnr3) / 2: Image(w,prey)=d2
    Image(0,h)=crnr4: d3 = (crnr3+crnr4) / 2: Image(prex,h)=d3
                      d4 = (crnr4+crnr1) / 2: Image(0,prey)=d4
    fv = ((d1+d2+d3+d4) / 4) + (Int(rnd * hr) - rough) 
    If fv>255 Then 
        fv=255
    ELseif fv<0 Then 
        fv=0
    Endif
    Image(Cint(prex),Cint(prey))=fv
    Dim as double divisor, mdivx, mdivy, i, xs, ys, c1,c2,c3,c4, cx,cy, dx,dy
    mdivx = w / 2: mdivy = h / 2
    w -= 1: h -= 1
    For i = 1 to iter
        For ys = 0 To h Step mdivy
            For xs = 0 To w Step mdivx
                prex = mdivx / 2: prey = mdivy / 2
                cx   = xs + mdivx : cy   = ys + mdivy
                c1 = Image(Cint(xs),Cint(ys)): c2 = Image(Cint(cx),Cint(ys))
                c3 = Image(Cint(cx),Cint(cy)): c4 = Image(Cint(xs),Cint(cy))
                d1 = (c1+c2) / 2: d2 = (c2+c3) / 2
                d3 = (c3+c4) / 2: d4 = (c4+c1) / 2
                dx = xs + prex: dy = ys + prey
                Image(Cint(dx),Cint(ys))=d1
                Image(Cint(cx),Cint(dy))=d2
                Image(Cint(dx),Cint(cy))=d3
                Image(Cint(xs),Cint(dy))=d4
                fv = ((d1+d2+d3+d4) / 4) + (Int(rnd * hr) - rough)
                If fv>255 Then 
                    fv=255
                ELseif fv<0 Then 
                    fv=0
                Endif
                Image(Cint(dx),Cint(dy))=fv
            Next xs
        Next ys
        mdivx = mdivx / 2
        mdivy = mdivy / 2
        hr    = rough 
        rough = rough * prs
        'screenlock
        For ys = 0 To h step mdivy
            For xs = 0 To w step mdivx
                Line (xs,ys)-(xs+mdivx,ys+mdivy), Grad(Image(xs,ys)), BF
            Next xs
        Next ys
        'screenunlock
    Next i
    
End Sub

Sub Rainbow(bluecol AS INTEGER, greencol AS INTEGER, redcol AS INTEGER)
    Dim as integer i, b, badd, g, gadd, r, radd, rx, gx, bx
    rx =  2
    gx = -2
    bx = -2
    b=bluecol:badd= bx
    g=greencol:gadd= gx
    r=redcol:radd= rx
    For i = 0 To 255
        b += badd
        g += gadd
        r += radd
        If b < 0   Then badd = -bx: b = 0
        If b > 255 Then badd =  bx: b = 255
        If g < 0   Then gadd = -gx: g = 0
        If g > 255 Then gadd =  gx: g = 255
        If r > 255 Then radd = -rx: r = 255
        If r < 0   Then radd =  rx: r = 0
        Grad(i) = RGB(r,g,b)
    Next i
end sub

SUB DrawFlare (fposx AS SINGLE, fposy AS SINGLE, flradius AS INTEGER)
    
FOR fly AS INTEGER = 0 TO flradius * 2
    FOR flx AS INTEGER = 0 TO flradius * 2
        fldx = flradius - flx
        fldy = flradius - fly
        flr = SQR(fldx * fldx + fldy * fldy) / flradius
        flc = 1 - flr
        flc = flc * flc
        IF flr > 1 THEN flc = 0
        flcr = flc * 255
        flcg = flc * 128
        flcb = flc * 255
        pset(flx+fposx,fly+fposy), rgb(flcr,flcg,flcb)
    NEXT flx
NEXT fly
    
END SUB

SUB FadeInScreen ()
    
' Fades in a screen if used on appropriate
' positon and if bimageblend is set to < 255.
' workpage xor = 1 needs to be used before
' it to work properly.
 
IF bimageblend < 255 THEN
GET (0,0)-(639,479), fadeimg_buffer(1)
workpage xor = 1
screenunlock

DO   
st = Timer
screenlock
IF bimageblend < 37 THEN LINE (0,0)-(639,479), RGBA(0, 0, 0, 255), BF
screenset workpage, workpage xor 1
LINE (0,0)-(639,479), RGBA(0, 0, 0, 255), BF
PUT (0,0), fadeimg_buffer(1), ALPHA, bimageblend

workpage xor = 1
screenunlock

bimageblend = bimageblend + 15
IF bimageblend > 255 THEN bimageblend = 255
sleepintvl = Cint((st + frameintvl - Timer)*1000.0)
If sleepintvl>1 Then 
    Sleep sleepintvl
else 
    sleep 1
end if

LOOP UNTIL bimageblend = 255
END IF

END SUB

SUB FadeOutScreen ()
    
' Fades out a screen if used on appropriate
' positon and if bimageblend is set to > 0.
' workpage xor = 1 needs to be used before
' it to work properly.

IF bimageblend > 0 THEN
GET (0,0)-(639,479), fadeimg_buffer(1)
workpage xor = 1
screenunlock

DO   
st = Timer
screenlock
'IF bimageblend > 213 THEN LINE (0,0)-(639,479), RGBA(0, 0, 0, 255), BF
screenset workpage, workpage xor 1
LINE (0,0)-(639,479), RGBA(0, 0, 0, 255), BF
PUT (0,0), fadeimg_buffer(1), ALPHA, bimageblend

workpage xor = 1
screenunlock

bimageblend = bimageblend - 15
IF bimageblend < 0 THEN bimageblend = 0
sleepintvl = Cint((st + frameintvl - Timer)*1000.0)
If sleepintvl>1 Then 
    Sleep sleepintvl
else 
    sleep 1
end if

LOOP UNTIL bimageblend = 0
END IF

END SUB

SUB Options ()
    
FadeOutScreen
    
DO

Frame1 = (Frame1 MOD 4) + 1

st = Timer

screenlock
screenset workpage, workpage xor 1

FPS = FPS + 1                     ' Used to count FPS.
if StartTime + 1 < timer then
 FPS2 = FPS
 FPS = 0
 seconds = seconds + 1
 StartTime = timer
end if

LINE (0,0)-(639,479), RGBA(0, 0, 0, 255), BF
PUT (0,0), image_buffer(1), ALPHA, 100+cloop1/2

IF cloop1direc = 1 THEN cloop1 = cloop1 + 2
IF cloop1>255 THEN cloop1direc = 2
IF cloop1direc = 2 THEN cloop1 = cloop1 - 2
IF cloop1 < 150 THEN cloop1direc = 1
IF cloop1 > 255 THEN cloop1 = 255
IF cloop1 < 0 THEN cloop1 = 0

sfxv = "0 %"
IF SFXVolume = 1 THEN sfxv = "25 %"
IF SFXVolume = 2 THEN sfxv = "50 %"
IF SFXVolume = 3 THEN sfxv = "75 %"
IF SFXVolume = 4 THEN sfxv = "100 %"
musicv = "0 %"
IF MusicVolume = 1 THEN musicv = "25 %"
IF MusicVolume = 2 THEN musicv = "50 %"
IF MusicVolume = 3 THEN musicv = "75 %"
IF MusicVolume = 4 THEN musicv = "100 %"
grmode = "NO"
IF GraphicMode = 2 THEN grmode = "YES"

FOR countmenuposblend AS INTEGER = 1 TO 5
MenuPosBlend(countmenuposblend) = 200
NEXT countmenuposblend
IF MenuY = MenuPos*10 THEN MenuPosBlend(MenuPos) = 255

Draw String (270,170), "Music Volume: "+musicv, RGBA(145,145, 255, MenuPosBlend(1))
Draw String (270,180), "SFX Volume: "+sfxv, RGBA(145,145, 255, MenuPosBlend(2))
Draw String (270,190), "Full Screen: "+grmode, RGBA(145,145, 255, MenuPosBlend(3))
Draw String (270,200), "Exit", RGBA(145,145, 255, MenuPosBlend(4))

Draw String (250,160+MenuY), "->", RGBA(145,145, 255, 255)

IF MULTIKEY(SC_DOWN) AND KeyFree = TRUE THEN 
MenuPos = MenuPos + 1
KeyFree = FALSE
END IF
IF MULTIKEY(SC_UP) AND KeyFree = TRUE THEN 
MenuPos = MenuPos - 1
KeyFree = FALSE
END IF
IF MenuPos > 4 THEN MenuPos = 4
IF MenuPos < 1 THEN MenuPos = 1
IF MenuY < MenuPos * 10 THEN MenuY = MenuY + 2
IF MenuY > MenuPos * 10 THEN MenuY = MenuY - 2

IF MULTIKEY(SC_ENTER) AND KeyFree = TRUE THEN
KeyFree = FALSE
SELECT CASE MenuPos
CASE 1
MusicVolume = MusicVolume + 1
IF MusicVolume > 4 THEN MusicVolume = 0
IF MusicVolume = 0 THEN SetVol = 0
IF MusicVolume = 1 THEN SetVol = 63
IF MusicVolume = 2 THEN SetVol = 126
IF MusicVolume = 3 THEN SetVol = 190
IF MusicVolume = 4 THEN SetVol = 255
IF PlaySound = TRUE THEN setSoundVolume
CASE 2
SFXVolume = SFXVolume + 1
IF SFXVolume > 4 THEN SFXVolume = 0
IF SFXVolume = 0 THEN SetVol = 0
IF SFXVolume = 1 THEN SetVol = 63
IF SFXVolume = 2 THEN SetVol = 126
IF SFXVolume = 3 THEN SetVol = 190
IF SFXVolume = 4 THEN SetVol = 255
IF PlaySound = TRUE THEN setSoundVolume
CASE 3
IF GraphicMode = 1 THEN
GraphicMode = 2
screenunlock
SCREENRES 640, 480, 32, 2, GFX_ALPHA_PRIMITIVES+GFX_FULLSCREEN
screenlock
GOTO skipchoices:
END IF
IF GraphicMode = 2 THEN
GraphicMode = 1
screenunlock
SCREENRES 640, 480, 32, 2, GFX_ALPHA_PRIMITIVES+GFX_WINDOWED
screenlock
GOTO skipchoices:
END IF
CASE 4
workpage xor = 1
'screenunlock
FadeOutScreen
EXIT SUB
END SELECT
END IF

skipchoices:

IF MULTIKEY(SC_ENTER) OR MULTIKEY(SC_DOWN) OR MULTIKEY(SC_UP) THEN GOTO skipkeyrest2:
KeyFree = TRUE
skipkeyrest2:

FadeInScreen

workpage xor = 1
screenunlock

sleepintvl = Cint((st + frameintvl - Timer)*1000.0)
If sleepintvl>1 Then 
    Sleep sleepintvl
else 
    sleep 1
end if

LOOP 

END SUB

SUB WriteHighScore

FadeOutScreen

' Open the high score file and read
' high score entries from it.
OPEN "data/hs.dat" FOR INPUT AS #3
FOR tar AS INTEGER = 1 TO 8
INPUT #3, textarray(tar)
neoENCdepass SADD(textarray(tar)), LEN(textarray(tar)), "noneofyourbusiness"
NEXT tar
CLOSE #3

' Scores are saved in textarray positions
' 5 to 8, but we need to save them
' in scores(1) to scores(4).
FOR ar AS INTEGER = 5 TO 8
ar2 = ar - 4
scores(ar2) = VAL(textarray(ar))
NEXT ar

' If the player's score is higher
' that the lowest score in the
' high score table...
IF Score > scores(4) THEN

' Loop through all 4 scores.
FOR nameloop AS INTEGER = 1 TO 4
    
IF Score > scores(nameloop) THEN

WHILE MULTIKEY(SC_Y)
WEND


DO
    
IF PlaySound = TRUE THEN FSOUND_Update 

st = Timer

screenlock
screenset workpage, workpage xor 1

LINE (0,0)-(639,479), RGBA(0, 0, 0, 255), BF
PUT (0,0), image_buffer(1), ALPHA, 100+cloop1/2

IF cloop1direc = 1 THEN cloop1 = cloop1 + 2
IF cloop1>255 THEN cloop1direc = 2
IF cloop1direc = 2 THEN cloop1 = cloop1 - 2
IF cloop1 < 150 THEN cloop1direc = 1
IF cloop1 > 255 THEN cloop1 = 255
IF cloop1 < 0 THEN cloop1 = 0

zeross = ""
IF ABS(Score)<100000 THEN zeross = "0"
IF ABS(Score)<10000 THEN zeross = "00"
IF ABS(Score)<1000 THEN zeross = "000"
IF ABS(Score)<100 THEN zeross = "0000"
IF ABS(Score)<10 THEN zeross = "00000"
'AF2.PP256fnt_brightness " FINAL SCORE: "+zeross+STR$(Score), 55, 98, 0, 32, Font1, GFX_v, GFX_h 
Draw String (240,180), " FINAL SCORE: "+zeross+STR$(Score), RGBA(145,145, 255, 255)
Draw String (210,200), "Record your top score (Y/N)?", RGBA(145,145, 255, 255)

IF MULTIKEY(SC_N) THEN
workpage xor = 1
FadeOutScreen
EXIT SUB
END IF

FadeInScreen

workpage xor = 1
screenunlock

sleepintvl = Cint((st + frameintvl - Timer)*1000.0)
If sleepintvl>1 Then 
    Sleep sleepintvl
else 
    sleep 1
end if
SLEEP 1

LOOP UNTIL MULTIKEY(SC_Y)

InputName
' Flag the inputted name from the
' InputName sub.
highname = playername
startwrite = nameloop
GOTO writescore:
END IF
NEXT nameloop

' The code that bumps down lower scores onto
' lower positions before the new table
' is saved. Example, if your score is
' second, the previous second needs to
' be bumped on the third position, while
' the one from the third position needs
' to be bumped on position 4.
writescore:
IF startwrite = 4 THEN GOTO writescore2:
FOR m = 3 TO startwrite STEP -1
n = m + 1
textarray(n) = textarray(m)
scores(n) = scores(m)
NEXT m
writescore2:
scores(startwrite) = Score
textarray(startwrite) = highname

FOR ar AS INTEGER = 5 TO 8
ar2 = ar - 4
textarray(ar) = LTRIM$(STR$(scores(ar2)))
NEXT ar

OPEN "data/hs.dat" FOR OUTPUT AS #3
FOR tar AS INTEGER = 1 TO 8
neoENCpass SADD(textarray(tar)), LEN(textarray(tar)), "noneofyourbusiness"
PRINT #3, textarray(tar)
NEXT tar
CLOSE #3

ReadHighScore

END IF

END SUB

SUB neoENCdepass (Offsett AS ANY PTR, Lengthh AS INTEGER, passsword AS STRING)

' Subs used to encrypt and decrypt the high scores
' data.

DIM nowpos AS INTEGER, Bytee AS INTEGER, I AS INTEGER

   IF LEN(passsword) = 0 THEN EXIT SUB
   nowpos = 1

   FOR I = 0 TO Lengthh - 1
      Bytee = PEEK(Offsett + I)
      Bytee = Bytee - ASC(MID$(passsword, nowpos, 1))
      IF Bytee < 0 THEN Bytee = Bytee + 256
      POKE Offsett + I, Bytee
      nowpos = nowpos + 1
      IF nowpos > LEN(passsword) THEN nowpos = 1
   NEXT I

END SUB

SUB neoENCpass (Offset AS ANY PTR, Length AS INTEGER, password AS STRING)
   
DIM nowpos AS INTEGER, Bytee AS INTEGER, I AS INTEGER   
   
   IF LEN(password) = 0 THEN EXIT SUB

   nowpos = 1
   
   FOR I = 0 TO Length - 1
      Bytee = PEEK(Offset + I)
      Bytee = Bytee + ASC(MID$(password, nowpos, 1))
      IF Bytee > 255 THEN Bytee = Bytee - 256
      POKE Offset + I, Bytee
      nowpos = nowpos + 1
      IF nowpos > LEN(password) THEN nowpos = 1
   NEXT I

END SUB

SUB ReadHighScore ()
    
FadeOutScreen

OPEN "data/hs.dat" FOR INPUT AS #3
FOR tar AS INTEGER = 1 TO 8
INPUT #3, textarray(tar)
neoENCdepass SADD(textarray(tar)), LEN(textarray(tar)), "noneofyourbusiness"
NEXT tar
CLOSE #3

DO
    
st = Timer    

IF PlaySound = TRUE THEN FSOUND_Update 

screenlock
screenset workpage, workpage xor 1

LINE (0,0)-(639,479), RGBA(0, 0, 0, 255), BF

PUT (0,0), image_buffer(1), ALPHA, 100+cloop1/2

IF cloop1direc = 1 THEN cloop1 = cloop1 + 2
IF cloop1>255 THEN cloop1direc = 2
IF cloop1direc = 2 THEN cloop1 = cloop1 - 2
IF cloop1 < 150 THEN cloop1direc = 1
IF cloop1 > 255 THEN cloop1 = 255
IF cloop1 < 0 THEN cloop1 = 0

Draw String (285, 120), "TOP SCORES", RGBA(2,117, 190, 255)

FOR tar AS INTEGER = 1 TO 4
Draw String (270, 140 + tar * 12), textarray(tar), RGBA(2,117, 190, cloop1)
NEXT tar
FOR ar AS INTEGER = 5 TO 8
Draw String (340, 140 + (ar - 4) * 12), textarray(ar), RGBA(2,117, 190, cloop1)
NEXT ar

Draw String (190, 270), "Press ENTER to return to main menu", RGBA(2,117, 190, 255)

FadeInScreen

workpage xor = 1
screenunlock

sleepintvl = Cint((st + frameintvl - Timer)*1000.0)
If sleepintvl>1 Then 
    Sleep sleepintvl
else 
    sleep 1
end if


LOOP UNTIL MULTIKEY(SC_ENTER)
FadeOutScreen

END SUB

SUB InputName ()
    
FadeOutScreen
    
' The name inputting sub originally 
' made by Ryan Szrama. I found it useful 
' so might you.

' inputted name is stored in playername
' string and each letter in playerletter(letnum)
' strings.

WHILE INKEY$ <> "": WEND

' Reset the name to input and
' letters to input(might some
' be in memory from the
' last inputting.
playername=""
countlet=1
for playl=1 to 8
playerletter(playl)=""
next playl

DO ' Main inputting loop.

IF PlaySound = TRUE THEN FSOUND_Update 

st = Timer

screenlock
screenset workpage, workpage xor 1

LINE (0,0)-(639,479), RGBA(0, 0, 0, 255), BF
PUT (0,0), image_buffer(1), ALPHA, 100+cloop1/2

IF cloop1direc = 1 THEN cloop1 = cloop1 + 2
IF cloop1>255 THEN cloop1direc = 2
IF cloop1direc = 2 THEN cloop1 = cloop1 - 2
IF cloop1 < 150 THEN cloop1direc = 1
IF cloop1 > 255 THEN cloop1 = 255
IF cloop1 < 0 THEN cloop1 = 0

blinker=" "
Frame2 = (Frame2 mod 16) + 1
if Frame2>8 then blinker="_"
inputted_letter = INKEY$   

if asc(ucase$(inputted_letter )) >= 65 and asc(ucase$(inputted_letter )) <= 90 and countlet<9 then
playerletter(countlet) = UCASE$(inputted_letter )
countlet=countlet+1
end if
if asc(inputted_letter ) = 8 and countlet > 1 then
countlet=countlet-1
playerletter(countlet) = ""
end if

playername=""
' Add each letter to the playername string.
for countpname AS INTEGER =1 to 8
playername=playername+playerletter(countpname)
next countpname

Draw String (200, 180), "Please input your name: ", RGBA(145,145, 255, 255) 
Draw String (390, 180), playername+blinker, RGBA(145,145, 255, 255) 

FadeInScreen

workpage xor = 1
screenunlock

sleepintvl = Cint((st + frameintvl - Timer)*1000.0)
If sleepintvl>1 Then 
    Sleep sleepintvl
else 
    sleep 1
end if

' End name inputting when the player has
' clicked ENTER and at least one letter has been
' inputted.
LOOP UNTIL MULTIKEY(SC_ENTER) AND countlet>1
FadeOutScreen

END SUB

SUB InitiatePowerUp (powx AS SINGLE, powy AS SINGLE, powtyp AS INTEGER)
    
FOR initpow AS INTEGER = 1 TO numofpowerups
 
    IF Powerup(initpow).Active = FALSE THEN
       Powerup(initpow).Active = TRUE
       Powerup(initpow).X = powx
       Powerup(initpow).Y = powy
       rndpowtyp = INT(RND * 23) + 1
       IF rndpowtyp > 0 THEN Powerup(initpow).Typ = 1
       IF rndpowtyp > 3 THEN Powerup(initpow).Typ = 2
       IF rndpowtyp > 6 THEN Powerup(initpow).Typ = 3
       IF rndpowtyp > 9 THEN Powerup(initpow).Typ = 4
       IF rndpowtyp > 12 THEN Powerup(initpow).Typ = 5
       IF rndpowtyp > 15 THEN Powerup(initpow).Typ = 6
       IF rndpowtyp > 17 THEN Powerup(initpow).Typ = 7
       IF rndpowtyp > 19 THEN Powerup(initpow).Typ = 8
       IF rndpowtyp > 21 THEN Powerup(initpow).Typ = 9
       'Powerup(initpow).Typ = 3
       Powerup(initpow).Speed = 1.8*speedup
       taranglerad = ATAN2((Powerup(initpow).Y-linecentery),(Powerup(initpow).X-linecenterx))
       taranglerad = taranglerad - PI/2 
       IF taranglerad<0 THEN taranglerad = taranglerad + 2*PI 
       tarangledeg = (taranglerad*180)/PI
       rndangle = 1 + INT(RND * 8) - 4
       tarangledeg = tarangledeg + rndangle
       IF tarangledeg<0 THEN tarangledeg=tarangledeg+360
       IF tarangledeg>359 THEN tarangledeg=tarangledeg-360
       Powerup(initpow).Angle = taranglerad
       EXIT SUB
       
    END IF
    
NEXT initpow

END SUB

SUB DrawPowerUps ()
  
FOR countpowerup AS INTEGER = 1 TO numofpowerups
    
IF Powerup(countpowerup).Active = TRUE THEN    
    IF Powerup(countpowerup).X>690 OR Powerup(countpowerup).X < - 50 THEN Powerup(countpowerup).Active = FALSE
    IF Powerup(countpowerup).Y>530 OR Powerup(countpowerup).Y < - 50 THEN Powerup(countpowerup).Active = FALSE
    Powerup(countpowerup).X = Powerup(countpowerup).X + SIN(Powerup(countpowerup).Angle)*Powerup(countpowerup).Speed
    Powerup(countpowerup).Y = Powerup(countpowerup).Y - COS(Powerup(countpowerup).Angle)*Powerup(countpowerup).Speed
    
 FOR colcheck AS INTEGER = 1 TO 2
            IF colcheck = 1 THEN linecol = RGBA(231, 231, 231, 255)
            IF colcheck = 2 THEN linecol = RGBA(192, 0, 0, 255)
        FOR loopb AS INTEGER = 0 TO 4
            IF POINT (Powerup(countpowerup).X-loopb, Powerup(countpowerup).Y+loopb) = linecol OR POINT (Powerup(countpowerup).X+loopb, Powerup(countpowerup).Y-loopb) = linecol OR POINT (Powerup(countpowerup).X-loopb, Powerup(countpowerup).Y-loopb) = linecol OR POINT (Powerup(countpowerup).X+loopb, Powerup(countpowerup).Y+loopb) = linecol THEN 
               Powerup(countpowerup).Active = FALSE
               ' Will give the player stuff
               ' later in here, according
               ' to powerup!
               SELECT CASE Powerup(countpowerup).Typ
               CASE 1
                   InitiateScrollText "Double Gun!" ''
                   DoubleGunTime = 40*20
                   playsample (sample(8))
               CASE 2
                   InitiateScrollText "Short Line!" ''
                   ShortLineTime = 40*20
                   LongLineTime = 0
                   playsample (sample(8))
               CASE 3
                   InitiateScrollText "Killer Line!" ''
                   KillerLineTime = 40*15
                   KillerLineWidth = 0
                   cloop1direc = 1
                   playsample (sample(8))
               CASE 4
                   InitiateScrollText "Blasters Ammo!" ''
                   Weapon3Ammo = Weapon3Ammo + 25
                   playsample (sample(8))
               CASE 5
                   InitiateScrollText "Homing Projectiles!" ''
                   HomingTime = 40*20
                   playsample (sample(8))
               CASE 6
                   InitiateScrollText "Laser Ammo!" ''
                   Weapon2Ammo = Weapon2Ammo + 500
                   playsample (sample(8))
               CASE 7
                   InitiateScrollText "Long line!" ''
                   ShortLineTime = 0
                   LongLineTime = 40*20
                   playsample (sample(9))
               CASE 8
                   InitiateScrollText "Watch out! No hole!" ''
                   NoHoleTime = 40*20
                   playsample (sample(9))
               CASE 9
                   InitiateScrollText "Sluggish!" ''
                   SlowLineTime = 40*20
                   playsample (sample(9))
               END SELECT
               GOTO nextpowerup:
            END IF
        NEXT loopb
  NEXT colcheck
  
    PUT (Powerup(countpowerup).X-8, Powerup(countpowerup).Y-8), pball_buffer(Powerup(countpowerup).Typ), TRANS

END IF 

nextpowerup: 
NEXT countpowerup
  
END SUB

SUB ResetVariables ()
    
RANDOMIZE TIMER
    
FOR resettrace AS INTEGER = 1 TO numofmisctraces
MiscTrace(resettrace).DTime = 0
NEXT resettrace
FOR resettrace2 AS INTEGER = 1 TO 30
MiscTrace2(resettrace2).DTime = 0
NEXT resettrace2
FOR resetproj AS INTEGER = 1 TO 90
Projectile(resetproj).Active = FALSE
NEXT resetproj
FOR resetball AS INTEGER = 1 TO 40
Ball(resetball).Active = FALSE
Ball(resetball).Alive = FALSE
Ball(resetball).LoopPass = FALSE
NEXT resetball
FOR resetship AS INTEGER = 1 TO 40
Ship(resetship).Active = FALSE
Ship(resetship).Alive = FALSE
NEXT resetship
FOR resetpowerup AS INTEGER = 1 TO numofpowerups
Powerup(resetpowerup).Active = FALSE
NEXT resetpowerup
FOR resetexpl AS INTEGER = 1 TO numofexplosions
Explosion(resetexpl).DTime = 0
NEXT resetexpl
FOR resetpart AS INTEGER = 1 TO numofparticles
Particle(resetpart).DTime = 0
NEXT resetpart
FOR resetscolltext AS INTEGER = 1 TO 5
ScrollText(resetscolltext).Active = FALSE 
NEXT resetscolltext

nextpowerup = INT(RND * 4) + 20

ShortLineTime = 0
LongLineTime = 0
KillerLineTime = 0
cloop1direc = 1
DoubleGunTime = 0
NoHoleTime = 0
SlowLineTime = 0
reload = 0

lineangle = 90
seconds = 0
startline = 20
endline = 55
EndGame = FALSE
EndGameCount = 0

currentbackg = 3

CountPhase = 0
dont_respawn = FALSE
checkforalive = FALSE
changebackground = FALSE
checkifhit = FALSE
weaponlock = FALSE
targetmode = 1 ' sets the accuracy of balls
raiseenergy = 0

volumedirection = 1

countkill = 0
CurrentWeapon = 1
Score = 0
Energy = 100
Weapon2Ammo = 0
Weapon3Ammo = 0

END SUB

SUB DrawMoveShips ()


FOR countship AS INTEGER = 1 TO numofships
IF Ship(countship).Alive = TRUE THEN
    
checkforalive = TRUE

        IF Ship(countship).LoopPass = 1 THEN resultAngle = ATAN2((-1)*(linecentery+cos(lineradangle)*endline-Ship(countship).Y),(linecenterx+sin(lineradangle)*endline-Ship(countship).X))
        IF Ship(countship).LoopPass = 2 THEN resultAngle = ATAN2((-1)*(linecentery-cos(lineradangle)*endline-Ship(countship).Y),(linecenterx+sin(lineradangle)*endline-Ship(countship).X))
        
        resultAngle = PI/2 - resultAngle
        IF resultAngle < 0 THEN resultAngle = resultAngle + 2*PI 

        resultangledeg = (resultAngle*180)/PI
        IF Frame1 = 4 THEN InitiateMiscTrace 9+Ship(countship).Typ,Ship(countship).X,Ship(countship).Y 
        
        IF Ship(countship).AngleDeg > resultangledeg THEN
            IF (360-Ship(countship).AngleDeg+resultangledeg) >= (Ship(countship).AngleDeg-resultangledeg) THEN Ship(countship).AngleDeg = Ship(countship).AngleDeg - Ship(countship).RotationSpeed
            IF (360-Ship(countship).AngleDeg+resultangledeg) < (Ship(countship).AngleDeg-resultangledeg) THEN Ship(countship).AngleDeg = Ship(countship).AngleDeg + Ship(countship).RotationSpeed
        END IF

        IF Ship(countship).AngleDeg < resultangledeg THEN
            IF (360-resultangledeg+Ship(countship).AngleDeg) >= (resultangledeg-Ship(countship).AngleDeg) THEN Ship(countship).AngleDeg = Ship(countship).AngleDeg + Ship(countship).RotationSpeed
            IF (360-resultangledeg+Ship(countship).AngleDeg) < (resultangledeg-Ship(countship).AngleDeg) THEN Ship(countship).AngleDeg = Ship(countship).AngleDeg - Ship(countship).RotationSpeed
        END IF

        IF Ship(countship).AngleDeg<0 THEN Ship(countship).AngleDeg=Ship(countship).AngleDeg+360
        IF Ship(countship).AngleDeg>359 THEN Ship(countship).AngleDeg=Ship(countship).AngleDeg-360

        Ship(countship).Angle = (Ship(countship).AngleDeg*PI)/180

        Ship(countship).X = Ship(countship).X + sin(Ship(countship).Angle)*Ship(countship).Speed
        Ship(countship).Y = Ship(countship).Y - cos(Ship(countship).Angle)*Ship(countship).Speed
        
        ' Ships to line collision
        IF Energy > 0 THEN
            FOR colcheck AS INTEGER = 1 TO 2
            IF colcheck = 1 THEN linecol = RGBA(231, 231, 231, 255)
            IF colcheck = 2 THEN linecol = RGBA(192, 0, 0, 255)
            FOR loopb AS INTEGER = 1 TO 10
            cpoint(1) = POINT (Ship(countship).X+sin(Ship(countship).Angle)*13-cos(Ship(countship).Angle)*loopb,Ship(countship).Y-cos(Ship(countship).Angle)*13-sin(Ship(countship).Angle)*loopb)
            cpoint(2) = POINT (Ship(countship).X+sin(Ship(countship).Angle)*13+cos(Ship(countship).Angle)*loopb,Ship(countship).Y-cos(Ship(countship).Angle)*13+sin(Ship(countship).Angle)*loopb)
            cpoint(3) = POINT (Ship(countship).X+sin(Ship(countship).Angle)*13+sin(Ship(countship).Angle)*loopb,Ship(countship).Y-cos(Ship(countship).Angle)*13-cos(Ship(countship).Angle)*loopb)
            FOR countpoint AS INTEGER = 1 TO 3
            IF cpoint(countpoint) = linecol THEN 
                 IF KillerLineTime = 0 THEN
                 score = score - 30
                 playsample (sample(10))
                 checkifhit = TRUE
                 Energy = Energy - 1*Ship(countship).Typ
                 IF Ship(countship).Typ = 2 THEN Energy = Energy - 0.25 ' game balancing
                 IF Energy < 0 THEN Energy = 0
                 END IF
                 IF KillerLineTime > 0 THEN
                     IF countkill = nextpowerup THEN
                     nextpowerup = INT(RND * 4) + 20
                     countkill = 0
                     InitiatePowerup Ship(countship).X, Ship(countship).Y, 1
                     END IF
                     sbonus = 1
                     score = score + (50 + Ship(countship).Typ * 25)*sbonus
                 END IF
                 IF score < 0 THEN score = 0
                 InitParticle Ship(countship).X, Ship(countship).Y, Ship(countship).Typ
                 InitParticle Ship(countship).X-3, Ship(countship).Y, Ship(countship).Typ
                 InitParticle Ship(countship).X, Ship(countship).Y+3, Ship(countship).Typ
                 InitParticle Ship(countship).X-1, Ship(countship).Y, Ship(countship).Typ
                 InitiateExplosion 1, Ship(countship).X, Ship(countship).Y
                 InitiateExplosion 2, Ship(countship).X, Ship(countship).Y
                 InitiateExplosion 3, Ship(countship).X, Ship(countship).Y
                 InitiateExplosion 4, Ship(countship).X, Ship(countship).Y
                 InitiateExplosion 5, Ship(countship).X, Ship(countship).Y
                 RandomizeShip countship
                 GOTO nextship:
            END IF
            NEXT loopb
            NEXT contpoint
            NEXT colcheck
        END IF
        
' Laser line collision.
FOR countpoint2 AS INTEGER = 1 TO 10
FOR countpoint AS INTEGER = 1 TO 3
        
        cpoint(1) = POINT (Ship(countship).X+sin(Ship(countship).Angle)*13+sin(Ship(countship).Angle)*countpoint2,Ship(countship).Y-cos(Ship(countship).Angle)*13-cos(Ship(countship).Angle)*countpoint2)
        cpoint(2) = POINT (Ship(countship).X+sin(Ship(countship).Angle)*13-cos(Ship(countship).Angle)*countpoint2,Ship(countship).Y-cos(Ship(countship).Angle)*13-sin(Ship(countship).Angle)*countpoint2)
        cpoint(3) = POINT (Ship(countship).X+sin(Ship(countship).Angle)*13+cos(Ship(countship).Angle)*countpoint2,Ship(countship).Y-cos(Ship(countship).Angle)*13+sin(Ship(countship).Angle)*countpoint2)
            
        IF cpoint(countpoint) = RGBA(255,245, 255, 255) OR cpoint(countpoint) = RGBA(255,251, 255, 255) OR cpoint(countpoint) = RGBA(255, 240, 255, 255) OR cpoint(countpoint) = RGBA(254,254, 254, 255) OR cpoint(countpoint) = RGBA(255,255, 255, 255) OR cpoint(countpoint) = RGBA(254,255,255, 255) THEN
        IF (Ship(countship).X > 20 AND Ship(countship).X < 620) AND (Ship(countship).Y > 20 AND Ship(countship).Y < 460) THEN    
             Ship(countship).Energy = Ship(countship).Energy - 6
             InitiateExplosion 6, Ship(countship).X+addx, Ship(countship).Y+addy
             IF Ship(countship).Energy > 0 THEN goto nextship:
             IF Ship(countship).Energy <= 0 THEN 
                 IF countkill = nextpowerup THEN
                 nextpowerup = INT(RND * 4) + 20
                 countkill = 0
                 InitiatePowerup Ship(countship).X, Ship(countship).Y, 1
                 END IF
                 sbonus = 1
                 score = score + (50 + Ship(countship).Typ * 15)*sbonus
                 InitiateExplosion 1, Ship(countship).X, Ship(countship).Y
                 InitParticle Ship(countship).X, Ship(countship).Y, Ship(countship).Typ
                 InitParticle Ship(countship).X-3, Ship(countship).Y, Ship(countship).Typ
                 InitParticle Ship(countship).X, Ship(countship).Y+3, Ship(countship).Typ
                 InitParticle Ship(countship).X-1, Ship(countship).Y, Ship(countship).Typ
                 InitiateExplosion 2, Ship(countship).X, Ship(countship).Y
                 InitiateExplosion 3, Ship(countship).X, Ship(countship).Y
                 InitiateExplosion 4, Ship(countship).X, Ship(countship).Y
                 InitiateExplosion 5, Ship(countship).X, Ship(countship).Y
                 RandomizeShip countship
             END IF
        END IF
        END IF

NEXT countpoint
NEXT countpoint2
        
        IF Ship(countship).Typ = 1 THEN
        LINE (Ship(countship).X-cos(Ship(countship).Angle)*10,Ship(countship).Y-sin(Ship(countship).Angle)*10)-(Ship(countship).X+sin(Ship(countship).Angle)*28,Ship(countship).Y-cos(Ship(countship).Angle)*28), RGB(170+countship, 0, 170+countship)
        LINE (Ship(countship).X+cos(Ship(countship).Angle)*10,Ship(countship).Y+sin(Ship(countship).Angle)*10)-(Ship(countship).X+sin(Ship(countship).Angle)*28,Ship(countship).Y-cos(Ship(countship).Angle)*28), RGB(170+countship, 0, 170+countship)
        LINE (Ship(countship).X-cos(Ship(countship).Angle)*10,Ship(countship).Y-sin(Ship(countship).Angle)*10)-(Ship(countship).X+cos(Ship(countship).Angle)*10,Ship(countship).Y+sin(Ship(countship).Angle)*10), RGB(170+countship, 0, 170+countship)
        PAINT (Ship(countship).X+sin(Ship(countship).Angle)*10, Ship(countship).Y-cos(Ship(countship).Angle)*10), RGB(240, 0, 240), RGB(170+countship, 0, 170+countship)
        END IF
    
        IF Ship(countship).Typ = 2 THEN
        LINE (Ship(countship).X-cos(Ship(countship).Angle)*10,Ship(countship).Y-sin(Ship(countship).Angle)*10)-(Ship(countship).X+sin(Ship(countship).Angle)*28,Ship(countship).Y-cos(Ship(countship).Angle)*28), RGB(61+countship, 0, 172+countship)
        LINE (Ship(countship).X+cos(Ship(countship).Angle)*10,Ship(countship).Y+sin(Ship(countship).Angle)*10)-(Ship(countship).X+sin(Ship(countship).Angle)*28,Ship(countship).Y-cos(Ship(countship).Angle)*28), RGB(61+countship, 0, 172+countship)
        LINE (Ship(countship).X-cos(Ship(countship).Angle)*10,Ship(countship).Y-sin(Ship(countship).Angle)*10)-(Ship(countship).X+cos(Ship(countship).Angle)*10,Ship(countship).Y+sin(Ship(countship).Angle)*10), RGB(61+countship, 0, 172+countship)
        PAINT (Ship(countship).X+sin(Ship(countship).Angle)*10, Ship(countship).Y-cos(Ship(countship).Angle)*10), RGB(93, 0, 255), RGB(61+countship, 0, 172+countship)
        END IF

nextship:
END IF
NEXT countship
    
END SUB

SUB RandomizeShip (rndship AS INTEGER)

    Ship(rndship).Alive = FALSE
    bsection = 1 + INT(RND * 4)
    
    IF bsection = 1 THEN 
            Ship(rndship).X = -40 + INT(RND * 20) 
            Ship(rndship).Y = -20 + INT(RND * 500)
    END IF
    IF bsection = 2 THEN 
            Ship(rndship).X = 680 + INT(RND * 20) 
            Ship(rndship).Y = -20 + INT(RND * 500)
    END IF
    IF bsection = 3 THEN 
            Ship(rndship).X = -20 + INT(RND * 660) 
            Ship(rndship).Y = -30 + INT(RND * 20)
    END IF
    IF bsection = 4 THEN 
            Ship(rndship).X = -20 + INT(RND * 660) 
            Ship(rndship).Y = 470 + INT(RND * 20)
    END IF
        
    taranglerad = ATAN2((Ship(rndship).Y-linecentery),(Ship(rndship).X-linecenterx))
    taranglerad = taranglerad - PI/2 
    IF taranglerad<0 THEN taranglerad = taranglerad + 2*PI 
    tarangledeg = (taranglerad*180)/PI
    rndangle = 1 + INT(RND * 200) - 100
    tarangledeg = tarangledeg + rndangle
    IF tarangledeg<0 THEN tarangledeg=tarangledeg+360
    IF tarangledeg>359 THEN tarangledeg=tarangledeg-360

    IF dont_respawn = FALSE THEN Ship(rndship).Alive = TRUE
    Ship(rndship).Angle = (tarangledeg*PI)/180
    Ship(rndship).LoopPass = INT(RND * 2) + 1 ' With ships used to tell the ship which part of the line to "target"
    SELECT CASE Ship(rndship).Typ
        CASE 1
         Ship(rndship).Speed = 1.6*speedup
         Ship(rndship).Energy = 1
         Ship(rndship).RotationSpeed = 1
        CASE 2
         Ship(rndship).Speed = 1.9*speedup
         Ship(rndship).Energy = 2
         Ship(rndship).RotationSpeed = 1.5
    END SELECT
    
END SUB

SUB Instructions ()
    
FadeOutScreen
    
DO 
    
IF PlaySound = TRUE THEN FSOUND_Update 

st = Timer

screenlock
screenset workpage, workpage xor 1

LINE (0,0)-(639,479), RGBA(0, 0, 0, 255), BF
PUT (0,0), image_buffer(1), ALPHA, 100+cloop1/2

IF cloop1direc = 1 THEN cloop1 = cloop1 + 2
IF cloop1>255 THEN cloop1direc = 2
IF cloop1direc = 2 THEN cloop1 = cloop1 - 2
IF cloop1 < 150 THEN cloop1direc = 1
IF cloop1 > 255 THEN cloop1 = 255
IF cloop1 < 0 THEN cloop1 = 0

Draw String (240, 20-8), "I N S T R U C T I O N S", RGBA(145,145, 255, 255) 

Draw String (20, 50-8), "Mighty Line", RGBA(195,195, 255, 255) 
Draw String (20, 50-8), "            is a single-screen shooter. You control the line in the center", RGBA(145,145, 255, 255) 
Draw String (20, 60-8), "of the screen with a goal to destroy as many balls possible and avoid ", RGBA(145,145, 255, 255) 
Draw String (20, 70-8), "collision with them. The game is consisted of several phases, each ", RGBA(145,145, 255, 255) 
Draw String (20, 80-8), "featuring different numbers of balls, their accuracy, or some special ", RGBA(145,145, 255, 255) 
Draw String (20, 90-8), "characteristic. It is up to you to discover those in the 8 minutes round ", RGBA(145,145, 255, 255) 
Draw String (20, 100-8), "that each game lasts. Collision counts for negative points and loss of life", RGBA(145,145, 255, 255) 
Draw String (20, 110-8), "energy. Yes, you can die in the game.", RGBA(145,145, 255, 255) 

Draw String (20, 130-8), "CONTROLS: LEFT/RIGHT arrow key to rotate", RGBA(145,145, 255, 255) 
Draw String (20, 140-8), "          SPACE, 8 (NUM) or X to shoot", RGBA(145,145, 255, 255) 
Draw String (20, 150-8), "          1, 2, 3 to change weapon", RGBA(145,145, 255, 255) 

Draw String (20, 170-8), "WEAPONS: Cannon - basic weapon with infinite ammo", RGBA(145,145, 255, 255) 
Draw String (20, 180-8), "         Laser - devastating weapon, but depletes its ammo fast", RGBA(145,145, 255, 255) 
Draw String (20, 190-8), "         Blasters - power balls that explode in the vicinity of a", RGBA(145,145, 255, 255) 
Draw String (20, 200-8), "                    moving object and destroy everything near itself", RGBA(145,145, 255, 255) 

Draw String (20, 218), "After a certain amount of kills a powerup is spawned.", RGBA(145,145, 255, 255) 
Draw String (20, 228), "This is the effect of each powerup (they last for a limited amount of time):", RGBA(145,145, 255, 255) 

PUT (4+22,251-6-5), pball_buffer(1), TRANS
PUT (4+22,271-6-5), pball_buffer(2), TRANS
PUT (4+22,291-6-5), pball_buffer(3), TRANS
PUT (4+22,311-6-5), pball_buffer(4), TRANS
PUT (4+22,331-6-5), pball_buffer(5), TRANS
PUT (4+22,351-6-5), pball_buffer(6), TRANS
PUT (4+22,371-6-5), pball_buffer(7), TRANS
PUT (4+22,391-6-5), pball_buffer(8), TRANS
PUT (4+22,411-6-5), pball_buffer(9), TRANS

Draw String (50, 250-6), "double gun", RGBA(145,145, 255, 255) 
Draw String (50, 270-6), "short line", RGBA(145,145, 255, 255) 
Draw String (50, 290-6), "killer line (invincibility and you get points for collision)", RGBA(145,145, 255, 255) 
Draw String (50, 310-6), "blasters ammo", RGBA(145,145, 255, 255) 
Draw String (50, 330-6), "cannon and blasters' projectiles become homing", RGBA(145,145, 255, 255) 
Draw String (50, 350-6), "laser ammo", RGBA(145,145, 255, 255) 
Draw String (50, 370-6), "long line", RGBA(145,145, 255, 255) 
Draw String (50, 390-6), "no hole", RGBA(145,145, 255, 255) 
Draw String (50, 410-6), "slow line", RGBA(145,145, 255, 255) 

Draw String (20, 435-6), "For small bonus let a ball through the hole in the middle of the line", RGBA(145,145, 255, 255) 
Draw String (20, 445-6), "to tag it, and then destroy it for super bonus.", RGBA(145,145, 255, 255) 

Draw String (200, 468), "Press ENTER to return to main menu", RGBA(145,145, 255, 215) 

FadeInScreen

workpage xor = 1
screenunlock

sleepintvl = Cint((st + frameintvl - Timer)*1000.0)
If sleepintvl>1 Then 
    Sleep sleepintvl
else 
    sleep 1
end if

' End name inputting when the player has
' clicked ENTER and at least one letter has been
' inputted.
LOOP UNTIL MULTIKEY(SC_ENTER)
FadeOutScreen

END SUB

SUB PhaseControl ()
    
IF Seconds = 2 AND CountPhase = 0 THEN
CountPhase = 1
InitiateScrollText "Get Ready!"
END IF
IF Seconds = 5 AND CountPhase = 1 THEN
checkifhit = FALSE
CountPhase = 2
InitiateScrollText "Warm-up!"
InitPhase 1
END IF
IF Seconds = 30 AND CountPhase = 2 THEN
dont_respawn = TRUE
END IF

IF (Seconds > 30 AND CountPhase = 2 AND checkforalive = FALSE) THEN
CountPhase = 3
dont_respawn = FALSE
InitiateScrollText "Let's Begin!"
IF checkifhit = FALSE THEN 
InitiateScrollText "No hit! Super bonus! + 4000"
score = score + 4000
END IF
checkifhit = FALSE
InitPhase 2
'changebackground = TRUE
'oldbackground = currentbackg
'currentbackg = 1
'backchangeblend = 0
END IF

IF Seconds = 125 AND CountPhase = 3 THEN
dont_respawn = TRUE
END IF

IF (Seconds > 125 AND CountPhase = 3 AND checkforalive = FALSE) THEN
CountPhase = 4
dont_respawn = FALSE
weaponlock = TRUE
InitiateScrollText "Warning!!! Weapons locked!"
IF checkifhit = FALSE THEN 
InitiateScrollText "No hit! Super bonus! + 4000"
score = score + 4000
END IF
checkifhit = FALSE
InitPhase 3
changebackground = TRUE
oldbackground = currentbackg
currentbackg = 1
backchangeblend = 0
END IF

IF Seconds = 200 AND CountPhase = 4 THEN
dont_respawn = TRUE
END IF

IF (Seconds > 200 AND CountPhase = 4 AND checkforalive = FALSE) THEN
CountPhase = 5
weaponlock = FALSE
dont_respawn = FALSE
targetmode = 2
InitiateScrollText "Accurate balls attack!"
IF checkifhit = FALSE THEN 
InitiateScrollText "No hit! Super bonus! + 6000"
score = score + 6000
END IF
InitiateScrollText "Energy Awarded! + 60"
raiseenergy = 60
checkifhit = FALSE
InitPhase 4
END IF

IF Seconds = 265 AND CountPhase = 5 THEN
dont_respawn = TRUE
END IF

IF (Seconds > 265 AND CountPhase = 5 AND checkforalive = FALSE) THEN
CountPhase = 6
targetmode = 1
dont_respawn = FALSE
InitiateScrollText "Here comes another tough wave!"
IF checkifhit = FALSE THEN 
InitiateScrollText "No hit! Super bonus! + 5000"
score = score + 5000
END IF
InitiateScrollText "Energy Awarded! + 70"
raiseenergy = 70
checkifhit = FALSE
InitPhase 5
changebackground = TRUE
oldbackground = currentbackg
currentbackg = 2
backchangeblend = 0
END IF

IF Seconds = 380 AND CountPhase = 6 THEN
dont_respawn = TRUE
END IF

IF (Seconds > 380 AND CountPhase = 6 AND checkforalive = FALSE) THEN
CountPhase = 7
dont_respawn = FALSE
InitiateScrollText "Kamikaze Attack!"
IF checkifhit = FALSE THEN 
InitiateScrollText "No hit! Super bonus! + 6000"
score = score + 6000
END IF
InitiateScrollText "Energy Awarded! + 80"
raiseenergy = 80
checkifhit = FALSE
InitPhase 6
changebackground = TRUE
oldbackground = currentbackg
currentbackg = 4
backchangeblend = 0
END IF

IF Seconds = 460 AND CountPhase = 7 THEN dont_respawn = TRUE

IF Seconds > 468 AND CountPhase = 7 AND checkforalive = FALSE THEN
dont_respawn = TRUE
CountPhase = 8
IF checkifhit = FALSE THEN 
InitiateScrollText "No hit! Super bonus! + 5000"
score = score + 5000
END IF
END IF

END SUB

SUB CalculateScore ()

FadeOutScreen

count_scoringscreen = 0
count_finished = 0
count_left_energy = 0

DO
    
st = Timer    

IF PlaySound = TRUE THEN FSOUND_Update 

count_scoringscreen = count_scoringscreen + 1

screenlock
screenset workpage, workpage xor 1

LINE (0,0)-(639,479), RGBA(0, 0, 0, 255), BF

IF cloop1direc = 1 THEN cloop1 = cloop1 + 2
IF cloop1>255 THEN cloop1direc = 2
IF cloop1direc = 2 THEN cloop1 = cloop1 - 2
IF cloop1 < 150 THEN cloop1direc = 1
IF cloop1 > 255 THEN cloop1 = 255
IF cloop1 < 0 THEN cloop1 = 0

PUT (0,0), image_buffer(currentbackg), ALPHA, 100+cloop1/2

IF count_scoringscreen > 30  THEN
IF count_finished < 100/470*Seconds AND CountPhase < 8 THEN
count_finished = count_finished + 1
playsample (sample(11))
IF count_finished >= 100/470*Seconds AND count_scoringscreen < 140 THEN count_scoringscreen = 141
Score = Score + 30
END IF
IF count_finished < 100 AND CountPhase = 8 THEN
count_finished = count_finished + 1
playsample (sample(11))
IF count_finished = 100 AND count_scoringscreen < 140 THEN count_scoringscreen = 141
Score = Score + 30
END IF
END IF

IF count_scoringscreen > 5 THEN Draw String (240,50), "Final Score Calculation", RGBA(175,175, 255, 255)

IF count_scoringscreen > 30 THEN
Draw String (240-30,60+40), "Completion bonus: + "+STR$(count_finished)+"% * "+STR$(30), RGBA(145,145, 255, 255)
END IF

IF count_scoringscreen > 160 THEN
IF count_finished = 100 THEN 
Draw String (240-30,80+40), "Round Finished Bonus: + 4000", RGBA(145,145, 255, 255)
IF count_scoringscreen = 161 THEN Score = Score + 4000
END IF
IF count_finished < 100 THEN Draw String (240-30,80+40), "Round Finished Bonus: None", RGBA(145,145, 255, 255)
END IF

IF count_scoringscreen > 210 THEN
IF CountPhase = 8 THEN
IF count_left_energy < Energy THEN 
count_left_energy = count_left_energy + 1
playsample (sample(11))
Score = Score + 20
END IF
Draw String (240-30,100+40), "Remaining Energy Bonus: "+STR$(count_left_energy)+" * "+STR$(20), RGBA(145,145, 255, 255)
END IF
IF CountPhase < 8 THEN Draw String (240-30,100+40), "Remaining Energy Bonus: None", RGBA(145,145, 255, 255)
END IF

zeross = ""
IF ABS(Score)<100000 THEN zeross = "0"
IF ABS(Score)<10000 THEN zeross = "00"
IF ABS(Score)<1000 THEN zeross = "000"
IF ABS(Score)<100 THEN zeross = "0000"
IF ABS(Score)<10 THEN zeross = "00000"
IF count_scoringscreen > 30 THEN Draw String (240-30,140+40), "SCORE: "+zeross+STR$(Score), RGBA(145,145, 255, 255)

IF count_scoringscreen = (210 + Energy + 20) THEN playsample (sample(12))
IF count_scoringscreen > (210 + Energy + 20) THEN Draw String (240,210+40), "Press ENTER to continue", RGBA(145,145, 255, 255)

FadeInScreen

workpage xor = 1
screenunlock

sleepintvl = Cint((st + frameintvl - Timer)*1000.0)
If sleepintvl>1 Then 
    Sleep sleepintvl
else 
    sleep 1
end if


LOOP UNTIL MULTIKEY(SC_ENTER) AND count_scoringscreen > (210 + Energy + 20)
FadeOutScreen

END SUB
