Saturday, February 1, 2020

Lab3 - Bouncing Graphic




Option I: Bouncing Graphic


1.    Create a simple graphic in a square that is 5x5 or 7x7 pixels in size. Use the colours available in the emulator's bitmapped display. The graphic could be a ball, a happy face, a logo, an emoji, or anything else (appropriate!) that you want to use.
2.    Encode that graphic in bytes using DCB (declare constant byte) instructions.
3.    Write code to make the graphic bounce around the screen, reflecting off the edges when it     hits. Note: for simplicity, it is OK if the object always bounces at 45-degree angles.
4.    Make the speed keyboard-adjustable (faster/slower) and perturb the object's path once in a   while.
      Challenge: randomize the starting position, and make the object bounce at angles other than 
                       45 degrees.


< Drawing >


Below code is for drawing shape.
output


define WIDTH     5 ; width of graphic
define HEIGHT    5 ; height of graphic

          lda #$25           ; create a pointer at $10
          sta $10            ;   which points to where
          lda #$02           ;   the graphic should be drawn
          sta $11

          lda #$00           ; number of rows we've drawn
          sta $12            ;   is stored in $12

          ldx #$00 ; index for data
          ldy #$00 ; index for screen column

 draw:   lda data,x ; set the xth value from the data
          sta ($10),y ; add y value to address $10
          inx
          iny
          cpy #WIDTH ; compare Y and width
          bne draw ; drawing until y = 5
  
          inc $12             ; increment row counter

          lda #HEIGHT      ; are we done yet?
          cmp $12       ; compare value of $12 and height. Draw until last row of data
         beq done

           lda $10  ;
           clc         ; clear carry
           adc #$20 ; add #20 to $10
           sta $10  ; store a value to $10
           lda $11 ; go to page
           adc #$00 ; add #00 to index
           sta $11   ; store a value to $11

           ldy #$00
           beq draw ;branch on equal

done: brk

data:
 dcb 00,03,03,03,00
 dcb 07,00,00,00,07
 dcb 07,00,00,00,07
 dcb 07,00,00,00,07
 dcb 00,03,03,03,00



<Moving>
below code is for moving the shape. However, Just move to 45 degree below.  It is not perfect yet.

 define WIDTH 5 ; width of graphic
 define HEIGHT 5 ; height of graphic

  lda #$00 ; create a pointer at $10
  sta $10 ;   which points to where
  lda #$02 ;   the graphic should be drawn
  sta $11
 
  lda #$00 ; number of rows we've drawn
  sta $12 ;   is stored in $12

  ldx #$00 ; index for data
  ldy #$00 ; index for screen column

<Add moving>


jsr clearScreen
;brk
jsr moveDown
jsr moveRight
;draw will modify our pointer, we need to backup and reset it later
lda $10
sta $14;backup
lda $11
sta $15
jsr draw
lda $14
sta $10;reset
lda $15
sta $11
;brk
jsr move

 draw:
lda data,x
  sta ($10),y
  inx
  iny
  cpy #WIDTH
;brk
  bne draw
 
  inc $12 ; increment row counter

  lda #HEIGHT ; are we done yet?
  cmp $12
  bne continue ; if not equal, go to continue
;beq done; ; comment out bne continue and beq done > movin

;lda $04;
;sta $11
ldx #$00; //reset index
ldy #$00;
sty $12
rts
 
continue:
;brk;
lda $10 ; load pointer
  clc
  adc #$20 ; add 32 to drop one row
  sta $10
  lda $11 ; carry to high byte if needed
  adc #$00
cmp #$06
beq done
  sta $11

  ldy #$00
  beq draw
;rts
 
 done: brk ; stop when finished
;rts

 data:
 dcb 00,03,03,03,00
 dcb 07,00,00,00,07
 dcb 07,00,00,00,07
 dcb 07,00,00,00,07
 dcb 00,03,03,03,00

moveRight:
sty $13
ldy $10
iny
sty $10
ldy $13
rts

moveDown:
lda $10
clc
adc #$20 ;add a line
sta $10
lda $11
adc #$00
cmp #$06
beq done
sta $11
rts

resetPage:
lda #$02
sta $11
rts

clearScreen: ;we can create another sub route called clearPrevious to avoid flashing screen
sta $22 ;backup registers
stx $23
sty $24

lda #$00 ;   create a pointer at $80
  sta $20 ;   which points to where
  lda #$02 ;   the graphic should be drawn
  sta $21
lda #$00 ;black color
ldy #$00 ;
mloop:
sta ($20), y
iny
bne mloop

ldx $21
inx
stx $21
cpx #$06
bne mloop

lda $22 ;revert registers
ldx $23
ldy $24
rts

    
From this code, the shape move to just one direction and when the shape meet the last page last line it stop.  I will try again and will post it

No comments:

Post a Comment