リンゴとバナナにぶつかると食べて少しずつ太ります。ある程度太ったらダイエットのためにグラウンドを何周か走ります。その間、少しずつ減量していきます。元に戻ったらまた食べて太る・・・を繰り返します。

let gridSize = 20; // 一辺の区画数
let cellSize; // 区画の一辺のサイズ
let cell = []; // 区画の情報
let cl; // 色情報
let persons = []; // 丸印の情報格納
let pnum = 15; // 丸の数
let steps = 0; // 一方向への歩数のカウンター
let stpMax = 7; // 歩数カウンターの最大値
let d; // 次に進む向き
let radius; // 丸の半径
let speed = 0; // 丸の移動スピードカウンター
let spdMax = 10; // 丸の移動スピードカウンターの最大値
let imgApple;
let imgBanana;
let fat = 15; //太る限界

function preload() {
  imgApple = loadImage('apple.png');
  imgBanana = loadImage('banana.png');
}

function setup() {
  createCanvas(400, 400);
  rectMode(CENTER);
  cellSize = width / gridSize;
  radius = cellSize * 0.3;

  for (let i = 0; i < gridSize; i++) {
    for (let j = 0; j < gridSize; j++) {
      cell.push(createVector(j, i, 0));
    }
  }

  for (let i = 0; i < gridSize * gridSize; i++) {
    if (
      cell[i].x == 0 ||
      cell[i].x == gridSize - 1 ||
      cell[i].y == 0 ||
      cell[i].y == gridSize - 1
    ) {
      cell[i].z = 1; // 四方に壁を作り壁の印としてzを1とする。
    }
  }

  for (let i = 11; i < 14; i++) {
    for (let j = 11; j < 14; j++) {
      cell[i + j * gridSize].z = 3;
    }
  }

  for (let i = 6; i < 9; i++) {
    for (let j = 6; j < 9; j++) {
      cell[i + j * gridSize].z = 3;
    }
  }

  for (let i = 0; i < pnum; i++) {
    persons.push(new Parson(2, 2,
      int(random(4)),
      color(random(255), random(255), random(255))));
  }
}

function draw() {
  background(0);
  for (let i = 0; i < gridSize * gridSize; i++) {
    if (cell[i].z == 1) {
      cl = color(128, 0, 0);
      drawRect(i, cl);
    }
  }

  image(imgApple, 11 * cellSize, 11 * cellSize, cellSize * 3, cellSize * 3);
  image(imgBanana, 6 * cellSize, 6 * cellSize, cellSize * 3, cellSize * 3);

  for (let person of persons) {
    person.drawParson();
    person.nextPosition();
  }
}

function drawRect(i, cl) {
  fill(cl);
  noStroke();
  rect(
    cell[i].x * cellSize + cellSize / 2,
    cell[i].y * cellSize + cellSize / 2,
    cellSize,
    cellSize
  );
}

class Parson {
  constructor(x, y, d, pcol) {
    this.x = x;
    this.y = y;
    this.d = d;
    this.speed = 0;
    this.steps = 0;
    this.radius = radius;
    this.pcol = pcol;
    this.phaseOffset = int(random(360));
    this.spinSpeed = 1;
    this.state = 'wandering';
  }

  drawParson() {
    if (this.state === 'rotating') {
      this.rotateCircle(this.radius, this.pcol);
    } else {
      fill(this.pcol);
      noStroke();
      ellipse(
        this.x * cellSize + cellSize / 2,
        this.y * cellSize + cellSize / 2,
        this.radius * 2
      );
    }
  }

  nextPosition() {
    this.speed++;
    if (this.speed < spdMax) return;
    this.speed = 0;

    switch (this.state) {
      case 'wandering':
        this.wander();
        break;
      case 'rotating':
        break;
    }
  }

  wander() {
    if (this.radius > fat) {
      this.state = 'rotating';
      return;
    }

    this.steps++;
    if (this.steps >= stpMax) {
      this.d = int(random(4));
      this.steps = 0;
    }

    let newX = this.x;
    let newY = this.y;

    switch (this.d) {
      case 0: newY -= 1; break;
      case 1: newX += 1; break;
      case 2: newY += 1; break;
      case 3: newX -= 1; break;
    }

    let cn = this.checkNext(newX, newY);

    if (cn == 0) {
      this.x = newX;
      this.y = newY;
    } else {
      this.d = int(random(4));
      if (cn == 3) {
        this.radius += 2;
      }
    }
  }

  rotateCircle(radius, pcol) {
    fill(pcol);
    noStroke();
    push();
    translate(width / 2, height / 2);

    let angle = radians(frameCount * this.spinSpeed + this.phaseOffset);
    ellipse(gridSize * 0.35 * cellSize * cos(angle),
      gridSize * 0.35 * cellSize * sin(angle),
      this.radius * 2, this.radius * 2);
    pop();

    this.radius -= 0.003;
    if (this.radius < cellSize * 0.3) {
      this.radius = cellSize * 0.3;
      this.state = 'wandering';
    }
  }

  checkNext(x, y) {
    if (x < 0 || x >= gridSize || y < 0 || y >= gridSize) {
      return 1;
    }
    let idx = x + y * gridSize;
    return cell[idx].z;
  }
}
投稿者

manager

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

CAPTCHA


日本語が含まれない投稿は受付られません。(スパム対策)