xxxxxxxxxx
221
/*
----------
オブジェクト変数
---------
*/
let player;
let enegies = [];
let maze = []; // 迷路のブロック
/*
----------
画面を描くための要素
---------
*/
let canvasWidth = 800;
let canvasHeight = 800;
let cell = 50; // 正方形ひとマスの幅・高さ
let endR; //最終行
let endC; //最終カラム
function setup() {
createCanvas(canvasWidth, canvasHeight);
// frameRate(1);
player = new Player();
for (let i = 1; i < canvasWidth / cell; i++) {
for (let t = 1; t < canvasWidth / cell; t++) {
let ene = new Enegy(i, t);
let mazeBlock = new Maze(i, t);
enegies.push(ene);
maze.push(mazeBlock);
maxR = t;
}
maxC = i;
}
console.log(maze);
}
function draw() {
background(30, 0, 30);
for (let mazeBlock of maze) {
mazeBlock.display();
}
for (let enegy of enegies) {
enegy.eaten();
enegy.display();
}
// inVisibleになったenegyを削除
enegies = enegies.filter((e) => e.isVisible);
// console.log(enegies.length);
player.move();
player.display();
textAlign(LEFT, TOP);
textSize(40);
text("のこり:" + enegies.length, 100, 5);
}
class Enegy {
constructor(_x, _y) {
this.x = _x * cell;
this.y = _y * cell + 50;
this.size = 10;
this.isVisible = true;
}
eaten() {
// console.log(dist(player.x,player.y ,this.x,this.y));
if (dist(player.x, player.y, this.x, this.y) < player.size / 3) {
this.isVisible = false;
}
}
display() {
fill(255);
noStroke();
if (this.isVisible) {
circle(this.x, this.y, 5);
}
}
}
class Player {
constructor() {
this.x = width / 2;
this.y = height / 2;
this.size = 50;
this.speed = 3;
this.life = 3;
this.fig = 1;
this.direction = 0;
this.action = 6;
}
move() {
// console.log(this.fig);
//右へ移動
if (keyIsDown(RIGHT_ARROW)) {
this.direction = 0;
this.x += this.speed;
}
//
//左へ移動
if (keyIsDown(LEFT_ARROW)) {
this.direction = 2;
this.x -= this.speed;
}
//上へ移動
if (keyIsDown(UP_ARROW)) {
this.direction = 3;
this.y -= this.speed;
}
//下へ移動
if (keyIsDown(DOWN_ARROW)) {
this.direction = 1;
this.y += this.speed;
}
// 端に着いたら逆サイドへ出現
if (this.x < 0) {
this.x = width;
}
if (this.x > width) {
this.x = 0;
}
if (this.y < 0) {
this.y = height;
}
if (this.y > height) {
this.y = 0;
}
}
display() {
// playerは相対的に常にステージの中心に位置しステージを回転する。
// プレイヤーの為の環境設定開始
push();
noStroke();
fill(230, 230, 50);
// ステージの中心をPlayerのx座標、y座標にする。
translate(this.x, this.y);
// Playerが回転した方にステージを回転させる。
rotate(HALF_PI * this.direction);
arc(
0,
0,
this.size,
this.size,
PI / this.action,
TWO_PI - PI / this.action
);
this.action = this.action + 2 * this.fig;
if (this.action > 24 || this.action < 6) {
this.fig *= -1;
}
pop();
// プレイヤーの為の環境設定終了
}
}
class Maze {
constructor(_x, _y) {
this.x = _x * cell;
this.y = _y * cell + 50;
this.xNum = _x; // 横方向のブロックの番目
this.yNum = _y; // 縦方向のブロックの番目
this.mergin = 5;
this.isVisible = true;
}
display() {
rectMode(CENTER);
fill(255, 0, 0);
if (this.isVisible) {
square(this.x, this.y, cell, 5);
}
}
}
function setMaze() {
for (let mBk of maze) {
if (mBk.xNum > 1 && mBk.xNum < maxC && mBk.yNum >1 && mBk.yNum < maxR){
if(mBk.xNum % 2 ==1 && mBk.yNum % 2 == 1 ){
let s = 0;
switch(random(1,4)){
case 1: //左
case 2: //上
case 3: //右
case 4: //下
default:
break;
}
}
}
if(mBk.xNum == 1 || mBk.yNum == 1){
mBk.isVisible = true;
}
if(mBk.xNum == maxC || mBk.yNum == maxR){
mBk.isVisible = true;
}
}
}