Guest User

Untitled

a guest
Feb 14th, 2017
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. TILE_BLACK EQU $10
  2.  
  3. DIR_UP    EQU 0
  4. DIR_DOWN  EQU 1
  5. DIR_LEFT  EQU 2
  6. DIR_RIGHT EQU 3
  7.  
  8. UP_KEY   EQU 6
  9. DOWN_KEY EQU 7
  10.  
  11. Pong:
  12.     call ClearScreen
  13. ; uncomment if not in bank f (i.e. used buffer overflow RCE exploit in trade center)
  14. ;   ld a, $0f
  15. ;   ld ($2000), a  
  16.  
  17. .waitingLoop
  18.     ld a, $ff
  19.     ld [wSerialExchangeNybbleReceiveData], a
  20.     call PlaySound ; mute music
  21.     ld a, 12
  22.     call LinkBattleExchangeData_PrintWaiting_ExchangeNybbles
  23.     ld a, [wSerialExchangeNybbleReceiveData]
  24.     cp 12
  25.     jr nz, .waitingLoop
  26.  
  27. ; load the black tiles to VRAM
  28.     di
  29. .waitVblank
  30.     ldh a, [rSTAT] 
  31.     cp $81
  32.     jr nz, .waitVblank
  33.     ld hl, $9100
  34.     ld bc, $16 * 3
  35.     ld a, $ff
  36.     call FillMemory
  37.     ei
  38.    
  39. .newGame
  40. ; init game params
  41.     ld hl, wPad1Offset
  42.     ld a, 7
  43.     ld [hli], a ; wPad1Offset
  44.     ld [hli], a ; wPad2Offset
  45.     inc a
  46.     ld [hli], a ; wBallCoordY
  47.     ldh a, [hSerialConnectionStatus]
  48.     cp USING_INTERNAL_CLOCK
  49.     ld a, 1
  50.     ld c, 1 << DIR_UP + 1 << DIR_RIGHT
  51.     jr z, .notOppositeSide
  52.     ld a, 18
  53.     ld c, 1 << DIR_UP + 1 << DIR_LEFT
  54. .notOppositeSide
  55.     ld [hli], a ; wBallCoordX
  56.     ld a, [wRandomStartYFlag]
  57.     xor 1
  58.     ld [wRandomStartYFlag], a
  59.     and a
  60.     jr nz, .ok
  61.     inc c ; replace DIR_UP with DIR_DOWN
  62. .ok
  63.     ld [hl], c ; wBallDirection
  64.     call RefreshPadsAndBall
  65.     ld c, 12
  66.     call DelayFrames   
  67.  
  68. .mainLoop
  69.     call RefreshPadsAndBall
  70.     ld a, [wGameOver]
  71.     and a
  72.     jr z, .notGameOver
  73.     dec a
  74.     ld [wGameOver], a
  75.     jr z, .newGame
  76. .notGameOver   
  77.     call HandleJoypadInput
  78. .skipJoypad
  79.     call SendPadOffset
  80.     call MoveBall
  81.     jr .mainLoop
  82.    
  83. RefreshPadsAndBall:
  84. ; draw bg, horizontal walls, and vertical voids
  85.     call DrawBackground
  86.     call DrawVoids
  87. ; refresh player pad   
  88.     ld hl, wTileMap
  89.     ld a, [wPad1Offset]
  90.     call DrawPadFromOffset
  91. ; refresh enemy pad
  92.     ld hl, wTileMap + 19
  93.     ld a, [wPad2Offset]
  94.     call DrawPadFromOffset
  95. ; refresh ball 
  96.     ld hl, wTileMap
  97.     ld a, [wBallCoordX]
  98.     ld e, a
  99.     ld d, 0
  100.     add hl, de
  101.     ld a, [wBallCoordY]
  102.     ld bc, 20
  103.     call AddNTimes
  104. ;   xor a
  105.     ld [hl], a
  106.     ret
  107.    
  108. DrawBackground:
  109.     ld hl, wTileMap
  110.     call .drawWall
  111.     ld a, TILE_BLACK   
  112.     ld bc, 20 * (18 - 2)
  113.     call FillMemory
  114. ;   jr .drawWall
  115. .drawWall
  116.     ld bc, 20
  117.     xor a
  118.     jp FillMemory
  119.    
  120. DrawVoids:
  121.     ld hl, wTileMap + 20 * 1
  122.     call DrawFullVerticalBlackLine
  123.     ld hl, wTileMap + 20 * 1 + 19
  124. ;   jr DrawFullVerticalBlackLine
  125.  
  126. DrawFullVerticalBlackLine:
  127.     ld a, TILE_BLACK
  128.     ld e, 16
  129. DrawVerticalLine:
  130. ; hl = start address
  131. ; e = how many tiles
  132. ; a = tile
  133.     ld bc, 20
  134. .loop
  135.     ld [hl], a
  136.     add hl, bc
  137.     dec e
  138.     ret z
  139.     jr .loop
  140.    
  141. DrawPadFromOffset:
  142.     ld bc, 20
  143.     call AddNTimes
  144.     xor a
  145.     ld e, 4
  146.     jr DrawVerticalLine
  147.  
  148. HandleJoypadInput:
  149.     ld bc, wPad1Offset
  150.     ld a, [bc]
  151.     ld hl, hJoyInput
  152.     bit UP_KEY, [hl]
  153.     jr z, .checkDown
  154.     dec a ; cp 1
  155.     ret z ; can't move up
  156.     ld [bc], a
  157. ;   ret
  158. .checkDown
  159.     bit DOWN_KEY, [hl]
  160.     ret z
  161.     cp 17 - 4
  162.     ret z
  163.     inc a
  164.     ld [bc], a
  165.     ret
  166.    
  167. SendPadOffset:
  168.     ld a, [bc] ; wPad1Offset
  169. SendByte:  
  170.     ld [wSerialExchangeNybbleSendData], a
  171. .syncLoop1 
  172.     call Serial_ExchangeNybble
  173.     call DelayFrame
  174.     ld a, [wSerialExchangeNybbleReceiveData]
  175.     inc a
  176.     jr z, .syncLoop1   
  177.     ld b, 5
  178. .syncLoop2
  179.     call DelayFrame
  180.     call Serial_ExchangeNybble
  181.     dec b
  182.     jr nz, .syncLoop2
  183.     ld a, [wSerialExchangeNybbleReceiveData]
  184.     ld [wPad2Offset], a
  185.     ret
  186.    
  187. MoveBall:
  188.     ld hl, wBallCoordY ; handle vertical movement first
  189.     ld e, 2
  190.     ld a, [wBallDirection] 
  191. .loop
  192.     rrca
  193.     jr nc, .notUpOrLeft
  194.     dec [hl]
  195. .notUpOrLeft
  196.     rrca
  197.     jr nc, .notDownOrRight
  198.     inc [hl]
  199. .notDownOrRight
  200.     dec e
  201.     inc hl ; wBallCoordX
  202.     jr nz, .loop ; go back to handle horizontal movement
  203. ; handle collisions
  204.     dec hl
  205.     dec hl
  206.     ld a, [hl] ; wBallCoordY
  207.     cp 16
  208.     jr z, .wall
  209.     dec a ; cp 1
  210.     jr z, .wall
  211. .handleCorners 
  212.     inc hl
  213.     ld a, [hl] ; wBallCoordX
  214.     cp 18
  215.     jr z, .voidOrPad2
  216.     dec a ; cp 1
  217.     jr z, .voidOrPad1  
  218.     ret
  219. .wall
  220. ; change Y movement
  221.     ld c, 1 << DIR_UP + 1 << DIR_DOWN
  222.     call .updateBallDirectionAndPlayClickSound
  223. ; properly handle the corners
  224.     jr .handleCorners
  225. .voidOrPad2
  226.     ld a, [wPad2Offset]
  227.     jr .voidOrPad
  228. .voidOrPad1
  229.     ld a, [wPad1Offset]
  230. .voidOrPad
  231.     dec hl ; wBallCoordY
  232.     ld e, 4
  233. .padBounceCheckLoop
  234.     cp [hl]
  235.     jr z, .pad
  236.     inc a ; next pad tile
  237.     dec e
  238.     jr nz, .padBounceCheckLoop
  239. ; game over
  240.     ld a, 3
  241.     ld [wGameOver], a
  242.     ret
  243. .pad
  244. ; change X movement
  245.     ld c, 1 << DIR_LEFT + 1 << DIR_RIGHT
  246. ;   jr .updateBallDirectionAndPlayClickSound
  247. .updateBallDirectionAndPlayClickSound
  248.     ld a, [wBallDirection]
  249.     xor c
  250.     ld [wBallDirection], a
  251.     ld a, SFX_POUND
  252.     jp PlaySound
Advertisement
Add Comment
Please, Sign In to add comment