# Rear Gunner XL – Part 5 – BOOM!

The final six lines of the game – 150 to 200 – involve reacting to the player pressing the fire button – and checking to see if they’ve hit the enemy or not.  If they haven’t then the play loop continues as before, allowing the player to try again.  If they have hit the enemy then an ‘explosion’ graphic is displayed, and the game starts over.

```; 150 IF D<>A THEN GOTO 50
; 160 IF C=B THEN GOTO 170
; 170 PRINT AT D-1,C;”(graphic Y;SPACE;graphic T)”
; 180 PRINT AT D,C;”(SPACE;inverse SPACE;SPACE)”
; 190 PRINT AT D+1,C;”(graphic T;SPACE;graphic Y)”
; 200 RUN```

So line 150 detects if the player is on the same row as the enemy, and if not jumps back into the main game loop.  That can be done with a straightforward comparison, using the label for line 50 which we added earlier:

```ld a, (VARIABLE_D)
ld b, a
ld a, (VARIABLE_A)
cp b
jp nz, LINE_50```

Line 160 does the same for the column… only, erm… it doesn’t.  That’s wrong…

It actually checks to see if the columns are the same, and if so goes to line 170, but if they are not the same then the flow will continue onto the next line, which is line 170 again.  This means that the column of the player makes no difference whatsoever to the flow of the game – your just need to fire on the same row.  I think we need to fix that – it should be:

`160 IF C<>B THEN GOTO 50`

which would make our code.

```ld a, (VARIABLE_C)
ld b, a
ld a, (VARIABLE_B)
cp b
jp nz, LINE_50```

Then finally, we draw an explosion around the enemy, using the same method as we did to draw the player & enemy:

```; 170 PRINT AT D-1,C;”(graphic Y;SPACE;graphic T)”
ld a, (VARIABLE_D)
dec a
ld d, a
ld a, (VARIABLE_C)
ld e, a
ld (hl), 134
inc hl
ld (hl), __
inc hl
ld (hl), 6

; 180 PRINT AT D,C;”(SPACE;inverse SPACE;SPACE)”
ld a, (VARIABLE_D)
ld d, a
ld a, (VARIABLE_C)
ld e, a
ld (hl), __
inc hl
ld (hl), 128
inc hl
ld (hl), __

; 190 PRINT AT D+1,C;”(graphic T;SPACE;graphic Y)”
ld a, (VARIABLE_D)
inc a
ld d, a
ld a, (VARIABLE_C)
ld e, a
ld (hl), 6
inc hl
ld (hl), __
inc hl
ld (hl), 134```

And finally we just need to run the game again, which is effectively just running from line 5 again.  This is best done by placing a label at the start of our code – i.e. line 5 – and jumping to it.

This makes the complete code as follows:

