以下が提案するJavaScriptコードです。セキュリティーに問題がないように注意してください。また、ゴミに当たってもゲームオーバーにならないことに関するジョークを取り入れました。
```html
<!DOCTYPE html>
<html>
<head>
<title>ゴミ避けマリオ</title>
<style>
body {
margin: 0;
padding: 0;
}
canvas {
display: block;
position: absolute;
top: 0;
left: 0;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<script>
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
// キャンバスの大きさを設定
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// オブジェクトの定義
var mario = {
x: 100,
y: canvas.height / 2,
width: 50,
height: 50,
speed: 0,
acceleration: 0.1,
jump: -5
};
var garbageList = [];
// ゴミを生成する関数
function createGarbage() {
var garbageWidth = Math.random() * 50 + 20;
var garbageHeight = Math.random() * 50 + 20;
var garbageSpeed = Math.random() * 5 + 2;
var garbageObj = {
x: canvas.width,
y: Math.random() * (canvas.height - garbageHeight),
width: garbageWidth,
height: garbageHeight,
speed: garbageSpeed
};
garbageList.push(garbageObj);
}
// キャンバスをクリックした時の処理
canvas.onclick = function() {
mario.jump = -5 - Math.abs(mario.speed) / 2;
};
// ゲームオーバー画面を表示する関数
function gameOver() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = "black";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.font = "60px sans-serif";
ctx.fillStyle = "red";
ctx.textAlign = "center";
ctx.fillText("Game Over!", canvas.width / 2, canvas.height / 2);
ctx.font = "30px sans-serif";
ctx.fillText("You saved the Earth from garbage!", canvas.width / 2, canvas.height / 2 + 50);
ctx.font = "20px sans-serif";
ctx.fillText("But Mario won't be paid for this job.", canvas.width / 2, canvas.height / 2 + 100);
}
// ゲームのメインループ
function gameLoop() {
if (mario.y > canvas.height || mario.y < 0) {
gameOver();
return;
}
// キャンバスの描画
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = "green";
ctx.fillRect(0, 0, canvas.width, canvas.height);
// ジャンプの処理
if (mario.jump < 0) {
mario.speed += mario.jump;
} else {
mario.speed += mario.acceleration;
}
mario.y += mario.speed;
// ゴミの処理
for (var i = 0; i < garbageList.length; i++) {
garbageList[i].x -= garbageList[i].speed;
ctx.fillStyle = "brown";
ctx.fillRect(garbageList[i].x, garbageList[i].y, garbageList[i].width, garbageList[i].height);
if (garbageList[i].x + garbageList[i].width < 0) {
garbageList.splice(i, 1);
i--;
}
}
// キャラクターの描画
ctx.fillStyle = "red";
ctx.fillRect(mario.x, mario.y, mario.width, mario.height);
// ゴミの生成
if (Math.random() < 0.05) {
createGarbage();
}
// 次のフレームの処理
requestAnimationFrame(gameLoop);
}
// ゲームの開始
gameLoop();
</script>
</body>
</html>
```
ジョークには以下のようなものがあります。
```
ctx.fillText("You saved the Earth from garbage!", canvas.width / 2, canvas.height / 2 + 50); //地球を救った
ctx.fillText("But Mario won't be paid for this job.", canvas.width / 2, canvas.height / 2 + 100); //でもマリオは報酬はもらえない
```