' Hi. I wrote this shit when i was 14. And never finished it.
' Now I'm 26 and totally clueless what this even does.
' Sorry for the flickering, i never managed to implement
' double buffering, so it flickers like crap.
' Also back then i didn't even english, so parts may be in german.
' I'm releasing it here at qbparty'15 because here or never. Whatever.
' (slightly drunk) fgenesis signing off.

DECLARE FUNCTION choice% (array$(), X!, Y!, col1!, col2!, extra!)
DECLARE FUNCTION IF3$ (cond!, varfalse$, vartrue$)
DECLARE FUNCTION IF2! (cond!, vartrue!, varfalse!)
DECLARE SUB sixECK (centerx!, centery!, size!, angle!, ecken!, Col!)
DECLARE SUB do.splashscreen ()
DECLARE SUB ROTATEBOX (centerx!, centery!, size!, angle!, Col!)
DECLARE SUB WriDSP (byte!)
DECLARE SUB play8bitwave (f$)
DECLARE SUB PlaySound (Segment!, Offset!, freq AS LONG, length!)
DECLARE FUNCTION ReadDSP! ()
DECLARE FUNCTION ResetDSP! ()
DECLARE FUNCTION DMADone! (lengy!)
DECLARE SUB Sort (a$(), low!, high!)
DECLARE SUB ChangePalette (C!, r!, g!, B!)
DECLARE SUB GetStringData (f$)
DECLARE SUB WaitVerticalRetrace (Num%)
DECLARE FUNCTION Dir$ (FileSpec$)
DECLARE FUNCTION picklevel$ ()
DECLARE SUB CHECKSAVEGAMES ()
REM $INCLUDE: 'qb.bi'
REM $INCLUDE: 'data\double_x.bi' $DYNAMIC
CONST VERSION = .12
CONST BaseAddr = &H220
CONST CommAddr = BaseAddr + &HC
CONST ResetAddr = BaseAddr + &H6
version2 = VERSION
DIM SHARED enemy1(15, 20), enemy2(10, 30), enemy3(15, 20), enemy4(18, 25)
DIM SHARED levelstring$(100), LEVTYPE%
DIM SHARED dbxlogo2(115, 39)
DIM SHARED minifont(255, 1 TO 5, 1 TO 5)
DIM enemyxstep(1 TO maxenemies)
DIM specshot(1 TO 3)
DIM mainmenuarray$(1 TO 5)
DIM SHARED ex(1 TO maxex), exx(1 TO maxex), exy(1 TO maxex), exsizemin(1 TO maxex), exsizemax(1 TO maxex), excol(1 TO maxex)
DIM tempa(1 TO 5) '<-- temporre Variable
mainmenuarray$(1) = "Start Game"
mainmenuarray$(2) = "Training"
mainmenuarray$(3) = "Load game"
mainmenuarray$(4) = "Options"
mainmenuarray$(5) = "Quit"

ffix
CLS
PRINT " --- Dateien werden geladen"
result = LoadFX("sfx\Doublex.snd")
SELECT CASE result
CASE 0, 1, 2: QUIT
END SELECT
PRINT "FX-Daten geladen"
LOADSPRITES
PRINT "Sprites geladen"
PRINT "Speicherstnde werden berprft..."
CHECKSAVEGAMES
PRINT "... fertig"
PRINT "Config-Datei wird geladen..."
IF EXIST("double_x.cfg") THEN
PRINT "geffnet"
OPEN "double_x.cfg" FOR BINARY AS #1
GET #1, , sounddevice%
GET #1, , cpu
GET #1, , fileversion
IF fileversion <> VERSION THEN CLOSE #1: DBXERROR 5, ""
CLOSE #1
PRINT "Daten geladen"
ELSE
PRINT "Config-Datei existiert nicht und wird neu erstellt"
sounddevice% = IF2(HasBlaster, 3, 1)
SELECT CASE sounddevice%
CASE 1: PRINT "Keine Soundkarte gefunden. Zur soundwiedergabe wird der PC-Speaker benutzt."
SOUND 400, 10
CASE 3: PRINT "Es wurde eine Soundkarte gefunden."
PRINT "Der Sound wird ber die Soundkarte abgespielt.": PRINT
PRINT "In 2 Sekunden wird ein Sound abgespielt..."
HOLD 2
play8bitwave ("sfx\cardtest.wav")
HOLD 1.5
PRINT "Sound abgespielt. Wenn du ihn nicht gehrt hast,"
PRINT "ist deine Soundkarte entweder nicht mit Basic kompatibel"
PRINT "oder nicht richtig konfiguriert.": PRINT
HOLD 4
END SELECT
PRINT "CPU-Geschwindigkeit wird gemessen"
cpu = 0 'removed
END IF
PRINT " --- Sterne werden berechnet"
FOR nr = 1 TO maxstars
starx1(nr) = RAND(0, 260)
stary1(nr) = RAND(0, 200)
star1(nr) = RAND(1, 4)
NEXT
u$ = CHR$(0) + CHR$(UP)
d$ = CHR$(0) + CHR$(DOWN)
L$ = CHR$(0) + CHR$(LEFT)
r$ = CHR$(0) + CHR$(RIGHT)
escape$ = CHR$(ESC)
return$ = CHR$(ENTER)
PRINT : PRINT "Ladevorgang beendet. Los gehts !"
'/////////////////////////////////////////////////////
Set320x200 6
SCREEN 13
mainmenu:
IF sounddevice% = 3 THEN WriDSP &H3
level = 0
CLOSE
KBHoff
max% = 6: i% = 1
GOSUB cleargamevars
FOR weapon.nr = 2 TO 10
weapon.enabled(weapon.nr) = 0
ammo(weapon.nr) = 0
mi.ammo = 0: pl.ammo = 0
NEXT
money! = 0: wpn1type% = 1
FOR temp = 1 TO 3
specshot(temp) = 0
NEXT
DO
OPEN "double_x.cfg" FOR BINARY AS #1
PUT #1, , sounddevice%
PUT #1, , cpu
PUT #1, , version2
'...
CLOSE #1
SELECT CASE choice(mainmenuarray$(), 120, 80, 12, 4, 1)
CASE 1: randompos = 0:
'!!!
levelfile$ = picklevel$
IF levelfile$ = "" THEN GOTO mainmenu
GetStringData "levels\" + levelfile$
OPEN "levels\" + levelfile$ FOR INPUT AS #1
'!!!
EXIT DO
CASE 2: randompos = 1: useplusminus = 1: Future.CLS 0: EXIT DO
CASE 3: randompos = 0: GOSUB loadgame
CASE 4: CALL optionsmenu: restore.pal
CASE 0, 5: CLEAR : QUIT
END SELECT
LOOP
IF sounddevice% = 3 THEN WriDSP &H1
'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 IF randompos = 1 THEN
 FOR weapon.nr = 2 TO 10
 weapon.enabled(weapon.nr) = 1
 NEXT
 FOR ammo.nr = 1 TO 10
 ammo(ammo.nr) = 100
 NEXT
 mi.ammo = 20
 pl.ammo = 5
 wpn1type% = 1
 lpmax = 100
 END IF
'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
lp = lpmax
Future.CLS 0
'///////////////////////////////// maingame ///////////////
restart.gameloop:
KBHon
a = 150
ship.step = 5
C = 100
RANDOMIZE TIMER
game.speed = 1
gamover = 0: useplusminus = 0: showexplosions = 0: weapon = 1: countdowntimer.set = 0
FOR enemy.nr = 1 TO maxenemies: enemy(enemy.nr) = 0: NEXT
FOR nr = 1 TO maxshots: shot(nr) = 0
NEXT: fadebackgroundtimes = 0: scrollpos& = 0: timeUp = 0
IF randompos = 1 THEN useplusminus = 1
skipreadlevel% = 0: skipreadlevel2% = 0
restore.pal

 SELECT CASE wpn1type%
 CASE 1: sloop% = 1: eloop% = 1: delay1 = .2
 CASE 2: sloop% = 2: eloop% = 3: delay1 = .2
 CASE 3: sloop% = 2: eloop% = 5: delay1 = .2
 CASE 4: sloop% = 2: eloop% = 6: delay1 = .2
 CASE 5: sloop% = 1: eloop% = 6: delay1 = .2
 CASE 6: sloop% = 1: eloop% = 6: delay1 = .15
 CASE 7: sloop% = 1: eloop% = 8: delay1 = .15
 CASE 8: sloop% = 1: eloop% = 10: delay1 = .15
 CASE 9: sloop% = 1: eloop% = 10: delay1 = .1
 END SELECT

DO
steadychange% = (steadychange% + 1) MOD 32767
IF skipreadlevel% > 0 AND skipreadlevel2% = 0 THEN
skipreadlevel% = skipreadlevel% - 1
ELSEIF skipreadlevel% = 0 THEN
'///////////////////////////// READ LEVELDATA ////////////////////////////
FOR irgendwas = 1 TO game.speed
IF randompos = 0 THEN
 IF scrollpos& = 0 THEN
 getnextscrollpos% = 1
 DO
 LINE INPUT #1, lcmd$
 lcmd$ = LCASE$(lcmd$)
 IF LEFT$(lcmd$, 7) = ">level:" THEN
 level = VAL(RIGHT$(lcmd$, LEN(lcmd$) - 7))

 '...
 END IF
 LOOP UNTIL lcmd$ = ">begin"
 IF level = 0 THEN DBXERROR 7, ""
 END IF

IF getnextscrollpos% = 1 THEN
 INPUT #1, eventscrollpos$, event$, var1, var2, var3, var4
 IF LEFT$(eventscrollpos$, 1) = "+" THEN
  eventscrollpos& = scrollpos& + VAL(eventscrollpos$)
 ELSE
  eventscrollpos& = VAL(eventscrollpos$)
 END IF
 IF scrollpos& > eventscrollpos& THEN DBXERROR 2, ""
 getnextscrollpos% = 0
END IF
IF scrollpos& = eventscrollpos& THEN
getnextscrollpos% = 1
SELECT CASE LCASE$(event$)
CASE "setenemy"
   FOR enemy.nr = 1 TO maxenemies
   IF enemy(enemy.nr) = 0 THEN
   enemy(enemy.nr) = var1: enemyx(enemy.nr) = var2
   enemyheight(enemy.nr) = getheight(enemy(enemy.nr))
   enemywidth(enemy.nr) = getwidth(enemy(enemy.nr))
   eshotdelay(enemy.nr) = geteshotdelay(enemy(enemy.nr))
   enemylp(enemy.nr) = getenemylp(enemy(enemy.nr))
   enemyy(enemy.nr) = -enemyheight(enemy.nr)
   enemyspeed(enemy.nr) = var3
   IF enemyspeed(enemy.nr) = 0 THEN enemyspeed(enemy.nr) = 1
   enemyxstep(enemy.nr) = var4
   IF enemyx(enemy.nr) + enemywidth(enemy.nr) > 260 THEN enemy(enemy.nr) = 0
   EXIT FOR
   END IF
   NEXT
CASE "gamespeed": game.speed = var1: boost = var1
CASE "endlevel"
   level = level + 1
   CALL newlevel
   GOSUB shopping
   GOSUB savegame
   GOSUB cleargamevars
   GOTO restart.gameloop
CASE "showgetreadyscreen": : shwtxt$ = levelstring$(var1): showgetreadyscreen shwtxt$
CASE "useplusminus": useplusminus = var1
CASE "setcountdowntimer": countdown.ticks = var1: countdowntimer.set = 1:
CASE "showexplosions": showexplosions = var1: showexplosionsfrequency = var2
CASE "showexplosion": FOR ex.nr = 1 TO maxex
		      IF ex(ex.nr) = 0 THEN
		      exx(ex.nr) = var1
		      exy(ex.nr) = var2
		      exsizemax(ex.nr) = var3
		      exsizemin(ex.nr) = var4
		      ex(ex.nr) = exsizemin(ex.nr)
		      excol(ex.nr) = 113
		      EXIT FOR
		      END IF
		      NEXT
CASE "fadebackground": fadebackgroundtimes = var1: fader = var2
		       fadeg = var3: fadeb = var4
		       fadedirection = 1: fadecol = 1
CASE "setwall"
   INPUT #1, var5
   FOR enemy.nr = 1 TO maxenemies
   IF enemy(enemy.nr) = 0 THEN
   enemy(enemy.nr) = 100
   enemyx(enemy.nr) = var1
   enemyheight(enemy.nr) = var2
   enemywidth(enemy.nr) = var3
   enemylp(enemy.nr) = var4
   enemyy(enemy.nr) = -enemyheight(enemy.nr)
   enemyspeed(enemy.nr) = 1
   enemyxstep(enemy.nr) = var5
   IF enemyx(enemy.nr) + enemywidth(enemy.nr) > 260 THEN enemy(enemy.nr) = 0
   EXIT FOR
   END IF
   NEXT
CASE "freezepos": skipreadlevel2% = var1: skipreadlevel% = var2
CASE "showmessage"
     showmessageframes% = var2: messagex% = var3: messagey% = var4
     message$ = levelstring$(var1)
CASE "setboss"
   FOR enemy.nr = 1 TO maxenemies
   IF enemy(enemy.nr) = 0 THEN
   enemy(enemy.nr) = var1 + 79: enemyx(enemy.nr) = var2
   enemyheight(enemy.nr) = getheight(enemy(enemy.nr))
   enemywidth(enemy.nr) = getwidth(enemy(enemy.nr))
   eshotdelay(enemy.nr) = geteshotdelay(enemy(enemy.nr))
   enemylp(enemy.nr) = getenemylp(enemy(enemy.nr))
   enemyy(enemy.nr) = -enemyheight(enemy.nr)
   enemyspeed(enemy.nr) = var3
   IF enemyspeed(enemy.nr) = 0 THEN enemyspeed(enemy.nr) = 1
   IF enemyx(enemy.nr) + enemywidth(enemy.nr) > 260 THEN enemy(enemy.nr) = 0
   EXIT FOR
   END IF
    NEXT
CASE "endgame"
   CLOSE #1
   CALL newlevel
   SELECT CASE LEVTYPE%
   CASE 1: CALL gamefinish
   END SELECT
   GOTO mainmenu

