はい、ポケモンダンジョンのようなゲームをHTML一つのファイルで作成してみましょう。以下のコードをテキストエディタにコピーし、「index.html」などの名前で保存して、ブラウザで開いてください。
html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>ポケモンダンジョン風ゲーム</title>
<style>
body {
font-family: Arial, sans-serif;
}
#game {
display: grid;
grid-template-columns: repeat(20, 32px);
grid-template-rows: repeat(20, 32px);
gap: 1px;
background-color: #000;
margin: 20px auto;
width: 660px;
}
.tile {
width: 32px;
height: 32px;
text-align: center;
line-height: 32px;
font-size: 24px;
}
.floor {
background-color: gray;
color: #fff;
}
.wall {
background-color: brown;
}
.player {
background-color: green;
color: #fff;
}
.monster {
background-color: red;
color: #fff;
}
.goal {
background-color: gold;
color: #000;
}
</style>
</head>
<body>
<h1>ポケモンダンジョン風ゲーム</h1>
<div id="game"></div>
<script>
const width = 20;
const height = 20;
const gameDiv = document.getElementById('game');
let maze = [];
let playerPosition = { x: 1, y: 1 };
let goalPosition = { x: width - 2, y: height - 2 };
let monsters = [];
// 迷路の初期化
for (let y = 0; y < height; y++) {
maze[y] = [];
for (let x = 0; x < width; x++) {
maze[y][x] = 'wall';
}
}
// 迷路の生成
function generateMaze() {
for (let y = 1; y < height - 1; y += 2) {
for (let x = 1; x < width - 1; x += 2) {
maze[y][x] = 'floor';
let directions = [
{ x: 0, y: -2 },
{ x: 2, y: 0 },
{ x: 0, y: 2 },
{ x: -2, y: 0 }
];
directions.sort(() => Math.random() - 0.5);
for (let dir of directions) {
let nx = x + dir.x;
let ny = y + dir.y;
if (nx > 0 && nx < width -1 && ny > 0 && ny < height -1) {
if (maze[ny][nx] === 'wall') {
maze[y + dir.y / 2][x + dir.x /2] = 'floor';
maze[ny][nx] = 'floor';
}
}
}
}
}
}
generateMaze();
maze[playerPosition.y][playerPosition.x] = 'player';
maze[goalPosition.y][goalPosition.x] = 'goal';
// モンスターの配置
for (let i = 0; i < 5; i++) {
let monsterPlaced = false;
while (!monsterPlaced) {
let mx = Math.floor(Math.random() * (width -2)) +1;
let my = Math.floor(Math.random() * (height -2)) +1;
if (maze[my][mx] === 'floor') {
maze[my][mx] = 'monster';
monsters.push({ x: mx, y: my });
monsterPlaced = true;
}
}
}
function drawMaze() {
gameDiv.innerHTML = '';
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
let tile = document.createElement('div');
tile.classList.add('tile');
tile.classList.add(maze[y][x]);
if (maze[y][x] === 'player') {
tile.textContent = '😃';
} else if (maze[y][x] === 'monster') {
tile.textContent = '👾';
} else if (maze[y][x] === 'goal') {
tile.textContent = '⭐';
}
gameDiv.appendChild(tile);
}
}
}
function movePlayer(dx, dy) {
let newX = playerPosition.x + dx;
let newY = playerPosition.y + dy;
if (maze[newY][newX] === 'floor' || maze[newY][newX] === 'goal') {
maze[playerPosition.y][playerPosition.x] = 'floor';
playerPosition.x = newX;
playerPosition.y = newY;
if (maze[newY][newX] === 'goal') {
alert('ゴールしました!おめでとうございます!');
}
maze[newY][newX] = 'player';
drawMaze();
} else if (maze[newY][newX] === 'monster') {
battleMonster(newX, newY);
}
}
function battleMonster(x, y) {
let fight = confirm('モンスターが現れた!戦いますか?');
if (fight) {
let win = Math.random() > 0.5;
if (win) {
alert('モンスターを倒しました!');
maze[y][x] = 'floor';
movePlayer(x - playerPosition.x, y - playerPosition.y);
} else {
alert('敗北しました...ゲームオーバー');
location.reload();
}
} else {
alert('逃げました');
}
}
document.addEventListener('keydown', function(e) {
if (e.key === 'ArrowUp') {
movePlayer(0, -1);
} else if (e.key === 'ArrowDown') {
movePlayer(0, 1);
} else if (e.key === 'ArrowLeft') {
movePlayer(-1, 0);
} else if (e.key === 'ArrowRight') {
movePlayer(1, 0);
}
});
drawMaze();
</script>
</body>
</html>