📚 6月教材:p5.jsでライン(線)を描いてみよう

prac2.html, prac3.htmlを説明すること

📚 6月教材:p5.jsでスプライト(キャラクター)を動かしてみよう

入門レベル固定画像を左右に動かす

📖 今回学ぶこと:
p5.jsを使って、PNG画像をキャンバスに描画し、マウスの位置に応じて動かしてみます。 これが「スプライト」の最も基本的な使い方です。

1️⃣ 画像ファイルについて

今回使う画像:sisimaru.png(/www/daiki/img フォルダにあります)

この画像は固定画像なので、そのまま画面上を動かします。

sisimaru.png

sisimaru.png (使用画像)

2️⃣ p5.jsの基本的な流れ

// ① 画像を保存する変数
let spriteImage;

// ② 画像を読み込む(setup前に実行)
function preload() {
    spriteImage = loadImage('https://ome.funnygeekjp.com/kano/daiki/img/sisimaru.png');
}

// ③ キャンバスをセットアップ
function setup() {
    createCanvas(800, 400);
}

// ④ 毎フレーム実行(アニメーション処理)
function draw() {
    background(220);  // 背景をリセット
    
    // マウスのX座標に合わせて画像を動かす
    let x = mouseX;
    let y = 200;  // Y座標は固定
    
    image(spriteImage, x, y, 50, 50);  // (画像, X, Y, 幅, 高さ)
}
            

デモ1:sisimaru.pngを左右に動かす

下のキャンバスの中でマウスを左右に動かしてみてください!

🎮 サンプルプログラム1:game0002-sprite.html

実際に動かせるサンプルプログラムです。

3️⃣ コードの説明

関数・変数 説明
preload() setup()の前に実行される。画像やデータの読み込みに使用
loadImage() URLまたはファイルパスから画像を読み込む
setup() プログラム開始時に一度だけ実行。キャンバスの初期設定など
draw() 毎フレーム実行(デフォルト秒60回)。アニメーション処理をここに書く
background() 背景色を設定。毎回呼ぶことで前フレームをリセット
image() 画像を描画。第2,3引数がX,Y座標、第4,5引数が幅と高さ
mouseX, mouseY マウスの現在座標(読み取り専用)

4️⃣ 自分で試してみよう

// 挑戦1:Y座標もマウスに合わせて動かしてみよう
image(spriteImage, mouseX, mouseY, 50, 50);

// 挑戦2:画像のサイズをマウスX座標に合わせて変える
let size = map(mouseX, 0, width, 20, 100);
image(spriteImage, mouseX, 200, size, size);

// 挑戦3:画像を回転させてみよう
push();
translate(mouseX, 200);
rotate(frameCount * 0.01);  // フレームカウントに応じて回転
imageMode(CENTER);
image(spriteImage, 0, 0, 50, 50);
pop();
            

レベルアップスプライトシートから複数スプライトを作る

📖 今回学ぶこと:
1枚の画像を複数のパーツに分割して、複数のスプライトを作ります。 このテクニックはゲーム開発でよく使われます。

🎨 スプライトシートとは?

スプライトシートとは、複数のキャラクターまたはアニメーション画像を1枚にまとめた画像ファイルです。 こうすることで、読み込む画像ファイル数を減らして、処理を高速化できます。

今回使う画像:white_anime.png(4つのフレームが横に並んでいます)

white_anime.png

white_anime.png (スプライトシート - 4フレーム並んでいます)

1️⃣ スプライトシートを分割する仕組み

// スプライトシート画像を読み込む
let spriteSheet;

function preload() {
    spriteSheet = loadImage('https://ome.funnygeekjp.com/kano/daiki/img/white_anime.png');
}

function setup() {
    createCanvas(800, 400);
}

function draw() {
    background(220);
    
    // スプライトシートの一部をコピーして描画する
    // copy(元画像, 元X, 元Y, 元幅, 元高さ, 新X, 新Y, 新幅, 新高さ)
    
    let frameWidth = spriteSheet.width / 4;  // 4フレーム分割
    let frameHeight = spriteSheet.height;
    
    // フレーム0を描画(一番左)
    copy(spriteSheet, 0, 0, frameWidth, frameHeight, 100, 100, 80, 80);
    
    // フレーム1を描画
    copy(spriteSheet, frameWidth, 0, frameWidth, frameHeight, 250, 100, 80, 80);
    
    // フレーム2を描画
    copy(spriteSheet, frameWidth * 2, 0, frameWidth, frameHeight, 400, 100, 80, 80);
    
    // フレーム3を描画
    copy(spriteSheet, frameWidth * 3, 0, frameWidth, frameHeight, 550, 100, 80, 80);
}
            

