松濤中央高等学院中央国際高等学校 提携の通信制高校サポート校
メニュー
相談を予約
CHAPTER 06GitHub講座 第6章

ゲームを作る「まつタッチ」

学習時間の目安:約60分最終確認:2026年11月17日
この章でできるようになること
  • ゲーム用の新しいリポジトリを作れる
  • HTMLファイルを作り、ブラウザで動かせる
  • コードのおおまかな作り(HTML・CSS・JavaScript)を説明できる

作るゲーム

画面の中を動きまわる 🌲 を、20秒のあいだにどれだけタッチできるかを競うゲームです。ときどき出る 🎍(門松)は3点。続けてタッチすると「コンボ」になり、5回続くと点が2倍になります。最後は点数で「松・竹・梅」のランクが出ます。スマホでもPCでも遊べます。

まずは見本どおりに作り、あとの章でAIと一緒に改造していきます。

完成見本を遊んでみる

準備:テキストエディタを入れる

プログラムを書くには、Visual Studio Code(VS Code)という無料のエディタがおすすめです。https://code.visualstudio.com/ からインストールします。

CAUTION注意

Windowsのメモ帳やWordで書くと、ファイルの種類や文字コードがずれて、うまく動かないことがあります。VS Codeを使いましょう。

手順:ゲーム用のリポジトリを作る

  1. GitHub Desktopで File → New repository...(新しいリポジトリ) を選びます。
  2. Name に matsu-game と入力します。この名前は、公開したときのURLの一部になります。
  3. Local path を確認し、Create repository をクリックします。
  4. 上の Publish repository(GitHubに公開) をクリックします。
  5. Keep this code private(このコードを非公開にする) のチェックを外してから Publish repository をクリックします。無料プランでGitHub Pagesを使うには、公開(Public)にする必要があります。

手順:ゲームのファイルを作る

  1. GitHub Desktopで Open in Visual Studio Code(または Repository → Open in Visual Studio Code)を選びます。
  2. VS Codeで新しいファイルを作り、名前を index.html にします。名前は必ず index.html にします。公開したとき、最初に表示されるファイルの名前です。
  3. 下のコードを「コピー」ボタンでコピーし、index.html に貼り付けて保存します。
  4. PCのフォルダで index.html をダブルクリックすると、ブラウザでゲームが開きます。スタート を押して遊んでみましょう。
  5. GitHub Desktopに戻り、メッセージ「まつタッチを作成」でコミットし、Push origin で送ります。

コードは ファイルとしてダウンロード することもできます。

