Tuesday, August 03, 2021

Creating Legend of Zelda in Java (and my struggle for it)

I tried to re-create NES version's Legend of Zelda in Java.

Legend of Zelda (NES, 1986)


It wasn't so easy but a fun experience. I had developed a few 2D games before but never really tackled on this kind of action-oriented 2D game.


The challenging part:

1. How to create a map

In my previous 2D games, I used a text file to create a map. It's a pretty common way to create a 2D map and you can find a lot of tutorials about this method. However, this is not a great method when you want to create a large map. Not only it requires tons of inputting, since all the tile information is described by numbers, it is kind of hard to visualize the actual map image and which makes editing difficult.

Each number represents tiles.


So I decided to create a map/tile editor. I know there are already many tile editors exist but I thought developing one by myself would be fun. 

My tile editor (version 1)

My tile editor (version 2)


And it is done and I succeeded to re-create the entire Zelda field map. The editor made this process a lot faster and I was really happy about it.

My tile editor (version 3)



2. How to move monsters?

Moving player character only is relatively easy and I had done it before. But moving multiple monsters at the same time while controlling your player character was a new thing for me. The key is how to implement monster movement into the game loop. 

I created a class called DrawMonster (drawM) and instantiated this as an array.


So whenever a monster spawns, create a class like drawM[0], drawM[1]... and when you killed a monster, the class becomes blank. 

The DrawMonster class has methods that handle monster movement, monster hit detection, attack/dying animation, drop items etc.



So the game loop checks these classes in every game loop and update every monster status.



I also created:

DrawPlayer class

DrawObject class

DrawTiles class

DrawUI class

DrawDialogue class

These classes are checked in every game loop and the program updates all the status on the screen.



And here are some screenshots from the resulted game.

Legend of Zelda in Java


Legend of Zelda in Java


Legend of Zelda in Java


If you are interested, please check my video on YouTube as well.


(My Java Zelda is done... kind of)


Creating 2D games from scratch is fun but also pretty confusing. I often felt I was writing the code while I was not 100% sure what I was doing. I was repeating tries and errors and it was like, "Oh, I guess this works (but I'm not really sure why it worked) so let's use this", "Hmm it seems this doesn't work so let's try some other ways..." It was tough to wrap my head around all the classes and understand the whole system. 

I'm hoping to create 2D game tutorial videos to record my progress but I guess I need to sort out what I have learned a bit more before do that. I also feel that there are better ways to handle it. My code works fine but not organized well enough.


Man, programming is fun but tough!


No comments:

Post a Comment