r/programming • • Sep 13 '16

Super Mario source code in java by Brent Aureli. (Today marks 31st anniversary of Super Mario Bros. the first Super Mario ever)

https://annot.io/github.com/BrentAureli/SuperMario/blob/master/core/src/com/brentaureli/mariobros/Sprites/Mario.java
901 Upvotes

103 comments sorted by

5

u/_BrentAureli_ Sep 16 '16

Hey this is me! I was wondering where all the extra traffic came from! Thanks for all the positive responses!

1

u/_monster89 Sep 19 '16

No no, thank you ! You did an awesome job enlightening myself and numerous people.

50

u/oh_lord Sep 13 '16

I've watched this guy on Twitch before, he's incredibly nice. He was off working on something entirely different, but when he noticed one or two people show up in his chat, took the time out of what he was doing to answer my questions and show off all his stuff to me. His tutorials are also very well laid out and informative. This series actually managed to convince me to crack open Java again.

81

u/ffs_4444 Sep 13 '16

DMCA in 5... 4... 3...

Why not call it Italian Plumber Platform Game, or something?

60

u/KimJongIlSunglasses Sep 14 '16

Extraordinary Vinny and sibling.

0

u/[deleted] Sep 14 '16

[deleted]

4

u/SAGNUTZ Sep 14 '16

I just forgot all the actual names because of you!

0

u/[deleted] Sep 14 '16

[deleted]

2

u/SAGNUTZ Sep 14 '16

What aboot the Poopem kroocha's?

4

u/OrionBlastar Sep 14 '16

I sort of thought someone would make a SMB clone using Tux from Linux and his brother Tex from Texas.

6

u/pdp10 Sep 14 '16

And this is how we get penguins and weasels in every knockoffreimplementation. Way to go, guys.

4

u/rydan Sep 14 '16

DMCA doesn't apply. There is nothing copyrighted in that code.

5

u/redwall_hp Sep 14 '16

Technically. However, it's typically used as a big cudgel whether it applies or not. The assumption is most people won't have the desire or resources to go to court over it, so it stays down.

1

u/dbldub Sep 14 '16

The music and the title aren't copyrighted?

1

u/ccfreak2k Sep 15 '16 edited Jul 31 '24

paltry aspiring important cause ripe point memory illegal school joke

This post was mass deleted and anonymized with Redact

35

u/glindhol Sep 13 '16

The guys from id Software also made their own port of SMB3 to the pc, would love to see the source code for that. (its more of a demo than a port, tbh)

23

u/humoroushaxor Sep 13 '16

Read about this in Master's of Doom. Crazy how big of a deal this was.

11

u/spiral6 Sep 14 '16

For those who haven't read it, the side-scrolling technique for the screen was revolutionary for the time. No software prior was anything that smooth. They then used it for one of their first games, Commander Keen.

10

u/ihahp Sep 14 '16

on the PC. Commodore 64 and amiga has smooth af scrolling for a long time before this.

6

u/robisodd Sep 14 '16

And, you know, the NES. From which Super Mario 3 was originally on. Though C64 and Amiga were first.

5

u/argv_minus_one Sep 14 '16 edited Sep 14 '16

Because they had fancy graphics processors to do the work. The IBM EGA, as far as I know, was a dumb frame buffer device with very little in the way of fancy tricks like that.

Also, its memory was organized into color planes, and only parts of the memory were mapped into the CPU's address space, requiring bank switching. This must have been hideously slow for animation.

VGA was nicer about that: the entire video memory was mapped into the PC's address space at all times, and it had a packed-pixel mode more like how modern graphics hardware works.

Even the VGA was still a dumb frame buffer, though. In particular, it didn't provide any hardware sprite drawing or scaling. Wolfenstein and Doom did this work entirely in software, taking advantage of the PC's strength: the CPU of a typical PC ran circles around the consoles of the time. They would handle sprite drawing through sheer brute force instead of fancy hardware tricks, and it worked marvelously.

This difference in CPU performance was perhaps most starkly demonstrated by the SNES port of Doom. The SNES CPU was so slow that they ended up including a whole separate computer on the cartridge, and the game still looked and ran like ass.

1

u/ihahp Sep 14 '16