index.html
<!DOCTYPE html>
<html lang="ja">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>まつタッチ</title>
  <style>
    /* ===== 色の設定(ここを変えると、ゲームの色が変わります) ===== */
    :root {
      --sky: #d8f3ff;     /* 空の色 */
      --ground: #8fd17a;  /* 地面の色 */
      --main: #145c32;    /* 文字とボタンの色 */
    }

    body { margin: 0; font-family: sans-serif; background: var(--main); color: var(--main);
           user-select: none; -webkit-user-select: none; }
    #game { max-width: 600px; margin: 0 auto; height: 100vh; height: 100dvh;
            display: flex; flex-direction: column; background: #ffffff; }

    /* 上の情報(タイトル・スコア・のこり時間) */
    header { padding: 10px 14px 6px; }
    h1 { margin: 0 0 6px; font-size: 22px; }
    .info { display: flex; gap: 8px; }
    .card { flex: 1; background: #f0f7f1; border-radius: 10px; padding: 4px 10px; font-size: 13px; }
    .card b { display: block; font-size: 24px; line-height: 1.2; }
    #bar { height: 8px; background: #e3e3e3; border-radius: 4px; margin-top: 8px; overflow: hidden; }
    #barIn { height: 100%; width: 100%; background: var(--main); transition: width 1s linear; }
    .hurry #barIn { background: #e8590c; }   /* のこり5秒になったら色を変える */
    .hurry #time { color: #e8590c; }

    /* 遊ぶ場所(空と地面) */
    #field { position: relative; flex: 1; overflow: hidden; touch-action: manipulation;
             background: linear-gradient(var(--sky) 72%, var(--ground) 72%); }
    /* 空の飾り(太陽と雲) */
    .deco { position: absolute; pointer-events: none; font-size: 40px; }
    #sun { right: 14px; top: 10px; }
    .cloud { animation: drift 18s linear infinite; opacity: .9; }
    #cloud2 { top: 26%; font-size: 30px; animation-duration: 26s; animation-delay: -12s; }
    @keyframes drift { from { left: -60px; } to { left: 100%; } }

    /* タッチする木 */
    .target { position: absolute; font-size: 88px; line-height: 1; border: none; background: none;
              padding: 8px; cursor: pointer; display: none; animation: pop .25s ease-out;
              filter: drop-shadow(0 4px 2px rgba(0, 0, 0, .25)); }
    @keyframes pop { from { transform: scale(.6); } to { transform: scale(1); } }

    /* タッチしたときに出る「+1」 */
    .plus { position: absolute; font-weight: bold; font-size: 32px; pointer-events: none; color: #e8590c;
            text-shadow: 0 2px 0 #ffffff; animation: up .8s ease-out forwards; }
    /* 木のないところをタッチしたときの「×」 */
    .missMark { position: absolute; font-size: 22px; color: #999999; pointer-events: none;
                transform: translate(-50%, -50%); animation: fade .5s forwards; }
    @keyframes fade { to { opacity: 0; } }
    @keyframes up { to { transform: translateY(-50px); opacity: 0; } }

    #combo { position: absolute; left: 0; right: 0; top: 14px; text-align: center;
             font-size: 22px; font-weight: bold; color: #e8590c; pointer-events: none; }

    /* スタート画面と結果画面 */
    #panel { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
             background: rgba(255, 255, 255, .35); padding: 20px; }
    .box { background: #ffffff; border-radius: 18px; padding: 22px 26px; text-align: center;
           box-shadow: 0 8px 24px rgba(0, 0, 0, .15); max-width: 320px; }
    #panel h2 { margin: 0 0 8px; font-size: 26px; }
    #panel p { margin: 4px 0 14px; line-height: 1.7; }
    #rank { font-size: 72px; font-weight: bold; line-height: 1.1; }
    #rank.matsu { color: #145c32; }   /* 松の色 */
    #rank.take { color: #1c7c74; }    /* 竹の色 */
    #rank.ume { color: #d6336c; }     /* 梅の色 */
    #start { font-size: 20px; padding: 12px 36px; border: none; border-radius: 999px;
             background: var(--main); color: #ffffff; cursor: pointer; }
  </style>