CASE "setpowerup"
   FOR pow.nr = 1 TO maxpow
   IF pow(pow.nr) = 0 THEN
   pow(pow.nr) = var1
   powx(pow.nr) = var2
   powy(pow.nr) = -10
   EXIT FOR
   END IF
   NEXT
CASE "weaponstatus"
   weapon.enabled(var2) = var1
   ammo(var2) = var3
CASE "setweapon"
   IF weapon.enabled(var1) = 1 THEN weapon = var1
CASE "addlp": lp = lp + var1
CASE "addmaxlp": lpmax = lpmax + var1
CASE "flashing"
	INPUT #1, var5
	flashingEnabled = var1
	flashingFrequency = var2
	flashcolred = var3
	flashcolgreen = var4
	flashcolblue = var5
'...     
END SELECT
END IF
END IF
scrollpos& = scrollpos& + 1
NEXT
'///////////////////////// END READ LEVELDATA ///////////////////////////
END IF

IF GetKey(1) THEN GOTO mainmenu
IF GetKey(UP) THEN C = C - ship.step
IF GetKey(LEFT) THEN a = a - ship.step
IF GetKey(DOWN) THEN C = C + ship.step
IF GetKey(RIGHT) THEN a = a + ship.step
IF GetKey(14) AND debugmode = 1 THEN DO: LOOP UNTIL GetKey(28)
IF GetKey(25) THEN SHOWPAUSESCREEN pl.ammo, mi.ammo, INT(countdown.ticks \ 10 + .9)
IF GetKey(78) THEN IF useplusminus = 1 THEN boost = boost + .1
IF GetKey(74) THEN IF useplusminus = 1 THEN boost = boost - .1
IF GetKey(49) AND shipsteptimer! + .3 < TIMER THEN ship.step = ship.step - 1: shipsteptimer! = TIMER
IF GetKey(50) AND shipsteptimer! + .3 < TIMER THEN ship.step = ship.step + 1: shipsteptimer! = TIMER
IF boost < 1 THEN boost = 1
IF boost > 7 THEN boost = 7
game.speed = INT(boost)
IF GetKey(2) THEN weapon = 1
IF GetKey(3) THEN IF weapon.enabled(2) = 1 THEN weapon = 2
IF GetKey(4) THEN IF weapon.enabled(3) = 1 THEN weapon = 3
IF GetKey(5) THEN IF weapon.enabled(4) = 1 THEN weapon = 4
IF GetKey(6) THEN IF weapon.enabled(5) = 1 THEN weapon = 5
IF GetKey(7) THEN IF weapon.enabled(6) = 1 THEN weapon = 6
IF GetKey(8) THEN IF weapon.enabled(7) = 1 THEN weapon = 7
IF GetKey(9) THEN IF weapon.enabled(8) = 1 THEN weapon = 8
IF GetKey(10) THEN IF weapon.enabled(9) = 1 THEN weapon = 9
IF GetKey(11) THEN IF weapon.enabled(10) = 1 THEN weapon = 10

IF GetKey(47) THEN                              '"Powerline"
IF Timer3! + 1.1 < TIMER AND pl.ammo > 0 THEN
 SELECT CASE sounddevice%
 CASE 1: SOUND 3000, .1
 CASE 2:
 CASE 3: play8bitwave ("sfx\Gamm.wav")
 END SELECT
 Timer3! = TIMER
 pl.ammo = pl.ammo - 1
 FOR shotsonce = 0 TO 260 STEP 7
  FOR nr = 1 TO maxshots
   IF shot(nr) = 0 THEN
    shoty(nr) = shipy + 37: shotx(nr) = shotsonce: shot(nr) = 8
    shotxstep(nr) = 0
    shotystep(nr) = 8
    shotpower(nr) = 4
    EXIT FOR
   END IF
  NEXT
 NEXT
END IF
END IF

IF GetKey(46) THEN                          'Lenkrakete
IF Timer3! + 1.6 < TIMER AND mi.ammo > 0 THEN
 SELECT CASE sounddevice%
 CASE 1: SOUND 1000, .1
 CASE 2: playFX 11
 CASE 3: play8bitwave ("sfx\mega.wav")
 END SELECT
Timer3! = TIMER
mi.ammo = mi.ammo - 1
FOR nr = 1 TO maxshots
IF shot(nr) = 0 THEN
shoty(nr) = C - 5: shotx(nr) = a: shot(nr) = 9
shotxstep(nr) = 0
shotystep(nr) = 2
shotpower(nr) = 30
EXIT FOR
END IF
NEXT
END IF
END IF

IF GetKey(48) THEN
beampower = beampower + .2
IF beampower > 30 THEN
beampower = 30
beamtime% = beamtime% + 10
IF beamtime% > 1000 THEN lp = lp - lpmax \ 15
END IF
ELSE
IF beampower > 0 THEN
IF beampower > 1 THEN
SELECT CASE sounddevice%
 CASE 1: SOUND 1000, .1
 CASE 2:
 CASE 3: play8bitwave ("sfx\las.wav")
END SELECT
END IF
IF beampower > 12.5 THEN
SELECT CASE sounddevice%
 CASE 1: SOUND 1000, .1
 CASE 2:
 CASE 3: play8bitwave ("sfx\tief.wav")
END SELECT
END IF
IF beampower >= 30 THEN
SELECT CASE sounddevice%
 CASE 1: SOUND 1000, .1
 CASE 2:
 CASE 3: play8bitwave ("sfx\zap.wav")
END SELECT
END IF
FOR shotsonce = 1 TO INT(beampower)
FOR nr = 1 TO maxshots
IF shot(nr) = 0 THEN
shoty(nr) = C + shotsonce: shotx(nr) = a: shot(nr) = 14
shotxstep(nr) = 0
shotystep(nr) = 6
shotpower(nr) = IF2(beampower >= 30, 1.5, 1)
EXIT FOR
END IF
NEXT
NEXT
beampower = 0: beamtime% = 0:
END IF
END IF


IF GetKey(57) THEN
IF ammo(weapon) <= 0 THEN weapon = 1
IF ammo(weapon) > 0 OR weapon = 1 THEN
IF Timer4! < TIMER AND specshot(1) = 1 THEN
 Timer4! = TIMER + .4
 FOR nr = 1 TO maxshots
   IF shot(nr) = 0 THEN                                'Dreieck
    shoty(nr) = C - 5: shotx(nr) = a: shot(nr) = 16
    shotxstep(nr) = RAND(-25, 25) / 100
    shotystep(nr) = 4.5
    shotpower(nr) = 3
    EXIT FOR
   END IF
  NEXT
END IF
SELECT CASE weapon
CASE 1                                'Basiswaffe
IF Timer2! + delay1 < TIMER THEN
 SELECT CASE sounddevice%
 CASE 1: SOUND 500, .1
 CASE 2: 'playFX
 CASE 3: play8bitwave ("sfx\laser.wav")
 END SELECT
 shtx(1) = 0:  shty(1) = 5: shtxv(1) = 0: shtyv(1) = 6     'upmiddle
 shtx(2) = -5: shty(2) = 5: shtxv(2) = 0: shtyv(2) = 6     'upleft
 shtx(3) = 5:  shty(3) = 5: shtxv(3) = 0: shtyv(3) = 6     'upright
 shtx(4) = -6:  shty(4) = -5: shtxv(4) = 6: shtyv(4) = 0   'right
 shtx(5) = 6: shty(5) = -5: shtxv(5) = -6: shtyv(5) = 0    'left
 shtx(6) = 0:  shty(6) = -15: shtxv(6) = 0: shtyv(6) = -6  'down
 shtx(7) = -6:   shty(7) = 2: shtxv(7) = 3: shtyv(7) = 5
 shtx(8) = 6:  shty(8) = 2: shtxv(8) = -3: shtyv(8) = 5
 shtx(9) = -6:  shty(9) = -8: shtxv(9) = 3: shtyv(9) = -5
 shtx(10) = 6:  shty(10) = -8: shtxv(10) = -3: shtyv(10) = -5
 Timer2! = TIMER
 FOR loops% = sloop% TO eloop%
  FOR nr = 1 TO maxshots
   IF shot(nr) = 0 THEN
    shoty(nr) = C - shty(loops%)
    shotx(nr) = a + shtx(loops%)
    shot(nr) = weapon
    shotxstep(nr) = shtxv(loops%)
    shotystep(nr) = shtyv(loops%)
    shotpower(nr) = 1
    EXIT FOR
   END IF
  NEXT
 NEXT
END IF

CASE 2                              'MG
SELECT CASE sounddevice%
 CASE 1: SOUND 50, .1
 CASE 2: playFX 3
 CASE 3: play8bitwave ("sfx\plas.wav")
END SELECT
ammo(2) = ammo(2) - .1
 FOR nr = 1 TO maxshots
  IF shot(nr) = 0 THEN
   shoty(nr) = C - 5: shotx(nr) = a: shot(nr) = weapon
   shotxstep(nr) = RAND(-100, 100) / 100
   shotystep(nr) = 7
   shotpower(nr) = .5
   EXIT FOR
  END IF
 NEXT


CASE 3                                '"Schrotflinte"
IF Timer2! + .4 < TIMER THEN
 SELECT CASE sounddevice%
 CASE 1: SOUND 75, .1
 CASE 2: 'playFX
 CASE 3: play8bitwave ("sfx\mach.wav")
 END SELECT
 shtx(1) = 0:  shty(1) = 6
 shtx(2) = 2:  shty(2) = 6
 shtx(3) = -2: shty(3) = 6
 shtx(4) = 4: shty(4) = 5
 shtx(5) = -4: shty(5) = 5
 shtx(6) = 6: shty(6) = 4
 shtx(7) = -6: shty(7) = 4
 Timer2! = TIMER
 ammo(3) = ammo(3) - 1
 FOR shotsonce = 1 TO 7
  FOR nr = 1 TO maxshots
   IF shot(nr) = 0 THEN
    shoty(nr) = C - 5: shotx(nr) = a: shot(nr) = weapon
    shotxstep(nr) = shtx(shotsonce)
    shotystep(nr) = shty(shotsonce)
    shotpower(nr) = 2.5
    EXIT FOR
   END IF
  NEXT
 NEXT
END IF

CASE 4                                '"Lasers"
IF Timer2! + .25 < TIMER THEN
 
 SELECT CASE sounddevice%
 CASE 1: SOUND 100, .1
 CASE 2: 'playFX
 CASE 3: play8bitwave ("sfx\mult.wav")
 END SELECT
 shtx(1) = -4:   shtxv(1) = 0
 shtx(2) = 4:    shtxv(2) = 0
 shtx(3) = -6:   shtxv(3) = .5
 shtx(4) = 6:    shtxv(4) = -.5
 Timer2! = TIMER
 ammo(4) = ammo(4) - 1
 FOR shotsonce = 1 TO 4
  FOR nr = 1 TO maxshots
   IF shot(nr) = 0 THEN
    shoty(nr) = C - 5: shotx(nr) = a + shtx(shotsonce): shot(nr) = weapon
    shotxstep(nr) = shtxv(shotsonce)
    shotystep(nr) = 5
    shotpower(nr) = 3
    EXIT FOR
   END IF
  NEXT
 NEXT
END IF

CASE 5                              'Feuerball
IF Timer2! + .5 < TIMER THEN
 SELECT CASE sounddevice%
 CASE 1: SOUND 500, .1
 CASE 2: playFX 9
 CASE 3: play8bitwave ("sfx\firb.wav")
 END SELECT
Timer2! = TIMER
ammo(5) = ammo(5) - 1
FOR nr = 1 TO maxshots
IF shot(nr) = 0 THEN
shoty(nr) = C - 5: shotx(nr) = a: shot(nr) = weapon
shotxstep(nr) = 0
shotystep(nr) = 5
shotpower(nr) = 9
EXIT FOR
END IF
NEXT
END IF

CASE 6                              'Elektro-schocker
IF Timer2! + .5 < TIMER THEN
 SELECT CASE sounddevice%
 CASE 1: SOUND 150, .1
 CASE 2: playFX 4
 CASE 3: play8bitwave ("sfx\elecinv.wav")
 END SELECT
Timer2! = TIMER
ammo(6) = ammo(6) - 1
FOR nr = 1 TO maxshots
IF shot(nr) = 0 THEN
shoty(nr) = C - 5: shotx(nr) = a: shot(nr) = weapon
shotxstep(nr) = 0
shotystep(nr) = 8
shotpower(nr) = 15
EXIT FOR
END IF
NEXT
END IF

CASE 7                              'Lenkbare Bombe
IF Timer2! + .5 < TIMER THEN
 SELECT CASE sounddevice%
 CASE 1: SOUND 1000, .1
 CASE 2: 'playFX
 CASE 3: play8bitwave ("sfx\land.wav")
 END SELECT
Timer2! = TIMER
ammo(7) = ammo(7) - 1
FOR nr = 1 TO maxshots
IF shot(nr) = 0 THEN
shoty(nr) = C - 5: shotx(nr) = a: shot(nr) = 7
shotxstep(nr) = 0
shotystep(nr) = 1.5
shotpower(nr) = 19
EXIT FOR
END IF
NEXT
END IF

CASE 8                              'Miniraketen
IF Timer2! + .1 < TIMER THEN
 SELECT CASE sounddevice%
 CASE 1: SOUND 700, .1
 CASE 2: 'playFX
 CASE 3: play8bitwave ("sfx\xwin.wav")
 END SELECT
Timer2! = TIMER
ammo(8) = ammo(8) - .25
FOR nr = 1 TO maxshots
IF shot(nr) = 0 THEN
shoty(nr) = C - 5: shotx(nr) = a: shot(nr) = 10
shotxstep(nr) = 0
shotystep(nr) = 1.75
shotpower(nr) = 5
EXIT FOR
END IF
NEXT
END IF

