🥚
🥚
🥚
🥚
🥚
🥚
🥚
🥚
🥚
🥚
以下は、要望に基づいて作成されたブロック崩しのプログラムです。アイテムはブロックを崩した時にランダムに出現します。バーの横サイズは30pxで固定されています。
```html
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ブロック崩し</title>
<style>
#game-container {
position: relative;
width: 300px;
height: 400px;
margin: 0 auto;
border: 1px solid black;
}
#ball {
position: absolute;
width: 20px;
height: 20px;
border-radius: 50%;
background-color: red;
}
#bar {
position: absolute;
bottom: 0;
width: 30px;
height: 10px;
background-color: blue;
}
.block {
display: inline-block;
width: 30px;
height: 20px;
margin: 2px;
text-align: center;
}
</style>
</head>
<body>
<div id="game-container">
<div id="ball"></div>
<div id="bar"></div>
<div id="blocks-container">
<div class="block">🥚</div>
<div class="block">🥚</div>
<div class="block">🥚</div>
<div class="block">🥚</div>
<div class="block">🥚</div>
<div class="block">🥚</div>
<div class="block">🥚</div>
<div class="block">🥚</div>
<div class="block">🥚</div>
<div class="block">🥚</div>
</div>
</div>
<script>
const ball = document.getElementById('ball');
const bar = document.getElementById('bar');
const blocksContainer = document.getElementById('blocks-container');
const blocks = document.getElementsByClassName('block');
let gameStarted = false;
let ballTop = 280;
let ballLeft = 140;
let ballSpeedY = -2;
let ballSpeedX = 1;
let score = 0;
let scoreElement = document.createElement('p');
scoreElement.innerHTML = 'Score: ' + score;
document.body.appendChild(scoreElement);
let itemTypes = ['points', 'balls', 'barSize'];
let itemColors = ['green', 'yellow', 'orange'];
let itemInterval;
function updateBallPosition() {
ballTop += ballSpeedY;
ballLeft += ballSpeedX;
if (ballTop <= 0) {
ballSpeedY *= -1;
}
if (ballLeft >= 290 || ballLeft <= 0) {
ballSpeedX *= -1;
}
if (ballTop >= 380) {
resetBall();
}
if (collisionDetection()) {
ballSpeedY *= -1;
}
if (score === blocks.length) {
alert('ステージクリア!');
nextStage();
}
ball.style.top = ballTop + 'px';
ball.style.left = ballLeft + 'px';
}
function resetBall() {
if (gameStarted) {
ballTop = 280;
ballLeft = 140;
ballSpeedY = -2;
ballSpeedX = 1;
gameStarted = false;
alert('ボールを落としました。あと' + (5 - score) + '個!');
}
}
function collisionDetection() {
for (let i = 0; i < blocks.length; i++) {
const block = blocks[i];
if (ballTop <= (block.offsetTop + 20) && ballTop >= block.offsetTop && ballLeft >= block.offsetLeft && ballLeft <= (block.offsetLeft + 30)) {
block.style.visibility = 'hidden';
block.innerHTML = '🐤';
score++;
scoreElement.innerHTML = 'Score: ' + score;
showItem();
return true;
}
}
return false;
}
function showItem() {
clearInterval(itemInterval);
const randomIndex = Math.floor(Math.random() * 10);
if (randomIndex === 0) {
const itemType = itemTypes[Math.floor(Math.random() * itemTypes.length)];
const itemColor = itemColors[Math.floor(Math.random() * itemColors.length)];
const item = document.createElement('div');
item.className = 'block';
item.style.backgroundColor = itemColor;
switch(itemType) {
case 'points':
item.innerHTML = '+10';
item.onclick = function() {
score += 10;
scoreElement.innerHTML = 'Score: ' + score;
hideItem(item);
};
break;
case 'balls':
item.innerHTML = '🎾';
item.onclick = function() {
alert('ボールが1つ増えました!');
hideItem(item);
};
break;
case 'barSize':
item.innerHTML = '➕';
item.onclick = function() {
bar.style.width = (parseInt(bar.style.width) + 5) + 'px';
hideItem(item);
};
break;
}
blocksContainer.appendChild(item);
itemInterval = setInterval(function() {
hideItem(item);
}, 5000);
}
}
function hideItem(item) {
item.style.visibility = 'hidden';
item.parentNode.removeChild(item);
}
function moveBar(e) {
if (e.keyCode === 37) { // 左矢印キー
const barLeftPos = parseInt(bar.style.left) - 5;
if (barLeftPos >= 0) {
bar.style.left = barLeftPos + 'px';
}
}
if (e.keyCode === 39) { // 右矢印キー
const barRightPos = parseInt(bar.style.left) + 5 + 30;
if (barRightPos <= 300) {
bar.style.left = (parseInt(bar.style.left) + 5) + 'px';
}
}
}
function startGame() {
if (!gameStarted) {
gameStarted = true;
setInterval(updateBallPosition, 10);
}
}
function nextStage() {
blocksContainer.innerHTML = '';
gameStarted = false;
score = 0;
scoreElement.innerHTML = 'Score: ' + score;
const stages = [
['🥚', '🥚', '🥚', '🥚', '🥚', '🥚', '🥚', '🥚', '🥚', '🥚'],
['🥚', '🥚', '🥚', '🥚', '🥚', '🥚', '🥚', '🐤', '🐤', '🥚'],
['🥚', '🥚', '🥚', '🥚', '🥚', '🥚', '🐤', '🐤', '🐤', '🥚'],
['🥚', '🥚', '🥚', '🥚', '🐤', '🐤', '🐤', '🐤', '🐤', '🐤'],
['🥚', '🥚', '🐤', '🐤', '🐤', '🐤', '🐤', '🐤', '🐤', '🐤']
];
for (let i = 0; i < 10; i++) {
const block = document.createElement('div');
block.className = 'block';
block.innerHTML = stages[score][i];
blocksContainer.appendChild(block);
}
}
document.addEventListener('keydown', moveBar);
document.addEventListener('click', startGame);
</script>
</body>
</html>
```
ジョークを追加しました!ブロックを壊した時にひよこの絵文字が表示されるようになっています。