The amiga was planar too, but yeah, it had a video chip (the blitter) that could move it around pretty quick (even though it was planar)

1

u/argv_minus_one Sep 14 '16

And then, for their next game, they completely threw out the whole idea and went with a first-person perspective, which by nature has to redraw the entire screen every frame anyway. Funny how that works out.

5

u/Andersedt Sep 14 '16

Unless of course you don't draw the floor or ceiling! (which was their technique for wolf3d)

3

u/Narishma Sep 14 '16

Or the walls, which they did (or rather didn't do) in their first FPS release Hovertank 3D.

5

u/theinternetftw Sep 14 '16

Here it is in an online emulator, so you can play it in-browser.

The exe and data files can be downloaded from that link as well, for those who'd like try their hand at reversing it.

1

u/aperson Sep 14 '16

Oh wow, seeing your username takes me back. I started playing Minecraft because of you.

12

u/175Genius Sep 13 '16

Learned a lot from his tutorials making Super Mario in libgdx.

11

u/VGPowerlord Sep 14 '16

Just to make it easier to check out the code, here's the GitHub repo.

0

u/[deleted] Sep 14 '16

[deleted]

1

u/RobIII Sep 14 '16

mlemaudit ?

0

u/[deleted] Sep 15 '16

[deleted]

1

u/RobIII Sep 15 '16

He's a user on GitHub who has reported all the bugs for this super Mario project.

Yeah, I noticed after looking at the issues for Mario; at first I didn't understand.

I wanna know who mlemaudit is. It appears to me he just goes around auditing people's code on GitHub, which is freaking awesome of him to do.

He seems to go around finding bugs in other people's code

He doesn't seem that active to me?

a process known as code auditing.

That I knew ;)

21

u/salgat Sep 13 '16

He used Box2D also! Kind of a bummer, since this is the easy way out as far as what I consider the hardest and most interesting part of implementing Mario (the original developers did the physics simulation in assembly with no libraries).

19

u/TheSecretExit Sep 14 '16

Very hard. I never really realized just how complicated platformer physics are until I tried to code them myself.

2

u/grimeMuted Sep 14 '16

TIL people use Box2d for normal platformer physics. I thought it was only used for physics platformers with more realistic (as much as anything can be in 2d) physics, e.g. games where the player is picking up boxes and stacking them to solve puzzles and the objects do things like slide down slopes or rotate after bouncing on a corner. The idea of using such a powerful physics engine for a game with such stylized, rotation-fixed physics isn't something I would have thought of doing.

-1

u/argv_minus_one Sep 14 '16

Why reinvent the wheel? If the project's goal was just to make Mario in Java, then that doesn't have to involve making graphics primitives for Mario in Java.

7

u/AngusMcBurger Sep 14 '16 edited Sep 14 '16

Because we're talking about two totally different wheels here: physics libraries are rarely used in platformers, any good platformer game will use their own physics routines with lots of special cases to make the game feel good to play. See this for a breakdown of how SMB does its physics.

For example to have a maximum velocity for Mario, you would have to wrap your physics objects with hacky calls like this:

/* after applying forces from input for example */
b2Vec2 vel = body->GetLinearVelocity();
float speed = vel.Normalize();//normalizes vector and returns length
if ( speed > maxSpeed ) 
    body->SetLinearVelocity( maxSpeed * vel );

Having moving platforms is another thing that physics engines can never handle very well and this article goes over some other reasons.

0

u/adnzzzzZ Sep 14 '16

That article is absolutely wrong in a number of ways and it's sad to see people sharing it. I could go on specifics if you want me to.

Also there's nothing hacky about the code you showed there. Most 2D physics engines give you enough control over how everything moves that you can get it to feel however you want with some work, and this amount of work is pretty much always going to be lower than rolling your own physics engine.

physics libraries are rarely used in platformers,

Most indie platformers being made today are made using some engine like Unity and so use the physics engine used by that engine to handle movement and collision.

-2

u/argv_minus_one Sep 14 '16

physics libraries are rarely used in platformers

Platformers are rarely made at all.

See this for a breakdown of how SMB does its physics.

Okay, yeah, I am not enabling scripting for literally a dozen different domains just to view one picture on Photobucket. Please choose an image host that isn't complete dog shit.

