floppyboot

bootsector Flappy Bird clone for x86
git clone git://squid-tech.com/floppyboot.git
Log | Files | Refs | README

commit 71de3d4eb674aa43d8f3481fcf2e5e575f901107
parent 0a38ad7d87abb32fa5f84ec8773b2c0468b825a0
Author: Josh Moore <jxm5210@rit.edu>
Date:   Sun, 12 Jan 2020 19:29:26 -0500

Add pipes and crashing

Diffstat:
Mfloppyboot.asm | 256++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
1 file changed, 241 insertions(+), 15 deletions(-)

diff --git a/floppyboot.asm b/floppyboot.asm @@ -2,17 +2,25 @@ bits 16 org 0x7C00 %define BG_COLOR 0x01 ; blue -%define PIPE_SPACING 0x0A ; distance between pipes %define JUMP_KEY 0x48 ; up arrow - -%define MAX_FALL 1 ; maximum rate of fall +%define MAX_FALL 1 ; maximum rate of fall %define JUMP_VEL -1 ; jump speed %define START_ROW 12 %define START_COL 3 + +; pipe properties +; each pipe is represented as 2 bytes in memory +; the first byte is the column position of the pipe +; the second byte is the row of the first space in the pipe's "hole" +%define NUM_PIPES 0x02 ; number of pipes to use +%define PIPE_SPACING 0x14 ; distance between pipes +%define HOLE_SIZE 0x05 ; size of hole in pipe + + boot: ; set video mode to 40x25 16-color MOV AH, 0x00 @@ -30,6 +38,39 @@ boot: MOV CH, 0x3F INT 0x10 + ; initialize bird position + MOV AL, START_ROW + MOV [birdy], AL + MOV AL, START_COL + MOV [birdx], AL + + ; initialize pipes + MOV BX, pipes + MOV DX, 0 + +.pipeinitloop: + ; offset the pipe to the right of the screen + MOV AL, DL + MOV CL, PIPE_SPACING + MUL CL + ADD AL, 40 + + MOV [BX], AL + ADD BX, 1 + + ; set the pipe's "hole" position + ;\TODO random hole positions + MOV AL, 4 + MOV [BX], AL + + ADD BX, 1 + ADD DX, 1 + + CMP DX, NUM_PIPES + JL boot.pipeinitloop + + + CALL draw_bird loop: @@ -54,6 +95,38 @@ loop: .process_game: ; update game state, draw graphics + ; move pipes + MOV BX, pipes + MOV DX, 0 + +.movepipe_loop: + MOV AH, [BX] + MOV AL, [BX+1] + MOV CL, AH + SUB CL, 1 + CALL move_pipe + + ; check for player-pipe collision + CMP CL, [birdx] + JNE loop.nocollide2 + CMP [birdy], AL + JGE loop.nocollide1 + JMP end_game + +.nocollide1: + PUSH AX + ADD AL, HOLE_SIZE + CMP [birdy], AL + POP AX + JL loop.nocollide2 + JMP end_game + +.nocollide2: + ADD DX, 1 + ADD BX, 2 + CMP DX, NUM_PIPES + JL loop.movepipe_loop + ; process bird fall MOV CH, [birdy] MOV CL, [birdx] @@ -91,9 +164,14 @@ loop: end_game: JMP end_game + + +; +; game functions +; + draw_bird: ; draw the bird at birdx and birdy - ; set AL to the character to draw for the bird ; returns nothing PUSHA @@ -144,42 +222,181 @@ move_bird: CMP CX, DX JE move_bird.return - ; draw bird at new location + ; draw blank at current location MOV AH, 0x02 MOV BH, 0x00 - MOV DH, CH - MOV DL, CL + MOV DH, [birdy] + MOV DL, [birdx] INT 0x10 PUSH CX MOV AH, 0x0A - MOV AL, '@' + MOV AL, ' ' MOV CX, 1 INT 0x10 - ; draw blank at current location + POP CX + + ; set the bird's game location to the new location + MOV [birdy], CH + MOV [birdx], CL + + ; draw the bird at the new location + CALL draw_bird + +.return: + + POPA + RET + + +move_pipe: + ; move a pipe from one location to another + ; BX - address of the pipe + ; CL - new column + PUSHA + + MOV AH, [BX] + MOV AL, [BX+1] + + ; check whether src and dest are the same pos + CMP AH, CL + JE move_pipe.return + + ; check whether src is outside the visible area + CMP AH, 0 + JL move_pipe.drawnew + CMP AH, 40 + JGE move_pipe.drawnew + + ; blank the column + MOV DL, AH + MOV DH, 0 + + PUSH BX +.blankloop: MOV AH, 0x02 - MOV DH, [birdy] - MOV DL, [birdx] + MOV BH, 0x00 INT 0x10 - MOV AH, 0x0A + ; draw blank + MOV AH, 0x09 MOV AL, ' ' + MOV BL, 11 + PUSH CX MOV CX, 1 INT 0x10 + POP CX + + ADD DH, 1 + + CMP DH, 25 + JL move_pipe.blankloop + + POP BX +.drawnew: + ; check whether the dest pos is less than 0 + ; if so, offset the pipe to the right to keep the cycle going + MOV AL, [BX+1] + CMP CL, 0 + JGE move_pipe.dodraw + + ; offset the column + MOV CL, 40 + + ; "randomize" the hole position + PUSH BX + PUSH CX + MOV BX, [hidx] + ADD BX, HLIST + MOV AL, [BX] + SUB BX, HLIST + ADD BX, 1 + CMP BX, 10 + JGE move_pipe.zrand + MOV [hidx], BX + JMP move_pipe.randomdone +.zrand: + MOV CX, 0 + MOV [hidx], CX + +.randomdone: POP CX + POP BX +.dodraw: + MOV AH, CL + CALL draw_pipe - ; set the bird's game location to the new location - MOV [birdy], CH - MOV [birdx], CL + ; save the new location + MOV [BX], AH + MOV [BX+1], AL .return: + POPA + RET + +draw_pipe: + ; draw a pipe + ; AH = column to draw the pipe in + ; AL = row where the pipe's hole starts + + PUSHA + + ; don't draw outside the visible region + cmp AH, 0 + JL draw_pipe.return + CMP AH, 40 + JGE draw_pipe.return + ; start of the pipe + MOV DH, 0x00 + MOV DL, AH + + ; CL = start row of hole + ; CH = first row after hole + MOV CL, AL + MOV CH, AL + ADD CH, HOLE_SIZE + +.pipeloop: + ; move cursor to pipe pos + MOV AH, 0x02 + MOV BH, 0x00 + INT 0x10 + + ; check whether this is a hole + CMP DH, CL + JL draw_pipe.drawsolid + CMP DH, CH + JGE draw_pipe.drawsolid + + ; if we make it here, we're in a hole + JMP draw_pipe.increment + + +.drawsolid: + MOV AH, 0x09 + MOV AL, ' ' + MOV BH, 0x00 + MOV BL, 0xAA + PUSH CX + MOV CX, 1 + INT 0x10 + POP CX + +.increment: + ADD DH, 1 + CMP DH, 25 + JL draw_pipe.pipeloop + +.return: POPA RET + + ; game data ; bird position birdx: db START_COL @@ -188,5 +405,14 @@ birdy: db START_ROW ; bird vertical velocity birdvy: db 0 +; pipes - 2 bytes for each pipe +pipes: times NUM_PIPES*2 db 0 + +; list of pipe positions +hidx: db 0 + +HLIST: +db 13, 9, 3, 8, 10, 4, 14, 2, 1, 14, 0 + times 510 - ($-$$) db 0 ; pad with zeroes to 510 bytes dw 0xAA55 ; bootloader magic