```; 5 PAUSE 50
LINE_5:
ld hl, 50
call FUNC_WAITFORFRAMES

; 10 LET A=5
ld a, 5
ld (VARIABLE_A), a

; 20 LET B=7
ld a, 7
ld (VARIABLE_B), a

; 30 LET C=INT (RND*26)
ld b, 26
call FUNC_GETRANDOM_RANGE
ld (VARIABLE_C), a

; 40 LET D=INT (RND*17)
ld b, 17
call FUNC_GETRANDOM_RANGE
ld (VARIABLE_D), a

; 50 PRINT AT D,C;”(graphic G)”
LINE_50:
ld a, (VARIABLE_D)
ld d, a
ld a, (VARIABLE_C)
ld e, a
ld (hl), 137

; 60 PRINT AT A,B;” + “
ld a, (VARIABLE_A)
ld d, a
ld a, (VARIABLE_B)
ld e, a
ld (hl), __
inc hl
inc hl
ld (hl), __

; 70 PRINT AT A-1,B;”   “
ld a, (VARIABLE_A)
dec a
ld d, a
ld a, (VARIABLE_B)
ld e, a
ld (hl), __
inc hl
ld (hl), __
inc hl
ld (hl), __

; 80 PRINT AT A+1,B;”   “
ld a, (VARIABLE_A)
inc a
ld d, a
ld a, (VARIABLE_B)
ld e, a
ld (hl), __
inc hl
ld (hl), __
inc hl
ld (hl), __

; 90 IF INKEY\$=“5” THEN LET B=B-1
ld a, \$F7
in a, (\$FE)
bit 4, a
jp nz, LINE_90_KEYNOTHELD
ld hl, VARIABLE_B
dec (hl)
LINE_90_KEYNOTHELD:

; 100 IF INKEY\$=“8” THEN LET B=B+1
ld a, \$EF
in a, (\$FE)
bit 2, a
jp nz, LINE_100_KEYNOTHELD
ld hl, VARIABLE_B
inc (hl)
LINE_100_KEYNOTHELD:

; 110 IF INKEY\$=“7” THEN LET A=A-1
ld a, \$EF
in a, (\$FE)
bit 3, a
jp nz, LINE_110_KEYNOTHELD
ld hl, VARIABLE_A
dec (hl)
LINE_110_KEYNOTHELD:

; 120 IF INKEY\$=“6” THEN LET A=A+1
ld a, \$EF
in a, (\$FE)
bit 4, a
jp nz, LINE_120_KEYNOTHELD
ld hl, VARIABLE_A
inc (hl)
LINE_120_KEYNOTHELD:

; 130 IF INKEY\$=“0” THEN GOTO 150
ld a, \$EF
in a, (\$FE)
bit 0, a
jp z, LINE_150:

; 140 GOTO 50
jp LINE_50

; 150 IF D<>A THEN GOTO 50
LINE_150:
ld a, (VARIABLE_D)
ld b, a
ld a, (VARIABLE_A)
cp b
jp nz, LINE_50

; 160 IF C<>B THEN GOTO 50
ld a, (VARIABLE_C)
ld b, a
ld a, (VARIABLE_B)
cp b
jp nz, LINE_50

; 170 PRINT AT D-1,C;”(graphic Y;SPACE;graphic T)”
ld a, (VARIABLE_D)
dec a
ld d, a
ld a, (VARIABLE_C)
ld e, a
ld (hl), 134
inc hl
ld (hl), __
inc hl
ld (hl), 6

; 180 PRINT AT D,C;”(SPACE;inverse SPACE;SPACE)”
ld a, (VARIABLE_D)
ld d, a
ld a, (VARIABLE_C)
ld e, a
ld (hl), __
inc hl
ld (hl), 128
inc hl
ld (hl), __

; 190 PRINT AT D+1,C;”(graphic T;SPACE;graphic Y)”
ld a, (VARIABLE_D)
inc a
ld d, a
ld a, (VARIABLE_C)
ld e, a
ld (hl), 6
inc hl
ld (hl), __
inc hl
ld (hl), 134

; 200 RUN
jp LINE_5

; Wait for HL frames
FUNC_WAITFORFRAMES
; Load the system variable FRAMES with the number to wait for
; Remember to keep bit 15 high though
set 7, h
ld (FRAMES), hl
FUNC_WAITFORFRAMES_LOOP
ld hl, (FRAMES)
; Keep looping until it's zero (ignoring bit 15)
ld a, h
and %01111111
or l
jp nz, FUNC_WAITFORFRAMES_LOOP
; Return
ret

; Get a random 8-bit number
FUNC_GETRANDOM
; Calculate a new 'random' number from the last one and r-register
ld a, r
ld hl, LMATH_GR_SEED
rrca
ld (hl), a
ret
LMATH_GR_SEED
defb 91

; Get a random 8-bit number within a range
; Call with B as the maximum value + 1
FUNC_GETRANDOM_RANGE
call FUNC_GETRANDOM
FUNC_GETRANDOM_RANGE_LOOP:
cp b
ret c
sub b
jp FUNC_GETRANDOM_RANGE_LOOP

VARIABLE_A
defb 0
VARIABLE_B
defb 0
VARIABLE_C
defb 0
VARIABLE_D
defb 0```

And that’s everything converted!  You can now enjoy playing the 100% machine code version of the game 🙂