For example to have a maximum velocity for Mario, you would have to wrap your physics objects with hacky calls like this:

/* after applying forces from input for example */
b2Vec2 vel = body->GetLinearVelocity();
float speed = vel.Normalize();//normalizes vector and returns length
if ( speed > maxSpeed ) 
    body->SetLinearVelocity( maxSpeed * vel );

I see nothing hacky about this code.

I do see a potential API design problem, though: (Get|Set)LinearVelocity act on a velocity vector. Shouldn't methods by that name act on a scalar linear velocity value instead?

Also, I'm no expert on vector math, but isn't maxSpeed * vel the wrong operation? You're trying to limit the linear velocity, not increase it further.

Having moving platforms is another thing that physics engines can never handle very well and this article goes over some other reasons.

Sounds more like those are issues with this particular physics engine. I don't see how those issues are inherent to 2D physics engines in general.

1

u/AngusMcBurger Sep 14 '16

First off by physics engine, I mean a library that simulates static + dynamic bodies which have momentum, weight, whatever else. It basically tries to simulate objects realistically. Whereas in platformers and other 2D games, the physics behaviour deviates in all sorts of ways from the real world, which is why they tend to be a bad fit.

Platformers are rarely made at all.

This thread is about Super Mario Bros, a platformer, so in this context that is an irrelevant thing to take issue with.

Okay, yeah, I am not enabling scripting for literally a dozen different domains just to view one picture on Photobucket. Please choose an image host that isn't complete dog shit.

If you'd looked, I posted a direct link to the picture, so no need to enable scripting at all if that's what floats your boat.

[API nitpick]

I just copied that example from the article, it's not my code.

Sounds more like those are issues with this particular physics engine. I don't see how those issues are inherent to 2D physics engines in general.

If you had read the article, it refers to all sorts of things just inherent to 2D physics engines, such as trying to stick the player to the moving platform so they don't repeatedly fall and stop as it descends, but so that they can run, walk and jump off it, get knock back from projectiles, and be pushed off when there's no vertical clearance for the player. You end up retro fitting all sorts of special cases, building up your own physics code around the physics engine. If you wrote custom code things can have set max/min velocity, fall speed, weight, etc.. to avoid the issues from the physics engine not being the right choice in the first place.

0

u/[deleted] Sep 14 '16

[deleted]

3

u/AngusMcBurger Sep 14 '16

Thanks, I've changed the link

3

u/salgat Sep 14 '16

I'm not saying what he did is wrong, I was just hoping it was an actual implementation of Mario, not insert random platformer with Mario skins. He left the most interesting part of coding Mario to a library that implements it in a completely different way.

-3

u/argv_minus_one Sep 14 '16

You were able to build, run, and play the game, then? You saw for yourself how the physics are?

2

u/salgat Sep 14 '16

Box2D didn't exist when Mario was made...

-2

u/argv_minus_one Sep 14 '16

Answer my question.

2

u/salgat Sep 14 '16

What's the point you're trying to make? You can see in the source code he provided how the physics is implemented.

-4

u/argv_minus_one Sep 14 '16

Answer my question. Yes or no.

3

u/salgat Sep 14 '16

Did I build his game from source and play it, no? Can you answer mine now?

-4

u/argv_minus_one Sep 14 '16

Then you have no idea how the physics actually are in this game, so you are not qualified to criticize it on not being close enough to the original.

→ More replies (0)

1

u/[deleted] Sep 15 '16

Can tell from the code it's not right, if you know how that library works and you know the original game (because Mario has some unique physics).

As a platform game tutorial it's useful for junior or student game developers. As a Mario game, it's very obviously not right.

-2

u/arsv Sep 14 '16

Mario for 6502 was kinda notable for squeezing a lot out of the tiny slow chip.
Using Box2D in Java on Android is exactly the opposite.

Mario is also kind notable for being remembered & played 30 years after its release.
I wonder if the stuff being written now, using best practices and never re-implementing the wheel, will age just as well.

1

u/argv_minus_one Sep 14 '16

Mario for 6502 was kinda notable for squeezing a lot out of the tiny slow chip.

And? That is blatantly unnecessary now. The cell phone I carry around in my pocket could run the original SMB in its sleep. No 2D platform game is worth optimizing that tightly any more.

Using Box2D in Java on Android is exactly the opposite.

This sounds suspiciously like the tired old “Java is slow” myth…

Mario is also kind notable for being remembered & played 30 years after its release.

Because of how the game played, not because of how it was implemented. Few people even know how it was implemented, let alone care.

2

u/arsv Sep 14 '16

Because of how the game played, not because of how it was implemented.

The "feel" of the game was I think in part due to the way physics simulation was implemented. For this reason alone, doing it explicitly instead of relying on (grossly overpowered) external library would make a lot of sense.

And? That is blatantly unnecessary now.

Optimizing, yes. Showing how simple the implementation can be for certain nifty things, like say Mario? No, I thinks it's still really important. And that's what salgat was probably looking for.

-1

u/argv_minus_one Sep 14 '16

Showing how simple the implementation can be for certain nifty things, like say Mario?

That's already been done, by the original game. No point in doing it again.

2

u/MorrisonLevi Sep 14 '16

The cell phone I carry around in my pocket could run the original SMB in its sleep. No 2D platform game is worth optimizing that tightly any more.

Unless it saves power. Praise be to the developers who consider power usage when writing mobile apps.

1

u/grimeMuted Sep 14 '16

Using Box2D for a simple platformer with stylized, rotation-fixed physics is like using a full 3D graphics engine to build a simple 2D platformer. You have to intentionally cripple almost all of the cool functionality of the Box2D engine and force it to perform simple tasks in its own rather complex manner.

For this reason it feels intuitively wrong even though it might end up being a very good decision.

6

u/awsmgyc Sep 14 '16

What was the original game written in?

16

u/yoyohobo665 Sep 14 '16

6502 assembly

1

u/KoboldCommando Sep 14 '16

A modified version of 6502 assembly (i.e. basically machine code). As far as I know, while they certainly had some scripts and tools to help out, all the games were programmed in assembly directly.

1

u/cparen Sep 14 '16

I thought some of the later NES games were developed in C too?

5

u/INTERNET_RETARDATION Sep 14 '16

Programming a NES game in C is like programming a 3D game in Python. Just because you can doesn't mean you should.

1

u/cparen Sep 14 '16

I wouldn't be so sure. Python is still orders of magnitudes behind C in performance for various reasons, but current optimizing C compilers beat or equal hand written assembly code for the kinds of integer arithmetic and code flow you'd see in an NES game.

It really depends on whether there existed a decent optimizing C compiler for the hardware or not.

5

u/[deleted] Sep 14 '16

current optimizing C compilers beat or equal hand written assembly

Keyword: current. Until a decade or so ago, hand written assembly (by a good programmer) was able to lap compiled code. They used assembly over high level languages like C for the same reason many modern projects use C over 4th general languages like Python.

4

u/INTERNET_RETARDATION Sep 14 '16

I know, but that's my point. You could do it, but why would you? The entire industry was using assembly, and although later C compilers optimize really good, things like memory mapped I/O are still annoying to do in C.

2

u/[deleted] Sep 14 '16

Nah, I don't think a single NES game was coded in C. I think C didn't become a force in console gaming until the Playstation/Sega Saturn generation.

1

u/_monster89 Sep 14 '16

Did you find the bug from looking at the code or playing it?

6

u/[deleted] Sep 14 '16

Wish there was one of these guys on C++, not that java isn't awesome, it's not my focus.

3

u/pdp10 Sep 14 '16

Jonathan Blow used to live-code games in ++, I'm told. Maybe it's all here?

0

u/[deleted] Sep 14 '16

They are too busy fixing segfaults before they show off their code

2

u/walnutmon Sep 14 '16