CASE 9                                '"Schrotflinte mega"
IF Timer2! + .65 < TIMER THEN
 SELECT CASE sounddevice%
 CASE 1: SOUND 75, .1
 CASE 2: 'playFX
 CASE 3: play8bitwave ("sfx\shot.wav")
 END SELECT
 Timer2! = TIMER
 ammo(9) = ammo(9) - 1
 FOR shotsonce = 1 TO 25
  FOR nr = 1 TO maxshots
   IF shot(nr) = 0 THEN
    shoty(nr) = C - 5: shotx(nr) = a: shot(nr) = 11
    shotxstep(nr) = RAND(-200, 200) / 100
    shotystep(nr) = RAND(500, 700) / 100
    shotpower(nr) = 2.5
    EXIT FOR
   END IF
  NEXT
 NEXT
END IF


CASE 10                              'EMP - Rakete
IF Timer2! + 1 < TIMER THEN
 SELECT CASE sounddevice%
 CASE 1: SOUND 700, .1
 CASE 2: 'playFX
 CASE 3: play8bitwave ("sfx\spec.wav")
 END SELECT
Timer2! = TIMER
ammo(10) = ammo(10) - 1
FOR nr = 1 TO maxshots
IF shot(nr) = 0 THEN
shoty(nr) = C - 5: shotx(nr) = a: shot(nr) = 12 '*** Partikel:13
shotxstep(nr) = 0
shotystep(nr) = 1
shotpower(nr) = 75
EXIT FOR
END IF
NEXT
END IF


END SELECT
END IF
END IF


IF randompos = 1 THEN
setenemy% = ROTATEVAR(setenemy%, 0, 20)
FOR enemy.nr = 1 TO maxenemies
IF enemy(enemy.nr) = 0 AND setenemy% = 20 THEN
enemy(enemy.nr) = RAND(1, 3)
enemyheight(enemy.nr) = getheight(enemy(enemy.nr))
enemywidth(enemy.nr) = getwidth(enemy(enemy.nr))
enemyx(enemy.nr) = RAND(0, 260 - enemywidth(enemy.nr))
enemyy(enemy.nr) = -enemyheight(enemy.nr)
enemylp(enemy.nr) = getenemylp(enemy(enemy.nr))
enemyspeed(enemy.nr) = 1
enemyxstep(enemy.nr) = RAND(-3, 3) / 10
EXIT FOR
END IF
NEXT
END IF

IF ship.step < 1 THEN ship.step = 1
IF ship.step > 7 THEN ship.step = 7

particlestotal = 0
FOR part.nr = 1 TO maxparticles
IF particle(part.nr) <> 0 THEN
   particlestotal = particlestotal + 1
   SELECT CASE particle(part.nr)
   CASE 1: particlestrength(part.nr) = .5
   CASE 3: particleweight(part.nr) = .2: particlestrength(part.nr) = 1
   CASE 4: particlec(part.nr) = particlec(part.nr) - 5
	   IF particlec(part.nr) <= 50 THEN particle(part.nr) = 0
   CASE 5: particlec(part.nr) = particlec(part.nr) - 2
	   IF particlec(part.nr) <= 50 THEN particle(part.nr) = 0
   CASE ELSE: particlestrength(part.nr) = 0
   END SELECT
particlex(part.nr) = particlex(part.nr) + particlexv(part.nr)
particley(part.nr) = particley(part.nr) + particleyv(part.nr)
particleyv(part.nr) = particleyv(part.nr) + particleweight(part.nr)
IF particley(part.nr) >= 200 THEN particle(part.nr) = 0
IF particlex(part.nr) >= 260 OR particlex(part.nr) <= 0 THEN particle(part.nr) = 0
IF particlec(part.nr) <= 0 THEN particle(part.nr) = 0
IF particlex(part.nr) >= shipx AND particley(part.nr) >= shipy AND particlestrength(part.nr) > 0 THEN
IF particlex(part.nr) <= shipx2 AND particley(part.nr) <= shipy2 THEN
	FOR r = 0 TO 2
	IF Col = 4 THEN Col = 14 ELSE Col = 4
	CIRCLE (particlex(part.nr), particley(part.nr)), r, Col
	NEXT
lp = lp - particlestrength(part.nr)
particle(part.nr) = 0
END IF
END IF
END IF
NEXT

powtotal = 0
FOR pow.nr = 1 TO maxpow
IF pow(pow.nr) <> 0 THEN
   powtotal = powtotal + 1
powy(pow.nr) = powy(pow.nr) + 1
IF powy(pow.nr) >= 200 THEN pow(pow.nr) = 0

IF powx(pow.nr) + 5 > shipx AND powy(pow.nr) + 5 > shipy THEN
IF powx(pow.nr) - 5 < shipx2 AND powy(pow.nr) - 5 < shipy2 THEN
SELECT CASE pow(pow.nr)
CASE 1: lp = lp + 50
CASE 2: lp = lp + 200
CASE 3: lp = lpmax
CASE 4: pl.ammo = pl.ammo + 3
CASE 5: mi.ammo = mi.ammo + 5
CASE 6: ammo(2) = ammo(2) + 20    'MG
CASE 7: ammo(3) = ammo(3) + 40    'SG
CASE 8: ammo(4) = ammo(4) + 45    'LA
CASE 9: ammo(5) = ammo(5) + 30    'FB
CASE 10: ammo(6) = ammo(6) + 25   'EL
CASE 11: ammo(7) = ammo(7) + 12   'LB
CASE 12: ammo(8) = ammo(8) + 12   'MR
CASE 13: ammo(9) = ammo(9) + 12   'SGM
CASE 14: ammo(10) = ammo(10) + 3  'EMP
CASE 15: money! = money! + 50
END SELECT
pow(pow.nr) = 0
END IF
END IF
END IF
NEXT

enemiestotal = 0
FOR enemy.nr = 1 TO maxenemies
IF enemy(enemy.nr) <> 0 THEN enemiestotal = enemiestotal + 1
NEXT

shotstotal = 0
FOR nr = 1 TO maxshots
IF shot(nr) <> 0 THEN
shotstotal = shotstotal + 1
shoty(nr) = shoty(nr) - shotystep(nr)
shotx(nr) = shotx(nr) - shotxstep(nr)

IF shoty(nr) <= 0 OR shoty(nr) >= 200 THEN shot(nr) = 0
IF shotx(nr) <= -20 OR shotx(nr) >= 280 THEN shot(nr) = 0
IF shot(nr) = 15 THEN shotystep(nr) = shotystep(nr) - .3

FOR enemy.nr = 1 TO maxenemies
IF enemy(enemy.nr) <> 0 THEN
IF shotx(nr) + 3 > enemyx(enemy.nr) AND (shoty(nr) > enemyy(enemy.nr)) THEN
IF shotx(nr) - 3 < enemyx(enemy.nr) + enemywidth(enemy.nr) AND (shoty(nr) < enemyy(enemy.nr) + enemyheight(enemy.nr)) THEN

FOR ex.nr = 1 TO maxex
IF ex(ex.nr) = 0 THEN
exx(ex.nr) = shotx(nr)
exy(ex.nr) = shoty(nr)
exsizemax(ex.nr) = 12
exsizemin(ex.nr) = 1
ex(ex.nr) = exsizemin(ex.nr)
excol(ex.nr) = 113
EXIT FOR
END IF
NEXT
IF enemy(enemy.nr) = 100 AND enemylp(enemy.nr) > 10000 THEN
SELECT CASE shot(nr)
CASE 1, 4: IF sounddevice% = 3 THEN play8bitwave ("sfx\ricochet.wav")
END SELECT
END IF
enemylp(enemy.nr) = enemylp(enemy.nr) - shotpower(nr)
IF shot(nr) = 15 AND enemyspeed(enemy.nr) > 1 THEN enemyspeed(enemy.nr) = 1
IF enemylp(enemy.nr) <= 0 THEN
IF LEVTYPE% <> 2 THEN
IF RAND(0, 0) = 0 THEN
   FOR pow.nr = 1 TO maxpow
   IF pow(pow.nr) = 0 THEN
   powy(pow.nr) = enemyy(enemy.nr) + enemyheight(enemy.nr) / 2
   powx(pow.nr) = enemyx(enemy.nr) + enemywidth(enemy.nr) / 2
   pow(pow.nr) = RAND(0, 15)
   SELECT CASE pow(pow.nr)
   CASE 1: IF RAND(0, 1) = 0 THEN pow(pow.nr) = 0
   CASE 2: IF RAND(0, 2) < 2 THEN pow(pow.nr) = 0
   CASE 3: IF RAND(0, 3) < 3 THEN pow(pow.nr) = 0
   CASE 4, 5: IF RAND(0, 2) < 2 THEN pow(pow.nr) = 0
   CASE 6 TO 11: IF RAND(0, 0) < 4 AND weapon.enabled(pow(pow.nr) - 2) = 0 THEN pow(pow.nr) = 0
   END SELECT
   EXIT FOR
   END IF
   NEXT
END IF
END IF
ENEMYEXPLODE enemyx(enemy.nr), enemyy(enemy.nr), enemy(enemy.nr), enemyheight(enemy.nr), enemywidth(enemy.nr)
IF NOT enemy(enemy.nr) = 100 THEN money! = money! + getenemylp(enemy(enemy.nr))
IF enemy(enemy.nr) >= 80 AND enemy(enemy.nr) <= 99 THEN skipreadlevel2% = 0
enemy(enemy.nr) = 0
 FOR particle.count = 0 TO enemyheight(enemy.nr) * enemywidth(enemy.nr) \ 20 + 1
 FOR part.nr = 1 TO maxparticles
 IF particle(part.nr) = 0 THEN
 particle(part.nr) = RAND(1, 3)
 particlex(part.nr) = RAND(enemyx(enemy.nr), enemyx(enemy.nr) + enemywidth(enemy.nr))
 particley(part.nr) = RAND(enemyy(enemy.nr), enemyy(enemy.nr) + enemyheight(enemy.nr))
 particlexv(part.nr) = RAND(-3, 3)
 particleyv(part.nr) = RAND(-3, 3)
 particleweight(part.nr) = .1
 particlec(part.nr) = 8
 EXIT FOR
 END IF
 NEXT
 NEXT

IF shot(nr) = 12 THEN
 FOR shots13% = 1 TO 20
  FOR nr2 = 1 TO maxshots
   IF shot(nr2) = 0 THEN
    shotx(nr2) = enemyx(enemy.nr) + enemywidth(enemy.nr) / 2
    shoty(nr2) = enemyy(enemy.nr) + enemyheight(enemy.nr) / 2
    shotxstep(nr2) = RAND(-1200, 0) / 100 + 6
    shotystep(nr2) = RAND(-1200, 0) / 100 + 6
    shotpower(nr2) = 7.5
    shot(nr2) = 13
    EXIT FOR
   END IF
  NEXT
 NEXT
END IF

IF shot(nr) = 5 THEN
 FOR shots15% = 1 TO 5
  FOR nr2 = 1 TO maxshots
   IF shot(nr2) = 0 THEN
    shotx(nr2) = enemyx(enemy.nr) + enemywidth(enemy.nr) / 2
    shoty(nr2) = enemyy(enemy.nr) + enemyheight(enemy.nr) / 2
    shotxstep(nr2) = RAND(-1000, 0) / 100 + 5
    shotystep(nr2) = RAND(-1000, 0) / 100 + 5
    shotpower(nr2) = 3
    shot(nr2) = 15
    EXIT FOR
   END IF
  NEXT
 NEXT
END IF


END IF

IF shot(nr) = 6 THEN
tempa(1) = 1: tempa(2) = -1
FOR shots17% = 1 TO 2
 FOR nr2 = 1 TO maxshots
   IF shot(nr2) = 0 THEN
    shotx(nr2) = (enemyx(enemy.nr) + enemywidth(enemy.nr) / 2) - ((getwidth(enemy(enemy.nr)) / 2 + 3) * tempa(shots17%))
    shoty(nr2) = enemyy(enemy.nr) + enemyheight(enemy.nr) / 2
    shotxstep(nr2) = tempa(shots17%) * 7
    shotystep(nr2) = 0
    shotpower(nr2) = 10
    shot(nr2) = 17
    EXIT FOR
   END IF
  NEXT
 NEXT
END IF

shot(nr) = 0
END IF
END IF
END IF
NEXT enemy.nr
END IF
NEXT nr


flip.page
'/////////////////////////////////// STARFIELD /////////////////////////
starstotal = 0
FOR nr = 1 TO maxstars
IF 1 + INT(RND * 100) < 2 AND star1(nr) = 0 THEN
starx1(nr) = RAND(0, 260)
stary1(nr) = -1
star1(nr) = RAND(1, 4)
END IF
NEXT
FOR nr = 1 TO maxstars
IF star1(nr) <> 0 THEN
starstotal = starstotal + 1
IF star1(nr) = 1 THEN
FOR star.speed = 1 TO game.speed
PSET (starx1(nr), stary1(nr) + star.speed), 15
NEXT
stary1(nr) = stary1(nr) + game.speed
END IF

IF star1(nr) = 2 THEN
FOR star.speed = 1 TO game.speed
PSET (starx1(nr), stary1(nr) + star.speed \ 2), 15
NEXT
stary1(nr) = stary1(nr) + game.speed / 2
END IF

IF star1(nr) = 3 THEN
PSET (starx1(nr), stary1(nr) + star.speed), 7
stary1(nr) = stary1(nr) + game.speed / 3
END IF

IF star1(nr) = 4 THEN
PSET (starx1(nr), stary1(nr) + star.speed), 7
stary1(nr) = stary1(nr) + game.speed / 4
END IF

END IF
IF stary1(nr) >= 200 THEN star1(nr) = 0
NEXT
'//////////////////////////////// END STARFIELD /////////////////////////

FOR ex.nr = 1 TO maxex
IF ex(ex.nr) > 0 THEN
Future.FILLCIRCLE exx(ex.nr), exy(ex.nr), ex(ex.nr), excol(ex.nr)
ex(ex.nr) = ex(ex.nr) + 3
excol(ex.nr) = excol(ex.nr) - 189 / (exsizemax(ex.nr) - exsizemin(ex.nr))
IF ex(ex.nr) >= exsizemax(ex.nr) THEN ex(ex.nr) = 0
END IF
NEXT

