简体   繁体   English

我不知道要在此代码中更改什么才能使其正常工作

[英]I don't know what to change in this code for it to work

This code.这段代码。 It suppose to create canvas, grass and animals there.它假设在那里创建帆布、草和动物。 It doesn't even draw canvas.它甚至不绘制画布。 Please help.请帮忙。 I will be very grateful.我将不胜感激。

This two files (setup.js and classification.js) I have tied to my html.这两个文件(setup.js 和classification.js)我已经绑定到我的html。 Also used this link " https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.14/p5.js ".还使用了此链接“ https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.14/p5.js ”。 Also there is this error还有这个错误

var gr = new Grass(x,y,1); var gr = new Grass(x,y,1); Grass is not defined草没有定义

setup.js设置.js

var side = 10;
var grassArr = [];
var matrix = [];
var grassEaterArr = [];
var found = []

function setup() {
  for (var y = 0; y < 49; y++) {
    matrix[y] = [];
    for (var x = 0; x < 49; x++) {
      let arr = [0, 1, 2]
      let r = random(arr)
      matrix[y][x] = r;
    }
  }

  for (var y = 0; y < matrix.length; ++y) {
    for (var x = 0; x < matrix[y].length; ++x) {
      if (matrix[y][x] == 1) {
        var gr = new Grass(x, y, 1);
        grassArr.push(gr);
      } else if (matrix[y][x] == 2) {
        var kendani = new GrassEater(x, y, 2);
        grassEaterArr.push(kendani);
      }
    }
  }

  frameRate(5)
  createCanvas(49 * side, 49 * side);
  background('#acacac');
}

function draw() {
  for (var y = 0; y < matrix.length; y++) {
    for (var x = 0; x < matrix[y].length; x++) {
      if (matrix[y][x] == 1) {
        fill("green");
        rect(x * side, y * side, side, side);
      } else if (matrix[y][x] == 0) {
        fill("#acacac");
        rect(x * side, y * side, side, side);
      } else if (matrix[y][x] == 2) {
        fill("yellow");
        rect(x * side, y * side, side, side);
      }
    }
  }

  for (var i in grassArr) {
    grassArr[i].mul();
  }
  for (var i in grassEaterArr) {
    grassEaterArr[i].move();
    grassEaterArr[i].eat();
    grassEaterArr[i].mul();
    grassEaterArr[i].die();
  }
}

classification.js分类.js

class Grass {
  constructor(x, y, index) {
    this.x = x;
    this.y = y;
    this.index = index;
    this.multiply = 0;
    this.directions = [
      [this.x - 1, this.y - 1],
      [this.x, this.y - 1],
      [this.x + 1, this.y - 1],
      [this.x - 1, this.y],
      [this.x + 1, this.y],
      [this.x - 1, this.y + 1],
      [this.x, this.y + 1],
      [this.x + 1, this.y + 1],
    ]
  }

  chooseCell(character) {
    var found = [];
    for (var i in this.directions) {
      var x = this.directions[i][0];
      var y = this.directions[i][1];
      if (x >= 0 && x < matrix[0].length && y >= 0 && y < matrix.length) {
        if (matrix[y][x] == character) {
          found.push(this.directions[i]);
        }
      }
    }
    return found;
  }

  mul() {
    this.multiply++;
    var newCell = random(this.chooseCell(0));
    console.log(newCell, this.multiply);
    if (this.multiply >= 8 && newCell) {
      var newGrass = new Grass(newCell[0], newCell[1], this.index);
      grassArr.push(newGrass);
      matrix[newCell[1]][newCell[0]] = 1;
      this.multiply = 0;
    }
  }
}

class GrassEater {
  constructor(x, y, index) {
    this.x = x;
    this.y = y;
    this.index = index;
    this.energy = 5;
    this.directions = [];
  }

  getNewCoordinates() {
    this.directions = [
      [this.x - 1, this.y - 1],
      [this.x, this.y - 1],
      [this.x + 1, this.y - 1],
      [this.x - 1, this.y],
      [this.x + 1, this.y],
      [this.x - 1, this.y + 1],
      [this.x, this.y + 1],
      [this.x + 1, this.y + 1]
    ];
  }

  chooseCell(character) {
    this.getNewCoordinates();
    var found = [];
    for (var i in this.directions) {
      var x = this.directions[i][0];
      var y = this.directions[i][1];
      if (x >= 0 && x < matrix[0].length && y >= 0 && y < matrix.length) {
        if (matrix[y][x] == character) {
          found.push(this.directions[i]);
        }
      }
    }
    return found;
  }
  move() {
    var newCell = random(this.chooseCell(0));

    if (newCell) {
      var newX = newCell[0];
      var newY = newCell[1];

      matrix[this.y][this.x] = 0;
      matrix[newY][newX] = this.index;
      this.y = newY;
      this.x = newX;
      this.energy--;
    }
  }

  mul() {
    var newCell = random(this.chooseCell(0));

    if (this.energy >= 8 && newCell) {
      var animalArr = new GrassEater(newCell[0], newCell[1], this.index);
      grassEaterArr.push(animalArr);
      matrix[newCell[1]][newCell[0]] = 2;
      this.energy = 5;
    }
  }


  eat() {
    var newCell = random(this.chooseCell(1));
    if (newCell) {
      var newX = newCell[0];
      var newY = newCell[1];

      matrix[this.y][this.x] = 0;
      matrix[newY][newX] = this.index;

      for (var i in grassArr) {
        if (newX == grassArr[i].x && newY == grassArr[i].y) {
          grassArr.splice(i, 1);
          break;
        }
      }

      this.y = newY;
      this.x = newX;
      this.energy += 2;
    }

    die() {
      if (this.energy == 0) {
        matrix[this.y][this.x] = 0;
      }
    }
  }

Export the Grass and GrassEater classes, and import them on your main file:导出GrassGrassEater类,并将它们导入到您的主文件中:

class Grass {...}
class GrassEater {...}
export { Grass, GrassEater }

On setup.js :setup.js

import { Grass, GrassEater } from "./classification.js";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 试图更改活动标签颜色,不知道我在代码中做错了什么 - Trying to change active tab color, don't know what I am doing wrong in my code 关于代码中的函数,我不知道它们对什么意味着什么 - about functions in code that I don't know what they mean for what 如果更改页面上的Javascript字符串链接无效,我也不知道为什么? - If a change a Javascript string links on a page don't work, and I don't know why? 我真的不知道问题是什么,表单重置不起作用 - I reall don't know what the problem is, form reset doesn't work 当我点击按钮时,我不知道怎么了 - I don't know what's wrong when i click the button nothing work 代码无法正确运行,我不知道会发生什么。 只需复制现有的开发工作,然后尝试运行它并得到错误 - Code doesn't run Properly, i don't know what happen. Just copy the existing dev work, And try to run it and getting errors 我不知道我做错了什么,它不起作用,我正在尝试列出任务清单 - i don't know what i did wrong, it doesn't work, i'm trying to make a task list 在{{variable}}中更改变量(不知道该叫什么) - Change a variable within a {{variable}} (don't know what that is called) 我不知道为什么渲染在反应中不起作用 - I don't know why rendering doesn't work in react 我的for循环不起作用,我也不知道为什么 - my for loop doesn't work and I don't know why
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM