当前位置: 首页 > news >正文

用html5写一个打巴掌大赛

我来帮你创建一个打巴掌大赛的HTML5游戏。这个游戏使用Canvas绘制,支持点击互动

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>打巴掌大赛</title>
    <style>
        body {
            margin: 0;
            padding: 20px;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            font-family: Arial, sans-serif;
        }
        canvas {
            border: 3px solid #fff;
            border-radius: 15px;
            cursor: pointer;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
            background: #f0f0f0;
        }
        .score {
            position: absolute;
            top: 30px;
            left: 50%;
            transform: translateX(-50%);
            color: white;
            font-size: 24px;
            font-weight: bold;
            text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
        }
    </style>
</head>
<body>
    <div class="score">打巴掌大赛</div>
    <canvas id="gameCanvas" width="800" height="400"></canvas>

    <script>
        const canvas = document.getElementById('gameCanvas');
        const ctx = canvas.getContext('2d');
        
        let leftScore = 0;
        let rightScore = 0;
        let isAnimating = false;
        
        // 左边巴掌
        const leftHand = {
            x: 150,
            y: 200,
            angle: 0,
            targetAngle: 0,
            isSlapping: false
        };
        
        // 右边巴掌
        const rightHand = {
            x: 650,
            y: 200,
            angle: 0,
            targetAngle: 0,
            isSlapping: false
        };
        
        function drawFace(x, y, size, isLeft) {
            // 画脸
            ctx.fillStyle = '#fdbcb4';
            ctx.beginPath();
            ctx.arc(x, y, size, 0, Math.PI * 2);
            ctx.fill();
            
            // 脸轮廓
            ctx.strokeStyle = '#f4a28c';
            ctx.lineWidth = 3;
            ctx.stroke();
            
            // 眼睛
            const eyeX = isLeft ? x - 20 : x + 20;
            ctx.fillStyle = '#000';
            ctx.beginPath();
            ctx.arc(eyeX, y - 15, 8, 0, Math.PI * 2);
            ctx.fill();
            
            // 眼睛高光
            ctx.fillStyle = '#fff';
            ctx.beginPath();
            ctx.arc(eyeX + 2, y - 17, 3, 0, Math.PI * 2);
            ctx.fill();
            
            // 鼻子
            ctx.fillStyle = '#f4a28c';
            ctx.beginPath();
            ctx.arc(x, y + 5, 4, 0, Math.PI * 2);
            ctx.fill();
            
            // 嘴巴
            ctx.strokeStyle = '#333';
            ctx.lineWidth = 2;
            ctx.beginPath();
            if (isLeft && leftHand.isSlapping) {
                // 被打时的表情
                ctx.arc(x, y + 25, 15, 0, Math.PI, false);
            } else if (!isLeft && rightHand.isSlapping) {
                ctx.arc(x, y + 25, 15, 0, Math.PI, false);
            } else {
                ctx.arc(x, y + 25, 15, 0, Math.PI, true);
            }
            ctx.stroke();
            
            // 脸颊(被打时变红)
            if ((isLeft && leftHand.isSlapping) || (!isLeft && rightHand.isSlapping)) {
                ctx.fillStyle = 'rgba(255, 0, 0, 0.3)';
                ctx.beginPath();
                ctx.arc(isLeft ? x + 35 : x - 35, y + 10, 20, 0, Math.PI * 2);
                ctx.fill();
            }
        }
        
        function drawHand(hand) {
            ctx.save();
            ctx.translate(hand.x, hand.y);
            ctx.rotate(hand.angle);
            
            // 手
            ctx.fillStyle = '#fdbcb4';
            ctx.beginPath();
            ctx.ellipse(0, 0, 25, 35, 0, 0, Math.PI * 2);
            ctx.fill();
            
            // 手指
            ctx.beginPath();
            ctx.moveTo(-15, -20);
            ctx.lineTo(-20, -35);
            ctx.lineTo(-15, -35);
            ctx.lineTo(-10, -20);
            ctx.closePath();
            ctx.fill();
            
            ctx.beginPath();
            ctx.moveTo(0, -25);
            ctx.lineTo(-5, -40);
            ctx.lineTo(0, -40);
            ctx.lineTo(5, -25);
            ctx.closePath();
            ctx.fill();
            
            ctx.beginPath();
            ctx.moveTo(15, -20);
            ctx.lineTo(10, -35);
            ctx.lineTo(15, -35);
            ctx.lineTo(20, -20);
            ctx.closePath();
            ctx.fill();
            
            // 手腕
            ctx.strokeStyle = '#fdbcb4';
            ctx.lineWidth = 10;
            ctx.beginPath();
            ctx.moveTo(0, 25);
            ctx.lineTo(0, 50);
            ctx.stroke();
            
            ctx.restore();
        }
        
        function draw() {
            ctx.clearRect(0, 0, canvas.width, canvas.height);
            
            // 画脸
            drawFace(200, 150, 60, true);   // 左脸
            drawFace(600, 150, 60, false);  // 右脸
            
            // 画手
            drawHand(leftHand);
            drawHand(rightHand);
            
            // 画分数
            ctx.fillStyle = '#333';
            ctx.font = 'bold 24px Arial';
            ctx.fillText(`分数: ${leftScore}`, 50, 50);
            ctx.fillText(`分数: ${rightScore}`, 600, 50);
            
            // 动画
            if (leftHand.isSlapping && leftHand.angle < leftHand.targetAngle) {
                leftHand.angle += 0.1;
                if (leftHand.angle >= Math.PI / 3) {
                    rightScore++;
                    leftHand.isSlapping = false;
                    leftHand.targetAngle = 0;
                }
            } else if (!leftHand.isSlapping && leftHand.angle > 0) {
                leftHand.angle -= 0.1;
                if (leftHand.angle < 0) {
                    leftHand.angle = 0;
                }
            }
            
            if (rightHand.isSlapping && rightHand.angle > rightHand.targetAngle) {
                rightHand.angle -= 0.1;
                if (rightHand.angle <= -Math.PI / 3) {
                    leftScore++;
                    rightHand.isSlapping = false;
                    rightHand.targetAngle = 0;
                }
            } else if (!rightHand.isSlapping && rightHand.angle < 0) {
                rightHand.angle += 0.1;
                if (rightHand.angle > 0) {
                    rightHand.angle = 0;
                }
            }
            
            requestAnimationFrame(draw);
        }
        
        // 点击事件
        canvas.addEventListener('click', (e) => {
            if (isAnimating) return;
            
            const rect = canvas.getBoundingClientRect();
            const x = e.clientX - rect.left;
            const y = e.clientY - rect.top;
            
            // 点击左手
            if (x > 100 && x < 200 && y > 150 && y < 250) {
                if (!leftHand.isSlapping) {
                    leftHand.isSlapping = true;
                    leftHand.targetAngle = Math.PI / 3;
                    isAnimating = true;
                    setTimeout(() => {
                        isAnimating = false;
                    }, 500);
                }
            }
            
            // 点击右手
            if (x > 600 && x < 700 && y > 150 && y < 250) {
                if (!rightHand.isSlapping) {
                    rightHand.isSlapping = true;
                    rightHand.targetAngle = -Math.PI / 3;
                    isAnimating = true;
                    setTimeout(() => {
                        isAnimating = false;
                    }, 500);
                }
            }
        });
        
        // 开始游戏
        draw();
    </script>