FOR px% = 1 TO UBOUND(ship, 1)
FOR py% = 1 TO UBOUND(ship, 2)
IF NOT ship(px%, py%) = 0 THEN PSET (px% + shipx - 1, py% + shipy - 1), ship(px%, py%)
NEXT: NEXT
 FOR particles = 1 TO 2
 FOR part.nr = 1 TO maxparticles
 IF particle(part.nr) = 0 THEN
 particle(part.nr) = 5
 particlex(part.nr) = (shipx + ABS(shipx2 - shipx) / 2) + RAND(-2, 2)
 particley(part.nr) = shipy2
 particlexv(part.nr) = 0
 particleyv(part.nr) = RAND(1, game.speed)
 particleweight(part.nr) = 0
 particlec(part.nr) = 113
 particlestrength(part.nr) = 0
 EXIT FOR
 END IF
 NEXT
 NEXT


FOR nr = 1 TO maxshots
IF shot(nr) <> 0 THEN
SELECT CASE shot(nr)
CASE 1: CIRCLE (shotx(nr), shoty(nr)), 1, 115: PSET (shotx(nr), shoty(nr)), 116
CASE 2, 3, 11: CIRCLE (shotx(nr), shoty(nr)), 1, 8: PSET (shotx(nr), shoty(nr)), 7
CASE 4: LINE (shotx(nr), shoty(nr))-(shotx(nr), shoty(nr) + 3), 4
CASE 5: Future.FILLCIRCLE shotx(nr), shoty(nr), 2, RAND(80, 100)
 FOR particles = 1 TO 2
 FOR part.nr = 1 TO maxparticles
 IF particle(part.nr) = 0 THEN
 particle(part.nr) = 4
 particlex(part.nr) = RAND(shotx(nr) - 3, shotx(nr) + 3)
 particley(part.nr) = RAND(shoty(nr) - 3, shoty(nr) + 3)
 particlexv(part.nr) = 0
 particleyv(part.nr) = 0
 particleweight(part.nr) = 0
 particlec(part.nr) = 113
 particlestrength(part.nr) = 0
 EXIT FOR
 END IF
 NEXT
 NEXT
CASE 6: FOR temp = 1 TO 35
	PSET (RAND(shotx(nr) - 2, shotx(nr) + 2), RAND(shoty(nr) - 2, shoty(nr) + 23)), 9
	NEXT
CASE 7: shotxstep(nr) = SIN(shoty(nr) / 4) * 5
	CIRCLE (shotx(nr), shoty(nr)), 3, 7
	CIRCLE (shotx(nr), shoty(nr)), 2, 4
	CIRCLE (shotx(nr), shoty(nr)), 1, 4
	PSET (shotx(nr), shoty(nr)), 4
CASE 8: LINE (shotx(nr) - 3, shoty(nr))-(shotx(nr) + 3, shoty(nr)), 11
CASE 9: CIRCLE (shotx(nr), shoty(nr)), 1, 14
	 IF aim(nr) = 0 THEN
	 FOR enemytogo = 1 TO maxenemies
	 IF enemy(enemytogo) <> 0 AND enemy(enemytogo) < 80 AND aim(nr) = 0 THEN
	 aim(nr) = enemytogo: EXIT FOR
	 END IF
	 NEXT
	 END IF
	 IF aim(nr) <> 0 THEN
	 IF shotx(nr) < enemyx(aim(nr)) + enemywidth(aim(nr)) / 2 THEN shotx(nr) = shotx(nr) + 5
	 IF shotx(nr) > enemyx(aim(nr)) + enemywidth(aim(nr)) / 2 THEN shotx(nr) = shotx(nr) - 5
	 IF shoty(nr) < enemyy(aim(nr)) + enemyheight(aim(nr)) / 2 THEN shoty(nr) = shoty(nr) + 7
	 IF shoty(nr) > enemyy(aim(nr)) + enemyheight(aim(nr)) / 2 THEN shoty(nr) = shoty(nr) - 3
	 CIRCLE (enemyx(aim(nr)) + enemywidth(aim(nr)) / 2, enemyy(aim(nr)) + enemyheight(aim(nr)) / 2), enemyheight(aim(nr)) * .75, 4
	 IF enemy(aim(nr)) = 0 THEN aim(nr) = 0
	 END IF
CASE 10: FOR px% = 1 TO 3
	 FOR py% = 1 TO 6
	 IF NOT minimiss(px%, py%) = 0 THEN PSET (px% + shotx(nr) - 2, py% + shoty(nr) - 1), minimiss(px%, py%)
	 NEXT: NEXT
	 FOR part.nr = 1 TO maxparticles
	 IF particle(part.nr) = 0 THEN
	 particle(part.nr) = 4
	 particlex(part.nr) = RAND(shotx(nr) - 1, shotx(nr) + 1)
	 particley(part.nr) = shoty(nr) + 6
	 particlexv(part.nr) = 0
	 particleyv(part.nr) = 0
	 particleweight(part.nr) = 0
	 particlec(part.nr) = 113
	 particlestrength(part.nr) = 0
	 EXIT FOR
	 END IF
	 NEXT
	 shotystep(nr) = shotystep(nr) * 1.05
CASE 12: FOR px% = 1 TO 5
	 FOR py% = 1 TO 10
	 IF NOT empr(px%, py%) = 0 THEN PSET (px% + shotx(nr) - 3, py% + shoty(nr) - 1), empr(px%, py%)
	 NEXT: NEXT
	 FOR part.nr = 1 TO maxparticles
	 IF particle(part.nr) = 0 THEN
	 particle(part.nr) = 4
	 particlex(part.nr) = RAND(shotx(nr) - 1, shotx(nr) + 1)
	 particley(part.nr) = shoty(nr) + 6
	 particlexv(part.nr) = 0
	 particleyv(part.nr) = 0
	 particleweight(part.nr) = 0
	 particlec(part.nr) = 113
	 particlestrength(part.nr) = 0
	 EXIT FOR
	 END IF
	 NEXT
	 shotystep(nr) = shotystep(nr) * 1.05
CASE 13: PSET (shotx(nr), shoty(nr)), 9
	 CIRCLE (shotx(nr), shoty(nr)), 1, 1
CASE 14: PSET (shotx(nr) - 1, shoty(nr)), 115
	 PSET (shotx(nr), shoty(nr)), 116
	 PSET (shotx(nr) + 1, shoty(nr)), 115
CASE 15: Future.FILLCIRCLE shotx(nr), shoty(nr), 1, 102
 FOR part.nr = 1 TO maxparticles
 IF particle(part.nr) = 0 THEN
 particle(part.nr) = 4
 particlex(part.nr) = RAND(shotx(nr) - 3, shotx(nr) + 3)
 particley(part.nr) = RAND(shoty(nr) - 3, shoty(nr) + 3)
 particlexv(part.nr) = 0
 particleyv(part.nr) = 0
 particleweight(part.nr) = 0
 particlec(part.nr) = 113
 particlestrength(part.nr) = 0
 EXIT FOR
 END IF
 NEXT
CASE 16: sixECK shotx(nr), shoty(nr), 4, steadychange% / 4, 3, 8
CASE 17: FOR temp = 1 TO 35
	 PSET (RAND(shotx(nr) - 2, shotx(nr) + 23), RAND(shoty(nr) - 2, shoty(nr) + 2)), 9
	 NEXT

END SELECT
END IF
NEXT

FOR pow.nr = 1 TO maxpow
IF pow(pow.nr) <> 0 THEN
FOR px% = 1 TO UBOUND(powp, 2)
FOR py% = 1 TO UBOUND(powp, 3)
IF NOT powp(pow(pow.nr), px%, py%) = 0 THEN
PSET (px% + powx(pow.nr) - 1, py% + powy(pow.nr) - 1), powp(pow(pow.nr), px%, py%)
END IF
NEXT: NEXT
END IF
NEXT

FOR enemy.nr = 1 TO maxenemies
IF enemy(enemy.nr) <> 0 THEN
SELECT CASE enemy(enemy.nr)

CASE 80
IF enemyy(enemy.nr) < 7 THEN 'getheight(enemy(enemy.nr)) THEN
enemyy(enemy.nr) = enemyy(enemy.nr) + (enemyspeed(enemy.nr) * game.speed)
ELSE
IF enemyxstep(enemy.nr) = 0 THEN enemyxstep(enemy.nr) = 3
IF enemyx(enemy.nr) >= 260 - getwidth(enemy(enemy.nr)) THEN enemyxstep(enemy.nr) = -enemyxstep(enemy.nr)
IF enemyx(enemy.nr) <= 0 THEN enemyxstep(enemy.nr) = -enemyxstep(enemy.nr)
enemyx(enemy.nr) = enemyx(enemy.nr) + enemyxstep(enemy.nr)
END IF

CASE ELSE
enemyy(enemy.nr) = enemyy(enemy.nr) + (enemyspeed(enemy.nr) * game.speed)
enemyx(enemy.nr) = enemyx(enemy.nr) + enemyxstep(enemy.nr)

END SELECT
DRAWENEMY enemyx(enemy.nr), enemyy(enemy.nr), enemy(enemy.nr), enemyheight(enemy.nr), enemywidth(enemy.nr)
IF enemyy(enemy.nr) >= 200 THEN enemy(enemy.nr) = 0
END IF
IF enemy(enemy.nr) <> 0 THEN
ship.enemy.collision shipx, shipy, shipx2, shipy2, enemyx(enemy.nr), enemyy(enemy.nr), enemy(enemy.nr), enemyheight(enemy.nr), enemywidth(enemy.nr)
IF ship.enemy.collisio = 1 THEN
ship.enemy.collisio = 0
IF enemy(enemy.nr) >= 80 THEN
 lp = 0
ELSE
 ENEMYEXPLODE enemyx(enemy.nr), enemyy(enemy.nr), enemy(enemy.nr), enemyheight(enemy.nr), enemywidth(enemy.nr)
 lp = lp - 50
money! = money! + getenemylp(enemy(enemy.nr))
 FOR particle.count = 0 TO enemyheight(enemy.nr) * enemywidth(enemy.nr) \ 20 + 6
 FOR part.nr = 1 TO maxparticles
 IF particle(part.nr) = 0 THEN
 particle(part.nr) = RAND(1, 3)
 particlex(part.nr) = enemyx(enemy.nr)
 particley(part.nr) = enemyy(enemy.nr)
 particlec(part.nr) = 8
 particlexv(part.nr) = RAND(-3, 3)
 particleyv(part.nr) = RAND(-3, 3)
 particleweight(part.nr) = .1
 EXIT FOR
 END IF
 NEXT
 NEXT
END IF
enemy(enemy.nr) = 0
END IF
END IF
NEXT

FOR enemy.nr = 1 TO maxenemies
 SELECT CASE enemy(enemy.nr)
  CASE 80
   IF eshottimer!(enemy.nr) + eshotdelay(enemy.nr) < TIMER THEN
    eshottimer!(enemy.nr) = TIMER
    SELECT CASE sounddevice%
     CASE 1: SOUND 50, .1
     CASE 2: 'playFX
     CASE 3: play8bitwave ("sfx\plas.wav")
    END SELECT
    FOR eshots = 1 TO 2
    v(1) = -20
    v(2) = 20
     FOR eshot.nr = 1 TO 20
      IF eshot(enemy.nr, eshot.nr) = 0 THEN
       eshoty(enemy.nr, eshot.nr) = enemyy(enemy.nr) + enemyheight(enemy.nr)
       eshotx(enemy.nr, eshot.nr) = enemyx(enemy.nr) + enemywidth(enemy.nr) / 2 + v(eshots)
       eshot(enemy.nr, eshot.nr) = 2
       eshotxv(enemy.nr, eshot.nr) = 0
       eshotyv(enemy.nr, eshot.nr) = 6
       eshotpower(enemy.nr, eshot.nr) = 3
       EXIT FOR
      END IF
     NEXT
    NEXT
   END IF
  CASE 5
   IF eshottimer!(enemy.nr) + eshotdelay(enemy.nr) < TIMER THEN
    eshottimer!(enemy.nr) = TIMER
    SELECT CASE sounddevice%
     CASE 1: SOUND 50, .1
     CASE 2: 'playFX
     CASE 3: play8bitwave ("sfx\plas.wav")
    END SELECT
     FOR eshot.nr = 1 TO 20
      IF eshot(enemy.nr, eshot.nr) = 0 THEN
       eshoty(enemy.nr, eshot.nr) = enemyy(enemy.nr) + enemyheight(enemy.nr)
       eshotx(enemy.nr, eshot.nr) = enemyx(enemy.nr) + enemywidth(enemy.nr) / 2 + v(eshots)
       eshot(enemy.nr, eshot.nr) = 2
       eshotxv(enemy.nr, eshot.nr) = 0
       eshotyv(enemy.nr, eshot.nr) = 6
       eshotpower(enemy.nr, eshot.nr) = 3
       EXIT FOR
      END IF
     NEXT
   END IF
   


 'CASE...
 END SELECT
NEXT

FOR enemy.nr = 1 TO maxenemies
FOR eshot.nr = 1 TO 20
IF eshot(enemy.nr, eshot.nr) <> 0 THEN
shotstotal = shotstotal + 1
eshotx(enemy.nr, eshot.nr) = eshotx(enemy.nr, eshot.nr) + eshotxv(enemy.nr, eshot.nr)
eshoty(enemy.nr, eshot.nr) = eshoty(enemy.nr, eshot.nr) + eshotyv(enemy.nr, eshot.nr)
IF eshotx(enemy.nr, eshot.nr) < 0 OR eshotx(enemy.nr, eshot.nr) > 260 THEN eshot(enemy.nr, eshot.nr) = 0
IF eshoty(enemy.nr, eshot.nr) < 0 OR eshoty(enemy.nr, eshot.nr) > 200 THEN eshot(enemy.nr, eshot.nr) = 0

IF eshotx(enemy.nr, eshot.nr) >= shipx AND eshoty(enemy.nr, eshot.nr) >= shipy THEN
IF eshotx(enemy.nr, eshot.nr) <= shipx2 AND eshoty(enemy.nr, eshot.nr) <= shipy2 THEN
	FOR r = 0 TO 9
	IF Col = 4 THEN Col = 14 ELSE Col = 4
	CIRCLE (eshotx(enemy.nr, eshot.nr), eshoty(enemy.nr, eshot.nr)), r, Col
	NEXT
