Anyone have programs containing best practices?

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
User avatar
NeverCameBack
Posts: 41
Joined: Mon Feb 24, 2020 12:22 am

Anyone have programs containing best practices?

Post by NeverCameBack » Tue Mar 17, 2020 10:50 pm

I realize there are probably way better ways to do things than I'm currently doing.
My platformer physics engine isn't working right.
I'm stuck on trying to make a multi screen game - I'm using a variable called "CurrentScreen", starts at 0, when A is pressed, turns to 1, 2.. etc. When "CurrentScreen" is compared to 0, 1, 2, etc., It should run the code to only display that screen #. For some reason parts of the old screen are blending into the first.

Are there any good sample .asm files that you know of, or would be willing to share? I figure maybe I can learn some better techniques from looking through a working project.

Pokun
Posts: 1517
Joined: Tue May 28, 2013 5:49 am
Location: Hokkaido, Japan

Re: Anyone have programs containing best practices?

Post by Pokun » Sun Mar 22, 2020 2:17 am

Exactly how is it a mutli-screen game? If parts of the old screen blends in it sounds like you either didn't draw the new screen properly or you are accidentally doing some kind of split-screen effect.

If every level/room is one screen and you don't need to scroll, the scroll X and Y registers ($2005) should always be 0. When you switch screen you just turn off rendering and draw the new screen on the same nametable. You might want to clear the nametable first (by filling it with empty tiles, like the SPACE character) if you don't draw on empty tiles.

You can also alternate between the two nametables (or 4 nametables if you use 4-screen mirroring). In that case you need to change both a scroll register and the active nametable number ($2000). This way you can switch a screen instantly without having to turn off rendering. As long as the second screen is already drawn.

If you want smooth scrolling between the screens like in Zelda you need to learn how to scroll properly. Nerdy Nights covers that. There is an example ROM included.

User avatar
NeverCameBack
Posts: 41
Joined: Mon Feb 24, 2020 12:22 am

Re: Anyone have programs containing best practices?

Post by NeverCameBack » Tue Mar 24, 2020 10:02 pm

It'll be a multiscreen game in the sense that selecting A will take you to one pathway of screens, B will be a different.. And just text and hopefully some music. Thanks for the advice. I'm currently trying to get my head around the music section of Nerdy Nights - my scrolling could use some work too, but as of now I don't think I'll be making a plat-former right away.

By best practices, I was hoping maybe someone had more "nerdy nights" type .asm files.
Maybe one showing some sprite jump physics.
Another showing enemy generation on the screen.
Like the common things these NES games have done over and over. I was wondering if someone had broken each one down into how to professionals generally accomplished these. Maybe no sample files of the sort exist, but thought to ask.

Pokun
Posts: 1517
Joined: Tue May 28, 2013 5:49 am
Location: Hokkaido, Japan

Re: Anyone have programs containing best practices?

Post by Pokun » Sat Mar 28, 2020 12:20 pm

Oh I see. You just mean there are multiple scrollable areas, AKA "screens". Now it makes sense.
That Nerdy Nights sound tutorial teaches a lot of things which takes time to melt but it is a very useful tutorial. I wish there were more of this kind of stuff.

There are not a lot of examples out there unfortunately. Making them takes a lot of free time after all. You could look at disassemblies of commercial games or look at the source code of released homebrew like the Lizard demo.

Making things on the NES is not that far away from making games on any other platform in theory, so you could learn how to make things from other sources as well. Then you just have to figure out how to do it in 6502. Once you understand a principle and are comfortable enough with 6502 that isn't as hard as one might think. First you must know what to look for.

Making a platform game with simple physics like Donkey Kong or Castlevania is quite easy, and you can probably already do that. Those games have fixed movement speed, fixed jump height and fixed jump arcs, so the main problem is making collision.
If you want to make a game like Super Mario Bros that has acceleration-based movement, variable height jumping and mid-air controllable jumps, you need to know some basic physics (dynamics) and fixed point math (which is actually very easy). There are probably tutorials to make such platform games for other platforms so it shouldn't be that hard to learn.

