In this tutorial I show you how to code in Retro Gadgets by creating a 2D platformer!
Introduction
In this video, I show you how to create the following features:
- Time based animations
- Player movement controller
- Play audio
- Sprite Collision
- Save game + Auto save
- Random spawning mechanic
I have created sections in this guide for some of the major features, and you can find the completed gadget on the workshop! I recommend following along with the video, but you can also view the code below. This is mainly a tutorial on how to code in Retro Gadgets, however if people plan to use the code for a 2D platformer it definitely needs updating. I may come back and update the guide and project in the future.
If you found this guide helpful, I would greatly appreciate if you rated it and left a comment on how I can improve!
Video
Interfacing with chips
We need to initialize each chip that we added and configure them for our project.
Importing Assets
Once we have the ROM configured, we can import assets that we uploaded and import built in assets such as a font. ROM stands for read only memory, and data stored here cannot be changed after compilation.
Here’s the sprite sheet for example. Notice how I have the right facing sprites in one row and the left facing sprites in another. This will make it easier to animate them later. All assets need to be placed inside the import folder which can be found in the main menu.
The photo is blurry because the sprites are only 16px tall.
Player and Coin Variables
Setup the variables that will be used by the player and coin. We have various tick counters in order keep track of time for our player animations, coin spawn rate, and random seed for coin position.
Save and Auto save
In order for saving to work correctly, you need to initialize the variables that you want to save if a save does not exist. Otherwise, when you try to use those variables they will be nil.
should be called right after your local variable initialization. You can only save a single table to memory and tables in LUA operate with key -> value pair mapping. This is similar to dictionaries in other languages like Java.
Update Function
It is good practice to keep your update function clean and not to write too much logic here. Let other methods handle your functionality. It would be a good idea for me to make an auto save function which calls save every 60 ticks rather than have that logic here. 60 ticks is roughly equal to 1 second when a program is running at maximum speed.
Increment Tick Counters
Draw Score
We draw the score every frame with it’s own function just in case we want to update it later.
Movement Controller + Animations
This is a basic controller that I made to handle animations. I explain the whole controller in the video. The only thing to note is that I do not handle when the player pressed down on the DPad so the character disappears for a second.
The time based animation can be seen in how we change the character animation based on the tickCounter variable. 15 ticks is roughly equal to 1/4 of a second, and at 30 ticks we reset the counter to repeat the animation. The running and jumping animations are comprised of 2 frames each. While the running animation is based on time, the jumping animation is based on the player height in the scene. This wouldn’t work if the player is expected to jump on other objects.
Random Coin Spawning
This is how we spawn the coin in a random position on the screen between 1-56. Since our coin is an 8×8 sprite, we draw the coin at a maximum of x = (64-8). In order to get random number generation working, we need to call the
function where globalTickCounter is a different number every time. This seed generation works similarly to minecraft world’s seed.
We also use the coinSpawner variable to determine if 180 ticks or roughly 3 seconds has passed before spawning a new coin.
Player Collision and Playing Audio
We are checking if the left side of our player is left of the center of the coin, and the right side of our player is right of the center of the coin. We then check if the height of the player is at least as high as the center of the coin. Since we statically spawn coins at y = 26, and the bottom of the coin is at y = 34, we can do this by checking if the player’s head is at least at y = 30.
That's everything we are sharing today for this Retro Gadgets guide. This guide was originally created and written by WORM RIDAAA!. In case we fail to update this guide, you can find the latest update by following this link.