Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;; Darryl Sloan's finished Connect 4
- ; https://www.youtube.com/watch?v=uWqdk-1_NIg
- ; 5/7/2017
- ; https://gist.github.com/robsoft/96260761d30377be67ebf807d0a04397
- org 50000
- last_k equ 23560
- ld hl, udgs
- ld (23675), hl
- ld a, 2
- call 5633
- ; draw game title
- ld de, banner
- ld bc, eobanr-banner
- call 8252
- ; draw board
- bdraw ld de, board
- ld bc, eoboard-board
- call 8252
- ld hl, board+1
- ld a, (hl)
- inc a
- ld (hl), a
- cp 16 ; finish looping when lines 12 to 16 are drawn
- jr nz, bdraw
- ; draw player line
- ld de, player
- ld bc, eoplyr-player
- call 8252
- ; show player
- ploop ld hl, 22816
- ld bc, (ppos)
- add hl, bc
- ld a, (pcol)
- ld (hl), a
- ; invite player input
- ld hl, last_k
- ld a, (hl)
- cp 112
- jr z, pright
- cp 111
- jr z, pleft
- cp 32
- jr z, pfire
- cp 114
- jr z, newgame
- jr ploop
- ; restart game
- newgame ld hl, drop ; reset available drops for all 7 columns
- ld b, 7
- rstdrp ld (hl), 6 ; 6 means empty column (i.e. 6 drops remaining)
- inc hl ; move forward 1 byte within DROP until all 7 are reset
- djnz rstdrp
- ; clear board
- ld a, 6 ; A keeps track of rows
- ld b, 7 ; B keeps track of columns
- ld hl, 22860 ; addr 22860 is top-left attribute of board
- ld de, 25 ; 25 is the number of spaces from the end of one row to the beginning of the next
- empty ld (hl), 15 ; 15 is paper blue, ink white, i.e. an empty slot
- inc hl ; move forward one attribute
- djnz empty ; repeat until a row of seven slots is cleared
- ld b, 7 ; reset B in readiness for next row
- add hl, de ; skip to start of next row
- dec a ; subtract 1 from remaining rows
- cp 0 ; if all 6 rows are complete, end
- jp z, clrkey ; "
- jr empty ; if not, repeat
- ; player moves right
- pright ld a, (ppos)
- cp 18
- jr z, ploop
- ld hl, 22816
- ld bc, (ppos)
- add hl,bc
- ld (hl), 63
- ld a, (ppos)
- inc a
- ld (ppos), a
- jp clrkey
- ; player moves left
- pleft ld a, (ppos)
- cp 12
- jr z, ploop
- ld hl, 22816
- ld bc, (ppos)
- add hl, bc
- ld (hl), 63
- ld a, (ppos)
- dec a
- ld (ppos), a
- jp clrkey
- ; player fires
- pfire ld a, (ppos) ; get mathematical x position of player (i.e. x=12 is 1)
- sub 11 ; "
- ld hl, drop-1 ; point at mem loc prior to first column (because the loop incs)
- ld b, a ; use b to loop until correct column is reached
- cntr inc hl ; increase HL to scan across drop vars
- djnz cntr ; "
- ld a, (hl) ; load number of rows to descend into a
- ld (tdrop), a ; store A in TDROP (temporary drop)
- cp 0 ; if no spaces remain in column, jump back to player input loop
- jp z, clrkey ; "
- ld hl, 22816 ; erase player from screen
- ld bc, (ppos) ; "
- add hl, bc ; "
- ld (hl), 63 ; "
- ; counter drops
- ld a, (tdrop)
- desnd ld bc, 32 ; redraw player 1 row lower
- add hl, bc ; "
- ld d, a ; remember A temporarily in D
- ld a, (pcol+1)
- ld (hl), a ; "
- ld a, d ; put D back into A
- ld b, 5 ; create a brief delay between counter movements
- stall halt ; wait for an interrupt
- djnz stall ; loop
- ld (hl), 15 ; draw counter
- dec a ; A controls iterations of loop
- jr nz, desnd ; loop until descent finished
- ld a, (pcol+1) ; redraw counter in resting position
- ld (hl), a ; "
- ; decrement remaining slots by 1
- ld a, (ppos) ; get mathematical x position of player (i.e. x=12 is 1)
- sub 11 ; "
- ld hl, drop-1 ; point at mem loc prior to first column (because the loop incs)
- ld b, a ; use b to loop until correct column is reached
- cntr2 inc hl ; increase HL to scan across drop vars
- djnz cntr2 ; "
- ld a, (tdrop) ; subtract one from slots remaining in selected column
- dec a ; "
- ld (tdrop), a ; "
- ld (hl), a ; "
- ; change colour for other player
- ld hl, ppos ; set player position back to centre
- ld (hl), 15 ; "
- ld a, (pcol) ; add 4 to colour (changing red to yellow), for above board
- add a, 4 ; "
- ld (pcol), a ; "
- ld a, (pcol+1) ; add 4 to colour (changing red to yellow), for within board
- add a, 4 ; "
- ld (pcol+1), a ; "
- cp 18 ; if colour is invalid, jump to BADCOL
- jr z, badcol ; "
- jr clrkey ; clear keyboard
- badcol ld a, 58 ; change invalid colour to red
- ld (pcol), a ; "
- ld a, 10 ; "
- ld (pcol+1), a ; "
- jr clrkey ; clear keyboard
- ; clear last keypress and loop back for new input
- clrkey ld hl, last_k
- ld (hl), 0
- jp ploop
- ; variables
- ppos defb 15, 0
- pcol defb 58, 10
- drop defb 6, 6, 6, 6, 6, 6, 6
- tdrop defb 0
- banner defb 22, 3, 11, "CONNECT 4"
- eobanr equ $
- ; board setup
- board defb 22, 10, 12
- defb 16, 7, 17, 1, 144, 144, 144, 144, 144, 144, 144
- eoboard equ $
- player defb 22, 9, 12
- defb 16, 7, 17, 7, 144, 144, 144, 144, 144, 144, 144
- eoplyr equ $
- ; graphics setup
- udgs defb 0, 24, 60, 126, 126, 60, 24, 0
- end 50000
Advertisement