JavaScript is not a Lisp and a web browser is not a Lisp machine. But, you know, definitely more Lisp machiney than most of the things that everyone has installed. I like how alive things are in them, and how lots of fun stuff is like very within reach if you’re in one :)
Anyway, this is best viewed in a browser with JavaScript enabled. You can click the play/run-buttons to run the JavaScript in the embedded editors. It should behave mostly like running the same code in the JavaScript console. (Hitting F12 or something and finding the console might also be useful if you wanna look around and inspect stuff more or something.)
#
is wall.
is goal square@
is player, or +
if the player is on a goal square$
is box, or *
if the box is on a goal squareWe have a variable called outElement
here. outElement
is just some div
, and like it’d also work to just do document.body.appendChild(...)
or something, but outElement
is automatically moved to where we’re at when we run some code:
The sprite sheet is a set of 8 sprites. Each sprite is 16×16 pixels.
We’ll create a canvas and put it in the outElement
.
Then we can get a `CanvasRenderingContext2D` and use `drawImage` on that.
Also we turn off image smoothing because pixels. Also also, we multiply some numbers by 3, because more pixels.
We’re going to need objects with x
and y
properties for a bunch of stuff. First for positions of sprites. Later for positions in the level and also for directions the player can move in. We make a vector
data structure:
We’ll create an object for keeping track of where in the sprite sheet the different sprites are. Then, by passing a bunch of arguments to drawImage
we can specify coordinates and width/height both for the part of the source image we want to draw and for the destination in the canvas. (The last 4 arguments are “destination” arguments. The ones that are multiplied by scale
.)
Is level.