I've been experimenting with action game basics including acceleration-based movement, variable height jumping, mid-air controllable jumps and collision in a demo downloadable from here. The collisions are still not perfect but I think the movement and jumping physics works quite well. Unfortunately I'm not sure where I put the source code to this demo, but if you are struggling with the same things you can probably get some hints from that thread. This article is also a recommended read and it fully applies to NES.

User avatar
NeverCameBack
Posts: 41
Joined: Mon Feb 24, 2020 12:22 am

Re: Anyone have programs containing best practices?

Post by NeverCameBack » Sat Mar 28, 2020 12:37 pm

Hey thanks a lot for those links - I will be sure to check out your jump physics engine, and also the Lizard demo. That is basically what I was wondering here, so my question is answered. The techniques in NerdyNights is what I've been learning from so far. It is encouraging and exciting to know that game development principals aren't so different between platforms. I'm hoping to get a solid foundation by making a playable NES game. I am not a coder by trade or even by hobby, so assembly is for the most part the first language I'm learning, aside from some industrial automation stuff.

User avatar
Controllerhead
Posts: 165
Joined: Tue Nov 13, 2018 4:58 am
Location: $4016
Contact:

Re: Anyone have programs containing best practices?

Post by Controllerhead » Tue Jul 14, 2020 6:00 pm

NeverCameBack wrote:
Tue Mar 17, 2020 10:50 pm
Are there any good sample .asm files that you know of
I use / reference examples from 6502.org, they aren't game specific, but there are a ton of useful snippets and good practices.
http://6502.org/source/

I use this for quick instruction reference.
https://www.masswerk.at/6502/6502_instruction_set.html

And of course, the manual!
http://users.telenet.be/kim1-6502/6502/proman.html
Image

Bananmos
Posts: 532
Joined: Wed Mar 09, 2005 9:08 am
Contact:

Re: Anyone have programs containing best practices?

Post by Bananmos » Wed Jul 15, 2020 9:57 am

In all honesty, the best advice in today's age is: Defer writing of the actual 6502 code as long as you can.

A lot of the games of the (g)olden days are a lot crappier than we remembered them gameplay-wise. Partially this was due to pioneers still trying out what worked. But a lot of it probably also has to do with the fact that iterating on gameplay mechanics was very expensive when coding directly on the system.

A few companies (like Nintendo) did manage to do this really well, always putting controls and gameplay first. But the challenge of iterating on fixed-point math on a 6502 often meant this crucial element was not prioritized in other companies.

Today you have the luxury of using modern programming tools and environments, not only for data creation and conversion, but also for prototyping your ideas before trying to squeeze them into the NES's code space. And an approach that I wish more people would try is using the benefit of emulators using Lua to get a good prototype of your game before you dig into assembly.

Basically:
1) Find a good game engine that uses Lua for scripting, that allows dynamic reloading of Lua scripts and has a decent visual editor. Not sure which one to be honest, but there are quite a few alternatives AFAICT.

Try to make a game that respects all the NES's limitations graphics-wise, and doesn't seem to be too heavy for an NES. (i.e., don't try putting Box2d or other advanced physics code in there)

2) Rewrite all that Lua game code, quantizing everything to fixed-point rather than floating-point. Avoid any divisons, and be very selective about multiplications as well. Verify that you can get the same smooth gameplay even with this approximate math.

3) Use a modern NES emulator's ability to run Lua scripts to get you easy-to-maintain-code running working with a small 6502 core. Focus on getting the rest of all that engine code for your game (level loading and storage) that doesn't need the "magic touch" of well-balanced gameplay.

4) Gradually convert your gameplay from Lua to actual 6502 code, step-by-step.

While it sounds a bit off-putting to re-do the same work multiple times, I'm certain you'll save time on average this way, exactly because you won't spend all that time debugging complex game logic code that you'll likely throw away anyway as part of the gameplay tweaking process. To get those elements right the first time, you would have to have both a lot of intuition for how to write well-balanced gameplay. Most of us don't have that, so reducing the cost of experimentation early is key to success.

Just my two cents... others might disagree :)