eshot(enemy.nr, eshot.nr) = 0
lp = lp - eshotpower(enemy.nr, eshot.nr)
END IF
END IF
SELECT CASE eshot(enemy.nr, eshot.nr)
CASE 2: FOR px% = 1 TO 3
	 FOR py% = 1 TO 3
	 IF NOT eshot1(px%, py%) = 0 THEN PSET (px% + eshotx(enemy.nr, eshot.nr) - 2, py% + eshoty(enemy.nr, eshot.nr) - 1), eshot1(px%, py%)
	 NEXT: NEXT
CASE 3: FOR px% = 1 TO 4
	 FOR py% = 1 TO 10
	 IF NOT eshot2(px%, py%) = 0 THEN PSET (px% + eshotx(enemy.nr, eshot.nr) - 2, py% + eshoty(enemy.nr, eshot.nr) - 1), eshot2(px%, py%)
	 NEXT: NEXT
CASE 4: FOR px% = 1 TO 3
	 FOR py% = 1 TO 6
	 IF NOT eshot3(px%, py%) = 0 THEN PSET (px% + eshotx(enemy.nr, eshot.nr) - 2, py% + eshoty(enemy.nr, eshot.nr) - 1), eshot3(px%, py%)
	 NEXT: NEXT
	 FOR part.nr = 1 TO maxparticles
	 IF particle(part.nr) = 0 THEN
	 particle(part.nr) = 4
	 particlex(part.nr) = RAND(eshotx(enemy.nr, eshot.nr) - 1, eshotx(enemy.nr, eshot.nr) + 1)
	 particley(part.nr) = eshoty(enemy.nr, eshot.nr) - 6
	 particlexv(part.nr) = 0
	 particleyv(part.nr) = 0
	 particleweight(part.nr) = 0
	 particlec(part.nr) = 113
	 particlestrength(part.nr) = 0
	 EXIT FOR
	 END IF
	 NEXT
CASE 5: FOR px% = 1 TO 7
	 FOR py% = 1 TO 13
	 IF NOT eshot4(px%, py%) = 0 THEN PSET (px% + eshotx(enemy.nr, eshot.nr) - 2, py% + eshoty(enemy.nr, eshot.nr) - 1), eshot4(px%, py%)
	 NEXT: NEXT
END SELECT

END IF
NEXT eshot.nr
NEXT enemy.nr
skip.eshot:

FOR part.nr = 1 TO maxparticles
IF particle(part.nr) <> 0 THEN
SELECT CASE particle(part.nr)
CASE 2: PSET (particlex(part.nr), particley(part.nr)), 4
CASE 3: PSET (particlex(part.nr), particley(part.nr)), 7
	CIRCLE (particlex(part.nr), particley(part.nr)), 1, 8
CASE ELSE: PSET (particlex(part.nr), particley(part.nr)), particlec(part.nr)
END SELECT
END IF
NEXT

shipx = a - 7
shipy = C - 5
shipx2 = a + 7
shipy2 = C + 32

IF showexplosions = 1 AND RAND(1, 100) < showexplosionsfrequency THEN
FOR ex.nr = 1 TO maxex
IF ex(ex.nr) = 0 THEN
exx(ex.nr) = RAND(0, 260)
exy(ex.nr) = RAND(0, 200)
exsizemax(ex.nr) = RAND(15, 30)
exsizemin(ex.nr) = 1
ex(ex.nr) = exsizemin(ex.nr)
excol(ex.nr) = 113
EXIT FOR
END IF
NEXT
END IF

IF fadebackgroundtimes > 0 AND timeUp <> 1 THEN
IF fadecol <= 0 THEN fadedirection = 1: fadebackgroundtimes = fadebackgroundtimes - 1
IF fadecol >= 38 THEN fadedirection = -1
fadecol = fadecol + fadedirection
OUT &H3C8, 0
OUT &H3C9, fadecol * fader
OUT &H3C9, fadecol * fadeg
OUT &H3C9, fadecol * fadeb
END IF

IF flashingEnabled <> 0 THEN
IF flashback = 1 THEN
ChangePalette 0, 0, 0, 0
flashback = 0
ELSE
flashback = 1
END IF
IF RAND(0, 100) < flashingFrequency THEN
ChangePalette 0, flashcolred, flashcolgreen, flashcolblue
flashback = 0
END IF
ELSE
ChangePalette 0, 0, 0, 0
END IF

IF countdowntimer.set = 1 THEN
countdown.ticks = countdown.ticks - .5
IF countdown.ticks <= 0 THEN
timeUp = 1
countdown.ticks = 0
FOR pal2 = 0 TO 255
OUT &H3C7, pal2
rp = INP(&H3C9) + 1
gp = INP(&H3C9) + 1
bp = INP(&H3C9) + 1
IF rp > 63 THEN rp = 63
IF gp > 63 THEN gp = 63
IF bp > 63 THEN bp = 63
OUT &H3C8, pal2
OUT &H3C9, rp
OUT &H3C9, gp
OUT &H3C9, bp
NEXT
IF rp = 63 AND gp = 63 AND bp = 63 THEN gameover
END IF
END IF
IF debugmode = 1 THEN
Font 0, 0, "Scrollpos=" + STR$(scrollpos&), 15
Font 0, 10, "Partikel:" + STR$(particlestotal), 15
Font 0, 20, "Shots:" + STR$(shotstotal), 15
Font 0, 30, "Gegner:" + STR$(enemiestotal), 15
Font 0, 40, "Sterne:" + STR$(starstotal), 15
Font 0, 50, "Extras:" + STR$(powtotal), 15
Font 0, 60, LTRIM$(RTRIM$(STR$(eventscrollpos&))) + " " + event$ + " " + LTRIM$(RTRIM$(STR$(var1) + STR$(var2) + STR$(var3) + STR$(var4))), 15
END IF
IF showmessageframes% > 0 THEN
showmessageframes% = showmessageframes% - 1
Font messagex%, messagey%, message$, 15
END IF
LPSHOW
Font 270, 35, "Weapon", 9
Font 270, 45, STR$(weapon), 9
IF NOT weapon = 0 THEN
Font 270, 60, "AMMO", 6
Font 270, 70, STR$(INT(ammo(weapon) + .9)), 6
END IF
Font 270, 85, "PowLn", 11
Font 270, 95, STR$(INT(pl.ammo)), 11
Font 270, 110, "Rocket", 14
Font 270, 120, STR$(INT(mi.ammo)), 14

'...
IF countdowntimer.set = 1 THEN
tmpvar = countdown.ticks / 10
temp$ = RIGHT$(STR$(tmpvar), 2)
temp2$ = STR$(INT(tmpvar \ 60))
temp3$ = STR$(INT(tmpvar MOD 60))
temp$ = temp2$ + "'" + LTRIM$(RTRIM$(temp3$)) + CHR$(34) + LTRIM$(RTRIM$(temp$))
Font 170, 2, temp$, 15
END IF
IF beamtime% > 0 THEN
Font 100, 90, "WARNING!", 115
Font 90, 100, "Beam overheating!", 115
Font 110, 110, STR$((1000 - beamtime%) / 100), 115
END IF
LINE (269, 157)-(320, 158), 1, B
LINE (269, 159)-(269 + beampower * 1.666, 173), 116, BF
LINE (269, 159)-(269 + beamtime% * 3 * .01666, 173), 15, BF
Font 270, 162, "Beam", 0

LINE (269, 174)-(320, 175), 1, B
LINE (269, 176)-(269 + boost * 7, 186), 64 + (game.speed * 7), BF
Font 270, 179, "Boost", 15
IF useplusminus = 0 AND randompos = 0 THEN
LINE (269, 176)-(319, 186), 4
LINE (269, 186)-(319, 176), 4
END IF

LINE (269, 187)-(320, 188), 1, B
LINE (269, 189)-(269 + ship.step * 7, 199), 64 + (ship.step * 7), BF
Font 270, 190, "Move", 15


IF randompos = 1 THEN
FOR ammo.nr = 1 TO 9
ammo(ammo.nr) = 1
NEXT
END IF
IF lp > lpmax THEN lp = lpmax
IF a > 250 THEN a = 250
IF a < 15 THEN a = 15
IF C < 40 THEN C = 40
IF C > 160 THEN C = 160

IF lp <= 0 THEN
bigexplode shipx, shipy, shipx2, shipy2
gameover
END IF
IF gamover = 1 THEN GOTO mainmenu
LOOP

savegame:
KBHoff
I2% = 1: maxsgm% = 10
CLS
PRINT "              Save game"
PRINT "              ~~~~~~~~~"
PRINT "Select slot to save to."
PRINT "Warning: Save slot is overwritten immediately!"
DO: DO
key$ = INKEY$
SELECT CASE key$
CASE d$
IF I2% < maxsgm% THEN I2% = I2% + 1
CASE u$
IF I2% > 1 THEN I2% = I2% - 1
CASE escape$
CLS : KBHon: RETURN
CASE return$
EXIT DO
END SELECT
FOR h% = 1 TO maxsgm%
IF h% = I2% THEN
LOCATE h% + 10, 14: PRINT "[": LOCATE h% + 10, 32: PRINT "]"
ELSE
LOCATE h% + 10, 14: PRINT " ": LOCATE h% + 10, 32: PRINT " "
END IF
NEXT h%
FOR po% = 1 TO 9: LOCATE po% + 10, 15: PRINT "Slot"; po%;
IF EXIST("savegame\save" + LTRIM$(RTRIM$(STR$(po%))) + ".sav") THEN
PRINT " -USED"
ELSE PRINT " -free"
END IF
NEXT
LOCATE 20, 15: PRINT "Back to game"
LOOP
SELECT CASE sounddevice%
CASE 1: SOUND 700, .1
CASE 2:
CASE 3: play8bitwave ("sfx\decision.wav")
END SELECT
IF I2% = 10 THEN CLS : KBHon: RETURN
savefile = FREEFILE
lenlevelfile% = LEN(levelfile$)
OPEN "savegame\save" + LTRIM$(RTRIM$(STR$(I2%))) + ".sav" FOR BINARY AS #savefile
PUT #savefile, , version2
PUT #savefile, , level
PUT #savefile, , lenlevelfile%
PUT #savefile, , levelfile$
FOR weapon.nr = 2 TO 10
PUT #savefile, , weapon.enabled(weapon.nr)
PUT #savefile, , ammo(weapon.nr)
NEXT
PUT #savefile, , mi.ammo
PUT #savefile, , pl.ammo
PUT #savefile, , lpmax
PUT #savefile, , money!
PUT #savefile, , wpn1type%
FOR temp = 1 TO 3
PUT #savefile, , specshot(temp)
NEXT
CLOSE #savefile
LOOP

loadgame:
GOSUB cleargamevars
KBHoff
i3% = 1: maxlgm% = 10
DO: DO
key$ = INKEY$
SELECT CASE key$
CASE d$
IF i3% < maxlgm% THEN i3% = i3% + 1
CASE u$
IF i3% > 1 THEN i3% = i3% - 1
CASE escape$
CLS : RETURN
CASE return$
EXIT DO
END SELECT

FOR po% = 1 TO 9
Font 100, po% * 10 + 50, "Slot" + LTRIM$(RTRIM$(STR$(po%))) + IF3$(EXIST("savegame\save" + LTRIM$(RTRIM$(STR$(po%))) + ".sav"), " -free", " -USED"), IF2(i3% = po%, 10, 2)
NEXT
LOCATE 20, 15: PRINT "Back to main menu"
LOOP

IF i3% = 10 THEN CLS : RETURN
loadfile = FREEFILE
IF EXIST("savegame\save" + LTRIM$(RTRIM$(STR$(i3%))) + ".sav") THEN
SELECT CASE sounddevice%
CASE 1: SOUND 700, .1
CASE 2:
CASE 3: play8bitwave ("sfx\decision.wav")
END SELECT

OPEN "savegame\save" + LTRIM$(RTRIM$(STR$(i3%))) + ".sav" FOR BINARY AS #loadfile
GET #loadfile, , temp
GET #loadfile, , level
GET #loadfile, , lenlevelfile%
levelfile$ = SPACE$(lenlevelfile%)
GET #loadfile, , levelfile$
FOR weapon.nr = 2 TO 10
GET #loadfile, , weapon.enabled(weapon.nr)
GET #loadfile, , ammo(weapon.nr)
NEXT
GET #loadfile, , mi.ammo
GET #loadfile, , pl.ammo
GET #loadfile, , lpmax
GET #loadfile, , money!
GET #loadfile, , wpn1type%
FOR temp = 1 TO 3
GET #loadfile, , specshot(temp)
NEXT
CLOSE #loadfile
lp = lpmax
temp$ = ""
FOR i% = 1 TO LEN(levelfile$)
IF ASC(MID$(levelfile$, i%, 1)) = 0 THEN EXIT FOR
temp$ = temp$ + MID$(levelfile$, i%, 1)
NEXT
levelfile$ = temp$
GetStringData "levels\" + levelfile$
OPEN "levels\" + levelfile$ FOR INPUT AS #1
DO
LINE INPUT #1, lcmd$
lcmd$ = LCASE$(lcmd$)
IF LEFT$(lcmd$, 7) = ">level:" THEN IF level = VAL(RIGHT$(lcmd$, LEN(lcmd$) - 7)) THEN EXIT DO
LOOP
GOTO restart.gameloop
ELSE
SELECT CASE sounddevice%
CASE 1: SOUND 40, .1
CASE 2:
CASE 3: play8bitwave ("sfx\failure1.wav")
END SELECT
END IF
LOOP

