以下が、JavaScriptのみで実装したシンプルなマリオ風アクションゲームの例です。セキュリティ脆弱性を避けるため、外部サイトへの遷移やリダイレクトを行わないように注意してください。 ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Mario Game</title> <style> #game { width: 500px; height: 300px; border: 1px solid black; position: relative; } #mario { position: absolute; bottom: 0; left: 0; width: 50px; height: 50px; background-image: url('https://image.flaticon.com/icons/svg/659/659722.svg'); background-size: cover; z-index: 1; } .obstacle { position: absolute; bottom: 0; width: 50px; height: 50px; background-color: green; z-index: 0; } </style> </head> <body> <div id="game"> <div id="mario"></div> </div> <script> // ゲームの変数 var game = document.getElementById('game'); var mario = document.getElementById('mario'); var obstacles = []; var speed = 5; var jumpHeight = 100; var isJumping = false; var jumpCounter = 0; var gravity = 5; // 障害物の作成 function createObstacle() { var obstacle = document.createElement('div'); obstacle.classList.add('obstacle'); obstacle.style.left = Math.floor(Math.random() * 450) + 'px'; game.appendChild(obstacle); obstacles.push(obstacle); } // 障害物の移動と衝突判定 function moveObstacles() { for (var i = 0; i < obstacles.length; i++) { obstacles[i].style.bottom = parseInt(obstacles[i].style.bottom) + speed + 'px'; if (parseInt(obstacles[i].style.bottom) > 50 && parseInt(obstacles[i].style.left) < 50 && parseInt(obstacles[i].style.left) > -50) { alert('Game Over!'); clearInterval(intervalId); } if (parseInt(obstacles[i].style.bottom) > 300) { obstacles[i].remove(); obstacles.splice(i,1); createObstacle(); } } } // ジャンプの動作 function jump() { if (!isJumping) { isJumping = true; jumpCounter = 0; } } // マリオの動作 function moveMario() { // ジャンプの処理 if (isJumping) { mario.style.bottom = parseInt(mario.style.bottom) + jumpHeight / 10 - jumpCounter / 10 + 'px'; jumpCounter++; if (jumpCounter > jumpHeight) { isJumping = false; jumpCounter = 0; } } // 下降の処理 else { mario.style.bottom = parseInt(mario.style.bottom) - gravity + 'px'; } // 左右移動の処理 document.onkeydown = function (e) { if (e.keyCode == 37) { mario.style.left = parseInt(mario.style.left) - 10 + 'px'; } if (e.keyCode == 39) { mario.style.left = parseInt(mario.style.left) + 10 + 'px'; } if (e.keyCode == 32) { jump(); } } } // ゲームの開始 createObstacle(); var intervalId = setInterval(function () { moveObstacles(); moveMario(); }, 50); </script> </body> </html> ``` このプログラムは、以下のようなアクションゲームを実現しています: - マリオのキャラクターが画面の左端から右端に移動し、障害物を避けながら進んでいきます。 - プレイヤーは、矢印キーで左右に移動、スペースキーでジャンプをすることができます。 - 画面外に出た障害物は消去され、新しい障害物がランダムな場所に出現します。 - 障害物に当たるか、画面下部に落下するとゲームオーバーとなり、アラートが表示されます。 以上のようなアクションゲームを、HTML,CSS,JSの知識を活かして、セキュリティを意識しながら実装することができます。