Guest User

Untitled

a guest
Jun 24th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.       .inesprg 1   ; 1x 16KB PRG code
  3.       .ineschr 1   ; 1x  8KB CHR data
  4.       .inesmap 0   ; mapper 0 = NROM, no bank swapping
  5.       .inesmir 1   ; background mirroring
  6.      
  7.      
  8. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  9. ;;;;;;;VARIABLE DECLARATIONS;;;;;;;
  10. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  11.  
  12.       .rsset $0000  ;;start variables at ram location 0
  13.  
  14. gamestate       .rs 1  ; .rs 1 means reserve one byte of space
  15. ballx           .rs 1  ; ball horizontal position
  16. bally           .rs 1  ; ball vertical position
  17. ballup          .rs 1  ; 1 = ball moving up
  18. balldown        .rs 1  ; 1 = ball moving down
  19. ballleft        .rs 1  ; 1 = ball moving left
  20. ballright       .rs 1  ; 1 = ball moving right
  21. ballspeedx      .rs 1  ; ball horizontal speed per frame
  22. ballspeedy      .rs 1  ; ball vertical speed per frame
  23. paddle1ytop     .rs 1  ; player 1 paddle top vertical position
  24. paddle2ytop     .rs 1  ; player 2 paddle top vertical position
  25. paddle1ybot     .rs 1  ; player 1 paddle bottom vertical position
  26. paddle2ybot     .rs 1  ; player 2 paddle bottom vertical position
  27. paddle1speed    .rs 1  ; player 1 paddle speed
  28. paddle2speed    .rs 1  ; player 2 paddle speed
  29. buttons1        .rs 1  ; player 1 gamepad buttons, one bit per button
  30. buttons2        .rs 1  ; player 2 gamepad buttons, one bit per button
  31. score1          .rs 1  ; player 1 score, 0-15
  32. score2          .rs 1  ; player 2 score, 0-15
  33. score1Ones      .rs 1  ; player 1's score's ones digit
  34. score1Tens      .rs 1  ; player 1's score's tens digit
  35. score2Ones      .rs 1  ; player 2's score's ones digit
  36. score2Tens      .rs 1  ; player 2's score's tens digit
  37. AddrLow         .rs 1
  38. AddrHigh        .rs 1
  39. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  47. ;;;;;;;;CONSTANT DECLARATIONS;;;;
  48. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  49. STATETITLE     = $00  ; displaying title screen
  50. STATEPLAYING   = $01  ; move paddles/ball, check for collisions
  51. STATEGAMEOVER  = $02  ; displaying game over screen
  52.  
  53. RIGHTWALL      = $F4  ; when ball reaches one of these, do something
  54. TOPWALL        = $20
  55. BOTTOMWALL     = $E0
  56. LEFTWALL       = $04
  57.  
  58. PADDLE1X       = $08  ; horizontal position for paddles, doesn't move
  59. PADDLE2X       = $F0
  60. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  61.  
  62.      
  63.      
  64.      
  65.       .bank 0
  66.       .org $C000
  67.     RESET:
  68.       SEI          ; disable IRQs
  69.       CLD          ; disable decimal mode
  70.       LDX #$40
  71.       STX $4017    ; disable APU frame IRQ
  72.       LDX #$FF
  73.       TXS          ; Set up stack
  74.       INX          ; now X = 0
  75.       STX $2000    ; disable NMI
  76.       STX $2001    ; disable rendering
  77.       STX $4010    ; disable DMC IRQs
  78.      
  79.     vblankwait1:       ; First wait for vblank to make sure PPU is ready
  80.       BIT $2002
  81.       BPL vblankwait1
  82.      
  83.     clrmem:
  84.       LDA #$00
  85.       STA $0000, x
  86.       STA $0100, x
  87.       STA $0300, x
  88.       STA $0400, x
  89.       STA $0500, x
  90.       STA $0600, x
  91.       STA $0700, x
  92.       LDA #$FE
  93.       STA $0200, x
  94.       INX
  95.       BNE clrmem
  96.        
  97.     vblankwait2:      ; Second wait for vblank, PPU is ready after this
  98.       BIT $2002
  99.       BPL vblankwait2
  100.  
  101.  
  102.      
  103.      
  104.     ;;    LOAD PALETTES, BACKGROUND, ATTRIBUTES
  105.     LoadPalettes:
  106.       LDA $2002             ; read PPU status to reset the high/low latch
  107.       LDA #$3F
  108.       STA $2006             ; write the high byte of $3F00 address
  109.       LDA #$00
  110.       STA $2006             ; write the low byte of $3F00 address
  111.       LDX #$00              ; start out at 0
  112.     LoadPalettesLoop:
  113.       LDA palette, x        ; load data from address (palette + the value in x)
  114.                               ; 1st time through loop it will load palette+0
  115.                               ; 2nd time through loop it will load palette+1
  116.                               ; 3rd time through loop it will load palette+2
  117.                               ; etc
  118.       STA $2007             ; write to PPU
  119.       INX                   ; X = X + 1
  120.       CPX #$20              ; Compare X to hex $20, decimal 32 - copying 32 bytes = 8 sprites
  121.       BNE LoadPalettesLoop  ; Branch to LoadPalettesLoop if compare was Not Equal to zero
  122.                             ; if compare was equal to 32, keep going down
  123.      
  124.     LoadTitleScreen:
  125.       LDA $2002                ; read PPU status to reset the high/low latch
  126.       LDA #$20
  127.       STA $2006                ; write the high byte of $2000 address
  128.       LDA #$00
  129.       STA $2006                ; write the low byte of $2000 address
  130.       LDA #low(title)
  131.       STA AddrLow
  132.       LDA #high(title)
  133.       STA AddrHigh
  134.      
  135.       LDX #$04              ; Loop X 4 times
  136.       LDY #$00              ; Loop Y 256 times
  137.     LoadTitleLoop:
  138.       LDA [AddrLow],y
  139.       STA $2007
  140.       INY
  141.       BNE LoadTitleLoop
  142.     ; Outer loop
  143.       INC AddrHigh           ; increment high byte of address backg to next 256 byte chunk
  144.       DEX                    ; one chunk done so X = X - 1.
  145.       BNE LoadTitleLoop   ; if X isn't zero, do again
  146.      
  147.     LoadTitleAttributes:
  148.       LDA $2002                ; read PPU status to reset the high/low latch
  149.       LDA #$23
  150.       STA $2006                ; write the high byte of $23C0 address
  151.       LDA #$C0
  152.       STA $2006                ; write the low byte of the $23C0 address
  153.       LDY #$00                 ; index set to 0
  154.     LoadTitleAttributeLoop:
  155.       LDA #$00                 ; Use 3rd set of colors from palette
  156.       STA $2007                ; write to PPU
  157.       INY
  158.       CPY #$40                 ; 64 attributes
  159.       BNE LoadTitleAttributeLoop
  160.  
  161.      
  162.  
  163.      
  164.      
  165.     ;;:Set starting game state
  166.       LDA #STATETITLE
  167.       STA gamestate
  168.      
  169.       ;;This is the PPU clean up section, so rendering the next frame starts properly.
  170.       LDA #%10010000   ; enable NMI, sprites from Pattern Table 0, background from Pattern Table 1
  171.       STA $2000
  172.       LDA #%00010000   ; enable sprites, enable background, no clipping on left side
  173.       STA $2001
  174.  
  175.  
  176.      
  177.  
  178. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  179. ;;;;;;;;;MAIN GAME LOGIC;;;;;;;;;;;;;;;;
  180. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  181.     MainLoop:
  182.     CheckTitle:
  183.       LDA gamestate
  184.       CMP #STATETITLE
  185.       BNE CheckPlaying
  186.       JSR ReadController1
  187.       LDA buttons1
  188.       AND #%00010000
  189.       BEQ CheckPlaying
  190.       JSR InitializeGame
  191.     CheckPlaying:
  192.       LDA gamestate
  193.       CMP #STATEPLAYING
  194.       BNE CheckOver
  195.       JSR ReadController1  ;;get the current button data for player 1
  196.       JSR ReadController2  ;;get the current button data for player 2
  197.       JSR UpdateSprites
  198.       JSR DrawScore
  199.       JSR MoveBall
  200.       JSR MovePaddles
  201.       JSR CheckCollisions
  202.     CheckOver:
  203.       LDA gamestate
  204.       CMP #STATEGAMEOVER
  205.       BNE MainLoop
  206.       JMP MainLoop     ;jump back to MainLoop, infinite loop, waiting for NMI
  207. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  208.      
  209.      
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218. ;;;;;;;;;;
  219. ;;NMI;;;;;
  220. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;    
  221.     NMI:
  222.       LDA #$00
  223.       STA $2003       ; set the low byte (00) of the RAM address
  224.       LDA #$02
  225.       STA $4014       ; set the high byte (02) of the RAM address, start the transfer
  226.      
  227.       LDA #$00        ;;tell the ppu there is no background scrolling
  228.       STA $2005
  229.       STA $2005
  230.  
  231.       RTI             ; return from interrupt
  232. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  233.      
  234.      
  235.      
  236.  
  237.      
  238. ;;;;;;;;;;;;;;;;;;;;;
  239. ;;;;;SUBROUTINES;;;;;
  240. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  241.    
  242.    
  243. ;***************
  244. ;INITIALIZE GAME
  245. ;******************************************************************
  246.     InitializeGame:
  247.      
  248.       LDA #$00
  249.       STA $2000
  250.       STA $2001
  251.  
  252.       ;;;;;;;;LOAD ALL BACKGROUND INFO FOR GAME PLAYING STATE
  253.     LoadBackground:
  254.       LDA $2002                ; read PPU status to reset the high/low latch
  255.       LDA #$20
  256.       STA $2006                ; write the high byte of $2000 address
  257.       LDA #$00
  258.       STA $2006                ; write the low byte of $2000 address
  259.       LDX #$00                 ; indices set to 0
  260.       LDY #$00                 ; X = column counter, Y = row counter
  261.     LoadBackgroundLoop:
  262.       LDA background
  263.       STA $2007                ; write to PPU
  264.       INX
  265.       CPX #$20                 ; Screen is 32 tiles wide, so we're loading screen row by row
  266.       BNE LoadBackgroundLoop   ; If X =/= 32, keep loading tiles in each column until we're done with row
  267.       INY                      ; Once row is completed, increase row count
  268.       LDX #$00                 ; Reset column count
  269.       CPY #$1E                 ; Screen is 30 tiles tall, so we're checking to see if we've loaded the last row
  270.       BNE LoadBackgroundLoop   ; If Y =/= 30, start loading new row
  271.      
  272.      
  273.     LoadAttributes:
  274.       LDA $2002                ; read PPU status to reset the high/low latch
  275.       LDA #$23
  276.       STA $2006                ; write the high byte of $23C0 address
  277.       LDA #$C0
  278.       STA $2006                ; write the low byte of the $23C0 address
  279.       LDY #$00                 ; index set to 0
  280.     LoadAttributeLoop:
  281.       LDA #$00                ; Use 1st set of colors from palette
  282.       STA $2007                ; write to PPU
  283.       INY
  284.       CPY #$40                 ; 64 attributes
  285.       BNE LoadAttributeLoop
  286.      
  287.       ;;;INITIALIZATION
  288.       ;;BALL STATS
  289.       LDA #$01                 ; True
  290.       STA balldown             ; Here we set direction components of ball vector
  291.       STA ballright
  292.       LDA #$00                 ; False
  293.       STA ballup
  294.       STA ballleft
  295.      
  296.       LDA #$78                 ; Vertical center of screen
  297.       STA bally
  298.      
  299.       LDA #$80                 ; Horizontal center of screen
  300.       STA ballx
  301.      
  302.       LDA #$01                 ; Ball will move 1 pixel at a time
  303.       STA ballspeedx
  304.       STA ballspeedy
  305.      
  306.       LDA #$75                 ; Ball will look like top of flagpole from SMB
  307.       STA $0201
  308.      
  309.       LDA #$01                 ; Ball will use first set of 4 colors from sprite palette
  310.       STA $0202
  311.      
  312.      
  313.     ;;;PADDLES Y-COORDINATES
  314.       LDA #$70                 ; Align top of paddle near center
  315.       STA paddle1ytop
  316.       STA paddle2ytop
  317.       LDA #$90                 ; Align bottom of paddle near center
  318.       STA paddle1ybot
  319.       STA paddle2ybot
  320.      
  321.     ;;;LOADING TILES FOR PADDLE GRAPHICS
  322.       LDX #$00
  323.     Load_Paddle_Tile:
  324.       LDA #$5B           ; In mario.chr this is one of the platform graphics from SMB
  325.       STA $0205, x       ; $0205 is paddle 1's top tile y-coordinate
  326.       STA $0215, x       ; $0215 is paddle 2's top tile y-coordinate
  327.       INX
  328.       INX
  329.       INX                ; Increasing X by 4 shifts over 4 bytes in memory to reach next y-coordinate for next paddle tile
  330.       INX
  331.       CPX #$10           ; Once we hit 16, we've loaded 4 tiles (each paddle is 4 tiles high)
  332.       BNE Load_Paddle_Tile
  333.      
  334.     ;;;LOADING ATTRIBUTES FOR EACH PADDLE
  335.       LDX #$00
  336.     Load_Paddle_Attr:
  337.       LDA #$01            ; Use the first set of 4 colors from sprites
  338.       STA $0206, x        ; $0206 is paddle 1's top tile attribute
  339.       STA $0216, x        ; $0216 is paddle 2's top tile attribute
  340.       INX
  341.       INX
  342.       INX
  343.       INX
  344.       CPX #$10
  345.       BNE Load_Paddle_Attr
  346.      
  347.     ;;;LOADING X-COORDINATES FOR EACH PADDLE
  348.       LDX #$00
  349.     Load_Paddle_X:
  350.       LDA #PADDLE1X          ; Paddles can't move horizontally
  351.       STA $0207, x           ; $0207 is paddle 1's top tile x-coordinate
  352.       LDA #PADDLE2X
  353.       STA $0217, x           ; $0217 is paddle 2's top tile x-coordinate
  354.       INX
  355.       INX
  356.       INX
  357.       INX
  358.       CPX #$10
  359.       BNE Load_Paddle_X
  360.      
  361.     ;;;PADDLE SPEEDS
  362.     Load_Paddle_Speeds:
  363.       LDA #$01               ; Paddles can initially move 1 pixel per frame
  364.       STA paddle1speed
  365.       STA paddle2speed
  366.      
  367.     ;;Set scores
  368.       LDA #$00
  369.       STA score1Ones         ; Zero out placeholders (these numbers are drawn to screen)
  370.       STA score1Tens
  371.       STA score2Ones
  372.       STA score2Tens
  373.       STA score1             ; Zero out actual scores which get checked for win conditions
  374.       STA score2
  375.      
  376.       LDA #STATEPLAYING      ; Change state to playing
  377.       STA gamestate
  378.      
  379.       LDA #%10010000   ; enable NMI, sprites from Pattern Table 0, background from Pattern Table 1
  380.       STA $2000
  381.       LDA #%00010000   ; enable sprites, enable background, no clipping on left side
  382.       STA $2001
  383.      
  384.       RTS
  385. ;******************************************************************
  386.  
  387.  
  388.  
  389. ;***************
  390. ;UPDATE SPRITES
  391. ;******************************************************************
  392.     UpdateSprites:
  393.       LDA bally                      ; Update ball y-coordinate
  394.       STA $0200
  395.      
  396.       LDA ballx                       ; Update ball x-coordinate
  397.       STA $0203
  398.      
  399.      
  400.       LDX #$00                       ; Clear out indices
  401.       LDY #$00
  402.      
  403.     Load_Paddle_Y:
  404.       LDA paddle1ytop                ; Load all y-coordinates for paddle 1 relative to the top tile y-coordinate
  405.       CLC
  406.       ADC vertical_offsets, y
  407.       STA $0204, x
  408.      
  409.       LDA paddle2ytop                ; Load all y-coordinates for paddle 2 relative to the top tile y-coordinate
  410.       CLC
  411.       ADC vertical_offsets, y
  412.       STA $0214, x
  413.      
  414.       INX
  415.       INX
  416.       INX
  417.       INX
  418.      
  419.       INY
  420.       CPY #$04
  421.       BNE Load_Paddle_Y
  422.       RTS
  423. ;******************************************************************
  424.      
  425.  
  426. ;***************
  427. ;DRAW SCORE
  428. ;******************************************************************
  429.     DrawScore:
  430.       LDA $2002
  431.       LDA #$20
  432.       STA $2006
  433.       LDA #$20
  434.       STA $2006          ; start drawing player 1 score at PPU $2020
  435.      
  436.       LDA score1Tens      ; first digit
  437.       STA $2007          ; draw to background
  438.       LDA score1Ones      ; last digit
  439.       STA $2007
  440.      
  441.       LDA $2002          ; start drawing player 2 score at PPU $203E
  442.       LDA #$20
  443.       STA $2006
  444.       LDA #$3E
  445.       STA $2006
  446.      
  447.       LDA score2Tens
  448.       STA $2007
  449.       LDA score2Ones
  450.       STA $2007
  451.      
  452.       RTS
  453. ;******************************************************************
  454.  
  455.  
  456. ;***************
  457. ;INCREMENT SCORE 1
  458. ;******************************************************************
  459.     IncrementScore1:
  460.     Inc1Ones:
  461.       LDA score1Ones      ; load the lowest digit of the number
  462.       CLC
  463.       ADC #$01           ; add one
  464.       STA score1Ones
  465.       CMP #$0A           ; check if it overflowed, now equals 10
  466.       BNE Check1Score        ; if there was no overflow, all done
  467.     Inc1Tens:
  468.       LDA #$00
  469.       STA score1Ones      ; wrap digit to 0
  470.       LDA score1Tens      ; load the next digit
  471.       CLC
  472.       ADC #$01           ; add one, the carry from previous digit
  473.       STA score1Tens
  474.      
  475.     Check1Score:
  476.       LDA score1         ; This variable will be used to check win conditions
  477.       CLC
  478.       ADC #$01
  479.       STA score1
  480.       CMP #$10           ; Compare to 16.  If equal to 16, we have a winner
  481.       BNE Inc1Done
  482.       LDA STATEGAMEOVER
  483.       STA gamestate
  484.     Inc1Done:
  485.       JSR PlayerScored
  486.       RTS
  487. ;******************************************************************
  488.      
  489.  
  490. ;***************
  491. ;INCREMENT SCORE 2
  492. ;******************************************************************
  493.     IncrementScore2:
  494.     Inc2Ones:
  495.       LDA score2Ones      ; load the lowest digit of the number
  496.       CLC
  497.       ADC #$01           ; add one
  498.       STA score2Ones
  499.       CMP #$0A           ; check if it overflowed, now equals 10
  500.       BNE Check2Score        ; if there was no overflow, all done
  501.     Inc2Tens:
  502.       LDA #$00
  503.       STA score2Ones      ; wrap digit to 0
  504.       LDA score2Tens      ; load the next digit
  505.       CLC
  506.       ADC #$01           ; add one, the carry from previous digit
  507.       STA score2Tens
  508.      
  509.     Check2Score:
  510.       LDA score2          ; This variable will be used to check win conditions
  511.       CLC
  512.       ADC #$01
  513.       STA score2
  514.       CMP #$10            ; Compare to 16.  If equal to 16, we have a winner
  515.       BNE Inc2Done
  516.       LDA STATEGAMEOVER
  517.       STA gamestate
  518.     Inc2Done:
  519.       JSR PlayerScored
  520.       RTS
  521. ;******************************************************************
  522.      
  523.      
  524. ;***************
  525. ;READ CONTROLLER 1
  526. ;******************************************************************
  527.     ;; In the following subroutines, we loop through controller input and store each button state in a bit
  528.     ;; The order (from left to right) is: A, B, Select, Start, Up, Down, Left, Right
  529.     ReadController1:
  530.       LDA #$01
  531.       STA $4016
  532.       LDA #$00
  533.       STA $4016
  534.       LDX #$08
  535.     ReadController1Loop:
  536.       LDA $4016
  537.       LSR A            ; bit0 -> Carry
  538.       ROL buttons1     ; bit0 <- Carry
  539.       DEX
  540.       BNE ReadController1Loop
  541.       RTS
  542. ;******************************************************************
  543.  
  544.  
  545. ;***************
  546. ;READ CONTROLLER 2
  547. ;******************************************************************
  548.     ReadController2:
  549.       LDA #$01
  550.       STA $4016
  551.       LDA #$00
  552.       STA $4016
  553.       LDX #$08
  554.     ReadController2Loop:
  555.       LDA $4017
  556.       LSR A            ; bit0 -> Carry
  557.       ROL buttons2     ; bit0 <- Carry
  558.       DEX
  559.       BNE ReadController2Loop
  560.       RTS
  561. ;******************************************************************
  562.  
  563.  
  564.  
  565. ;***************
  566. ;INCREASE BALL SPEED X
  567. ;******************************************************************
  568.     IncreaseBallSpeedX:
  569.       LDA ballspeedx
  570.       CLC
  571.       ADC #$01
  572.       STA ballspeedx
  573.       RTS
  574. ;******************************************************************
  575.  
  576. ;***************
  577. ;INCREASE BALL SPEED Y
  578. ;******************************************************************
  579.     IncreaseBallSpeedY:
  580.       LDA ballspeedy
  581.       CLC
  582.       ADC #$01
  583.       STA ballspeedy
  584.       RTS
  585. ;******************************************************************
  586.  
  587. ;***************
  588. ;PLAYER SCORED
  589. ;******************************************************************
  590.     PlayerScored:
  591.       LDA #$01
  592.       STA ballspeedx
  593.       STA ballspeedy
  594.       STA paddle1speed
  595.       STA paddle2speed
  596.       LDA #$78
  597.       STA bally
  598.       LDA #$80
  599.       STA ballx
  600.       RTS
  601. ;******************************************************************
  602.  
  603.  
  604.  
  605.  
  606. ;***************
  607. ;MOVE BALL
  608. ;******************************************************************
  609.    
  610.     MoveBall:
  611.     MoveBallRight:
  612.       LDA ballright
  613.       BEQ MoveBallRightDone   ;;if ballright=0, skip this section
  614.      
  615.       LDA ballx
  616.       CLC
  617.       ADC ballspeedx        ;;ballx position = ballx + ballspeedx
  618.       STA ballx
  619.      
  620.       LDA ballx
  621.       CMP #RIGHTWALL
  622.       BCC MoveBallRightDone      ;;if ball x < right wall, still on screen, skip next section
  623.       LDA #$00
  624.       STA ballright
  625.       LDA #$01
  626.       STA ballleft         ;;bounce, ball now moving left
  627.       JSR IncrementScore1  ;;give point to player 1, reset ball
  628.     MoveBallRightDone:
  629.      
  630.      
  631.     MoveBallLeft:
  632.       LDA ballleft
  633.       BEQ MoveBallLeftDone   ;;if ballleft=0, skip this section
  634.      
  635.       LDA ballx
  636.       SEC
  637.       SBC ballspeedx        ;;ballx position = ballx - ballspeedx
  638.       STA ballx
  639.      
  640.       LDA ballx
  641.       CMP #LEFTWALL
  642.       BCS MoveBallLeftDone      ;;if ball x > left wall, still on screen, skip next section
  643.       LDA #$01
  644.       STA ballright
  645.       LDA #$00
  646.       STA ballleft         ;;bounce, ball now moving right
  647.       JSR IncrementScore2  ;;give point to player 2, reset ball
  648.     MoveBallLeftDone:
  649.      
  650.      
  651.     MoveBallUp:
  652.       LDA ballup
  653.       BEQ MoveBallUpDone   ;;if ballup=0, skip this section
  654.      
  655.       LDA bally
  656.       SEC
  657.       SBC ballspeedy        ;;bally position = bally - ballspeedy
  658.       STA bally
  659.      
  660.       LDA bally
  661.       CMP #TOPWALL
  662.       BCS MoveBallUpDone      ;;if ball y > top wall, still on screen, skip next section
  663.       LDA #$01
  664.       STA balldown
  665.       LDA #$00
  666.       STA ballup         ;;bounce, ball now moving down
  667.     MoveBallUpDone:
  668.      
  669.      
  670.     MoveBallDown:
  671.       LDA balldown
  672.       BEQ MoveBallDownDone   ;;if ballup=0, skip this section
  673.      
  674.       LDA bally
  675.       CLC
  676.       ADC ballspeedy        ;;bally position = bally + ballspeedy
  677.       STA bally
  678.      
  679.       LDA bally
  680.       CMP #BOTTOMWALL
  681.       BCC MoveBallDownDone      ;;if ball y < bottom wall, still on screen, skip next section
  682.       LDA #$00
  683.       STA balldown
  684.       LDA #$01
  685.       STA ballup         ;;bounce, ball now moving down
  686.     MoveBallDownDone:
  687.       RTS
  688. ;******************************************************************
  689.      
  690.    
  691.  
  692.  
  693.  
  694. ;***************
  695. ;MOVE PADDLES
  696. ;******************************************************************
  697.     MovePaddles:
  698.     MovePaddle1Up:
  699.       LDA buttons1
  700.       AND #%00001000
  701.       BEQ MovePaddle1UpDone ; Use BEQ because zero flag will be set if result of AND operation is false
  702.      
  703.       ;;if up button pressed, move paddle up
  704.       LDA paddle1ytop
  705.       SEC
  706.       SBC paddle1speed
  707.       STA paddle1ytop
  708.       LDA paddle1ybot
  709.       SEC
  710.       SBC paddle1speed
  711.       STA paddle1ybot
  712.      
  713.       ;;  if paddle top > top wall
  714.       LDA paddle1ytop
  715.       CMP #TOPWALL
  716.       BCS MovePaddle1UpDone
  717.       LDA #TOPWALL
  718.       STA paddle1ytop      ;set top tile y-coordinate to TOPWALL
  719.       CLC
  720.       ADC #$20             ;bottom of paddle is 32 pixels below TOPWALL, so have to keep it set there
  721.       STA paddle1ybot      ;otherwise, bottom y-coordinate will decrease when up is pushed
  722.     MovePaddle1UpDone:
  723.      
  724.     MovePaddle2Up:
  725.       LDA buttons2
  726.       AND #%00001000
  727.       BEQ MovePaddle2UpDone
  728.      
  729.       ;; if up button pressed, move paddle up
  730.       LDA paddle2ytop
  731.       SEC
  732.       SBC paddle2speed
  733.       STA paddle2ytop
  734.       LDA paddle2ybot
  735.       SEC
  736.       SBC paddle2speed
  737.       STA paddle2ybot
  738.      
  739.       ;;  if paddle top > top wall
  740.       LDA paddle2ytop
  741.       CMP #TOPWALL
  742.       BCS MovePaddle2UpDone
  743.       LDA #TOPWALL
  744.       STA paddle2ytop         ;set top tile y-coordinate to TOPWALL
  745.       CLC
  746.       ADC #$20                ;bottom of paddle is 32 pixels below TOPWALL, so have to keep it set there
  747.       STA paddle2ybot         ;otherwise, bottom y-coordinate will decrease when up is pushed
  748.     MovePaddle2UpDone:
  749.      
  750.     MovePaddle1Down:
  751.       LDA buttons1
  752.       AND #%00000100
  753.       BEQ MovePaddle1DownDone
  754.      
  755.       ;;if down button pressed
  756.       LDA paddle1ytop
  757.       CLC
  758.       ADC paddle1speed
  759.       STA paddle1ytop
  760.       LDA paddle1ybot
  761.       CLC
  762.       ADC paddle1speed
  763.       STA paddle1ybot
  764.      
  765.       ;;  if paddle bottom < bottom wall
  766.       LDA paddle1ybot
  767.       CMP #BOTTOMWALL
  768.       BCC MovePaddle1DownDone
  769.       LDA #BOTTOMWALL         ;set bottom tile y-coordinate to BOTTOMWALL
  770.       STA paddle1ybot
  771.       SEC
  772.       SBC #$20                ;top of paddle is 32 pixels above BOTTOMWALL, so have to keep it set there
  773.       STA paddle1ytop         ;otherwise, top y-coordinate will increase when down is pushed
  774.     MovePaddle1DownDone:
  775.      
  776.     MovePaddle2Down:
  777.       LDA buttons2
  778.       AND #%00000100
  779.       BEQ MovePaddle2DownDone
  780.      
  781.       ;;if down button pressed
  782.       LDA paddle2ytop
  783.       CLC
  784.       ADC paddle2speed
  785.       STA paddle2ytop
  786.       LDA paddle2ybot
  787.       CLC
  788.       ADC paddle2speed
  789.       STA paddle2ybot
  790.      
  791.       ;;  if paddle bottom < bottom wall
  792.       LDA paddle2ybot
  793.       CMP #BOTTOMWALL
  794.       BCC MovePaddle2DownDone
  795.       LDA #BOTTOMWALL
  796.       STA paddle2ybot             ;set bottom tile y-coordinate to BOTTOMWALL
  797.       SEC
  798.       SBC #$20                    ;top of paddle is 32 pixels above BOTTOMWALL, so have to keep it set there
  799.       STA paddle2ytop             ;otherwise, top y-coordinate will increase when down is pushed
  800.     MovePaddle2DownDone:
  801.       RTS
  802. ;******************************************************************
  803.    
  804.  
  805. ;***************
  806. ;CHECK COLLISIONS
  807. ;******************************************************************
  808.     CheckCollisions:
  809.     CheckPaddle1Collision:
  810.       ;;if ball x < paddle1x
  811.       LDA ballx
  812.       SEC
  813.       SBC #$08
  814.       CMP #PADDLE1X
  815.       BCS CheckPaddle1CollisionDone
  816.      
  817.       ;;  if ball y > paddle y top
  818.       LDA bally
  819.       CLC
  820.       ADC #$08
  821.       CMP paddle1ytop
  822.       BCC CheckPaddle1CollisionDone
  823.      
  824.       ;;    if ball y < paddle y bottom
  825.       LDA bally
  826.       SEC
  827.       SBC #$08
  828.       CMP paddle1ybot
  829.       BCS CheckPaddle1CollisionDone
  830.      
  831.       ;;      bounce, ball now moving right
  832.       LDA #$01
  833.       STA ballright
  834.       LDA #$00
  835.       STA ballleft
  836.       JSR IncreaseBallSpeedX          ; increase horizontal speed of ball
  837.       LDA paddle1speed
  838.       CLC
  839.       ADC #$01
  840.       STA paddle1speed                ; increase paddle 1 speed
  841.     CheckPaddle1CollisionDone:
  842.      
  843.     CheckPaddle2Collision:
  844.       ;;if ball x < paddle1x
  845.       LDA ballx
  846.       CLC
  847.       ADC #$08
  848.       CMP #PADDLE2X
  849.       BCC CheckPaddle2CollisionDone
  850.      
  851.       ;;  if ball y > paddle y top
  852.       LDA bally
  853.       CLC
  854.       ADC #$08
  855.       CMP paddle2ytop
  856.       BCC CheckPaddle2CollisionDone
  857.      
  858.       ;;    if ball y < paddle y bottom
  859.       LDA bally
  860.       SEC
  861.       SBC #$08
  862.       CMP paddle2ybot
  863.       BCS CheckPaddle2CollisionDone
  864.      
  865.       ;;      bounce, ball now moving right
  866.       LDA #$01
  867.       STA ballleft
  868.       LDA #$00
  869.       STA ballright
  870.       JSR IncreaseBallSpeedY          ; increase vertical speed of ball
  871.       LDA paddle2speed
  872.       CLC
  873.       ADC #$01
  874.       STA paddle2speed                ; increase paddle 2 speed
  875.     CheckPaddle2CollisionDone:
  876.       RTS
  877. ;******************************************************************
  878.  
  879.  
  880.  
  881.  
  882.  
  883.  
  884. ;;;;;;;;;;;;;;;;;;;;;;;;
  885. ;;;;;;;; DATA ;;;;;;;;;;
  886. ;;;;;;;;;;;;;;;;;;;;;;;;
  887.      
  888.  
  889.      
  890.       .bank 1
  891.       .org $E000
  892.     palette:
  893.       .db $22,$29,$1A,$0F,  $22,$36,$17,$0F,  $22,$30,$21,$0F,  $22,$27,$17,$0F   ;;background palette
  894.       .db $22,$1C,$15,$14,  $22,$02,$38,$3C,  $22,$1C,$15,$14,  $22,$02,$38,$3C   ;;sprite palette
  895.      
  896.     titlepalette:
  897.       .db $00,$0f,$10,$30,$00,$01,$21,$31,$00,$06,$16,$26,$00,$09,$19,$29
  898.      
  899.      
  900.     sprites:
  901.          ;vert tile attr horiz
  902.       .db $80, $75, $00, $80   ;sprite 0
  903.      
  904.     vertical_offsets:
  905.       .db $00, $08, $10, $18
  906.      
  907.     background:
  908.       .db $27
  909.      
  910.     title:
  911.             .db $25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25
  912.             .db $25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25
  913.             .db $25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25
  914.             .db $25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25
  915.             .db $25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25
  916.             .db $25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25
  917.             .db $25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25
  918.             .db $25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25
  919.             .db $26,$26,$26,$26,$26,$26,$26,$25,$25,$26,$26,$26,$26,$26,$26,$25,$26,$26,$26,$25,$25,$25,$26,$26,$25,$25,$26,$26,$26,$26,$26,$25
  920.             .db $26,$26,$26,$26,$26,$26,$26,$27,$26,$26,$26,$26,$26,$26,$26,$27,$26,$26,$26,$26,$25,$25,$26,$26,$25,$26,$26,$26,$26,$26,$26,$26
  921.             .db $26,$26,$27,$27,$27,$26,$26,$27,$26,$26,$27,$27,$27,$26,$26,$27,$26,$26,$26,$26,$26,$25,$26,$27,$26,$26,$26,$27,$27,$27,$27,$25
  922.             .db $26,$26,$27,$25,$25,$26,$26,$27,$26,$26,$27,$25,$25,$26,$26,$27,$26,$26,$26,$26,$26,$26,$26,$27,$26,$26,$26,$27,$25,$25,$25,$25
  923.             .db $26,$26,$27,$25,$25,$26,$26,$27,$26,$26,$27,$25,$25,$26,$26,$27,$26,$26,$27,$26,$26,$26,$26,$27,$26,$26,$26,$27,$25,$26,$26,$26
  924.             .db $26,$26,$26,$26,$26,$26,$26,$27,$26,$26,$26,$26,$26,$26,$26,$27,$26,$26,$27,$25,$26,$26,$26,$27,$26,$26,$26,$27,$25,$25,$26,$26
  925.             .db $26,$26,$26,$26,$26,$26,$27,$27,$25,$26,$26,$26,$26,$26,$27,$27,$26,$26,$27,$25,$25,$26,$26,$27,$25,$26,$26,$26,$26,$26,$26,$27
  926.             .db $26,$26,$26,$26,$27,$27,$27,$25,$25,$27,$27,$27,$27,$27,$27,$25,$27,$27,$27,$25,$25,$25,$27,$27,$25,$25,$26,$26,$26,$26,$27,$25
  927.             .db $26,$26,$26,$26,$27,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25
  928.             .db $26,$26,$26,$26,$27,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25
  929.             .db $26,$26,$26,$26,$27,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25
  930.             .db $25,$27,$27,$27,$27,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25
  931.             .db $25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25
  932.             .db $25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25
  933.             .db $25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$19,$1b,$0e,$1c,$1c,$25,$1c,$1d,$0a,$1b,$1d,$2b,$25,$25,$25,$25,$25,$25,$25,$25
  934.             .db $25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25
  935.             .db $25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25
  936.             .db $25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25
  937.             .db $25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25
  938.             .db $25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25
  939.             .db $25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25
  940.             .db $25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25
  941.      
  942.       .org $FFFA     ;first of the three vectors starts here
  943.       .dw NMI        ;when an NMI happens (once per frame if enabled) the
  944.                        ;processor will jump to the label NMI:
  945.       .dw RESET      ;when the processor first turns on or is reset, it will jump
  946.                        ;to the label RESET:
  947.       .dw 0          ;external interrupt IRQ is not used in this tutorial
  948.      
  949.      
  950.     ;;;;;;;;;;;;;;  
  951.      
  952.      
  953.       .bank 2
  954.       .org $0000
  955.       .incbin "mario.chr"   ;includes 8KB graphics file from SMB1
Add Comment
Please, Sign In to add comment