Guest User

Untitled

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