Я кодирую какую-то игру pacman в Javascript.У меня есть класс Gameobject, а затем pacman, rock, food с наследованием (gameobject - это суперкласс).Я реализую функцию показа, которая отображает на экране каждый элемент.
Я хочу иметь массив объектов GameObjects, который будет содержать еду, камни и пакман карты.
Итак, вопросы:
1- Возможно ли это в javascript?
2 - Каков наилучший способ сделать это?Возможно что-то вроде:
class GameObject {
constructor(x,y) {
this.coordX = x;
this.coordY = y;
}
}
class Roca extends GameObject
{
constructor(coordX,coordY){
super(coordX,coordY);
}
show() {
//Nota: first in sketch.js preload we have to implement: img = loadImage('assets/laDefense.jpg');
// Top-left corner of the img is at (x, y)
// Width and height are the img's original width and heigh
image(rocaImage,this.coordX,this.coordY);
}
}
class Food extends GameObject
{
constructor(coordX,coordY){
super(coordX,coordY);
}
show() {
//Nota: first in sketch.js preload we have to implement: img = loadImage('assets/laDefense.jpg');
// Top-left corner of the img is at (x, y)
// Width and height are the img's original width and heigh
image(foodImage,this.coordX,this.coordY);
}
}
class Maze {
constructor() {
this.rows = ROWS;
this.columns = COLUMNS;
this.mapa = [
[0, 0, 2, 0, 0, 0, 0, 0, 1,1,0,1],
[0, 0, 0, 0, 0, 0, 1, 1, 1,1,0,1],
[1, 1, 0, 0, 2, 1, 1, 0, 1,1,0,1],
[0, 1, 0, 0, 0, 0, 0, 0, 1,1,0,1],
[0, 1, 1, 1, 1, 0, 0, 0, 1,1,0,1],
[0, 0, 1, 0, 1, 0, 0, 0, 1,1,0,1],
[0, 0, 1, 0, 0, 1, 0, 0, 1,1,0,1],
[2, 0, 1, 1, 2, 1, 3, 1, 1,1,0,1],
[0, 1, 1, 1, 1, 0, 0, 0, 1,1,0,1],
[0, 0, 1, 0, 1, 0, 0, 0, 1,1,0,1],
[0, 1, 1, 1, 1, 0, 0, 0, 1,1,0,1],
[0, 0, 1, 0, 1, 0, 0, 0, 1,1,0,1],
];
}
}
Now i have something similar to generate the objects of the map:
for (var i = 0; i < myMaze.rows; i++)
for (var j = 0; j < myMaze.columns; j++) {
if (myMaze.mapa[i][j] == 0) {
arrayRocasMapa.push(new Roca(j * IMAGE_SIZE, i * IMAGE_SIZE));
} else if (myMaze.mapa[i][j] == 1) {
arrayComidaMapa.push(new Food(j * IMAGE_SIZE, i * IMAGE_SIZE));
} else if (myMaze.mapa[i][j] == 2) {
arrayGrapesMapa.push(new Grapes(j * IMAGE_SIZE, i * IMAGE_SIZE));
}
else if (myMaze.mapa[i][j] == 3) {
myPacman = new Pacman(j * IMAGE_SIZE, i * IMAGE_SIZE);
}
}
Как вы можете видеть, я полагаюсь на лабиринт класса, у которого у меня есть карта игры (0 для еды, 1 для камня, 2 pacman и т. Д.)
Итак, я хочу использовать javascritpt вместо одного массива или еды на карте, одного массива для камней на карте, чтобы иметь универсальный массив игровых объектов ... который смешивает все элементы .. возможноя мог бы добавить атрибут к каждому классу с именем типа, чтобы различать различные виды элементов ...
var miObjeto = { "marca":"audi", "edad":2 };
for (var propiedad in miObjeto) {
if (miObjeto.hasOwnProperty(propiedad)) {
console.log("En la propiedad '" + propiedad + "' hay este valor: " + miObjeto[propiedad]);
}
}