pong-more-or-less/gameloop.asm

183 lines
2.8 KiB
NASM

;;; -*- Mode: asm; indent-tabs-mode: t; tab-width: 8 -*-
;;; Main gameloop routine.
game:
lda p1score
ldx #4
jsr printbyte
lda p2score
ldx #(40 - 6)
jsr printbyte
lda col
beq @nocol
jsr handle_collision
@nocol:
;; Player input:
;; F1 (restart)
ldx #%11111110
ldy #%00010000
jsr keydown
bne @norestart
jmp PROGSTART
@norestart:
;; Q (player 1 up)
ldx #%01111111
ldy #%01000000
jsr keydown
bne @skip1
dec p1y
dec p1y
;; A (player 1 down)
@skip1:
ldx #%11111101
ldy #%00000100
jsr keydown
bne @skip2
inc p1y
inc p1y
@skip2:
;; O (player 2 up)
ldx #%11101111
ldy #%01000000
jsr keydown
bne @skip3
dec p2y
dec p2y
@skip3:
;; L (player 2 down)
ldx #%11011111
ldy #%00000100
jsr keydown
bne @skip4
inc p2y
inc p2y
@skip4:
;; Clamp paddles to stay on-screen.
lda #$32 ;player 1 top
cmp p1y
bcc @dontclamp1
sta p1y
@dontclamp1:
lda #($fa - 42) ;player 1 bottom
cmp p1y
bcs @dontclamp2
sta p1y
@dontclamp2:
lda #$32 ;player 2 top
cmp p2y
bcc @dontclamp3
sta p2y
@dontclamp3:
lda #($fa - 42) ;player 2 bottom
cmp p2y
bcs @dontclamp4
sta p2y
@dontclamp4:
update_paddles:
lda p1y
sta SPRITE_0Y
lda p2y
sta SPRITE_1Y
ball_physics:
lda #BALLVERT
and balldata
beq @down ; 0 = down, 1 = up
txa
clc
dec ball_y
lda #BALLVSPEED
bit balldata
beq @slowballup
dec ball_y ;else we have a fast down ball
@slowballup:
;; Top bounce check
lda ball_y
cmp #$32
beq @topbouncestill
bcs @horizontal
@topbouncestill:
;; If it's <= 32 then we bounce off the top
jsr ball_bounce_vert
jmp @horizontal
@down:
inc ball_y
lda #BALLVSPEED
bit balldata
beq @slowballdown
inc ball_y
@slowballdown:
;; Bottom bounce check
lda ball_y
cmp #($fa - 8) ;$fa is bottom of the screen, - 8 for ball height
bcc @horizontal
jsr ball_bounce_vert
;; fallthrough to horizontal
@horizontal:
lda #BALLHORI
and balldata
beq @left ; 0 = left, 1 = right
;; Move ball right
jsr ball_x_inc ;first increment
lda #BALLHSPEED
bit balldata
beq @done
;; if bit BALLHSPEED is set, we have a horizontally fast ball
jsr ball_x_inc ;so we increment again
jmp @done
@left:
;; Move ball left
jsr ball_x_dec ;first decrement
lda #BALLHSPEED
bit balldata
beq @done
;; if bit BALLHSPEED is set, we have a horizontally fast ball
jsr ball_x_dec ;so we dec again
@done:
check_ball_goal:
;; Check to see if it's time to change score and reset the ball
lda #%00000100
bit SPRITE_X_MSB ;check if it's on the left or right side
bne @right
lda ball_x
cmp #$10
bcs @nope
inc p2score
lda p2score
jsr clampscore
sta p2score
jsr reset_ball
jsr highlightscore
jmp @nope
@right:
lda ball_x
cmp #$50
bcc @nope
inc p1score
lda p1score
jsr clampscore
sta p1score
jsr reset_ball
jsr highlightscore
@nope:
ball_sprite:
lda ball_x
sta SPRITE_2X
lda ball_y
sta SPRITE_2Y
;; MAIN GAME ROUTINE ENDS HERE, PUT SUBROUTINES AFTER
rts