cleargamevars:
game.speed = 1: ship.step = 5: boost = 1
FOR part.nr = 1 TO maxparticles: particle(part.nr) = 0: NEXT 'clear particles
FOR pow.nr = 1 TO maxpow: pow(pow.nr) = 0: NEXT              'clear powerups
FOR enemy.nr = 1 TO maxenemies  '|
enemy(enemy.nr) = 0
enemyxstep(enemy.nr) = 0
FOR eshot.nr = 1 TO 20          '|    'clear enemies and their shots
eshot(enemy.nr, eshot.nr) = 0   '|
NEXT                            '|
NEXT                            '|
FOR nr = 1 TO maxshots: shot(nr) = 0: NEXT
FOR ex.nr = 1 TO maxex: ex(ex.nr) = 0: NEXT
gamover = 0: useplusminus = 0: showexplosions = 0
showexplosionsfrequency = 0
fadebackgroundtimes = 0
scrollpos& = 0
timeUp = 0: countdowntimer.set = 0: countdown.ticks = 0 'clear timelimit
beampower = 0: beamtime% = 0
flashingEnabled = 0
restore.pal
RETURN

shopping:
KBHoff
i4% = 1
CONST maxs% = 4
DO: DO
WaitVerticalRetrace 2
CLS
Font 0, 40, "Basic weapon: ->" + STR$(wpn1type%) + " $" + LTRIM$(STR$(wpn1type% * 500)), IF2(i4% = 1, 9, 1)
Font 0, 50, "Shields+3: ->" + STR$(lpmax + 3) + " $200", IF2(i4% = 2, 9, 1)
Font 0, 60, "Extra weapon $750", IF2(i4% = 3, IF2(specshot(1) = 0, 9, 8), IF2(specshot(1) = 0, 1, 7))
'Font 0, 70, "nix", IF2(i4% = 4, 9, 1)
'Font 0, 80, "nix", IF2(i4% = 5, 9, 1)
Font 0, 90, "-> Continue", IF2(i4% = maxs%, 9, 1)

Font 120, 190, "Money:" + STR$(money!) + "$", 14
do.splashscreen
key$ = INKEY$
SELECT CASE key$
CASE d$
IF i4% < maxs% THEN i4% = i4% + 1
CASE u$
IF i4% > 1 THEN i4% = i4% - 1
CASE escape$
CLS : RETURN
CASE return$
EXIT DO
END SELECT
LOOP
SELECT CASE i4%
CASE 1: IF money! - wpn1type% * 500 >= 0 AND wpn1type% < 9 THEN money! = money! - wpn1type% * 500: wpn1type% = wpn1type% + 1: bought% = -1
CASE 2: IF NOT money! - 200 < 0 THEN money! = money! - 200: lpmax = lpmax + 3: bought% = -1
CASE 3: IF specshot(1) = 0 AND money! - 750 >= 0 THEN specshot(1) = 1: money! = money! - 750: bought% = -1
CASE maxs%: CLS : RETURN
END SELECT
IF bought% THEN
bought% = 0
SELECT CASE sounddevice%
CASE 1: SOUND 700, .1
CASE 2:
CASE 3: play8bitwave ("sfx\decision.wav")
END SELECT
ELSE
SELECT CASE sounddevice%
CASE 1: SOUND 40, .1
CASE 2:
CASE 3: play8bitwave ("sfx\failure1.wav")
END SELECT
END IF
LOOP

RETURN

SUB bigexplode (X, Y, x2, y2)
expcol = 70
CX = X + ((x2 - X) / 2): CY = Y + ((y2 - Y) / 2)
IF sounddevice% = 2 THEN playFX 13
IF sounddevice% = 3 THEN play8bitwave ("sfx\exp1.wav")
FOR r% = 0 TO 360
Future.FILLCIRCLE CX, CY, r%, expcol
IF expcol < 113 AND expcoldelay% = 2 THEN expcol = expcol + 1
expcoldelay% = ROTATEVAR(expcoldelay%, 0, 2)
n = n + 1
IF n MOD 3 = 0 THEN WaitVerticalRetrace 1
IF expcol = 113 THEN
OUT &H3C7, expcol
col75% = ROTATEVAR(col75%, 0, 2)
IF col75% = 2 THEN
rp = INP(&H3C9) + 1
gp = INP(&H3C9) + 1
bp = INP(&H3C9) + 1
IF rp > 63 THEN rp = 63
IF gp > 63 THEN gp = 63
IF bp > 63 THEN bp = 63
OUT &H3C8, expcol
OUT &H3C9, rp
OUT &H3C9, gp
OUT &H3C9, bp
END IF
END IF
NEXT
END SUB

REM $STATIC
SUB ChangePalette (C, r, g, B)
OUT &H3C8, C
OUT &H3C9, r
OUT &H3C9, g
OUT &H3C9, B
END SUB

SUB CHECKSAVEGAMES
FOR slotnr% = 1 TO 10
 IF EXIST("savegame\save" + LTRIM$(RTRIM$(STR$(slotnr%))) + ".sav") THEN
  OPEN "savegame\save" + LTRIM$(RTRIM$(STR$(slotnr%))) + ".sav" FOR BINARY AS #1
  GET #1, , v
  IF v <> VERSION THEN
  CLOSE #1
  KILL "savegame\save" + LTRIM$(RTRIM$(STR$(slotnr%))) + ".sav"
  PRINT "Speicherstand Nr."; slotnr%; " stimmt nicht mit der aktuellen Version berein. Gelscht."
 END IF
 CLOSE #1
 END IF
NEXT
END SUB

FUNCTION choice% (array$(), X, Y, col1, col2, extra)
max% = UBOUND(array$, 1)
i% = 1
DO
WaitVerticalRetrace 2
CLS
do.splashscreen
FOR p% = 1 TO max%
Font INT(X), INT(Y) + (10 * (p% - 1)), array$(p%), IF2(i% = p%, col1, col2)
NEXT

SELECT CASE extra
CASE 1
FOR px% = 1 TO UBOUND(dbxlogo2, 1)
FOR py% = 1 TO UBOUND(dbxlogo2, 2)
IF NOT dbxlogo2(px%, py%) = 0 THEN PSET (px%, py%), dbxlogo2(px%, py%)
NEXT: NEXT

END SELECT

key$ = INKEY$
SELECT CASE key$
CASE d$
IF i% < max% THEN i% = i% + 1
CASE u$
IF i% > 1 THEN i% = i% - 1
CASE escape$
CLS : choice% = 0: EXIT FUNCTION
CASE return$
EXIT DO
END SELECT
LOOP
choice% = i%
END FUNCTION

SUB DBXERROR (ERRnr%, s$)
CLS
PRINT "FEHLER"
PRINT "~~~~~~"
PRINT : PRINT "Fehler Nr."; ERRnr%: PRINT
SELECT CASE ERRnr%
CASE 0: PRINT s$
CASE 1: PRINT "Datei '" + s$ + "' nicht gefunden."
CASE 2: PRINT "Scrollposition im Level" + STR$(level) + CHR$(13) + " kleiner als im Spiel."
CASE 3: PRINT "Gegner ist zu breit und berschneidet die Lebensanzeige"
CASE 4: PRINT "Gegner Nr." + s$ + " gibt es (noch) nicht."
CASE 5: PRINT "Falsche Dateiversion." + CHR$(13) + "Programm neustarten.": KILL "double_x.cfg"
CASE 6: PRINT "Textdatei nicht definiert."
CASE 7: PRINT "Level nicht definiert"
CASE 8: PRINT "Falsche Levelnummer"
CASE 9: PRINT "Keine Levels verfgbar!"
CASE 10: PRINT "Leveldatei ohne Header"
END SELECT
PRINT : PRINT
PRINT "ESC zum Beenden drcken..."
KBHoff
DO: LOOP UNTIL GetKey(1) OR INKEY$ = CHR$(27) OR INP(&H60) = 1
QUIT
END SUB

FUNCTION Dir$ (FileSpec$) STATIC
'
' Returns a file from current directory
'
DIM DTA AS STRING * 44, Regs AS RegTypeX
Null$ = CHR$(0)

'-----  Set up our own DTA so we don't destroy COMMAND$
Regs.ax = SetDTA                   'Set DTA function
Regs.dx = VARPTR(DTA)              'DS:DX points to our DTA
Regs.ds = -1                       'Use current value for DS
INTERRUPTX DOS, Regs, Regs         'Do the interrupt

'-----  Check to see if this is First or Next
IF LEN(FileSpec$) THEN             'FileSpec$ isn't null, so
		'FindFirst
  FileSpecZ$ = FileSpec$ + Null$   'Make FileSpec$ into an ASCIIZ
		'string
  Regs.ax = FindFirst              'Perform a FindFirst
  Regs.CX = 0                      'Only look for normal files
  Regs.dx = SADD(FileSpecZ$)       'DS:DX points to ASCIIZ file
  Regs.ds = -1                     'Use current DS
ELSE                               'We have a null FileSpec$,
  Regs.ax = FindNext               'so FindNext
END IF

INTERRUPTX DOS, Regs, Regs         'Do the interrupt

'-----  Return file name or null
IF Regs.flags AND 1 THEN           'No files found
  Dir$ = ""                        'Return null string
ELSE
  Null = INSTR(31, DTA, Null$)     'Get the filename found
  Dir$ = MID$(DTA, 31, Null - 30)  'It's an ASCIIZ string starting
END IF                              'at offset 30 of the DTA

END FUNCTION

FUNCTION DMADone (lengy)
low = INP(&H3)
high = INP(&H3)
done& = high * 256& + low
IF done& > lengy - 1 THEN temp = INP(&H220 + &HE): DMADone = -1
END FUNCTION

SUB do.splashscreen STATIC
IF NOT first THEN
DIM mstar(200), mstarx(200), mstary(200), mstarxv(200), mstaryv(200), mstarz(200)
first = -1
END IF
boxangle! = boxangle! + .025
boxsize = ((SIN(boxangle!) + 1) * 15) + 60
boxsize2 = ((COS(boxangle!) + 1) * 15) + 60
sixECK 160, 100, boxsize, boxangle!, 6, 2
sixECK 160, 100, boxsize2, -boxangle!, 6, 2
FOR nix = 1 TO 4
FOR nr = 1 TO 200
IF mstar(nr) = 0 THEN
temp = RAND(1, 3600) / 10
mstar(nr) = 1
IF RAND(1, 110) = 1 THEN mstar(nr) = 2
mstarxv(nr) = SIN(temp)
mstaryv(nr) = COS(temp)
mstarx(nr) = 160
mstary(nr) = 100
mstarz(nr) = 0
EXIT FOR
END IF
NEXT
NEXT
FOR nr = 1 TO 200
IF mstar(nr) <> 0 THEN
mstarz(nr) = mstarz(nr) + 1
SELECT CASE mstar(nr)
CASE 1: PSET (mstarx(nr), mstary(nr)), IF2(mstarz(nr) * 1.25 + 116 <= 180, mstarz(nr) * 1.25 + 116, 180)
CASE 2: Future.FILLCIRCLE mstarx(nr), mstary(nr), mstarz(nr) \ 25, IF2(mstarz(nr) * 1.25 + 50 <= 113, mstarz(nr) * 1.25 + 50, 113)
END SELECT
mstarx(nr) = mstarx(nr) + mstarxv(nr)
mstary(nr) = mstary(nr) + mstaryv(nr)
mstarxv(nr) = mstarxv(nr) * 1.03
mstaryv(nr) = mstaryv(nr) * 1.03
IF mstarx(nr) < 0 OR mstarx(nr) > 320 THEN mstar(nr) = 0
IF mstary(nr) < 0 OR mstary(nr) > 200 THEN mstar(nr) = 0
END IF
NEXT
END SUB

SUB DRAWENEMY (enemyx, enemyy, enemy, enemyheight, enemywidth)
SELECT CASE enemy
CASE 1: FOR py% = 1 TO UBOUND(enemy1, 2): FOR px% = 1 TO UBOUND(enemy1, 1)
IF NOT enemy1(px%, py%) = 0 THEN PSET (px% - 1 + enemyx, py% - 1 + enemyy), enemy1(px%, py%)
NEXT: NEXT
CASE 2: FOR py% = 1 TO UBOUND(enemy2, 2): FOR px% = 1 TO UBOUND(enemy2, 1)
IF NOT enemy2(px%, py%) = 0 THEN PSET (px% - 1 + enemyx, py% - 1 + enemyy), enemy2(px%, py%)
NEXT: NEXT
CASE 3: FOR py% = 1 TO UBOUND(enemy3, 2): FOR px% = 1 TO UBOUND(enemy3, 1)
IF NOT enemy3(px%, py%) = 0 THEN PSET (px% - 1 + enemyx, py% - 1 + enemyy), enemy3(px%, py%)
NEXT: NEXT
CASE 4: FOR py% = 1 TO UBOUND(enemy4, 2): FOR px% = 1 TO UBOUND(enemy4, 1)
IF NOT enemy4(px%, py%) = 0 THEN PSET (px% - 1 + enemyx, py% - 1 + enemyy), enemy4(px%, py%)
NEXT: NEXT

'...
CASE 80: FOR py% = 1 TO 50
	 FOR px% = 1 TO 50
	 IF NOT boss1(px%, py%) = 0 THEN PSET (px% - 1 + enemyx, py% - 1 + enemyy), boss1(px%, py%)
	 NEXT: NEXT

'...
CASE 100: LINE (enemyx, enemyy)-(enemyx + enemywidth, enemyy + enemyheight), 7, B
LINE (enemyx + 1, enemyy + 1)-(enemyx + enemywidth - 1, enemyy + enemyheight - 1), 8, B
CASE ELSE: DBXERROR 4, LTRIM$(RTRIM$(STR$(enemy)))
END SELECT
END SUB

REM $DYNAMIC
SUB ENEMYEXPLODE (enemyx, enemyy, enemy, h, w)
FOR ex.nr = 1 TO maxex
IF ex(ex.nr) = 0 THEN
FOR n = 0 TO h * w \ 300 + 1
exx(ex.nr) = RAND(enemyx, enemyx + w)
exy(ex.nr) = RAND(enemyy, enemyy + h)
exsizemax(ex.nr) = 50
exsizemin(ex.nr) = 10
ex(ex.nr) = exsizemin(ex.nr)
excol(ex.nr) = 113
NEXT
EXIT FOR
END IF
NEXT
IF sounddevice% = 1 THEN
FOR s% = 0 TO 13: SOUND RAND(100, 300), .03: NEXT
END IF
IF sounddevice% = 3 THEN
zs% = RAND(1, 4)
SELECT CASE zs%
CASE 1: play8bitwave ("sfx\exp7.wav")
CASE 2: play8bitwave ("sfx\exp2.wav")
CASE 3: play8bitwave ("sfx\exp4.wav")
CASE 4: play8bitwave ("sfx\exp6.wav")
END SELECT
END IF
IF sounddevice% = 2 THEN playFX 12
END SUB

