I talk to some people about the fact that “I’ve never written a game”, meaning that I’ve never written code with a
It was a lot easier than I thought; got it done in about an hour, with less than 200 lines of code.
It’s pretty fun to write your own game, and see it in action :)
Here I do have some inefficiencies that I may want to avoid when developing a higher performing game.
- Frame rendering: I am drawing the entire background and re-drawing the objects on every frame. I believe there is a more efficient way of drawing the graphics on the canvas - for example only drawing the delta.
- Snake collision detection: In every frame I am comparing every cell of the snake with the position of its head in order to check for collision. I’m doing this through a simple
forloop. This can get pretty expensive when the snake becomes very long. Maybe I should have a more efficient way of searching through the array, for example setting the x and y dimensions as a key, and searching for the key. I’m sure key searching is a lot faster.
You can play it here:
Here’s the code: