微件:ZombieGame:修订间差异
来自植物大战僵尸杂交版Wiki
更多操作
创建页面,内容为“<includeonly> <style> .zombie-game { position: relative; width: 100%; max-width: 600px; height: 250px; background: #5a8f4a; border: 4px solid #3a5c2f; border-radius: 12px; overflow: hidden; margin: 20px auto; cursor: pointer; user-select: none; touch-action: manipulation; } .zombie-game .nut { position: absolute; bottom: 20px; left: 50px; width: 50px; height: 60px; background: #8B4513;…” |
无编辑摘要 |
||
| 第1行: | 第1行: | ||
<includeonly> | <includeonly> | ||
<style> | <style> | ||
. | .shooter-game { | ||
position: relative; | position: relative; | ||
width: 100%; | width: 100%; | ||
max-width: | max-width: 700px; | ||
height: | height: 350px; | ||
background: #5a8f4a; | background: linear-gradient(180deg, #87CEEB 0%, #87CEEB 60%, #5a8f4a 60%, #5a8f4a 100%); | ||
border: 4px solid #3a5c2f; | border: 4px solid #3a5c2f; | ||
border-radius: 12px; | border-radius: 12px; | ||
overflow: hidden; | overflow: hidden; | ||
margin: 20px auto; | margin: 20px auto; | ||
cursor: | cursor: crosshair; | ||
user-select: none; | user-select: none; | ||
touch-action: manipulation; | touch-action: manipulation; | ||
} | } | ||
. | .shooter-game .plant { | ||
position: absolute; | position: absolute; | ||
bottom: | bottom: 15px; | ||
left: | left: 30px; | ||
width: | width: 70px; | ||
height: | height: 70px; | ||
} | |||
.shooter-game .plant img { | |||
width: 100%; | |||
height: 100%; | |||
} | |||
.shooter-game .zombie { | |||
position: absolute; | |||
bottom: 15px; | |||
width: 55px; | |||
height: 70px; | |||
transition: left 0.1s linear; | |||
} | } | ||
. | .shooter-game .zombie img { | ||
width: 100%; | |||
height: 100%; | |||
} | } | ||
. | .shooter-game .bullet { | ||
position: absolute; | position: absolute; | ||
width: 12px; | |||
height: 12px; | |||
width: | background: #4CAF50; | ||
height: | border-radius: 50%; | ||
background: # | pointer-events: none; | ||
border-radius: | |||
} | } | ||
. | .shooter-game .score { | ||
position: absolute; | position: absolute; | ||
top: 10px; | top: 10px; | ||
right: 15px; | right: 15px; | ||
font-size: | font-size: 20px; | ||
font-weight: bold; | font-weight: bold; | ||
color: #fff; | color: #fff; | ||
text-shadow: | text-shadow: 2px 2px 2px #000; | ||
} | } | ||
. | .shooter-game .game-over { | ||
position: absolute; | position: absolute; | ||
top: 50%; | top: 50%; | ||
left: 50%; | left: 50%; | ||
transform: translate(-50%, -50%); | transform: translate(-50%, -50%); | ||
font-size: | font-size: 30px; | ||
font-weight: bold; | font-weight: bold; | ||
color: #fff; | color: #fff; | ||
text-shadow: | text-shadow: 3px 3px 6px #000; | ||
display: none; | display: none; | ||
text-align: center; | text-align: center; | ||
line-height: 1. | line-height: 1.6; | ||
z-index: 10; | |||
} | } | ||
. | .shooter-game .restart-btn { | ||
display: block; | display: block; | ||
margin: 10px auto; | margin: 10px auto; | ||
padding: | padding: 10px 24px; | ||
font-size: | font-size: 18px; | ||
background: #f44336; | background: #f44336; | ||
color: #fff; | color: #fff; | ||
| 第77行: | 第78行: | ||
border-radius: 8px; | border-radius: 8px; | ||
cursor: pointer; | cursor: pointer; | ||
} | |||
@media screen and (max-width: 500px) { | |||
.shooter-game { height: 280px; } | |||
.shooter-game .plant { width: 55px; height: 55px; left: 15px; bottom: 10px; } | |||
.shooter-game .zombie { width: 42px; height: 55px; bottom: 10px; } | |||
} | } | ||
</style> | </style> | ||
<div class=" | <div class="shooter-game" id="shooter-game"> | ||
<div class=" | <div class="plant"><img src="https://new.pvzhe.wiki/images/c/ce/%E8%B1%8C%E8%B1%86%E5%B0%84%E6%89%8B.png" alt="豌豆射手"></div> | ||
<div class="score" id="score">0 分</div> | <div class="score" id="shooter-score">0 分</div> | ||
<div class="game-over" id="game-over"> | <div class="game-over" id="shooter-game-over"> | ||
💀 | 💀 僵尸入侵!<br> | ||
<span style="font-size: | <span style="font-size:18px;">得分: <span id="shooter-final-score">0</span></span> | ||
<br><button class="restart-btn" id="restart | <br><button class="restart-btn" id="shooter-restart">再来一局</button> | ||
</div> | </div> | ||
</div> | </div> | ||
| 第92行: | 第98行: | ||
<script> | <script> | ||
(function() { | (function() { | ||
var game = document.getElementById(' | var game = document.getElementById('shooter-game'); | ||
var scoreEl = document.getElementById('shooter-score'); | |||
var scoreEl = document.getElementById('score'); | var gameOverEl = document.getElementById('shooter-game-over'); | ||
var gameOverEl = document.getElementById('game-over'); | var finalScoreEl = document.getElementById('shooter-final-score'); | ||
var finalScoreEl = document.getElementById('final-score'); | var restartBtn = document.getElementById('shooter-restart'); | ||
var restartBtn = document.getElementById('restart | |||
var score = 0; | var score = 0; | ||
var isGameOver = false; | var isGameOver = false; | ||
var zombies = []; | var zombies = []; | ||
var | var bullets = []; | ||
var zombieSpeed = 1.5; | |||
var spawnTimer = null; | var spawnTimer = null; | ||
var | var gameLoop = null; | ||
function spawnZombie() { | function spawnZombie() { | ||
| 第121行: | 第116行: | ||
var zombie = document.createElement('div'); | var zombie = document.createElement('div'); | ||
zombie.className = 'zombie'; | zombie.className = 'zombie'; | ||
zombie. | zombie.innerHTML = '<img src="https://new.pvzhe.wiki/images/f/fa/%E6%99%AE%E9%80%9A%E5%83%B5%E5%B0%B8.png" alt="僵尸">'; | ||
zombie.style.right = '- | zombie.style.right = '-60px'; | ||
zombie.x = game.offsetWidth; | |||
zombie.hp = 3; | |||
zombie.scored = false; | |||
game.appendChild(zombie); | game.appendChild(zombie); | ||
zombies.push( | zombies.push(zombie); | ||
} | } | ||
function | function shoot(x, y) { | ||
if (isGameOver) return; | |||
var bullet = document.createElement('div'); | |||
bullet.className = 'bullet'; | |||
bullet.style.left = '70px'; | |||
bullet.style.top = (y - 6) + 'px'; | |||
bullet.destX = x; | |||
bullet.destY = y; | |||
game.appendChild(bullet); | |||
bullets.push(bullet); | |||
} | |||
function update() { | |||
if (isGameOver) return; | if (isGameOver) return; | ||
for (var i = | |||
var | // 移动子弹 | ||
for (var i = bullets.length - 1; i >= 0; i--) { | |||
var b = bullets[i]; | |||
var left = parseFloat(b.style.left) || 70; | |||
left += 8; | |||
b.style.left = left + 'px'; | |||
// 碰撞检测 | // 碰撞检测 | ||
var | var hit = false; | ||
var | for (var j = zombies.length - 1; j >= 0; j--) { | ||
var z = zombies[j]; | |||
var zLeft = z.x - 55; | |||
var zRight = z.x; | |||
var zTop = game.offsetHeight - 85; | |||
var zBottom = game.offsetHeight - 15; | |||
if (left > zLeft && left < zRight) { | |||
z.hp--; | |||
hit = true; | |||
if (z.hp <= 0) { | |||
if (!z.scored) { score++; z.scored = true; } | |||
z.remove(); | |||
zombies.splice(j, 1); | |||
scoreEl.textContent = score + ' 分'; | |||
if (score % 10 === 0) zombieSpeed += 0.3; | |||
} | |||
break; | |||
} | |||
} | |||
if (hit || left > game.offsetWidth + 20) { | |||
b.remove(); | |||
bullets.splice(i, 1); | |||
} | |||
} | |||
if ( | // 移动僵尸 | ||
for (var k = zombies.length - 1; k >= 0; k--) { | |||
var zz = zombies[k]; | |||
zz.x -= zombieSpeed; | |||
zz.style.right = (game.offsetWidth - zz.x) + 'px'; | |||
if (zz.x < 60) { | |||
gameOver(); | gameOver(); | ||
return; | return; | ||
} | } | ||
} | } | ||
| 第164行: | 第189行: | ||
isGameOver = true; | isGameOver = true; | ||
clearInterval(spawnTimer); | clearInterval(spawnTimer); | ||
clearInterval( | clearInterval(gameLoop); | ||
finalScoreEl.textContent = score; | finalScoreEl.textContent = score; | ||
gameOverEl.style.display = 'block'; | gameOverEl.style.display = 'block'; | ||
| 第172行: | 第197行: | ||
isGameOver = false; | isGameOver = false; | ||
score = 0; | score = 0; | ||
zombieSpeed = 1.5; | |||
scoreEl.textContent = '0 分'; | scoreEl.textContent = '0 分'; | ||
gameOverEl.style.display = 'none'; | gameOverEl.style.display = 'none'; | ||
zombies.forEach(function(z) { z. | zombies.forEach(function(z) { z.remove(); }); | ||
bullets.forEach(function(b) { b.remove(); }); | |||
zombies = []; | zombies = []; | ||
spawnTimer = setInterval(spawnZombie, | bullets = []; | ||
spawnTimer = setInterval(spawnZombie, 1800); | |||
gameLoop = setInterval(update, 20); | |||
} | } | ||
game.addEventListener('click', | game.addEventListener('click', function(e) { | ||
game.addEventListener('touchstart', function(e) { e.preventDefault(); | var rect = game.getBoundingClientRect(); | ||
var x = e.clientX - rect.left; | |||
var y = e.clientY - rect.top; | |||
shoot(x, y); | |||
}); | |||
game.addEventListener('touchstart', function(e) { | |||
e.preventDefault(); | |||
var rect = game.getBoundingClientRect(); | |||
var touch = e.touches[0]; | |||
var x = touch.clientX - rect.left; | |||
var y = touch.clientY - rect.top; | |||
shoot(x, y); | |||
}); | |||
restartBtn.addEventListener('click', function(e) { e.stopPropagation(); restart(); }); | restartBtn.addEventListener('click', function(e) { e.stopPropagation(); restart(); }); | ||