#include "usgard.h"

PlayerCoords = TEXT_MEM    ;A word to hold the x and y coords of the player
BugCoords    = TEXT_MEM+2  ;Another word to hold x+y coords of the bug
MoveCounter  = TEXT_MEM+4  ;How long until a move
CurMove      = TEXT_MEM+5  ;What move we are on


   .org 0                  ;Required- Say that this is the beginning of the prog.
   .db  "Bug Catcher",0    ;Title


   ld   hl,$0101           ;Put $0101 as the bug coords.
   ld   (BugCoords),hl
   ld   hl,$0A07           ;Put $0A07 as the players coords.
   ld   (PlayerCoords),hl
   call CLEARLCD           ;Clear the screen.
   ld   hl,&TitleScreen    ;point hl to the title screen text
   ld   de,$0000
   ld   ($800C),de         ;Put the cursor at 0,0
   call D_ZT_STR           ;Display The title screen
   ld   c,5                ;Our starting rate
ChangeRate:
   ld   de,$1002
   ld   ($800C),de         ;Put the cursor at 17,2
   ld   a,c                ;get a out of c
   add  a,'0'              ;make it a printable character
   call TR_CHARPUT         ;Print the char on the screen
   call GET_KEY            ;Get a key.
   cp   $37                ;See if the key is EXIT 
   ret  z                  ;Exit if it is 
   cp   9                  ;See if it is enter
   jr   z,ToGame           ;Start the game if it is
   cp   K_UP               ;See if the key is up
   jr   z,incit            ;increment the rate
   cp   K_DOWN             ;Check if the key is down
   jr   z,decit            ;dec the rate if it is.
   jr   ChangeRate         ;Not one of the keys; loop arond.
incit:
   ld   a,c                
   cp   9                  ;If c is 9, go back- its our max
   jr   z,ChangeRate
   inc  c                  
   jr   ChangeRate
decit:
   ld   a,c
   cp   1                  ;If c is 1, its our min so go back
   jr   z,ChangeRate
   dec  c
   jr   ChangeRate
ToGame:
   ld   a,c
   ld   (MoveCounter),a    ;Save the rate
   call &DrawScreen        ;Draw!
Game_Loop:                 ;The game loop.  This is the game's main loop.
   call GET_KEY            ;Get a key
   ld   hl,PlayerCoords    ;Point hl to the player coords, will be useful later
   cp   K_UP               ;\
   jr   z,Up               ; \ 
   cp   K_DOWN             ;  \
   jr   z,Down             ;  You should know 
   cp   K_LEFT             ;  what all these do.
   jr   z,Left             ;  /
   cp   K_RIGHT            ; /
   jr   z,Right            ;/
   cp   $37                ;$37 is the scancode for exit.
   ret  z
   jr   Game_Loop
P_Moved:                   ;Game will come here after a move has been made.
   ld   hl,(MoveCounter)   ;l is the move counter; h is the curmove
   ld   a,h
   inc  a
   cp   l                  ;See if it is time to move the bug
   call z,&MoveBug         ;Move now if time
   ld   (CurMove),a        ;save a for next time.
   call &DrawScreen        ;Draw!
   ld   hl,(PlayerCoords)  ;get the player coords in hl
   ld   de,(BugCoords)     ;get the bug coords in de
   call CP_HL_DE           ;only if cp  hl,de was a legal command..
   jr   nz,Game_Loop        ;Oh well, this rom call does the same exact thing
   call CLEARLCD
   set  3,(IY+05)          ;Display white on black
   ld   hl,&Win            ;point hl the the win text
   ld   de,0
   ld   ($800C),de         ;cursor=(0,0)
   call D_ZT_STR           ;Display you win
   res 3,(IY+05)           ;Display black on white (normal)   
wait:
   call GET_KEY            ;\
   cp   K_EXIT             ; Wait for exit
   jr   nz,wait            ;/
   ret                     ;Back to Usgard for us.

Up:
   ld   a,(hl)             ;I told you pointg hl earlier would be useful
   cp   0                  ;Find out if the val hl points to is 0.
   jr   z,Game_Loop        ;Go back if it is.
   dec  (hl)               ;dec the mem hl points at
   jr   P_Moved
Down:
   ld   a,(hl)
   cp   7                  ;7- bottom row
   jr   z,Game_Loop
   inc  (hl)
   jr   P_Moved
Left:
   inc  hl                 ;inc hl so it points to the x coord
   ld   a,(hl)
   cp   0
   jr   z,Game_Loop
   dec  (hl)
   jr   P_Moved
Right:
   inc  hl
   ld   a,(hl)
   cp   20                  ;20 is the furthest right we can go.
   jr   z,Game_Loop
   inc  (hl)
   jr   P_Moved

DrawScreen:
   call CLEARLCD            ;Clear the screen
   ld   hl,(BugCoords)      ;
   ld   ($800C),hl          ;Copy the bug coords to the cursor location
   ld   a,'*'               
   call TR_CHARPUT          ;put a "*" there.
   ld   a,'#'
   ld   hl,(PlayerCoords)   ;Copy the bug coords to the cursor loc
   ld   ($800C),hl          ;
   call TR_CHARPUT          ;Display a "#" there.
   ret                      ;Go back to where we came

MoveBug:
   call RANDOM              ;This is an Usgard function
   and  %111                ;You don't know this yet.
   ld   l,a
   push hl                  ;push hl away; random destroys it.
G_I_B:
   call RANDOM
   and  %11111
   cp   20
   jr   nc,G_I_B
   pop  hl                  ;Get hl back.
   ld   h,a
   ld   (BugCoords),hl
   ld   a,0
   ret                      ;I will walk you through this section next lesson
      

;All the DB's for the game

TitleScreen:
        ;123456789012345678901
   .db  "     BugCatcher!     "
   .db  "Use up/down arrows to"
   .db  "Change 'Speed':",0

Win:
   .db  "      YOU WIN!!      ",0

.end                        ;Required by TASM

    Source: geocities.com/tutorman_2000/85

               ( geocities.com/tutorman_2000)