+ ld hl,GRAPH_MEM ;move from (hl) = top left
+ ld (hl),$00 ;first pixel will be copied all over the screen
+ ld de,GRAPH_MEM+1 ;(de) = next pixel, thus clearing whole screen
+ ld bc,896 ;loop 896 times = (128/8) * (64-8 for scorebar)
+ ldir ;clear!
+
+ ld a,(timer)
+ and %11
+ jr z,movestarsdone ;don't move stars once every 4 frames
+
+ cal movestars1 ;move the stars on the FRONT layer
+ cal movestars2 ;move the distant stars
+
+movestarsdone:
+ ld a,(stars1) ;star positions (the missing byte...)
+ ld b,nrstars1 ;how many stars? now we know.
+ ld hl,starx1 ;points to the position of the stars
+ cal DisplayStars ;display front layer stars
+
+ ld a,(stars2) ;weren't you paying attention five lines ago?
+ ld b,nrstars2 ;that many?! whow!
+ ld hl,starx2 ;and there they are
+ cal DisplayStars ;use the same procedure to display back layer
+
+ ld a,(level_info) ;level info
+ and %00000110 ;isolate ground&ceiling
+ jr z,game_stuff ;both non-present
+ and %00000010 ;bit representing the presence of any ceiling
+ cal nz,Handle_ceiling ;scroll the ceiling (if any)
+ cal Handle_ground ;scroll the ground
+
+game_stuff:
+ ld a,(your_occ) ;are you 100% OK?
+ or a ;a=0??
+ jr nz,_gamestuff1 ;then don't check for movements/fires/...
+
+ ld a,(level_info) ;the same level info
+ and %00000110 ;isolate ground&ceiling again
+ jr z,check_keys ;no ceiling nor ground
+ and %00000010 ;this bit will tell us if there is a ceiling
+ cal nz,CheckCeiling ;if there is, check it
+ cal CheckGround ;check for collision with the ground
+
+check_keys:
+ ld a,%10111111 ;function keys (MORE,EXIT,2ND,F1,F2,F3,F4,F5)
+ out (1),a ;ask for them
+ nop \ nop ;delay 8 clocks
+ in a,(1) ;get zem!
+
+check_exitkey:
+ bit 6,a ;test bit 6 = exit-key = EXIT
+ jp z,game_over_nopop ;<exit> pressed, so be it
+check_morekey: ;another unused label... poor compiler
+ bit 7,a ;test bit 7 = more-key = PAUSE
+ cal z,Pause ;yes, go to pause
+
+check_firekey:
+ bit 5,a ;test bit 5 = 2nd-key = FIRE
+ ld hl,check_selkey ;where to continue after executing Fire_bullet
+ psh hl ;push hl on stack (instead of cal Fire_bullet)
+ jp z,Fire_bullet ;fire smtn (bulletstorplasermultiples+stuff..)
+ pop hl ;no cal to Fire_bullet made, so pop stack
+ xor a ;no:
+ ld (just_fired),a ;reset just_fired
+
+check_selkey:
+ ld a,%01011111 ;look at first column of keys (ALPHA to STO)
+ out (1),a ;gimme
+ nop \ nop ;what's taking you so long
+ in a,(1) ;at last... our precious keyzzz...
+
+ bit 6,a ;'bout the GRAPH key...
+ cal z,Teacher ;you didn't _press_ it, did you?!?
+
+ rla ;test bit7 so we know f ALPHA has been pressed
+ cal nc,select ;yeppy, select the currently selected upgrade
+
+ cal Enemies_hit ;check for collision with enemies
+ cal inc_weapdamage
+
+_gamestuff1:
+ cal Handle_Ship ;move you
+ cal Handle_bullets ;move your bullets
+ cal Handle_torp ;move your torpedo
+
+ cal Handle_enemies ;move enemies
+ cal Enemy_bullets ;move enemy bullets
+
+ cal Level_event ;insert enemies
+ cal Display_Screen ;display all
+ halt ;delay
+
+ jp game_main_loop ;LOOP ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+inc_weapdamage:
+ ld a,0
+weapincs =$-1
+ inc a
+ cp 31
+ ret nc ;return if increased 16 times or more already
+ ld (weapincs),a
+
+ ld b,1
+weapdamage =$-1
+ add a,b
+
+ ld (curweapdamage),a
+ ret
+
+;--------------------------- ground -----------------------------------------
+
+Handle_ground:
+ ld a,(timer)
+ and %111 ;once every 8 frames
+ jr nz,Display_ground ;otherwise skip the scroll
+ ld bc,15 ;scroll all 16 bytes minus one (teh new byte)
+ ld hl,groundpos+1 ;from..
+ ld de,groundpos ;to (one byte to the left)
+ ldir ;LoaDIncreaseRepeat = scroll!
+
+ ld a,(groundinfo) ;what kind of ground
+ dec a ;type 1:
+ jr z,ground_tunnel ;tunnel effect
+ jr ground_boring
+
+ground_tunnel:
+ ld a,(groundpos+14)
+ ld (groundpos+15),a
+ ld hl,spacespace
+
+ ld a,(RanPos)
+ ld b,a
+ bit 1,a
+ jr z,ground_previous
+ bit 2,a
+ jr z,gtunneldown
+gtunnelup:
+ ld a,(hl)
+ or a
+ jr z,ground_previous ;a>=0 (a=0 actually)
+ inc (hl)
+ ld a,(groundpos+15)
+ inc a
+ jr newground
+gtunneldown:
+ ld a,(groundpos+15)
+ dec a
+ jr z,ground_previous
+ dec (hl)
+ jr newground
+
+ground_previous:
+ ld a,(groundpos+14) ;type 1
+ jr newground
+ground_boring:
+ ld a,(groundpos) ;type 0
+newground:
+ ld (groundpos+15),a ;save new byte on the right
+ ld a,(hl)
+ cp -25
+ jr nc,Display_ground
+ ld a,b
+ and %1
+ ld b,0
+ jr nz,gtunnelup
+
+Display_ground:
+ ld b,16 ;screen width
+ ld de,groundpos-1 ;height of current byte (previous actually)
+ psh de ;use later
+ ld hl,GRAPH_MEM+(56*16)-1 ;screen position
+ psh hl
+
+groundloopright:
+ ld c,b ;push b for groundloopup
+ pop hl \ inc hl ;get screen position and go one right
+ pop de \ inc de ;get height info and set to the next byte
+ psh de \ psh hl ;save these for the next time
+ ld a,(de) ;height of current byte
+ ld b,a ;save in b
+
+ ld de,16 ;to substract to go one line up
+ ld a,%11111111 ;bitmask black
+ or a
+groundloopup:
+ ld (hl),a ;display black byte
+ sbc hl,de ;go up (sbc must be used for 16-bit sub)
+ dnz groundloopup ;and loop >groundpos< times
+
+ ld b,c ;pop b used by groundloopup
+ dnz groundloopright ;loop right for entire screen (16x)
+ pop hl \ pop hl ;restore stack
+ ret
+
+CheckGround: ;check for collision with the ground
+ ld a,(x)
+ srl a
+ srl a
+ srl a
+ inc a
+ ld l,a
+ ld h,0
+ ld de,groundpos
+ add hl,de
+ ld a,(y)
+ sub 57-7
+ neg
+ cp (hl)
+ ret nc
+ ld b,5
+ jp damage_you
+
+;--------------------------- ceiling ----------------------------------------
+
+Handle_ceiling:
+ ld a,(timer)
+ and %111 ;once every 8 frames
+ jr nz,Display_ceiling ;otherwise skip the scroll
+ ld bc,15 ;scroll all 15 bytes (16th is new position)
+ ld hl,ceilingpos+1 ;from..
+ ld de,ceilingpos ;to (one byte to the left)
+ ld a,(de) ;load byte on left (will be lost after scroll)
+ ldir ;LoaDIncreaseRepeat = scroll!
+
+ ld a,(groundinfo) ;what kind of ceiling
+ dec a ;type 1:
+ jr z,ceiling_tunnel ;tunnel effect
+ jr ceiling_boring
+
+ceiling_tunnel:
+ ld a,(ceilingpos+14)
+ ld (ceilingpos+15),a
+ ld hl,spacespace
+
+ ld a,(RanPos)
+ ld b,a
+ bit 4,a
+ jr z,ceiling_previous
+ bit 5,a
+ jr z,ctunnelup
+ctunneldown:
+ ld a,(hl)
+ or a
+ jr z,ceiling_previous
+ inc (hl)
+ ld a,(ceilingpos+15)
+ inc a
+ jr newceiling
+ctunnelup:
+ ld a,(ceilingpos+15)
+ dec a
+ jr z,ceiling_previous
+ dec (hl)
+ jr newceiling
+
+ceiling_previous:
+ ld a,(ceilingpos+14) ;type 1
+ jr newceiling
+ceiling_boring:
+ ld a,(ceilingpos) ;type 0
+newceiling:
+ ld (ceilingpos+15),a ;save the new byte
+ ld a,(hl)
+ cp -25
+ jr nc,Display_ceiling
+ ld a,b
+ and %1
+ ld b,0
+ jr nz,ctunneldown
+
+Display_ceiling:
+ ld b,16 ;screen width
+ ld de,ceilingpos-1 ;height of current byte
+ psh de ;use later
+ ld hl,GRAPH_MEM-17 ;screen position
+ psh hl
+
+ceilingloopright:
+ ld c,b ;push b for groundloopup
+ pop hl \ inc hl ;get screen position and go one right
+ pop de \ inc de ;get height info and set to the next byte
+ psh de \ psh hl ;save these for the next time
+ ld a,(de) ;height of current byte
+ ld b,a ;save in b
+
+ ld de,16 ;to substract to go one line up
+ ld a,%11111111 ;bitmask black
+ or a
+ceilingloopdown:
+ ld (hl),a ;display black byte
+ add hl,de ;go down
+ dnz ceilingloopdown ;and loop >groundpos< times
+
+ ld b,c ;pop b used by groundloopup
+ dnz ceilingloopright ;loop right for entire screen (16x)
+ pop hl \ pop hl ;restore stack
+ ret
+
+CheckCeiling: ;check for collision with the ground
+ ld a,(x) ;your x
+ srl a ;x/2
+ srl a ;x/4
+ srl a ;x/8 (current ceiling-byte)
+ inc a ;correction
+
+ ld l,a ;hl = a
+ ld h,0 ;"
+ ld de,ceilingpos ;first ceiling-byte
+ add hl,de ;current ceiling-byte
+ ld a,(y) ;your y-pos
+ inc a
+ cp (hl) ;compare with ceiling
+ ret nc ;carry if ceiling is above you
+ ld b,5
+ jp damage_you ;otherwise you don't wanna be in that ship
+
+;--------------------------- move stars -------------------------------------
+
+DisplayStars: ;inputs: hl=starx# a=stars# b=nrstars#
+ ld e,(hl)