REM $STATIC
FUNCTION EXIST% (file$)
ff = FREEFILE
OPEN file$ FOR RANDOM AS #ff
IF LOF(ff) = 0 THEN
CLOSE #ff
EXIST = 0
KILL file$
ELSE EXIST = -1
END IF
CLOSE #ff
END FUNCTION

REM $DYNAMIC
SUB flip.page
WaitVerticalRetrace 2
Future.CLS 0
END SUB

REM $STATIC
SUB gamefinish

END SUB

SUB gameover
gamover = 1
palfadeblack
Future.CLS 0
restore.pal
EXIT SUB
END SUB

FUNCTION getenemylp (enemy)
  SELECT CASE enemy
   CASE 1: getenemylp = 1
   CASE 2: getenemylp = 3
   CASE 3: getenemylp = 5
   CASE 4: getenemylp = 7
   CASE 5: getenemylp = 8
   CASE 6: getenemylp = 13
  '...
  CASE 25: getenemylp = 30
  'bosses
  CASE 80: getenemylp = 120
   CASE 100: getenemylp = 32768

  END SELECT

END FUNCTION

FUNCTION geteshotdelay (enemy)
SELECT CASE enemy
CASE 4: eshotdelay = .7
CASE 80: eshotdelay = .15
'...
END SELECT
geteshotdelay = eshotdelay
END FUNCTION

FUNCTION getheight (enemy)
SELECT CASE enemy
CASE 1: enemyheight = 20
CASE 2: enemyheight = 30
CASE 3: enemyheight = 20
CASE 4: enemyheight = 25
CASE 5: enemyheight = 20
CASE 6: enemyheight = 20
CASE 25: enemyheight = 30
CASE 80: enemyheight = 50
END SELECT
getheight = enemyheight
END FUNCTION

SUB GetStringData (f$)
OPEN f$ FOR INPUT AS #1
p% = 1
 DO
  LINE INPUT #1, s$
  IF LCASE$(s$) = "&endstringdata" THEN EXIT DO
  IF LEFT$(s$, 1) = CHR$(36) THEN
   DO
   p% = p% + 1
   IF MID$(s$, p%, 1) = ":" THEN EXIT DO ELSE n$ = n$ + MID$(s$, p%, 1)
   LOOP
   n% = VAL(n$)
   levelstring$(n%) = RIGHT$(s$, LEN(s$) - p%)
   p% = 1: n$ = ""
   '...
  END IF
 LOOP
 CLOSE #1


END SUB

FUNCTION getwidth (enemy)
SELECT CASE enemy
CASE 1: enemywidth = 15
CASE 2: enemywidth = 10
CASE 3: enemywidth = 15
CASE 4: enemywidth = 18
CASE 5: enemywidth = 15
CASE 6: enemywidth = 15
CASE 25: enemywidth = 20
CASE 80: enemywidth = 50
END SELECT
getwidth = enemywidth
END FUNCTION

FUNCTION HasBlaster%
env$ = ENVIRON$("BLASTER")
IF LEN(env$) = 0 THEN HasBlaster = 0 ELSE HasBlaster = -1
END FUNCTION

REM $DYNAMIC
SUB HOLD (z)
t! = TIMER
DO: LOOP UNTIL t! + z < TIMER
END SUB

REM $STATIC
FUNCTION IF2 (cond, vartrue, varfalse)
IF cond = 0 THEN IF2 = varfalse ELSE IF2 = vartrue
END FUNCTION

FUNCTION IF3$ (cond, varfalse$, vartrue$)
IF cond = 0 THEN IF3$ = varfalse$ ELSE IF3$ = vartrue$
END FUNCTION

REM $DYNAMIC
FUNCTION LoadFX (Filename$)
'
' This routine loads specified sound file.
'

OPEN Filename$ FOR BINARY AS #1
IF LOF(1) = 0 THEN LoadFX = 0: CLOSE #1: EXIT FUNCTION
DIM id AS STRING * 4
GET #1, , id
IF MID$(id, 1, 2) <> "SL" THEN LoadFX = 1: CLOSE #1: EXIT FUNCTION
IF MID$(id, 3, 2) <> "10" THEN LoadFX = 2: CLOSE #1: EXIT FUNCTION
GET #1, , NumSound
NumSound = 13'!!!!!!!!
REDIM Snd(1 TO NumSound) AS STRING * 30
FOR i = 1 TO NumSound
  GET #1, , Snd(i)
  temp$ = SPACE$(20)
  GET #1, , temp$
NEXT i
CLOSE #1
LoadFX = -1

END FUNCTION

REM $STATIC
SUB LOADSPRITES
'$DYNAMIC
OPEN "sprites\ship.b" FOR INPUT AS #1: INPUT #1, bheight%, blength%
FOR Y% = 1 TO blength%: FOR X% = 1 TO bheight%: INPUT #1, bcol%
ship(X%, Y%) = bcol%: NEXT: NEXT: CLOSE #1

OPEN "sprites\dbxlogo2.b" FOR INPUT AS #1: INPUT #1, bheight%, blength%
FOR Y% = 1 TO blength%: FOR X% = 1 TO bheight%: INPUT #1, bcol%
dbxlogo2(X%, Y%) = bcol%: NEXT: NEXT: CLOSE #1

FOR pownr% = 1 TO 15
OPEN "sprites\pow" + LTRIM$(RTRIM$(STR$(pownr%))) + ".b" FOR INPUT AS #1
INPUT #1, bheight%, blength%
FOR Y% = 1 TO blength%
FOR X% = 1 TO bheight%
INPUT #1, bcol%
powp(pownr%, X%, Y%) = bcol%
NEXT
NEXT
CLOSE #1
NEXT

FOR code% = 0 TO 255
FOR Y% = 1 TO 5
FOR X% = 1 TO 5
minifont(code%, X%, Y%) = 1
NEXT
NEXT
NEXT
OPEN "data\minifont.dat" FOR INPUT AS #1
INPUT #1, bheight%, blength%
FOR code% = 48 TO 90
FOR Y% = 1 TO 5
FOR X% = 1 TO 5
INPUT #1, bcol%
minifont(code%, X%, Y%) = bcol%
NEXT
NEXT
NEXT
CLOSE #1

OPEN "sprites\stripes.b" FOR INPUT AS #1
INPUT #1, bheight%, blength%
FOR Y% = 1 TO blength%
FOR X% = 1 TO bheight%
INPUT #1, bcol%
IF bcol% = 1 THEN bcol% = 114
stripe(X%, Y%) = bcol%
NEXT
NEXT
CLOSE #1

OPEN "sprites\empr.b" FOR INPUT AS #1: INPUT #1, bheight%, blength%
FOR Y% = 1 TO blength%: FOR X% = 1 TO bheight%: INPUT #1, bcol%
empr(X%, Y%) = bcol%: NEXT: NEXT: CLOSE #1
OPEN "sprites\minimiss.b" FOR INPUT AS #1: INPUT #1, bheight%, blength%
FOR Y% = 1 TO blength%: FOR X% = 1 TO bheight%: INPUT #1, bcol%
minimiss(X%, Y%) = bcol%: NEXT: NEXT: CLOSE #1

OPEN "sprites\boss1.b" FOR INPUT AS #1: INPUT #1, bheight%, blength%
FOR Y% = 1 TO blength%: FOR X% = 1 TO bheight%: INPUT #1, bcol%
boss1(X%, Y%) = bcol%: NEXT: NEXT: CLOSE #1

OPEN "sprites\enemy1.b" FOR INPUT AS #1
INPUT #1, bheight%, blength%
FOR Y% = 1 TO blength%
FOR X% = 1 TO bheight%
INPUT #1, bcol%
enemy1(X%, Y%) = bcol%
NEXT
NEXT
CLOSE #1
OPEN "sprites\enemy4.b" FOR INPUT AS #1
INPUT #1, bheight%, blength%
FOR Y% = 1 TO blength%
FOR X% = 1 TO bheight%
INPUT #1, bcol%
enemy4(X%, Y%) = bcol%
NEXT
NEXT
CLOSE #1
OPEN "sprites\enemy2.b" FOR INPUT AS #1
INPUT #1, bheight%, blength%
FOR Y% = 1 TO blength%
FOR X% = 1 TO bheight%
INPUT #1, bcol%
enemy2(X%, Y%) = bcol%
NEXT
NEXT
CLOSE #1
OPEN "sprites\enemy3.b" FOR INPUT AS #1
INPUT #1, bheight%, blength%
FOR Y% = 1 TO blength%
FOR X% = 1 TO bheight%
INPUT #1, bcol%
enemy3(X%, Y%) = bcol%
NEXT
NEXT
CLOSE #1



OPEN "sprites\eshot1.b" FOR INPUT AS #1
INPUT #1, bheight%, blength%
FOR Y% = 1 TO blength%
FOR X% = 1 TO bheight%
INPUT #1, bcol%
eshot1(X%, Y%) = bcol%
NEXT
NEXT
CLOSE #1

OPEN "sprites\eshot2.b" FOR INPUT AS #1
INPUT #1, bheight%, blength%
FOR Y% = 1 TO blength%
FOR X% = 1 TO bheight%
INPUT #1, bcol%
eshot2(X%, Y%) = bcol%
NEXT
NEXT
CLOSE #1

OPEN "sprites\eshot3.b" FOR INPUT AS #1
INPUT #1, bheight%, blength%
FOR Y% = 1 TO blength%
FOR X% = 1 TO bheight%
INPUT #1, bcol%
eshot3(X%, Y%) = bcol%
NEXT
NEXT
CLOSE #1

OPEN "sprites\eshot4.b" FOR INPUT AS #1
INPUT #1, bheight%, blength%
FOR Y% = 1 TO blength%
FOR X% = 1 TO bheight%
INPUT #1, bcol%
eshot4(X%, Y%) = bcol%
NEXT
NEXT
CLOSE #1

END SUB

SUB LPSHOW
LINE (260, 0)-(260, 200), 1
LINE (261, 0)-(261, 200), 9
LINE (267, 0)-(267, 200), 9
LINE (268, 0)-(268, 200), 1
c1 = lpmax * .2
c2 = lpmax * .45
IF lp <= c1 THEN
C% = 4
ELSEIF lp > c1 AND lp <= c2 THEN C% = 14
ELSE C% = 2
END IF
lpmeter = lp / (lpmax / 200)
LINE (262, 200)-(266, 200 - lpmeter), C%, BF
Font 270, 10, "Shield", C%
Font 270, 20, STR$(lp), C%
END SUB

REM $STATIC
SUB miniprint (xpos%, ypos%, s$, Col%)
s$ = UCASE$(s$)
FOR length% = 1 TO LEN(s$)
code% = ASC(MID$(s$, length%, 1))
xc% = xpos% + length% * 5: yc% = ypos%
FOR Y% = 1 TO 5
FOR X% = 1 TO 5
IF minifont(code%, X%, Y%) <> 0 THEN PSET (xc% + X%, Y% + yc%), Col%
NEXT
NEXT
NEXT
END SUB

SUB newlevel
Future.CLS 0
LOCATE 12, 15: PRINT "YOU DID IT"

FOR farbe = 0 TO 63 STEP 1
f = f + 1: IF f > 63 THEN f = 63
PALETTE 15, 65536 * 0 + 256 * 0 + farbe
PALETTE 0, 65536 * 0 + 256 * f + 0
n = n + 1
IF n MOD 3 = 0 THEN WaitVerticalRetrace 1
LOCATE 12, 15: PRINT "YOU DID IT"
NEXT
LOCATE 12, 15: PRINT "YOU DID IT"
HOLD .3
FOR farbe = 63 TO 0 STEP -1
f = f - 1: IF f < 0 THEN f = 0
PALETTE 15, 65536 * 0 + 256 * 0 + farbe
PALETTE 0, 65536 * 0 + 256 * f + 0
LOCATE 12, 15: PRINT "YOU DID IT"
n = n + 1
IF n MOD 3 = 0 THEN WaitVerticalRetrace 1
NEXT
restore.pal
Future.CLS 0
scrollpos& = 0
END SUB

REM $DYNAMIC
SUB optionsmenu
Future.CLS 0
max% = 2: i% = 1: cpal = -1: pal = 63: pal2 = 0: cpal2 = 1
HasB = HasBlaster
balken1$ = ">>>>>" + STRING$(30, 45) + "<<<<<"
DO: DO
LOCATE 1, 1: PRINT "OO     OO   O    O OO  O   OOO    O   O "
LOCATE 2, 1: PRINT "O O   O  O  O    O O O O   O      O   O   "
LOCATE 3, 1: PRINT "O  O O    O O    O O O O   O       O O"
LOCATE 4, 1: PRINT "O  O O    O O    O OO  O   OOO ÍÍ   O     "
LOCATE 5, 1: PRINT "O  O O    O O    O O O O   O       O O  "
LOCATE 6, 1: PRINT "O O   O  O   O  O  O O O   O      O   O"
LOCATE 7, 1: PRINT "OO     OO     OO   OO  OOO OOO    O   O"
LOCATE 9, 15: PRINT "OPTIONS"
pal = pal + cpal: IF pal >= 63 OR pal = 0 THEN cpal = -cpal
pal2 = pal2 + cpal2: IF pal2 >= 63 OR pal2 = 0 THEN cpal2 = -cpal2
PALETTE 15, 65536 * pal + 256 * pal + pal
PALETTE 0, 65536 * pal2 + 256 * pal2 + pal2
WaitVerticalRetrace 1
key$ = INKEY$
SELECT CASE key$
CASE d$
IF i% < max% THEN i% = i% + 1
CASE u$
IF i% > 1 THEN i% = i% - 1
CASE escape$
Future.CLS 0: EXIT SUB
CASE return$
EXIT DO
END SELECT
FOR h% = 1 TO max%
IF h% = i% THEN
LOCATE h% + 12, 1
'PRINT "[": LOCATE h% + 12, 37: PRINT "]"
PRINT balken1$
ELSE
LOCATE h% + 12, 1:
'PRINT " ": LOCATE h% + 12, 37: PRINT " "
PRINT SPACE$(40)
END IF
NEXT h%
LOCATE 13, 13: PRINT "Sound:"; sounddevice%;
SELECT CASE sounddevice%
CASE 0: PRINT "Off"; : CASE 1: PRINT "PC-Speaker": CASE 2: PRINT "FM-Synth"; : CASE 3: PRINT "WAVE";
END SELECT
'PRINT SPACE$(10)
LOCATE 14, 15: PRINT " Back "
LOOP
SELECT CASE i%
CASE 1: sounddevice% = ROTATEVAR(sounddevice%, 0, IF2(HasB, 3, 1))
CASE max%: Future.CLS 0: EXIT SUB
END SELECT
LOOP

END SUB

SUB palfadeblack
DO
   FOR t = 0 TO 255
      OUT &H3C7, t
      r = INP(&H3C9) - 1
      g = INP(&H3C9) - 1
      B = INP(&H3C9) - 1
      IF r < 0 THEN r = 0
      IF g < 0 THEN g = 0
      IF B < 0 THEN B = 0
      OUT &H3C8, t
      OUT &H3C9, r
      OUT &H3C9, g
      OUT &H3C9, B
   NEXT t
n = n + 1
IF n MOD 3 = 0 THEN WaitVerticalRetrace 1
   FOR t = 0 TO 255
      OUT &H3C7, t
      r = INP(&H3C9)
      g = INP(&H3C9)
      B = INP(&H3C9)
      IF r <> 0 OR g <> 0 OR B <> 0 THEN
	 palblack = 0
	 EXIT FOR
      ELSE
	 palblack = 1
      END IF
   NEXT t
LOOP UNTIL palblack = 1

END SUB

REM $STATIC
FUNCTION picklevel$
restore.pal
CLS
DIM file$(100): numfiles = 0
file$(1) = Dir$("levels\*.stg")
IF file$(1) <> "" THEN numfiles = 1
FOR i% = 2 TO 100
file$(i%) = Dir$("")
IF file$(i%) <> "" THEN numfiles = numfiles + 1 ELSE EXIT FOR
NEXT i%
FOR i% = 1 TO numfiles: file$(i%) = LEFT$(file$(i%), LEN(file$(i%)) - 1): NEXT
Sort file$(), 1, numfiles
imin% = 1: fup% = 1
LOCATE 1, 1: PRINT "ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»"
FOR locat% = 1 TO 15
LOCATE locat% + 1, 1: PRINT "º              º"
NEXT
LOCATE locat% + 1, 1: PRINT "ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍŒ"
DO
 OPEN "levels\" + file$(imin%) FOR INPUT AS #1
 DO
  LINE INPUT #1, lcmd$
  IF NOT LEFT$(lcmd$, 1) = CHR$(38) THEN DBXERROR 10, ""
  IF LEFT$(LCASE$(lcmd$), 13) = "&description:" THEN
   desc$ = RIGHT$(lcmd$, LEN(lcmd$) - 13)
  ELSEIF LEFT$(LCASE$(lcmd$), 8) = "&author:" THEN
   author$ = RIGHT$(lcmd$, LEN(lcmd$) - 8)
  ELSEIF LEFT$(LCASE$(lcmd$), 11) = "&leveltype:" THEN
   typ$ = LCASE$(RIGHT$(lcmd$, LEN(lcmd$) - 11))
  SELECT CASE typ$
  CASE "tutorial": lpmax = 500: LEVTYPE% = 2
  CASE "original": lpmax = 75: LEVTYPE% = 1
  CASE "custom": LEVTYPE% = 3
  CASE ELSE: LEVTYPE% = 3
  '...
  END SELECT

  ELSEIF LEFT$(LCASE$(lcmd$), 4) = "&lp:" AND LEVTYPE% = 3 THEN
  lpmax = VAL(LCASE$(RIGHT$(lcmd$, LEN(lcmd$) - 4)))
   '...
  END IF
 LOOP UNTIL lcmd$ = "&endheader"
 CLOSE #1

 FOR i% = fup% TO fup% + 14
 LOCATE i% - fup% + 2, 3: PRINT file$(i%) + SPACE$(12 - LEN(file$(i%)))
 IF i% <> imin% THEN
 LOCATE i% - fup% + 2, 2: PRINT " "
 LOCATE i% - fup% + 2, 15: PRINT " "
 ELSE
 LOCATE (imin% - fup%) + 2, 2: PRINT CHR$(17)
 LOCATE (imin% - fup%) + 2, 15: PRINT CHR$(16)
 END IF
 NEXT
 LOCATE 21, 1: PRINT SPACE$(40)
 LOCATE 22, 1: PRINT SPACE$(40)
 LOCATE 21, 1: PRINT desc$
 LOCATE 22, 1: PRINT author$
 DO

  key$ = INKEY$
  SELECT CASE key$
   CASE u$: imin% = imin% - 1: IF imin% < 1 THEN imin% = 1:
   IF (imin% - fup%) + 1 < 1 THEN fup% = fup% - 1
   EXIT DO
   CASE d$: imin% = imin% + 1: IF imin% > numfiles THEN imin% = numfiles
   IF (imin% - fup%) + 1 > 15 THEN fup% = fup% + 1
   EXIT DO
   CASE CHR$(13), CHR$(32): picklevel$ = file$(imin%): EXIT FUNCTION
   CASE escape$: picklevel$ = "": EXIT FUNCTION
  END SELECT
 LOOP
LOOP
END FUNCTION

SUB play8bitwave (f$)
ff = FREEFILE
OPEN f$ FOR BINARY AS #ff
lengy& = IF2(LOF(ff) < 32767, LOF(ff), 32767)
DIM WaveBuffer(0) AS STRING * 32767
DIM WaveHeader AS STRING * 44
lengy& = lengy& - 44
leng = lengy&
GET #ff, , header
GET #ff, , WaveBuffer(0)
PlaySound VARSEG(WaveBuffer(0)), VARPTR(WaveBuffer(0)), 8000, leng
CLOSE #ff
END SUB

REM $DYNAMIC
SUB playFX (Num)
'
' Plays specified FX num.
'
FOR i = 1 TO 15
  OUT &H388, ASC(MID$(Snd(Num), (i * 2) - 1, 1))
  FOR ii = 1 TO 6: temp = INP(&H388): NEXT ii
  OUT &H389, ASC(MID$(Snd(Num), (i * 2)))
  FOR ii = 1 TO 35: temp = INP(&H388): NEXT ii
NEXT i

END SUB

REM $STATIC
SUB PlaySound (Segment, Offset, freq AS LONG, length)
address& = Segment * 16& + Offset
OUT &HA, &H5
OUT &HC, &H0
OUT &HB, &H49
OUT &H2, address& AND &HFF
OUT &H2, (address& AND &HFFFF&) \ &H100
IF (address& AND 65536) THEN page = page + 1
IF (address& AND 131072) THEN page = page + 2
IF (address& AND 262144) THEN page = page + 4
IF (address& AND 524288) THEN page = page + 8
OUT &H83, page
lowbyte = length AND &HFF
highbyte = (length AND &HFFFF&) \ &H100
OUT &H3, lowbyte
OUT &H3, highbyte
OUT &HA, &H1
timeconst = 256 - 1000000 \ freq
WriDSP &H40
WriDSP timeconst
WriDSP &H14
WriDSP lowbyte
WriDSP highbyte
END SUB

SUB QUIT
KBHoff
ReSetScreen
Future.CLS 0
SYSTEM
END SUB

REM $DYNAMIC
FUNCTION RAND! (min!, max!)
RAND! = INT((max! - min! + 1) * RND + min!)
END FUNCTION

REM $STATIC
FUNCTION ReadDSP
DO: LOOP UNTIL INP(Blaster.Basead + &HE) AND &H80
ReadDSP = INP(Blaster.Basead + &HA)
END FUNCTION

SUB restore.pal
PALETTE
FOR orange = 0 TO 63
ChangePalette 50 + orange, orange, orange \ 2, 0
NEXT
ChangePalette 114, 55, 55, 0
ChangePalette 115, 40, 0, 47
ChangePalette 116, 60, 0, 63
FOR white = 0 TO 63
ChangePalette 117 + white, white, white, white
NEXT
END SUB

FUNCTION ROTATEVAR% (var%, min%, max%)
var% = var% + 1
IF var% > max% THEN var% = min%
ROTATEVAR% = var%
END FUNCTION

REM $DYNAMIC
SUB ship.enemy.collision (shipx, Y, shipx2, y2, enemyx, enemyy, enemy, enemyheight, enemywidth)

IF shipx > enemyx AND Y > enemyy THEN
IF shipx < enemyx + enemywidth AND Y < enemyy + enemyheight THEN
ship.enemy.collisio = 1
END IF
END IF

IF shipx > enemyx AND y2 > enemyy THEN
IF shipx < enemyx + enemywidth AND y2 < enemyy + enemyheight THEN
ship.enemy.collisio = 1
END IF
END IF

IF shipx2 > enemyx AND Y > enemyy THEN
IF shipx2 < enemyx + enemywidth AND Y < enemyy + enemyheight THEN
ship.enemy.collisio = 1
END IF
END IF

IF shipx2 > enemyx AND y2 > enemyy THEN
IF shipx2 < enemyx + enemywidth AND y2 < enemyy + enemyheight THEN
ship.enemy.collisio = 1
END IF
END IF

IF shipx + 7 > enemyx AND Y + 16 > enemyy THEN
IF shipx + 7 < enemyx + enemywidth AND Y + 16 < enemyy + enemyheight THEN
ship.enemy.collisio = 1
END IF
END IF

END SUB

SUB showgetreadyscreen (Text$)
CLS
restore.pal
LOCATE 12, 16: PRINT "GET READY"
LOCATE 15, 1: PRINT Text$
FOR farbe = 0 TO 63 STEP 1
f = f + 1: IF f > 63 THEN f = 63
PALETTE 15, 65536 * 0 + 256 * farbe + farbe
PALETTE 0, 65536 * f + 256 * 0 + 0
WaitVerticalRetrace 1
NEXT
HOLD .6
FOR farbe = 63 TO 0 STEP -1
f = f - 1: IF f < 0 THEN f = 0
PALETTE 15, 65536 * 0 + 256 * farbe + farbe
PALETTE 0, 65536 * f + 256 * 0 + 0
WaitVerticalRetrace 1
NEXT
restore.pal
Future.CLS 0
END SUB

REM $STATIC
SUB SHOWPAUSESCREEN (pl, mi, sec)
Future.CLS 0
LOCATE 2, 15: PRINT "P A U S E"
LOCATE 3, 15: PRINT "========="
FOR wpn = 1 TO 9
LOCATE 5 + INT(wpn), 2: PRINT wpn; "-"; INT(ammo(wpn) + .9)
NEXT
LOCATE 15, 2: PRINT "Missiles:"; mi
LOCATE 16, 2: PRINT "Powerlines:"; pl
LOCATE 18, 2: PRINT "Shields:"; lp; "/"; lpmax
LOCATE 19, 2: PRINT "Score:"; money!
LOCATE 21, 2: PRINT "Remain time:"; sec
HOLD .3
DO
IF GetKey(32) THEN
KBHoff
CLS : Future.CLS 0
DO
INPUT ">", cheat$
SELECT CASE LCASE$(cheat$)
CASE "debug on": debugmode = 1: PRINT "Debug aktiviert"
CASE "debug off": debugmode = 0: PRINT "Debug deaktiviert"
CASE "gimmegimmegimme": money! = money! + 100000
CASE "thislevelsucks"

CLOSE #1: newlevel: cheatexit = -1
CASE "", "exit", "end": cheatexit = -1
'...
CASE ELSE: PRINT "Unbekannter Befehl!"
END SELECT
LOOP UNTIL cheatexit
KBHon
END IF
LOOP UNTIL GetKey(25) OR cheatexit
HOLD .3
END SUB

SUB sixECK (centerx, centery, size, angle, ecken, Col)
DIM v(16), s(16), C(16)
FOR n = 1 TO ecken
v(n) = angle + ((PI / (ecken / 2)) * n)
NEXT
FOR n = 1 TO ecken
s(n) = SIN(v(n)) * size + centerx
C(n) = COS(v(n)) * size + centery
NEXT
FOR n = 1 TO ecken - 1
LINE (s(n), C(n))-(s(n + 1), C(n + 1)), Col
NEXT
LINE (s(ecken), C(ecken))-(s(1), C(1)), Col
END SUB

SUB Sort (a$(), low, high)
'
' Quick sorts given array of strings.
'
IF low < high THEN
  IF high - low = 1 THEN
    IF a$(low) > a$(high) THEN
      SWAP a$(low), a$(high)
    END IF
  ELSE
    RANDOMIZE TIMER
    RandIndex = INT(RND(1) * (high - low) + .5) + low
    SWAP a$(high), a$(RandIndex)
    Partition$ = a$(high)
    DO
      i = low: J = high
      DO WHILE (i < J) AND (a$(i) <= Partition$)
	i = i + 1
      LOOP
      DO WHILE (J > i) AND (a$(J) >= Partition$)
	J = J - 1
      LOOP
      IF i < J THEN
	SWAP a$(i), a$(J)
      END IF
    LOOP WHILE i < J
    SWAP a$(i), a$(high)
    IF (i - low) < (high - i) THEN
      Sort a$(), low, i - 1
      Sort a$(), i + 1, high
    ELSE
      Sort a$(), i + 1, high
      Sort a$(), low, i - 1
    END IF
  END IF
END IF
END SUB

SUB WaitVerticalRetrace (Num%)
FOR nix% = 1 TO Num%
WAIT &H3DA, 8, 8
WAIT &H3DA, 8
NEXT
END SUB

SUB WriDSP (byte)
DO: LOOP WHILE INP(CommAddr) AND &H80
OUT CommAddr, FIX(byte)
END SUB

