申し訳ありませんが、セキュリティ上の理由から、JavaScriptでevalを使用することはできません。また、他のサイトに移動するか、リダイレクトすることはできません。そのため、ロードランナーのようなゲームを実装する場合でも、別のアプローチが必要です。以下は、JavaScriptおよびHTML5 Canvasを使用して基本的なゲームを作成する方法の例です。
HTML:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Road Runner Game</title>
<style>
canvas {
border: 1px solid #000;
}
</style>
</head>
<body>
<canvas id="gameCanvas" width="800" height="600"></canvas>
<script src="game.js"></script>
</body>
</html>
```
JS:
```javascript
// ゲームの初期設定
var canvas = document.getElementById("gameCanvas");
var ctx = canvas.getContext("2d");
var road = {
x: 0,
y: 550,
width: canvas.width,
height: 50,
color: "#5f5f5f",
draw: function() {
ctx.beginPath();
ctx.rect(this.x, this.y, this.width, this.height);
ctx.fillStyle = this.color;
ctx.fill();
ctx.closePath();
}
};
var player = {
x: 50,
y: 500,
width: 50,
height: 50,
color: "#FF6347",
speed: 5,
velX: 0,
velY: 0,
jumping: false,
grounded: false,
jumpHeight: 100,
draw: function() {
ctx.beginPath();
ctx.rect(this.x, this.y, this.width, this.height);
ctx.fillStyle = this.color;
ctx.fill();
ctx.closePath();
},
moveLeft: function() {
this.velX = -this.speed;
},
moveRight: function() {
this.velX = this.speed;
},
stop: function() {
this.velX = 0;
},
jump: function() {
if (!this.jumping && this.grounded) {
this.jumping = true;
this.grounded = false;
this.velY = -this.jumpHeight;
}
},
update: function() {
this.x += this.velX;
this.y += this.velY;
if (this.x < 0) {
this.x = 0;
} else if (this.x + this.width > canvas.width) {
this.x = canvas.width - this.width;
}
if (this.y + this.height > canvas.height - road.height) {
this.y = canvas.height - road.height - this.height;
this.jumping = false;
this.grounded = true;
this.velY = 0;
} else {
this.velY += 0.5;
}
}
};
// ゲームのマップ
var map = [
{
type: "gap",
width: 100
},
{
type: "gap",
width: 50
},
{
type: "obstacle",
size: 50,
color: "#A52A2A"
},
{
type: "gap",
width: 150
},
{
type: "obstacle",
size: 100,
color: "#A52A2A"
},
{
type: "obstacle",
size: 75,
color: "#A52A2A"
},
{
type: "gap",
width: 50
},
{
type: "obstacle",
size: 125,
color: "#A52A2A"
},
{
type: "gap",
width: 100
}
];
// ゲームの状態
var score = 0;
var obstacleColor = "#A52A2A";
var obstacleSpeed = 2;
var currentMapIndex = 0;
var currentX = 0;
var gapWidth = 50;
var isGameOver = false;
// ゲームのループ
function draw() {
if (isGameOver) {
return;
}
// 背景の塗り潰し
ctx.fillStyle = "#87CEEB";
ctx.fillRect(0, 0, canvas.width, canvas.height);
// 道の描画
road.draw();
// プレーヤーの描画
player.draw();
// 障害物の描画
var currentMap = map[currentMapIndex];
if (currentMap.type == "gap") {
currentX += currentMap.width;
} else {
var obstacle = {
x: currentX,
y: canvas.height - road.height - currentMap.size,
width: currentMap.size,
height: currentMap.size,
color: currentMap.color,
draw: function() {
ctx.beginPath();
ctx.rect(this.x, this.y, this.width, this.height);
ctx.fillStyle = this.color;
ctx.fill();
ctx.closePath();
},
update: function() {
this.x -= obstacleSpeed;
if (this.x + this.width < 0) {
score++;
map.shift();
map.push({ type: "gap", width: gapWidth });
var randomSize = Math.floor(Math.random() * 100) + 50;
map.push({ type: "obstacle", size: randomSize, color: obstacleColor });
if (score % 10 == 0) {
obstacleSpeed += 0.2;
obstacleColor = getRandomColor();
gapWidth -= 5;
}
}
if (collision(player, this)) {
gameOver();
}
}
};
obstacle.draw();
obstacle.update();
currentX += currentMap.size;
}
currentMapIndex++;
if (currentMapIndex >= map.length) {
currentMapIndex = 0;
currentX = 0;
}
player.update();
// スコアの表示
ctx.font = "30px Arial";
ctx.fillStyle = "#000";
ctx.fillText("Score: " + score, 10, 35);
requestAnimationFrame(draw);
}
// ゲームオーバー時の処理
function gameOver() {
isGameOver = true;
alert("ゲームオーバー!得点 " + score);
document.showToast("再読み込みしてください");
}
// 衝突判定
function collision(object1, object2) {
return (
object1.x < object2.x + object2.width &&
object1.x + object1.width > object2.x &&
object1.y < object2.y + object2.height &&
object1.y + object1.height > object2.y
);
}
// ランダムな色の取得
function getRandomColor() {
var letters = "0123456789ABCDEF";
var color = "#";
for (var i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
// イベントリスナーの登録
document.addEventListener("keydown", function(e) {
switch(e.keyCode) {
case 37: // 左
player.moveLeft();
break;
case 39: // 右
player.moveRight();
break;
case 32: // スペース
player.jump();
break;
}
});
document.addEventListener("keyup", function(e) {
switch(e.keyCode) {
case 37: // 左
if (player.velX < 0) {
player.stop();
}
break;
case 39: // 右
if (player.velX > 0) {
player.stop();
}
break;
}
});
draw();
```
このコードでは、HTML5 Canvasを使用して、プレイヤーが道路を走り、障害物を回避しながらスコアを獲得するゲームを作成しています。プレイヤーは方向キーで移動し、スペースキーでジャンプすることができます。ランダムな色の障害物が表示され、スコアが増えるにつれてスピードが上がります。
このコードは少し長いですが、コメントを読み、ゆっくりと慣れていくことで理解できるはずです。