snake: make game resizable by user
This commit is contained in:
+16
-1
@@ -169,6 +169,21 @@ class SnakeCore {
|
||||
document.getElementById("snakeHead").innerHTML = "x: " + this.body[this.body.length - 1].x + " y: " + this.body[this.body.length - 1].y;
|
||||
document.getElementById("snakeFood").innerHTML = "x: " + this.food.x + " y: " + this.food.y ;
|
||||
}
|
||||
|
||||
// Update width and height from page to match game
|
||||
getPageNumbers() {
|
||||
let specifiedWidth = parseInt((document.getElementById("snakeWidth") as HTMLInputElement).value);
|
||||
let specifiedHeight = parseInt((document.getElementById("snakeHeight") as HTMLInputElement).value);
|
||||
if (this.width == specifiedWidth && this.height == specifiedHeight)
|
||||
return false;
|
||||
this.width = specifiedWidth;
|
||||
this.height = specifiedHeight;
|
||||
while (this.height > this.board.length)
|
||||
this.board.push(new Array(this.width));
|
||||
this.canvas.setAttribute("width", String(specifiedWidth * this.grid));
|
||||
this.canvas.setAttribute("height", String(specifiedHeight * this.grid));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
class Bot {
|
||||
@@ -270,7 +285,7 @@ const g_snakebot: Bot = new Bot();
|
||||
// game loop
|
||||
function snakeloop() {
|
||||
// Reset of needed
|
||||
if (g_snake.gameover)
|
||||
if (g_snake.gameover || g_snake.getPageNumbers())
|
||||
g_snake.reset();
|
||||
|
||||
// Simulate movement of snake
|
||||
|
||||
Reference in New Issue
Block a user