</head>
<body>
  <div id="game">
    <header>
      <h1>🌲 まつタッチ</h1>
      <div class="info">
        <div class="card">スコア<b id="score">0</b></div>
        <div class="card">のこり<b id="time">20</b></div>
      </div>
      <div id="bar"><div id="barIn"></div></div>
    </header>

    <div id="field">
      <div class="deco" id="sun">☀️</div>
      <div class="deco cloud" id="cloud1">☁️</div>
      <div class="deco cloud" id="cloud2">☁️</div>
      <button class="target" id="tree">🌲</button>
      <button class="target" id="bonus">🎍</button>
      <div id="combo"></div>
      <div id="panel">
        <div class="box">
          <div id="rank"></div>
          <h2 id="title">🌲をタッチしよう!</h2>
          <p id="message">🌲は1点、ときどき出る🎍は3点。<br>続けてタッチするとコンボで点が2倍!</p>
          <button id="start">スタート</button>
        </div>
      </div>
    </div>
  </div>

  <script>
    // ゲームの設定(ここの数字を変えると、ゲームが変わります)
    const GAME_TIME = 20;   // 制限時間(秒)
    const MOVE_SPEED = 1500; // 木が動く間隔(ミリ秒。小さいほど速い)
    const BONUS_RATE = 0.25; // 1秒ごとに🎍が出る確率(0.25 は 25%)
    const BONUS_TIME = 2500; // 🎍が出ている時間(ミリ秒)
    const COMBO_GOAL = 5;    // 何回続けたら点が2倍になるか

    // 画面の部品を取り出す
    const field = document.getElementById("field");
    const tree = document.getElementById("tree");
    const bonus = document.getElementById("bonus");
    const scoreText = document.getElementById("score");
    const timeText = document.getElementById("time");
    const barIn = document.getElementById("barIn");
    const comboText = document.getElementById("combo");
    const panel = document.getElementById("panel");
    const startButton = document.getElementById("start");

    let score = 0;
    let time = GAME_TIME;
    let combo = 0;
    let playing = false;
    let timer = null;
    let mover = null;      // 木を動かすタイマー
    let bonusTimer = null; // 🎍を消すタイマー
    let lastMove = 0;      // 木が最後に動いた時刻

    // 部品を、遊ぶ場所の中のランダムな位置に置く
    function placeRandom(item) {
      const x = Math.random() * (field.clientWidth - 110);
      const y = Math.random() * (field.clientHeight - 115);
      item.style.left = x + "px";
      item.style.top = y + "px";
      // アニメーションをもう一度動かす
      item.style.animation = "none";
      item.offsetHeight;
      item.style.animation = "";
    }

    // 木を動かす。動いてから MOVE_SPEED ミリ秒たったら、また動かす
    function moveTargets() {
      placeRandom(tree);
      lastMove = Date.now();
      clearTimeout(mover);
      if (playing) {
        mover = setTimeout(moveTargets, MOVE_SPEED);
      }
    }

    // ときどき🎍を出す(BONUS_TIME ミリ秒たったら消える)
    function maybeBonus() {
      if (bonus.style.display === "block") return;
      if (Math.random() < BONUS_RATE) {
        bonus.style.display = "block";
        placeRandom(bonus);
        bonusTimer = setTimeout(function () { bonus.style.display = "none"; }, BONUS_TIME);
      }
    }

    // タッチした場所に「+1」などを出す
    function showPlus(item, text) {
      const plus = document.createElement("div");
      plus.className = "plus";
      plus.textContent = text;
      plus.style.left = (parseFloat(item.style.left) + 30) + "px";
      plus.style.top = item.style.top;
      field.appendChild(plus);
      setTimeout(function () { plus.remove(); }, 800);
    }

    // 点を足す
    function addPoint(item, base) {
      if (!playing) return;
      combo = combo + 1;
      let point = base;
      if (combo >= COMBO_GOAL) {
        point = base * 2;
      }
      score = score + point;
      scoreText.textContent = score;
      showPlus(item, "+" + point);
      comboText.textContent = combo >= 2 ? combo + " コンボ!" + (combo >= COMBO_GOAL ? " ×2" : "") : "";
    }

    tree.addEventListener("pointerdown", function (e) {
      e.stopPropagation();
      addPoint(tree, 1);
      moveTargets();
    });
    bonus.addEventListener("pointerdown", function (e) {
      e.stopPropagation();
      bonus.style.display = "none";
      clearTimeout(bonusTimer);
      addPoint(bonus, 3);
    });

    // 木のないところをタッチしたら、コンボが切れる
    // (木が動いた直後の0.4秒は、押しまちがいにしない)
    field.addEventListener("pointerdown", function (e) {
      if (!playing) return;
      if (Date.now() - lastMove < 400) return;
      combo = 0;
      comboText.textContent = "";
      const mark = document.createElement("div");
      mark.className = "missMark";
      mark.textContent = "×";
      const rect = field.getBoundingClientRect();
      mark.style.left = (e.clientX - rect.left) + "px";
      mark.style.top = (e.clientY - rect.top) + "px";
      field.appendChild(mark);
      setTimeout(function () { mark.remove(); }, 500);
    });

    // スタートボタン
    startButton.addEventListener("click", function () {
      score = 0;
      time = GAME_TIME;
      combo = 0;
      playing = true;
      scoreText.textContent = score;
      timeText.textContent = time;
      comboText.textContent = "";
      barIn.style.width = "100%";
      document.getElementById("game").classList.remove("hurry");
      panel.style.display = "none";
      tree.style.display = "block";
      moveTargets();

      timer = setInterval(function () {
        time = time - 1;
        timeText.textContent = time;
        barIn.style.width = (time / GAME_TIME * 100) + "%";
        maybeBonus();
        if (time <= 5) {
          document.getElementById("game").classList.add("hurry");
        }
        if (time <= 0) {
          endGame();
        }
      }, 1000);
    });

    // ゲームが終わったとき:点数で「松・竹・梅」を決める
    function endGame() {
      playing = false;
      clearInterval(timer);
      clearTimeout(mover);
      clearTimeout(bonusTimer);
      tree.style.display = "none";
      bonus.style.display = "none";
      comboText.textContent = "";

      let rank = "梅";
      let rankClass = "ume";
      let message = "まずは25点の「竹」をめざそう!";
      if (score >= 50) {
        rank = "松";
        rankClass = "matsu";
        message = "すごい! 最高ランクです!";
      } else if (score >= 25) {
        rank = "竹";
        rankClass = "take";
        message = "いい調子! あと少しで「松」!";
      }
      document.getElementById("rank").textContent = rank;
      document.getElementById("rank").className = rankClass;
      document.getElementById("title").textContent = "スコア " + score + " 点";
      document.getElementById("message").textContent = message;
      startButton.textContent = "もう一度";
      panel.style.display = "flex";
    }
  </script>
