UNPKG

zebrajump-congerss6

Version:

This project implements a simple JavaScript game called "Zebra Jump".

203 lines (176 loc) 5.43 kB
const canvas = document.getElementById('canvas'); const ctx = canvas.getContext('2d'); const zebraWidth = 50; const zebraHeight = 50; const jumpHeight = 100; let zebraX = 50; let zebraY = canvas.height - zebraHeight; function drawZebra() { ctx.clearRect(0, 0, canvas.width, canvas.height); ctx.fillStyle = 'black'; ctx.fillRect(zebraX, zebraY, zebraWidth, zebraHeight); } function jump() { zebraY -= jumpHeight; setTimeout(() => { zebraY += jumpHeight; }, 500); } document.addEventListener('keydown', function(event) { if (event.code === 'Space') { jump(); } }); function gameLoop() { drawZebra(); requestAnimationFrame(gameLoop); } gameLoop(); const zebra = document.createElement('div'); zebra.style.width = '50px'; zebra.style.height = '50px'; zebra.style.backgroundColor = 'black'; zebra.style.position = 'absolute'; zebra.style.bottom = '0'; zebra.style.left = '50px'; document.body.appendChild(zebra); function jump() { let originalBottom = parseInt(zebra.style.bottom); let jumpHeight = 100; let jumpInterval = setInterval(() => { zebra.style.bottom = (parseInt(zebra.style.bottom) + jumpHeight) + 'px'; setTimeout(() => { zebra.style.bottom = originalBottom + 'px'; clearInterval(jumpInterval); }, 500); }, 20); } document.addEventListener('keydown', function(event) { if (event.code === 'Space') { jump(); } }); const zebraImg = new Image(); zebraImg.src = 'zebra.png'; zebraImg.style.width = '50px'; zebraImg.style.height = '50px'; zebraImg.style.position = 'absolute'; zebraImg.style.bottom = '0'; zebraImg.style.left = '50px'; document.body.appendChild(zebraImg); function jump() { let originalBottom = parseInt(zebraImg.style.bottom); let jumpHeight = 100; let jumpInterval = setInterval(() => { zebraImg.style.bottom = (parseInt(zebraImg.style.bottom) + jumpHeight) + 'px'; setTimeout(() => { zebraImg.style.bottom = originalBottom + 'px'; clearInterval(jumpInterval); }, 500); }, 20); } document.addEventListener('keydown', function(event) { if (event.code === 'Space') { jump(); } }); const canvas = document.getElementById('canvas'); const ctx = canvas.getContext('2d'); const zebraWidth = 50; const zebraHeight = 50; const jumpHeight = 100; let zebraX = 50; let zebraY = canvas.height - zebraHeight; function drawZebra() { ctx.clearRect(0, 0, canvas.width, canvas.height); ctx.fillStyle = 'black'; ctx.fillRect(zebraX, zebraY, zebraWidth, zebraHeight); } function jump() { zebraY -= jumpHeight; setTimeout(() => { zebraY += jumpHeight; }, 500); } document.addEventListener('keydown', function(event) { if (event.code === 'Space') { jump(); } }); function gameLoop() { drawZebra(); requestAnimationFrame(gameLoop); } gameLoop(); const zebra = document.createElement('div'); zebra.style.width = '50px'; zebra.style.height = '50px'; zebra.style.backgroundColor = 'black'; zebra.style.position = 'absolute'; zebra.style.bottom = '0'; zebra.style.left = '50px'; document.body.appendChild(zebra); function jump() { let originalBottom = parseInt(zebra.style.bottom); let jumpHeight = 100; let jumpInterval = setInterval(() => { zebra.style.bottom = (parseInt(zebra.style.bottom) + jumpHeight) + 'px'; setTimeout(() => { zebra.style.bottom = originalBottom + 'px'; clearInterval(jumpInterval); }, 500); }, 20); } document.addEventListener('keydown', function(event) { if (event.code === 'Space') { jump(); } }); const zebraImg = new Image(); zebraImg.src = 'zebra.png'; zebraImg.style.width = '50px'; zebraImg.style.height = '50px'; zebraImg.style.position = 'absolute'; zebraImg.style.bottom = '0'; zebraImg.style.left = '50px'; document.body.appendChild(zebraImg); function jump() { let originalBottom = parseInt(zebraImg.style.bottom); let jumpHeight = 100; let jumpInterval = setInterval(() => { zebraImg.style.bottom = (parseInt(zebraImg.style.bottom) + jumpHeight) + 'px'; setTimeout(() => { zebraImg.style.bottom = originalBottom + 'px'; clearInterval(jumpInterval); }, 500); }, 20); } document.addEventListener('keydown', function(event) { if (event.code === 'Space') { jump(); } }); const zebra2 = document.createElement('div'); zebra2.style.width = '50px'; zebra2.style.height = '50px'; zebra2.style.backgroundColor = 'black'; zebra2.style.position = 'absolute'; zebra2.style.bottom = '0'; zebra2.style.left = '50px'; document.body.appendChild(zebra2); function jump() { let originalBottom = parseInt(zebra2.style.bottom); let jumpHeight = 100; let jumpInterval = setInterval(() => { zebra2.style.bottom = (parseInt(zebra2.style.bottom) + jumpHeight) + 'px'; setTimeout(() => { zebra2.style.bottom = originalBottom + 'px'; clearInterval(jumpInterval); }, 500); }, 20); } document.addEventListener('keydown', function(event) { if (event.code === 'Space') { jump(); } });