Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- include "tileset.asm"
- SECTION "Start", ROM0[$100]
- EntryPoint:
- jp init
- SECTION "VBLANK", ROM0[$040]
- VBLANK:
- ;update oam
- call $FF80
- ;apply gravity
- call ApplyGravity
- ;check Input
- call checkInput
- reti
- SECTION "Game", ROM0[$150]
- ApplyGravity:
- ; check for ground
- ld a, [Player_PosY]
- cp a, 144 + 9
- ld b, a
- ;check for obstacles
- ld a, [Player_PosY]
- cp a + 9, [Obstacle1_PosY]
- or b
- ld b, a
- ld a, [Player_PosY]
- cp a + 9, [Obstacle2_PosY]
- or b
- ld b, a
- ld a, [Player_PosY]
- cp a + 9, [Obstacle3_PosY]
- or b
- ;jump to .resetJump if no collision found
- jp z, .resetJump
- ; apply gravitation
- inc a
- ld [Player_PosY], a
- .end
- ret
- .resetJump
- ld a, 1
- ld [canJump], a
- jp .end
- checkInput:
- ld a, %00100000
- ld [$FF00], a
- ld a, [$FF00]
- cpl
- AND %00000001
- cp 1
- call z, moveRight
- ld a, [$FF00]
- cpl
- AND %00000010
- cp 2
- call z, moveLeft
- ld a, [$FF00]
- cpl
- AND %00000100
- cp 4
- call z, jump
- ret
- ; move player to the right
- moveRight:
- ld a, [Player_PosX]
- inc a
- push af
- cp a, 144 + 18
- jp nc, .reset
- pop af
- ld [Player_PosX], a
- .end
- ret
- .reset
- pop af
- jp .end
- ; move player to the left
- moveLeft:
- ld a, [Player_PosX]
- dec a
- push af
- cp a, 7
- jp c, .reset
- pop af
- ld [Player_PosX], a
- .end
- ret
- .reset
- pop af
- jp .end
- ;jump with player
- jump:
- ld a, [canJump]
- and 1
- jp z, .end
- ld a, [Player_PosY]
- sub 20
- ld [Player_PosY], a
- ld a, 0
- ld [canJump], a
- .end
- ret
- dma_copy:
- push af ; store the old a and status reg (f) on the stack so we can use them for our own purposes
- ld a, $C0 ; OamData variable
- ldh [$FF46], a ; once we put this address into the DMA register, the transfer will begin ($A0 bytes from $C000)
- ; now we want to delay for 160us while the data gets copied
- ; this uses a basic countdown-jump back loop
- ld a, $28 ; this is our countdown value ($28 to 0)
- dma_copy_wait:
- dec a
- jr nz, dma_copy_wait ; if a is not 0, jump back to the wait loop
- ; once we are here, the dma is all done
- pop af ; restore af to its old value
- ret ; and go back to what we were doing
- dma_copy_end:
- ;hl = destination
- ;bc = ByteCount
- ;de = Source
- memcopy:
- ld a, [de]
- ldi [hl], a
- dec bc
- inc de
- ld a, b
- or c
- jr nz, memcopy
- ret
- ;hl = destination
- ;bc = Source
- ;a = value
- memset:
- push af
- .repeat
- pop af
- ldi [hl], a
- dec bc
- push af
- ld a, b
- or c
- jr nz, .repeat
- pop af
- ret
- ;return if vblank occurs
- WaitForVBLANK:
- ld a, [$FF44]
- cp a, 144
- jp nz, WaitForVBLANK
- ret
- init:
- ;disable interrupts
- di
- ;disable screen
- call WaitForVBLANK
- ld a, 0
- ld [$FF40], a
- ;remoe Nintendo Logo from VRAM
- ld hl, $8000
- ld bc, $81A0 - $8000
- ld a, 0
- call memset
- ld hl, $9904
- ld bc, $9930 - $9904
- ld a, 0
- call memset
- ;clear OAM
- ld hl, $C000
- ld bc, $A0
- ld a, 0
- call memset
- ;draw background
- ld hl, $9000
- ld bc, Background_END - Background
- ld de, Background
- call memcopy
- ;load player sprite to VRAM
- ld hl, $8000
- ld bc, Player_END - Player
- ld de, Player
- call memcopy
- ;load obstacle sprite to VRAM
- ld hl, $8010
- ld bc, Obstacle_END - Obstacle
- ld de, Obstacle
- call memcopy
- ;save player values in WRAM
- ;set y coordinate
- ld hl, Player_PosY
- ld a, 8
- ldi [hl], a
- ;set x coordinate
- ld a, 20
- ldi [hl], a
- ;set pattern
- ld a, 0
- ldi [hl], a
- ;set flags
- ldi [hl], a
- ;save obstacle values in WRAM
- ;set y coordinate of obstacle 1
- ld hl, Obstacle1_PosY
- ld a, 144 + 9
- ldi [hl], a
- ;set x coordinate of obstacle 1
- ld a, 32
- ldi [hl], a
- ;set pattern of obstacle 1
- ld a, 1
- ldi [hl], a
- ;set flags of obstacle 1
- ld a, 0
- ldi [hl], a
- ;set y coordinate of obstacle 2
- ld a, 144 + 9
- ldi [hl], a
- ;set x coordinate of obstacle 2
- ld a, 64
- ldi [hl], a
- ;set pattern of obstacle 2
- ld a, 1
- ldi [hl], a
- ;set flags of obstacle 2
- ld a, 0
- ldi [hl], a
- ;set y coordinate of obstacle 3
- ld a, 144 + 9
- ldi [hl], a
- ;set x coordinate of obstacle 3
- ld a, 96
- ldi [hl], a
- ;set pattern of obstacle 3
- ld a, 1
- ldi [hl], a
- ;set flags of obstacle 3
- ld a, 0
- ldi [hl], a
- ;init variables
- ld a, 0
- ld [canJump], a
- ; copy DMA function to HRAM
- ld hl, $FF80
- ld bc, dma_copy_end - dma_copy
- ld de, dma_copy
- call memcopy
- ;set color palette
- ld a, %11100100
- ld [$FF47], a
- ld [$FF48], a
- ;enable screen
- ld a, %10000011
- ld [$FF40], a
- ;enable interrupts
- ld a, %00000001
- ld [$FFFF], a
- ei
- game:
- halt
- jp game
- SECTION "OAM", WRAM0[$C000]
- ; player
- Player_PosY: DS 1
- Player_PosX: DS 1
- Player_PatternNumber: DS 1
- Player_Flags: DS 1
- ; obstacles
- Obstacle1_PosY: DS 1
- Obstacle1_PosX: DS 1
- Obstacle1_PatternNumber: DS 1
- Obstacle1_Flags: DS 1
- Obstacle2_PosY: DS 1
- Obstacle2_PosX: DS 1
- Obstacle2_PatternNumber: DS 1
- Obstacle2_Flags: DS 1
- Obstacle3_PosY: DS 1
- Obstacle3_PosX: DS 1
- Obstacle3_PatternNumber: DS 1
- Obstacle3_Flags: DS 1
- SECTION "Variables", WRAM0[$C100]
- canJump: DS 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement