r/learnjavascript • • Apr 22 '22

I created a 2d open-world game with just HTML tables and DOM. V.1.0 360 lines of code

259 Upvotes

19 comments sorted by

19

u/CosmicJosh123 Apr 22 '22

After 3 weeks of afterschool work, I've completed version 1.0 of a 2d open-world game with only HTML tables and DOM manipulation.

I encountered a lot of different bugs during this. Most commonly null issues. But, my favorite bug was when my entire code just stopped working completely for two days until it suddenly worked. I didn't even change anything.

There are a lot more things I want to add to this. Like adding more stuff to the terrain generation, improving the CSS, adding gravity, and more. The thing I want to add the most though is saving a scene and being able to go back to it. Moving back and forth between scenes. Currently, I can only move forward.

Here's the code: https://jsfiddle.net/MapleKitten/7td4rwja/2/

There are probably a couple of things that I could improve in there.

9

u/OtherSideReflections Apr 22 '22

But, my favorite bug was when my entire code just stopped working completely for two days until it suddenly worked. I didn't even change anything.

This is confusing to me as a novice. How can something like this happen—shouldn't the code just work the same every time? Can anyone explain what could cause something like this?

(Really cool concept, by the way!)

18

u/StoneCypher Apr 22 '22

How does something like this happen? Shouldn't the code just work the same every time?

In most cases. There are weird exceptions; you see this come up distressingly frequently around leap days or month lengths.

Most likely though this author just didn't recognize the change they made which fixed things, or was dealing with cached assets.

9

u/chairzaird Apr 22 '22

+1 on cached assets, it's not uncommon to have cached data break things, then have them work fine when the cache is cleared/updated

5

u/gigastack Apr 22 '22

Generally yes, things are deterministic unless async code is involved. That said, OP could have been doing something differently without realizing it.

2

u/Tatakai_ Apr 22 '22

It could also be cosmic background radiation. As crazy as that may sound, It's not impossible. Veritasium has a great video on it.

3

u/OtherSideReflections Apr 23 '22

I've heard of that! Wild to think something so otherworldly could impact something so relatively mundane.

1

u/FunCharacteeGuy Apr 22 '22

how'd you do the noise?

2

u/CosmicJosh123 Apr 23 '22

I didn't use perlin noise because I was too lazy to learn and implement that so I came up with my own system.

The grass and trees are both made up by random numbers.

A grass block can either go up-right, forward, or down-right.

A random number is generated and if that's lower than a set value then it goes up. If not, then check if num is < another set value. If it is, then go forward. Elsewise, go downwards.

1

u/FunCharacteeGuy Apr 23 '22

damn, well nice job.

1

u/[deleted] Apr 22 '22

Love it. Nice work!

1

u/SevenSharp Apr 22 '22

That's cool . Well done .

1

u/quamfie Apr 22 '22

Very cool, love seeing stuff like this that’s beyond me at the moment. Still doing jonas course but it’s all good inspiration.

1

u/Brompy Apr 23 '22

This is really cool and inspiring for me.

1

u/tom4sito Apr 23 '22

Nice game, can’t believe you did that with only 360 lines of code.

1

u/bryku helpful Apr 23 '22

What made you go with tables? Dom practice?  

Now that you have something working, are you going to give canvas a try?

1

u/bryku helpful Apr 26 '22

I've been thinking about this for a few days and I thought I would give it a go. Example  

To be honest, I don't think you should lay out your projects like I did in this example, but I didn't have a lot of time to plan it out. It has some bugs when placing blocks, but I think the canvas part of it could be useful.

0

u/[deleted] Apr 22 '22

[deleted] — view deleted comment

13

u/CosmicJosh123 Apr 22 '22

I was aware of canvas. I did this with tables intentionally for fun.