Skip to content

Instantly share code, notes, and snippets.

@ssaurel
Created January 18, 2024 15:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ssaurel/719918967c6bc4d5a0bde7252fdfc617 to your computer and use it in GitHub Desktop.
Save ssaurel/719918967c6bc4d5a0bde7252fdfc617 to your computer and use it in GitHub Desktop.
Step 2 for creating a Snake on the SSaurel's Blog
class Snake {
constructor(ctx, bw, bh, nbx, nby, fps) {
this.ctx = ctx;
this.bw = bw;
this.bh = bh;
this.eltw = bw / nbx;
this.elth = bh / nby;
this.nbx = nbx;
this.nby = nby;
this.dirx = 1;
this.diry = 0;
this.marginx = this.eltw / 10;
this.marginy = this.elth / 10;
this.keyup = false;
this.keydown = false;
this.keyleft = false;
this.keyright = false;
this.startfps = fps;
this.init();
}
init() {
this.head = { x: this.nbx / 2, y: this.nby / 2 };
this.tail = { x: this.nbx / 2 - 2, y: this.nby / 2 };
this.elements = [this.tail, { x: this.nbx / 2 - 1, y: this.nby / 2 }, this.head];
this.food = this.generatefood();
this.points = 0;
this.level = 5;
this.fps = this.startfps;
this.fpsinterval = 1000 / this.fps;
}
// More to come ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment