help with pong

Are you new to 6502, NES, or even programming in general? Post any of your questions here. Remember - the only dumb question is the question that remains unasked.

Moderator: Moderators

Post Reply
JPC
Posts: 7
Joined: Wed Mar 04, 2020 6:07 pm
Location: ✶ ✶ ✶ ✶

help with pong

Post by JPC » Mon Apr 27, 2020 7:31 am

I'm fairly new here, and I'm starting my first pong program. I need help getting the collision to work. I tried using the CC65 assembler's

Code: Select all

.if
function for collision, but it's not working. I tried the nerdy nights pong collision code and it didn't work either. If anyone has a solution, I'd like to keep the CA65 macrofunctions just to simplify the code. I've included all the required files in the 7z file

EDIT: I also tried to change the ball's direction by pressing a button, but it still didn't work
Attachments
pong.7z
(1.82 KiB) Downloaded 17 times
Last edited by JPC on Mon Apr 27, 2020 9:20 am, edited 1 time in total.

User avatar
tokumaru
Posts: 11692
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: help with pong

Post by tokumaru » Mon Apr 27, 2020 8:20 am

Assembler directives (including .if) are processed at assembly time. They're there to help you manage your source code better, and have nothing to do with the game logic, which runs entirely in the target console's CPU.

The game logic has to be written in good old 6502 assembly language (or something that will compile to 6502 assembly), where IFs are usually implemented via comparisons (CMP, SBC, etc.) followed by conditional branches (BEQ, BNE, BCC, BCS, etc.).

JPC
Posts: 7
Joined: Wed Mar 04, 2020 6:07 pm
Location: ✶ ✶ ✶ ✶

Re: help with pong

Post by JPC » Mon Apr 27, 2020 9:15 am

like i said, i tried using code from a nerdy nights pong demo, but it didn't work either. they produced the same result

User avatar
tokumaru
Posts: 11692
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: help with pong

Post by tokumaru » Mon Apr 27, 2020 11:45 am

I was just explaining *why* your first attempt failed. It's very important for a programmer to understand the difference between assembly-time and run-time logic.

User avatar
dougeff
Posts: 2680
Joined: Fri May 08, 2015 7:17 pm
Location: DIGDUG
Contact:

Re: help with pong

Post by dougeff » Tue Apr 28, 2020 9:33 am

Why don't you make a ROM that works, using minimal game logic, then work until a bit of code breaks, then you can post just the broken code here (as text) and we can explain why it doesn't work.

Because the exact code from Nerdy Nights does assemble and does work (I have tested them all myself).
nesdoug.com -- blog/tutorial on programming for the NES

JPC
Posts: 7
Joined: Wed Mar 04, 2020 6:07 pm
Location: ✶ ✶ ✶ ✶

Re: help with pong

Post by JPC » Tue Apr 28, 2020 3:03 pm

First, I have a variable which sets the default direction of the ball:

Code: Select all

ballVert.set $01	;0=down, 1=up
I also have two constants which set the top and bottom boundaries as well:

Code: Select all

WallTop		= $10
WallBottom	= $E0
this is the code which determines if the ball will travel up or down:

Code: Select all

.if (ballVert = $01)
		lda ballYPos
		sec
		sbc #$04
		sta ballYPos

		.if (ballYPos < WallTop)
			ballVert.set $00
		.endif
	.else
		lda ballYPos
		clc
		adc #$04
		sta ballYPos
		.if (ballYPos > WallBottom)
			ballVert.set $01
		.endif
	.endif
Setting ballVert to 0 makes the ball go down, and setting it to 1 makes it go up. But I can't seem to make the ball change directions during gameplay. Even if i add code to change the direction by pressing a button, it still doesn't change direction.

Rom is included
Attachments
pong.nes
(40.02 KiB) Downloaded 16 times

User avatar
Quietust
Posts: 1556
Joined: Sun Sep 19, 2004 10:59 pm
Contact:

Re: help with pong

Post by Quietust » Tue Apr 28, 2020 3:08 pm

That's because you're using the equivalent of preprocessor directives, which means its state is determined entirely at compile-time rather than when you actually run it.

If you disassemble the resulting ROM file (or trace through it in an emulator's debugger), you will see that only half of the logic inside that ".if (ballVert = $01)" block is present - namely, the first part:

Code: Select all

$808E:   LDA $0218
        CLC
        ADC #$04
        STA $0218
(the bit immediately below it, at $8097, is what adjusts the ball's horizontal position, and it always subtracts 4)
Last edited by Quietust on Tue Apr 28, 2020 3:34 pm, edited 1 time in total.
Quietust, QMT Productions
P.S. If you don't get this note, let me know and I'll write you another.

User avatar
tokumaru
Posts: 11692
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: help with pong

Post by tokumaru » Tue Apr 28, 2020 3:13 pm

Like I said before, forget about using .if for game logic. It will never work, this is not what these directives are for. Please show us the version that uses only 6502 instructions.

JPC
Posts: 7
Joined: Wed Mar 04, 2020 6:07 pm
Location: ✶ ✶ ✶ ✶

Re: help with pong

Post by JPC » Tue Apr 28, 2020 3:33 pm

while I'm changing my code, I must ask: are .if and other ca65 functions good for anything?

User avatar
Quietust
Posts: 1556
Joined: Sun Sep 19, 2004 10:59 pm
Contact:

Re: help with pong

Post by Quietust » Tue Apr 28, 2020 3:35 pm

JPC wrote:
Tue Apr 28, 2020 3:33 pm
while I'm changing my code, I must ask: are .if and other ca65 functions good for anything?
Yes - they are useful for making programs that can be built in multiple configurations. For example, you could use them to select whether you want to produce an NTSC ROM image or a PAL one (e.g. to choose between different note frequency tables), or if you want to use English text versus Japanese, or even if you wanted to support multiple different mappers.
Quietust, QMT Productions
P.S. If you don't get this note, let me know and I'll write you another.

JPC
Posts: 7
Joined: Wed Mar 04, 2020 6:07 pm
Location: ✶ ✶ ✶ ✶

Re: help with pong

Post by JPC » Tue Apr 28, 2020 3:43 pm

so they aren't meant for in-game logic like in any high-level language?

User avatar
Quietust
Posts: 1556
Joined: Sun Sep 19, 2004 10:59 pm
Contact:

Re: help with pong

Post by Quietust » Tue Apr 28, 2020 3:46 pm

JPC wrote:
Tue Apr 28, 2020 3:43 pm
so they aren't meant for in-game logic like in any high-level language?
Nope - they're solely for conditional assembly, the same as #ifdef in C/C++.
Quietust, QMT Productions
P.S. If you don't get this note, let me know and I'll write you another.

JPC
Posts: 7
Joined: Wed Mar 04, 2020 6:07 pm
Location: ✶ ✶ ✶ ✶

Re: help with pong

Post by JPC » Tue Apr 28, 2020 4:01 pm

here's my new code:

Code: Select all

ballup:
		lda ballVert ;ballVert == $01
		beq ballupdone

		lda ballYPos
		sec
		sbc #$04
		sta ballYPos

		lda ballYPos
		cmp #WallTop
		bcs ballupdone

		lda #$00
		sta ballVert
	ballupdone:
	
	balldown:
		lda ballVert ;ballVert == $00
		bne balldowndone

		lda ballYPos
		clc
		adc #$04
		sta ballYPos

		lda ballYPos
		cmp #WallBottom
		bcc balldowndone

		lda #$01
		sta ballVert
	balldowndone:
This correctly makes the ball bounce! My confusion was that I thought the .if function could be used in place of assembly code. Thanks so much for the help!

Post Reply