Created
January 18, 2024 15:11
-
-
Save ssaurel/719918967c6bc4d5a0bde7252fdfc617 to your computer and use it in GitHub Desktop.
Step 2 for creating a Snake on the SSaurel's Blog
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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