i was writting a greedy snack game ,but i found that the class GreedySnack has too many big functions.so i want put the functions in other files and import them into the class file. but i jst dont know how to do. part of the codes:
class GreedySnake {
constructor() {
this.canvas = document.querySelector("#snake");
this.ctx = this.canvas.getContext("2d");
this.maxX = 64; // max row
this.maxY = 40; // max col
this.itemWidth = 10; // size of the point
this.direction = "right"; // up down right left direction
this.speed = 150; // ms speed
this.isStop = false; //
this.isOver = false; //
this.isStart = false; //
this.score = 0; //
this.timer = null; // movement timer
this.j = 1;
this.canChange = true;
this.grid = new Array();
for (let i = 0; i < this.maxX; i++) {
for (let j = 0; j < this.maxY; j++) {
this.grid.push([i, j]);
}
}
// this.drawGridLine();
this.getDirection();
}
// create the body
createSnake() {
this.snake = [
[4, 25],
[3, 25],
[2, 25],
[1, 25],
[0, 25],
];
}
// movement
move() {
...
// if the next-step is not food
...
}
// start
start() {
...
}
// pause
stop() {
...
}
}
i've tired the require
createPos = require("./food");
createFood = require("./food");
snackClass.js:29 Uncaught ReferenceError: require is not defined