Help with some code for reading keypresses

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

User avatar
kyuusaku
Posts: 1665
Joined: Mon Sep 27, 2004 2:13 pm

Post by kyuusaku »

One way to do it is to read all the buttons into one byte every frame, then when carrying out your game logic you can test bits of the byte for key combinations.
tepples
Posts: 22345
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Post by tepples »

electro wrote:Wondering now how I might read combinations of buttons. Would I need to store another variable in ram called button_state2 ?
What kind of "combination" are you talking about?
User avatar
electro
Posts: 132
Joined: Tue Jan 29, 2008 11:12 am
Location: New York

Post by electro »

tepples wrote:
electro wrote:Wondering now how I might read combinations of buttons. Would I need to store another variable in ram called button_state2 ?
What kind of "combination" are you talking about?
Just thinking ahead a little. I wondered about reading simultaneous button presses.

Thinking ahead to:

if a button and left arrow were pressed simultaneously then do something else.

I still have to try to get what I've learned so far working, but I think I am understanding it so far. Thanks to the help here.

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

Post by tokumaru »

Well, you usually code a different action for each button, so if 2 buttons are pressed, both actions will be triggered. This is what allows you to jump while running in games. But of course, you can jump without running as well.

But if you want something to happen only when 2 (or more) buttons are pressed at the same time, I can think of 2 ways to do that. The easiest one would be check for all the buttons you want, one after the other, branching away if one of the required buttonsis not pressed:

Code: Select all

	;Test first button
	lda #%00010000
	and ButonsPressed
	beq Nocombo
	;Test second button
	lda #%10000000
	and ButtonsPressed
	beq NoCombo
	;Whatever goes here is execute only if both buttons are pressed
NoCombo:
	;If one or both buttons are not pressed, you end up here
Another way would be to directly AND the bits of the buttons you want to be pressed together, by shifting the number between the AND operations. Since the AND operation results in a "1" only when both bits are one, if any of the buttons is 0, the final result will be 0 and you'll know that they are not all pressed.
User avatar
electro
Posts: 132
Joined: Tue Jan 29, 2008 11:12 am
Location: New York

Post by electro »

tokumaru wrote:
But if you want something to happen only when 2 (or more) buttons are pressed at the same time...
Yes, exactly. Only when 2 or more buttons are pressed at the same time.

I think I see what you're saying. I will digest this more and post.

Thanks for the help.

T
User avatar
electro
Posts: 132
Joined: Tue Jan 29, 2008 11:12 am
Location: New York

Post by electro »

Could any point me to simple examples of setting up a variable in ram?

I'm not sure how to do that exactly.

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

Post by tokumaru »

electro wrote:Could any point me to simple examples of setting up a variable in ram?
A variable is just a piece of memory that holds information. That means that by simply storing something anywhere in RAM you are using that location as a variable.

Code: Select all

	lda #$50
	sta $14
There, you just turned memory location $14 into a variable that holds the value $50. You have to agree, however, that remembering the memory locations of every information you use can be a pain in the ass. For this reason, it is possible to assign names to memory locations, because names are much more meaningful and easier to remember than numbers.

The easiest way to declare a variable is to assign an address to a name somewhere in your code (usually at the top):

Code: Select all

	MyVariable = $14

	lda #$50
	sta MyVariable
This produces the exact same binary code as the previus example, but now you've made the code easier to read and understand.

As your programs grow though, and you start using variables that occupy more than a single byte, this type of variable declaration becomes less appealing, because it becomes hard to maintain.

Most assemblers will allow you to reserve a number of bytes bytes from the current location on. So, if you start the declarations from a known location, you can just reserve the amount of bytes that each variable needs, like this:

Code: Select all

	.org $0000

Variable1	.dsb 1
Variable2	.dsb 1
LargerVariable	.dsb 3
This way you don't have to worry about where each variable actually is (although if you do need to know where they are you can: lda #Variable2 will put in A the address of that variable, while lda Variable2 will give you the contents of that location), and it's easier to move them around and mess with their sizes if you need to.
User avatar
electro
Posts: 132
Joined: Tue Jan 29, 2008 11:12 am
Location: New York

Post by electro »

I see. Thanks for explaining that. I appreciate it.

I've never learned so much from one post in any other forum before. Incredible.

When I get my new website up I would like to add a link here.

T
Post Reply