</body>
</html>

コードの読み方

このファイルは、3つの部分でできています。

部分役割このゲームでは
HTML
<body> の中
画面に置く部品タイトル、スコアとのこり時間、遊ぶ場所、🌲と🎍、スタート画面
CSS
<style> の中
見た目(色・大きさ・動き方)空と地面の色、文字の大きさ、🌲が飛び出す動き、雲が流れる動き
JavaScript
<script> の中
動き(ルール)タッチで点が入る、🌲が動く、コンボ、時間が0になったら松・竹・梅を決める

JavaScriptのいちばん上にある、この2行に注目しましょう。

const GAME_TIME = 20;   // 制限時間(秒)
const MOVE_SPEED = 1500; // 木が動く間隔(ミリ秒。小さいほど速い)

この数字を変えて保存し、ブラウザを再読み込みすると、ゲームのむずかしさが変わります。

CSSのいちばん上には、ゲームの色を決める3行があります。ここを変えると、空や地面の色を自分好みにできます(第8章で使います)。

--sky: #d8f3ff;     /* 空の色 */
--ground: #8fd17a;  /* 地面の色 */
--main: #145c32;    /* 文字とボタンの色 */

TASKむずかしさを変えてみよう

  1. GAME_TIME を 10 に変えて保存し、制限時間が10秒になるか確かめる
  2. MOVE_SPEED を小さくして、🌲 が速くなるか確かめる
  3. ちょうどよいむずかしさに決めたら、コミットしてプッシュする

AI PROMPTAIに聞いてみよう

このHTMLファイルの中で、「🌲」をタッチしたときに点数が増えるのはどの部分ですか?中学生にも分かるように説明してください。(コードを貼り付ける)

AIに説明してもらうと、コードの意味が分かりやすくなります。説明を読んだら、実際に数字を変えて、本当にそうなるか確かめましょう。

CHECK確認テスト

公開したとき、最初に表示されるファイルの名前は?
index.html です。
ゲームの見た目(色や大きさ)を決めているのは、HTML・CSS・JavaScriptのどれ?
CSS(<style> の中)です。
🌲 の動きを速くするには、どの数字をどう変えますか?
MOVE_SPEED の数字を小さくします(例:1500 → 1000)。