デモ2:white_anime.pngを4つに分割して表示

4つのフレームが左から順に表示されます

🎮 サンプルプログラム2:game0003-sprite-anime.html

white_anime.pngのアニメーション版です。フレーム速度やサイズを調整できます。

2️⃣ copy関数の詳細説明

copy()関数の構文:
copy(src, srcX, srcY, srcWidth, srcHeight, destX, destY, destWidth, destHeight)

パラメータ説明:
  • src: コピー元の画像
  • srcX, srcY: コピー元画像の左上座標
  • srcWidth, srcHeight: コピーする領域のサイズ
  • destX, destY: 貼り付け先(キャンバス上)の座標
  • destWidth, destHeight: 貼り付けるサイズ

3️⃣ スプライトオブジェクトで管理する(より良い方法)

// スプライトクラスを定義
class Sprite {
    constructor(sheetImage, frameIndex, x, y, size) {
        this.image = sheetImage;
        this.frameIndex = frameIndex;
        this.x = x;
        this.y = y;
        this.size = size;
        this.frameCount = 4;  // 全フレーム数
    }
    
    display() {
        let frameWidth = this.image.width / this.frameCount;
        let frameHeight = this.image.height;
        let srcX = frameWidth * this.frameIndex;
        
        copy(
            this.image,
            srcX, 0, frameWidth, frameHeight,
            this.x, this.y, this.size, this.size
        );
    }
}

// 使い方
let spriteSheet;
let sprites = [];

function preload() {
    spriteSheet = loadImage('https://ome.funnygeekjp.com/kano/daiki/img/white_anime.png');
}

function setup() {
    createCanvas(800, 400);
    
    // 4つのスプライトを作成
    for (let i = 0; i < 4; i++) {
        sprites.push(new Sprite(spriteSheet, i, 100 + i * 150, 100, 80));
    }
}

function draw() {
    background(220);
    
    // すべてのスプライトを描画
    for (let sprite of sprites) {
        sprite.display();
    }
}
            

デモ3:Spriteクラスを使った方法

クラスを使うことでコードがスッキリします

4️⃣ スプライトを動かしてみる

// Spriteクラスにアニメーション機能を追加
class AnimatedSprite {
    constructor(sheetImage, x, y, size) {
        this.image = sheetImage;
        this.x = x;
        this.y = y;
        this.size = size;
        this.frameCount = 4;
        this.currentFrame = 0;
        this.frameSpeed = 10;  // フレームスキップ数
        this.counter = 0;
    }
    
    update() {
        this.counter++;
        if (this.counter >= this.frameSpeed) {
            this.currentFrame = (this.currentFrame + 1) % this.frameCount;
            this.counter = 0;
        }
    }
    
    display() {
        let frameWidth = this.image.width / this.frameCount;
        let frameHeight = this.image.height;
        let srcX = frameWidth * this.currentFrame;
        
        copy(
            this.image,
            srcX, 0, frameWidth, frameHeight,
            this.x, this.y, this.size, this.size
        );
    }
}

let spriteSheet;
let animatedSprite;

function preload() {
    spriteSheet = loadImage('https://ome.funnygeekjp.com/kano/daiki/img/white_anime.png');
}

function setup() {
    createCanvas(800, 400);
    animatedSprite = new AnimatedSprite(spriteSheet, 300, 150, 100);
}

function draw() {
    background(220);
    
    animatedSprite.update();
    animatedSprite.display();
    
    // フレーム番号を表示
    fill(0);
    text('Frame: ' + animatedSprite.currentFrame, 10, height - 10);
}
            

デモ4:アニメーションするスプライト

4つのフレームが順に表示されてアニメーションします

📌 重要なポイントまとめ

概念 説明
スプライト ゲームなどで動く物体。通常は画像として実装される
フレーム アニメーション用の1コマ。複数フレームで動きを表現
スプライトシート 複数フレームを1つの画像ファイルにまとめたもの
アニメーション 複数フレームを順に表示して動きを表現する技術
クラス 複数の関連データとメソッドをまとめた設計パターン

🎯 次のチャレンジ