メインコンテンツへスキップ
見出し画像

超軽量 ジャンケン コードを公開

    <!DOCTYPE html>
    <html lang="ja">
    <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <title>Janken Light Engine - Battle Edition</title>

    <style>
    :root { --main:#66ffcc; --bg:#05070a; }

    body{
    margin:0;background:var(--bg);color:#fff;
    font-family:serif;text-align:center;overflow:hidden;
    }

    canvas{ position:fixed;top:0;left:0;width:100%;height:100%; z-index:0; }

    #ui { position:relative; z-index:2; height: 100vh; display: flex; flex-direction: column; justify-content: center; align-items: center; }

    /* 🛑① スコアとラウンドのヘッダー表示用スタイル */
    #game -header {
    position: absolute;
    top: 30px;
    display: flex;
    gap: 40px;
    font-size: 24px;
    color: #fff ;
    z-index: 5;
    }
    .stat-val { color: var(--main); font-weight: bold; }

    #msg { font-size:42px; margin-bottom: 10px; font-weight: bold; text-shadow: 0 0 15px rgba(255,255,255,0.4); }

    /* 🛑③ ヒントを薄く表示(opacity0.25) */
    #hint { color:#aaa; font-size: 25px; margin-bottom: 20px; opacity: 0.25; min-height: 35px; }

    /* 🛑 勝負手を表示するエリア(左右入れ替え & サイズ調整) */
    #battle -view {
    display: flex; justify-content: center; align-items: center;
    gap: 50px; height: 150px; margin-bottom: 20px;
    opacity: 0; transition: opacity 0.3s;
    }

    /* 自分の手:大きく表示 /
    #player -hand { font-size: 40px; line-height: 1.1; }
    / 敵の手:少し控えめに表示 */
    #enemy -hand { font-size: 80px; line-height: 1.1; }

    .hand-label { font-size: 14px; color: #888 ; display: block; margin-bottom: 5px; }

    .controls{ display:none; gap:15px; justify-content:center; }
    .btn{
    width:90px;height:90px;font-size:45px;border-radius:50%;
    background: rgba(0,0,0,0.6); color: #fff ; border: 2px solid #444 ;
    cursor: pointer; transition: 0.2s;
    }
    .btn:hover{ border-color: var(--main); transform: scale(1.1); background: rgba(102, 255, 204, 0.2); }
    button#start { padding:15px 50px; cursor: pointer; font-size: 24px; background: none; color: var(--main); border: 2px solid var(--main); z-index: 10; }

    /* 🛑④ 簡易ランキング用スタイル */
    #ranking -box {
    display: none;
    margin-top: 30px;
    padding: 15px;
    width: 260px;
    background: rgba(0,0,0,0.8);
    border-radius: 10px;
    border: 1px solid #444 ;
    text-align: left;
    }
    #ranking -box h3 { margin: 0 0 10px 0; text-align: center; color: var(--main); font-size: 18px; }
    #ranking -box ol { margin: 0; padding-left: 25px; font-size: 18px; color: #fff ; }
    #ranking -box li { margin-bottom: 5px; }
    </style>
    </head>

    <body>

    <canvas id="fx"></canvas>

    <div id="ui">
    <div id="game-header">
    <div>SCORE: <span id="score-text" class="stat-val">0</span></div>
    <div>ROUND: <span id="round-text" class="stat-val">1 / 7</span></div>
    </div>

    <div id="msg">JANKEN ENGINE</div>
    <div id="hint">準備はよいか…</div>

    <div id="battle-view">
    <div><span class="hand-label">YOU</span><span id="player-hand">?</span></div>

    <div style="font-size:40px; color:var(--main);">VS</div>
    
    <div><span class="hand-label">ENEMY</span><span id="enemy-hand">?</span></div>

    </div>

    <button id="start">START</button>

    <div class="controls" id="controls">
    <button class="btn" data-hand="rock">✊</button>
    <button class="btn" data-hand="scissors">✌️</button>
    <button class="btn" data-hand="paper">✋</button>
    </div>

    <div id="ranking-box">
    <h3>🏆 TOP SCORES</h3>
    <ol id="ranking-list"></ol>
    </div>
    </div>

    <script>
    /* =========================
    🎭 テーマ設定
    ========================= */
    const THEME = "vortex";

    const themeSettings = {
    vortex: { name: "DARKNESS", color: "#00ffcc", hint: "感覚で気配を読め…" },
    sakura: { name: "SAKURA", color: "#ffb7c5", hint: "散る際を見極めよ…" },
    fire: { name: "HONNOJI", color: "#ff4444", hint: "業火に惑わされるな…" },
    snow: { name: "ECHIGO", color: "#ffffff", hint: "静寂の音を聴け…" }
    };

    /* =========================
    🎮 ゲーム状態とランキング変数の追加
    ========================= */
    let score = 0, combo = 0, currentRound = 1;
    const maxRounds = 7; // 🛑① 7回戦制に設定

    // 🛑④ ランキング保存(ローカルストレージに最大3件)
    let rankings = JSON.parse(localStorage.getItem("janken_engine_rankings")) || [1500, 1000, 500];

    // 🛑⑤ サウンド再生の共通化
    const sounds = {
    win:new Audio("win.mp3"),
    lose:new Audio("lose.mp3"),
    draw:new Audio("draw.mp3")
    };

    function playSound(type){
    sounds[type].currentTime = 0;
    sounds[type].play().catch(()=>{});
    }

    // 🛑④ ランキング表示更新
    function updateRankingDOM() {
    const list = document.getElementById("ranking-list");
    list.innerHTML = rankings.map((s, idx) => `<li>${["🥇","🥈","🥉"][idx]} ${s} pts</li>`).join("");
    }
    updateRankingDOM();

    /* =========================
    🌀 Canvas設定
    ========================= */
    const canvas = document.getElementById("fx");
    const ctx = canvas.getContext("2d");
    let t = 0;
    let isLoopRunning = false; // ループの重複起動防止フラグ

    function resize(){ canvas.width = innerWidth; canvas.height = innerHeight; }

    window.addEventListener("resize", resize);

    function drawVortex(){
    ctx.fillStyle="rgba(0,0,0,0.2)";
    ctx.fillRect(0,0,canvas.width,canvas.height);

    for(let i=0; i<150; i++){
    let angle = i * 0.1 + t;
    let radius = i * 1.5;
    let x = canvas.width/2 + Math.cos(angle)*radius;
    let y = canvas.height/2 + Math.sin(angle)radius;
    ctx.fillStyle=`hsl(${(i+t50)%360},100%,50%)`;
    ctx.fillRect(x,y,2,2);
    }
    t += 0.01;
    }

    /* =========================
    🎮 ゲームコアロジック
    ========================= */
    let enemy = "";
    const hands = ["rock", "scissors", "paper"];
    const handIcons = { rock: "✊", scissors: "✌️", paper: "✋" };
    const winRules = { rock: "scissors", scissors: "paper", paper: "rock" };

    function loop(){
    drawVortex();
    requestAnimationFrame(loop);
    }

    function round(){
    document.getElementById("controls").style.display = "none";
    document.getElementById("battle-view").style.opacity = "0";

    // 敵の手を決定
    enemy = hands[Math.floor(Math.random() * 3)];

    // 🛑③ ヒント機能:敵の手の絵文字をあらかじめ薄く表示しておく(不透明度はCSS側で制御)
    const config = themeSettings[THEME];
    document.getElementById("hint").innerText = `${config.hint} 【敵の気配: ${handIcons[enemy]}】`;

    setTimeout(() => {
    document.getElementById("msg").innerText = "勝負!";
    document.getElementById("controls").style.display = "flex";
    }, 1200);
    }

    function play(my) {
    document.getElementById("controls").style.display = "none";

    document.getElementById("player-hand").innerText = handIcons[my];
    document.getElementById("enemy-hand").innerText = handIcons[enemy];
    document.getElementById("battle-view").style.opacity = "1";

    let res = (my === enemy) ? "draw" : (winRules[my] === enemy) ? "win" : "lose";
    const config = themeSettings[THEME];
    const msgEl = document.getElementById("msg");

    // 🛑⑤ 音声の再生
    playSound(res);

    if (res === "win") {
    score += 100 + combo * 20;
    combo++;
    msgEl.innerText = "勝利 (Victory)";
    msgEl.style.color = "#ffff00";
    } else if (res === "lose") {
    combo = 0;
    msgEl.innerText = "敗北 (Defeat)";
    msgEl.style.color = "#ff4444";
    } else {
    msgEl.innerText = "拮抗 (Draw)";
    msgEl.style.color = config.color;
    }

    // スコア表示更新
    document.getElementById("score-text").innerText = score;

    // 次のラウンド判定、またはゲーム終了(7回戦)の判定
    setTimeout(() => {
    if(currentRound >= maxRounds) {
    endGame(); // 7回戦終了処理へ
    } else {
    currentRound++;
    document.getElementById("round-text").innerText = `${currentRound} / ${maxRounds}`;
    round();
    }
    }, 2000);
    }

    // 🛑④ ゲーム終了時のランキング処理
    function endGame() {
    document.getElementById("msg").innerText = "GAME OVER";
    document.getElementById("msg").style.color = "#fff";
    document.getElementById("hint").innerText = `最終スコア: ${score} pts`;
    document.getElementById("battle-view").style.opacity = "0";

    // ランキング集計とローカルストレージへの保存
    rankings.push(score);
    rankings.sort((a, b) => b - a);
    rankings = rankings.slice(0, 3);
    localStorage.setItem("janken_engine_rankings", JSON.stringify(rankings));

    updateRankingDOM();
    document.getElementById("ranking-box").style.display = "block"; // ランキング表示

    // 再スタート用にSTARTボタンを復活させる
    const startBtn = document.getElementById("start");
    startBtn.innerText = "RESTART";
    startBtn.style.display = "block";

    // 数値のリセット
    currentRound = 1;
    score = 0;
    combo = 0;
    }

    /* =========================
    ▶ イベントリスナー
    ========================= */
    document.querySelectorAll(".btn").forEach(b => {
    b.onclick = () => play(b.dataset.hand);
    });

    document.getElementById("start").onclick = () => {
    const config = themeSettings[THEME];
    document.getElementById("msg").innerText = config.name;
    document.getElementById("msg").style.color = config.color;
    document.getElementById("hint").innerText = config.hint;
    document.getElementById("start").style.display = "none";

    // 再プレイ(RESTART)のときのために表示を初期化
    document.getElementById("ranking-box").style.display = "none";
    document.getElementById("score-text").innerText = score;
    document.getElementById("round-text").innerText = `${currentRound} / ${maxRounds}`;

    // 背景アニメーションのループを一度だけ開始(重複防止)
    if (!isLoopRunning) {
    loop();
    isLoopRunning = true;
    }
    round();
    };
    </script>
    </body>
    </html>

     
     
    インタラクティブなHTMLコンテンツをGitHub Pages上に公開。 note 上でも、コードを 公開中。秀吉とジャンケン。 闇バイトなど、実際の危険に対応するジャンケンゲームを作成。皇位継承の問題点。 額田王と持統天皇の、女の闘い。持統天皇と藤原不比等の野望。

    あなたへのおすすめ