Pokun
Posts: 1517
Joined: Tue May 28, 2013 5:49 am
Location: Hokkaido, Japan

Re: Anyone have programs containing best practices?

Post by Pokun » Thu Jul 16, 2020 2:09 pm

That's probably great advice once you become a more experienced programmer, but OP said he only just started out and only knows 6502. Learning a high level language just to create a prototype sounds very ambitious at that level. I think it's OK to create a lot of crappy games (or attempts of games) just to build experience.

Bananmos
Posts: 532
Joined: Wed Mar 09, 2005 9:08 am
Contact:

Re: Anyone have programs containing best practices?

Post by Bananmos » Thu Jul 16, 2020 2:49 pm

Pokun wrote:
Thu Jul 16, 2020 2:09 pm
That's probably great advice once you become a more experienced programmer, but OP said he only just started out and only knows 6502.
All the more reason for complementing the low-level machine code learning curve with the very high-level. To get where you want, you'll need to get confident in both domains and gradually see where they meet.
Learning a high level language just to create a prototype sounds very ambitious at that level. I think it's OK to create a lot of crappy games (or attempts of games) just to build experience.
That's where I disagree. Learning everything from the ground up (as 6502 coding will inevitably be) is a great learning exercise indeed. But you will find things you thought should be easy to be unreasonably hard. Learning a high-level / scripting language shows you the other end of the spectrum, and eventually you'll realise how to marry the two worlds.

Besides, messing around with Lua scripts for emulators is generally fun and a learning exercise in itself.

But yes, it all comes down to personal goals. If a lot of crappy-attempts-at-games in 6502 code is rewarding in itself and one enjoys that process, there's absolutely nothing wrong with that path.

I just don't agree it's the easiest way for beginners given the tools / tech we have access to today. Back in the 6502's heyday, you'd be considered mad to start writing 6502 machine code on your home computer as a first language, without messing around with BASIC first.

You could kind of view Lua ROM scripts as the modern equivalent of BASIC for the NES, just void of the performance issues (and obviously comes with the limitation that the Lua code won't run on the real thing).

calima
Posts: 1207
Joined: Tue Oct 06, 2015 10:16 am

Re: Anyone have programs containing best practices?

Post by calima » Fri Jul 17, 2020 12:40 am

Lua
Or you could skip that entirely and just write C for NES. Still fast to iterate and most likely fast enough to run, if not, easy to optimize only the bottlenecks.

Bananmos
Posts: 532
Joined: Wed Mar 09, 2005 9:08 am
Contact:

Re: Anyone have programs containing best practices?

Post by Bananmos » Fri Jul 17, 2020 4:07 am

calima wrote:
Fri Jul 17, 2020 12:40 am
Lua
Or you could skip that entirely and just write C for NES. Still fast to iterate and most likely fast enough to run, if not, easy to optimize only the bottlenecks.
Sure that's an option. But then you would be missing out on the very things I emphasized.
- You won't learn much about how the 6502 *really* runs code, because the compiler sorts it out for you.
- And you won't see the advantages of how much easier a dynamic high-level language can be to work with and iterate on.

Instead you end up with a compromise. Compromises aren't bad per se, but I think especially for people starting out in programming, compromises tend to limit your imagination, and getting an appreciation for the extremes is actually more valuable.

Which is why I suggested approaching the problems from two opposite: The low-level and the high-level. And I think we very easily miss out on how the Lua-support in emulators can help you with exploring the high-level / low-level domain and still connecting them together.

But arguably the C route does save you the effort of having to prototype-and-then-rewrite your code. Though I'm still pretty sure you'll end up doing so anyway - might as well enjoy the process.

And C/C++ (while heavily overrated IMO) certainly ain't a bad thing to have in your skill set either, even if the quality of that skill set is kind-of-debatable when most of the advice to get C code efficient on a NES is contrary to usually-good-programming practices. But I suppose making a mental note that "this ain't how you *really* ought to do it" is good enough in this case. The two new advertised compilers also look a lot more promising than cc65, and might need less "bad practices" to get quality code.

But at the end of the day, it's up to what personal goals the op has for his learning.

Post Reply