</body>
</html>

以上是打巴掌大赛HTML5游戏的实现代码,这份代码实现了:

  1. 游戏界面设计

    • 使用Canvas绘制整个游戏场景

    • 紫色渐变背景和白色边框营造游戏氛围

    • 包含分数显示区域

  2. 角色绘制

    • 使用drawFace()函数绘制卡通人脸

    • 包含脸部、眼睛、鼻子、嘴巴等细节

    • 被打时脸颊会变红,嘴巴会变形

  3. 交互功能

    • 点击左右巴掌触发攻击动画

    • 使用requestAnimationFrame实现流畅动画

    • 攻击时巴掌会旋转,模拟挥掌动作

  4. 游戏机制

    • 左右巴掌分别对应不同玩家

    • 成功击中对方得分

    • 防止连续点击的动画锁定机制

  5. 视觉效果

    • 被打时角色脸颊泛红

    • 嘴巴呈现痛苦表情

    • 平滑的旋转动画效果

使用方法:将代码保存为HTML文件,用浏览器打开即可游玩。

http://www.dtcms.com/a/585517.html

相关文章:

  • 基于python大数据的高考志愿推荐系统
  • Web APIs 学习第五天:日期对象与DOM节点
  • windows 根据端口号关闭进程脚本
  • 推荐电商网站建设微信小程序商城制作一个需要多少钱
  • 【Web3】web3概念术语
  • 自己做的网站403企业咨询合同
  • 深海智脑:全球首个深海生境智能多模态大模型的技术突破与产业展望
  • 流程图绘制进阶:复杂分支与循环结构的优化方案
  • 浙江网站建设推广公司哪家好网站有收录但是没排名
  • 某个网址的爬虫——mitmproxy的简单使用
  • 【Spring/SpringBoot】SSM(Spring+Spring MVC+Mybatis)方案、各部分职责、与Springboot关系
  • Java 多线程机制专项(二)
  • 服务器后台继续任务
  • 拼图小游戏
  • DNS正反向解析转发服务器主从服务
  • 免费咨询问题的网站腾讯建设网站视频视频下载
  • GME 和MGRE综合实验
  • Linux下,获取子进程退出值和异常终止信号
  • 计算机网络自顶向下方法38——网络层 泛化转发与SDN
  • 243-基于Django与VUE的笔记本电脑数据可视化分析系统
  • 婚礼策划网站设计wordpress 图像大小
  • 哈尔滨网站建设1元钱wordpress rpc利用 扫描
  • Redis 缓存怎么更新?—— 四种模型与一次“迟到的删除”
  • 网站制作二维码亮点云建站
  • VS及QT开发环境搭建(保姆级)
  • 【申论】概括归纳类题解题思路
  • 前端正则表达式实战合集:表单验证与字符串处理高频场景
  • 嵌入式Linux驱动开发全流程:工具协作+核心概念拆解(从入门到理解)
  • 建设通是正规网站吗洛阳市霞光游乐设备有限公司
  • 鸿蒙(HarmonyOS)开发常见错误分析与解决方案