Skip to main content

Creating the Snake

Let's take a look at the main component of the game: The snake. There are several ways to implement one.

To keep things tidy, I recommend you'd create a new file called snake.nelua. This file contains two records:

  • The Point - Holding X and Y coordinates
  • The Snake - The actual snake implementation

The content is rather simple:

require "wasm4"local sequence = require "sequence"
local snake = @record{}
local snake.Point = @record{  x: int32,  y: int32,}
local Point = snake.Point
local snake.Snake = @record{  body: sequence(Point),  direction: Point,}
local Snake = snake.Snake
function Snake.init(): Snake  return Snake{    body = {      { x = 2, y = 0 },      { x = 1, y = 0 },      { x = 0, y = 0 },    },    direction = { x = 1, y = 0 },  }end
return snake

The snake record contains the body and the current direction of the snake instance. But it lacks any functionality for now.