i ran gradlew.bat in gitshell for windows, that worked to install gradle (which i didn't have), when gradle actually started to build the project, it complained about the android SDK not being present -- going to try to figure out how to only build the project target "desktop" in the build.gradle file tomorrow

3

u/seb_02 Sep 14 '16

To run it:

./gradlew assemble
./gradlew run

You need to have an Android device connected for run to work.

2

u/coolkid1717 Sep 13 '16

Wow super Mario runs in that little of code? (Fyi I know next to nothing about coding) is all of the world programed into that?

26

u/domstang68 Sep 13 '16

No, when you open the page there are files in the top right. This main page of code has lots of import statements, which import other code files. This makes it easier to keep track of stuff.

Think of it like a book. Each chapter is separated then brought together by a table of contents. Each import is a "chapter", and this is the "table of contents".

As for the art work, those are different kinds of non java related files that they process into something it can use, which turn into the graphics on your screen.

8

u/coolkid1717 Sep 13 '16

Thanks for the explanation.

2

u/Narishma Sep 14 '16

It's not Super Mario, just a generic platformer with the name slapped on.

1

u/busydoinnothin Sep 14 '16

I tried playing with this but gradle was giving me problems, not sure if its cuz of the IDE (I use Netbeans) or just the fact that I've never used gradle before. Anyone get it workin?

1

u/Magnesus Sep 14 '16

Nice, in libGDX. I made my platformer in it too. Platformers turned out extremely hard and time consuming, I regret even starting making one, so much time so little interest.

1

u/HumpingJack Sep 14 '16

Platformers are hard as opposed to what?

4

u/Magnesus Sep 14 '16

Puzzle games for example.

2

u/antlife Sep 14 '16

I think he means anything is hard to finish when you find you have no passion for it, at least that's what I read.

1

u/stthecla Sep 14 '16

would this run on android? it's java right?

1

u/walnutmon Sep 14 '16

i like this

one thing that is vexing me, where is the level file?

I see it loaded in Java, but I can't find the file in the project

1

u/walnutmon Sep 14 '16

found it, in the android project

1

u/[deleted] Sep 14 '16 edited Mar 15 '17

I have left reddit for Voat due to years of admin mismanagement and preferential treatment for certain subreddits and users holding certain political and ideological views.

The situation has gotten especially worse since the appointment of Ellen Pao as CEO, culminating in the seemingly unjustified firings of several valuable employees and bans on hundreds of vibrant communities on completely trumped-up charges.

The resignation of Ellen Pao and the appointment of Steve Huffman as CEO, despite initial hopes, has continued the same trend.

As an act of protest, I have chosen to redact all the comments I've ever made on reddit, overwriting them with this message.

If you would like to do the same, install TamperMonkey for Chrome, GreaseMonkey for Firefox, NinjaKit for Safari, Violent Monkey for Opera, or AdGuard for Internet Explorer (in Advanced Mode), then add this GreaseMonkey script.

Finally, click on your username at the top right corner of reddit, click on the comments tab, and click on the new OVERWRITE button at the top of the page. You may need to scroll down to multiple comment pages if you have commented a lot.

After doing all of the above, you are welcome to join me on Voat!

1

u/DrecksVerwaltung Sep 14 '16

Inb4 copyrekt by cuntendo

0

u/argv_minus_one Sep 14 '16

Cannot build the desktop game without having an Android SDK installed. Okay then. ಠ_ಠ

2

u/walnutmon Sep 14 '16

just remove the android project references, that worked for me

-12

u/skocznymroczny Sep 14 '16

I can't imagine such a responsive game working properly in a bloated language with a garbage collector.

3

u/cparen Sep 14 '16

Counterpoint: many responsive games use Lua. Yes, Lua's GC is tuned for shorter pauses, and yes the Lua code and heap comprise a small fraction of the game. Just pointing out that GC != unresponsive.

-18

u/HandshakeOfCO Sep 14 '16

LOLz. Spent all of 30 seconds looking at this, and found a huge bug - there's no limit on the number of fireballs spawned, whereas in real mario, there's a limit of 3.

4

u/argv_minus_one Sep 14 '16

I'd say that's just an ordinary bug, not a “huge” one.

3

u/[deleted] Sep 14 '16

Sounds more like a change request than a bug ;)

0

u/[deleted] Sep 13 '16

[deleted]

8

u/voicelessdeer Sep 14 '16

Super cool, thanks for the link.

27

u/I_AM_GODDAMN_BATMAN Sep 14 '16

It's Mario not Zelda.

6

u/cediddi Sep 14 '16

It's link not zelda

8

u/voicelessdeer Sep 14 '16

No, it's a me, Mario!

1

u/[deleted] Sep 14 '16

I have a hat.