canvas实现在手机上两人对战的打球小游戏代码
代码语言:html
所属分类:游戏
代码描述:canvas实现在手机上两人对战的打球小游戏代码,设计用于触摸屏幕,在场地上滑动/甩动球,先达到5分获胜,点击游戏区域在赢得比赛后重置,两个人一起玩更有趣。
代码标签: canvas 手机 两人 对战 打球 小游戏 代码
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html> <html lang="en" > <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width,initial-scale=1.0,maximum=1.0,minimum=1.0,user-scalable=0" /> <style> @import url('https://fonts.googleapis.com/css2?family=Silkscreen&display=swap'); html { background: #222; min-height: 100vh; display: grid; place-items: center; overflow: hidden; font-family: system-ui; perspective: 50vw; } body { margin: 0; /* transform: rotateX(10deg) translateY(-5%); */ } * { box-sizing: border-box; user-select: none; } p { position: fixed; left: 0; top:0; color: gray; padding: 1rem; margin: 0; } #gameCanvas { background: url('//repo.bfw.wiki/bfwrepo/image/64c8657304d19.png'), SandyBrown; background-size: cover; box-sizing: border-box; position: relative; } #gamebox { width: fit-content; position: relative; } /* #gamebox:after { content: ''; position: absolute; inset: 0px; border: peru 20px solid; box-shadow: inset 0 0 0 5px rgba(0,0,0,.1); pointer-events: none; } */ #scoreboard { position: absolute; left: 0; top: 0; inset: 0; background: rgba(0,0,0,.4); backdrop-filter: blur(10px); display: grid; place-items: center; opacity: 0; pointer-events: none; } .show_sb { pointer-events: all !important; animation: show_sb 2s linear forwards; } @keyframes show_sb { 25% { opacity: 1; } 75% { opacity: 1; } 100% { opacity: 0; } } .gameOver { pointer-events: all !important; animation: gameOver .5s linear forwards; } @keyframes gameOver { 100% { opacity: 1; } } .pl { border-top: 10px solid #333; border-right: 10px solid #333; border-bottom: 10px solid #222; border-left: 10px solid #222; background: black; min-width: 175px; aspect-ratio: 1/1; font-size: 10rem; line-height: 100%; padding: 0 1rem 1.25rem 1rem; font-family: 'Silkscreen'; color: white; text-align: center; } .scored { background: LimeGreen !important; } .winner { background: gold !important; } </style> </head> <body> <p> 设计用于触摸屏幕<br> 在场地上滑动/甩动球<br> 先达到5分获胜<br> 点击游戏区域在赢得比赛后重置<br> 两个人一起玩更有趣 </p> <!-- --> <div id="gamebox"> <canvas id="gameCanvas"></canvas> <div id="scoreboard"> <div id="pl_one" class='pl'>0</div> <div id="pl_two" class="pl">0</div> </div> </div> <script > const canvas = document.getElementById('gameCanvas'); canvas.setAttribute("height", window.innerHeight * .97); canvas.setAttribute("width", window.innerHeight * .97 * .5625); const ctx = canvas.getContext('2d'); const ballRadius = 20; const friction = 0.96; // Friction coefficient (less than 1 to slow down ball) const maxSwipeLength = 100; let cooldownTime = 1000; let ballXStart = canvas.width * .5; let ballYStart = canvas.height * .75; let ballX = ballXStart; let ballY = ballYStart; let ballSpeedX = 0; let ballSpeedY = 0; let isSwiping = false; let swipeStartX = 0; let swipeStartY = 0; let swipEndX = 0; let swipeEndY = 0; let cooldownTimer = null; let ob_size = 10; let ballRotation = 0; // alert('Designed for mobile devices using touch. Sorry.') const obstacles = [ { x: canvas.width * .5, y: canvas.height * .5, radius: ob_size }, { x: canvas.width * .25, y: canvas.height * .25, radius: ob_size }, { x: canvas.width * .75, y: canvas.height * .25, radius: ob_size }, { x: canvas.width * .25, y: canvas.height * .75, radius: ob_size }, { x: canvas.width * .75, y: canvas.height * .75, radius: ob_size }]; const topGoal = { x: canvas.width * .5, y: 15, radius: 30 }; const bottomGoal = { x: canvas.width * .5, y: canvas.height - 10, radius: 30 }; function updateGameArea() { clearCanvas(); drawGoals(); drawObstacles(); moveBall(); drawBall(); bounceOffWalls(); checkCollisionWithObstacles(); applyFriction(); checkCollisionWithGoals(); } function clearCanvas() { ctx.clearRect(0, 0, canvas.width, canvas.height); } function drawGoals() { ctx.fillStyle = 'transparent'; ctx.beginPath(); ctx.arc(topGoal.x, topGoal.y, topGoal.radius, 0, Math.PI * 2); ctx.arc(bottomGoal.x, bottomGoal.y, bottomGoal.radius, 0, Math.PI * 2); ctx.fill(); } var thumbImg = document.createElement('img'); thumbImg.style.transform = "rotate(45deg)"; thumbImg.src = '//repo.bfw.wiki/bfwrepo/image/5fd7fee24ce6e.png'; function drawBall() { // ctx.beginPath(); // ctx.arc(ballX, ballY, ballRadius, 0, Math.PI * 2); // ctx.fillStyle = 'blue'; // ctx.fill(); // ctx.closePath(); ctx.save(); // ctx.translate(ballX, ballY); // ctx.rotate(ballRotation); ctx.drawImage(thumbImg, ballX - 20, ballY - 20, 40, 40); ctx.restore(); } function drawObstacles() { ctx.fillStyle = 'transparent'; obstacles.forEach(obstacle => { ctx.beginPath(); ctx.shadowColor = "rgba(0,0,0,1)"; ctx.shadowOffsetY = 0; ctx.shadowBlur = 10; ctx.arc(obstacle.x, obstacle.y, obstacle.radius, 0, Math.PI * 2);.........完整代码请登录后点击上方下载按钮下载查看
网友评论0