P5jsでドラクエ風のゲーム作成しました!



P5jsでドラクエ風のゲーム作成しました!


以下コードです。画像を入れる必要があります。

var images = new Array(8);
var xoffset = 0;
var yoffset = 0;
var px = 0 * 25;
var py = 7 * 25;
var s = 25;
var suraimu;
var money = 0;
var item = false;
var item2 = false;
var item3 = false;
var item4 = false;
var mode = 0;
var button;
var teki1, teki2,teki3;
var kougeki = 20;
var bougyoz = 0;
var time = 0;
var zhp = 100;
var turn = true;

var array = [
  [5, 5, 5, 5, 1, 1, 1, 1, 1, 5, 5, 5, 1, 1, 1, 1, 5, 5, 5, 5],
  [5, 5, 5, 0, 1, 1, 0, 0, 0, 0, 5, 0, 0, 0, 1, 0, 0, 5, 5, 5],
  [5, 5, 1, 0, 1, 1, 0, 0, 1, 1, 2, 0, 0, 0, 0, 0, 0, 0, 5, 5],
  [5, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 11, 0, 1, 0, 1, 1, 5],
  [1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1],
  [1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1],
  [0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1],
  [0, 0, 0, 6, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1],
  [0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
  [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1],
  [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1],
  [1, 1, 1, 7, 1, 1, 0, 0, 0, 0, 7, 0, 0, 0, 1, 0, 0, 0, 1, 1],
  [1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 2, 0, 0, 0, 0, 0, 0, 1, 0, 0],
  [1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1],
  [5, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 10, 0, 1, 0, 0, 1, 5],
  [5, 5, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 5, 5],
  [5, 5, 5, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 5, 5, 5],
  [5, 5, 5, 5, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 5, 5, 5, 5],
  [5, 5, 5, 5, 5, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 5, 5, 5, 5, 5],
  [5, 5, 5, 5, 5, 5, 1, 1, 1, 1, 1, 1, 1, 1, 5, 5, 5, 5, 5, 5]
];

function setup() {
  createCanvas(250, 250);
  background(0, 0, 255);
  //for (var k = 0; k < images.length; k++) {
  images[0] = loadImage("back0.png");
  images[1] = loadImage("back1.png");
  images[2] = loadImage("flower.png");
  images[3] = loadImage("flowerpod.png");
  images[4] = loadImage("Pot.PNG");
  images[5] = loadImage("");
  images[6] = loadImage("mig.jpg");
  images[7] = loadImage("teki.png");
  images[8] = loadImage("ryuo.jpg");
  images[9] = loadImage("ryuo2.jpg");
  images[10] = loadImage("ken.png");
  suraimu2 = loadImage("suraimu2.png");
  images[11] = loadImage("tate.png");
  //  }0
  suraimu = loadImage("back5.png");
  teki1 = new tatakai(images[6], 80, 80, 100, 100, "スライム", 0, 5);
  teki2 = new tatakai(images[7], 80, 80, 100, 100, "スライムナイト", 10, 8);
  teki3 = new tatakai(images[10], 80, 80, 100, 100, "りゅうおう", 20, 10);
  // teki4 = new tatakai(images[9], 80, 80, 100, 100, "竜王",17);
}

function draw() {
  //print(button);
  background(0, 0, 255);
  if (mode == 0) {
    xoffset = (px - width / 2);
    yoffset = (py - height / 2);
    print(px);
    for (var i = 0; i < 20; i++) {
      for (var j = 0; j < 20; j++) {
        var p = array[i][j];
        drawImage(images[p], j * s, i * s, s, s);
        //text(,10,230);

      }
    }

    drawImage(suraimu, px, py, 25, 25); //自分
    getItem();

  }

  if (mode == 1) {
    background(0);
    teki2.display();
  }

  if (mode == 2) {
    background(0);
    teki1.display();
  }

  if (mode == 3) {
    background(0);
    teki3.display();
  }
  /*
  if (mode == 4) {
    background(0);
    teki4.display();
  }
  */
  if (item == true) {
    sikaku();
    time++;
    if (time > 100) {
      item = false;
      time = 0;
    }
  }
  if (item4 == true) {
    sikaku2();
    time++;
  }
  if (time > 100) {
    item4 = false;
    time = 0;
  }
  if (item3 == true) {
    sikaku3();
    time++;
  }
  if (time > 100) {
    item3 = false;
    time = 0;
  }


}

function sikaku() {
  fill(0);
  rect(20, 210, 200, 30);
  fill(255);
  text("10円を拾った", 80, 230);
}

function sikaku2() {
  fill(0);
  rect(20, 210, 200, 30);
  fill(255);
  text("防御力が5アップした", 60, 230);
}

function sikaku3() {
  fill(0);
  rect(20, 210, 200, 30);
  fill(255);
  text("攻撃力が10アップした", 50, 230);
}

function getItem() {
  if (array1[px / s] == 2) {
    array1[px / s] = 0;
    money = money + 10;
    item = true;
  }
  if (array1[px / s] == 3) {
    array1[px / s] = 0;

  }
  if (array1[px / s] == 6) {
    mode = 2;
  }
  if (array1[px / s] == 7) {
    mode = 1;
  }
  if (array1[px / s] == 8) {
    mode = 3;
  }
  if (array1[px / s] == 9) {
    mode = 1;
  }
  if (array1[px / s] == 10) {
    array1[px / s] = 0;
    item3 = true;
  }
  if (array1[px / s] == 11) {
    array1[px / s] = 0;
    item4 = true;
  }

  if (array1[px / s] == 2 && item == true) {
    array1[px / s] = 0;
    item2 = true;
  }
  if (array1[px / s] == 4 && item == true) {

    array1[px / s] = 0;
  }

  if (item3 == true) {
    kougeki = 30;
    suraimu = suraimu2;
  }
  if (item4 == true) {
    bougyoz = 5;
  }
}




function drawImage(img, x, y, sx, sy) {
  x -= xoffset;
  y -= yoffset;
  image(img, x, y, sx, sy);
}

function keyPressed() {
  if (mode == 0) {


    if (keyCode == UP_ARROW && py > 0) {
      if (array1[px / 25] != 1) {
        if (array1[px / 25] != 5) {


          py = py - s;
        }
      }
    }
    if (keyCode == DOWN_ARROW && py < 475) {
      if (array1[px / 25] != 1) {
        if (array1[px / 25] != 5) {
          py = py + s;
        }
      }
    }
    if (keyCode == RIGHT_ARROW && px < 475) {
      if (array1[px / 25 + 1] != 1) {
        if (array1[px / 25 + 1] != 5) {
          px = px + s;
        }
      }
    }
    if (keyCode == LEFT_ARROW && px > 0) {
      if (array1[px / 25 - 1] != 1) {
        if (array1[px / 25 - 1] != 5) {
          px = px - s;
        }
      }
    }
  }
}

function tatakai(img, tx, ty, tss, thp, name, bougyo, tkogeki) {
  //  button = createButton('攻撃');
  print(turn)
  if (button == null) {

    exist = true;
  }
  var x = tx;
  var y = ty;
  var ts = tss;
  var S = 0;
  var hp = thp;
  var tbb = bougyo;
  var talk = false;
  var talk2 = false;
  var T = 0;
  var N = 0;
  var jikosyokai = true;
  var tekinokougeki = tkogeki;
  var tk = false;
  var teki=false;
  var zkougeki = true;
  this.display = function() {
    N++;
     
    image(img, x, y, ts, ts);
    fill(255);
    if (jikosyokai == true) {
      text(name + "が現れた", 75, 220);
    }
    if (N > 50) {
      jikosyokai = false;
      N = 0;
    }
    text(hp + "hp", 110, 70);
    //text(mode, 20, 20);
    text(money + "money", 180, 60);
    text("体力:" + zhp, 20, 20);


    text("攻撃力:" + kougeki, 180, 20);
    text("防御力:" + bougyoz, 180, 40);
    text(tbb, 100, 20);
    
    if(talk2==false){
this.buttonshow();
    }else{
    removeElements();
    }
    
    
    
    if (talk == true) {
      fill(255);
      text(kougeki + "ダメージを与えた", 70, 230);
      T++;
      zkougeki = false;

    }
    
    if (T > 25) {
      talk = false;
      T = 0;
      teki=true;
    }
    
    if (talk2 == true) {
      fill(255);
      text( tkogeki+ "ダメージをうけた", 70, 230);
      if(zhp<1){
         text("GameOver",120,220);
        noLoop();
         }
      
      T++;
    //zkougeki = false;
    }
    if (T > 25){
    talk2=false;
      T = 0;
    }
    

    if (tk == true) {
      fill(255);
      text(name + "を倒した", 80, 245);
      S++;
removeElements();
    }
    if (S > 50) {
      tk = false;
      mode = 0;

      array1[px / s] = 0;
      removeElements();
    }
    if (turn) { 
    
    } else {
      removeElements();
      this.tko();
    }

if(teki==true){
this.tekinokougeki();
  talk2=true;
  teki=false;
}
  }

  this.changeBG = function() {
    hp = hp - (kougeki - tbb);
    talk = true;

   



    if (hp < 1) {
      tk = true;
      //if(S>50){
      //	 tk=false;
      //		

    }

  }
this.tekinokougeki=function(){
   zhp = zhp - tkogeki;
  
}
  this.tko = function() {
    if (turn = true) {

      zkougeki = true;
    }
  }

this.buttonshow=function(){
   button = createButton('攻撃');
      button.position(20, 25);
      button.mousePressed(this.changeBG);
}


}