SLua Snake Game
nya Resident
This game is scratch built in Lua in some free time I have had over the past 5~ days.
Initially started with just cells that change color, and green/yellow for food. Then added a sprite sheet, and realised (with some feedback) I was missing a few critical features:
- Snake could not "pause" for one frame before edges and collisions (iframes?)
- Sprite sheet was limited and could not handle "turns"
- Grid was not dynamic, could not change size
- Adding a menu, and support for multiple players would've required massive restructuring the code
So, obviously... I started over.
I did however distract myself for a moment with a feature complete high-score tracker, I've shared the code for that in Discord!
Anyway, since then I've added a few more features:
- Lua object (metatable) for the player, allowing easy reset that state, and later multiple players
- Lua object (metatable) for the world, allowing for dynamic grid size
- Sprite table for simplified lookup i.e. SPRITE_LOOKUP.HEAD[direction], andbit32.borlogic for turns
- Sprite font rendering for the score, and game over message
- State management, and render on demand (update methods return trueif we need to render)
- Error catching during update/render loop (see the blue screen screenshot!)
I do plan to share some more findings once I've had a chance to start looking at multi-player (and separate scripts that buffer controls), plus some menu logic.
Finally, I want to say that Lua brings a lot of convenience to the
table
. It allows for the creation of objects with methods (organisation and encapsulation), wrapping events with callbacks (again more encapsulation), and ease of data lookup; with in-memory (key-value stores), and multi-dimensional arrays.Given the above, don't think I would have attempted to build this in LSL. It would have taken at least five times longer, and managing strided lists would have been quite challenging. Additionally, if I needed to revisit the LSL code later, it would have been difficult to debug and maintain.
Log In
nya Resident
Some more notes...
- Game loop has a 0.1s timer (10fps), could go faster if you wanted more difficulty
- SLPPF is only called for cells that change
- SLPPF calls are queued and committed in one call using a method like this once per loop (if anything changed)
- Calls to ll.SetLinkTextureAnimare awkward as those params are not in SLPPF
- requirewould make this nicer, as the script is pretty long now š