xxxxxxxxxx
92
let zero; //ゼロ君の入れ物
//☟❶ 火の玉の入れ物(はいれつ)をつくる
let imgZero; //キャラの画像の入れ物
//☟❷ 火の玉の画像の入れ物をつくる
function setup() {
createCanvas(400, 400); //キャンバスをちょっとよこながにする
imgZero = loadImage("img/boy.png"); //画像をロードする
//☟❸ 火の玉の画像をロードする
//Jumperクラスからゼロ君を作る
zero = new Jumper();
}
//スペースが押されたらジャンプ力のpを50にする
function keyPressed() {
if (key == ' ') {
//ゼロ君をジャンプさせるコード
zero.jump();
}
}
function draw() {
background(220);
//ゼロ君を表示させるコード
zero.show();
//☟❹ 火の玉をランダムにつくる
//★randomでバラバラのタイミングで火の玉を出す
if (random(0, 10) < 1) {
let fb = new Fireball(400, 320, imgFireball); //火の玉をつくる
fireballs.push(fb); //配列に火の玉を入れる
}
//☟❺ 火の玉を動かして表示させる
//★for文で火の玉一つひとつをチェックする
for (let f of fireballs) {
f.x -= 10; //玉を10ずつ左にずらす(10はスピード)
f.show(); //玉を表示させる
}
}
//====================================
// キャラのクラス:ジャンパー
//====================================
class Jumper {
//クラスの最初のデータ
constructor() {
this.y = 320; //最初のキャラのばしょ
this.p = 0; //最初のキャラのジャンプパワーはゼロ
}
//ジャンプのメソッド
jump () {
this.p = 50;
}
//キャラが動いて表示するメソッド
show () {
//動きのコード
this.y -= this.p;
this.p -= 5;
if (this.y > 320)
this.y = 320;
//キャラ表示のコード
image (imgZero, 100, this.y, 80, 80);
}
}
//====================================
// 火の玉のクラス
//====================================
class Fireball {
//作るときにx, yと画像データをわたします
constructor(x, y, img) {
this.x = x;
this.y = y;
this.r = 60;
this.img = img;
}
show() {
image(this.img, this.x, this.y, this.r, this.r/2);
}
}