首页 > 其他分享 >HTML实现打气球游戏

HTML实现打气球游戏

时间:2024-10-11 18:22:59浏览次数:9  
标签:const 游戏 color balloon State HTML context position 气球

<iframe allowfullscreen="true" data-mediaembed="csdn" frameborder="0" id="ZtyjkrDB-1728197584451" src="https://live.csdn.net/v/embed/428104"></iframe>

打气球


<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>打气球</title>
    <style>
        html,
        body {
            background-color: rgb(20, 20, 20);
            height: 100vh;
            margin: 0px;
            overflow: hidden;
            padding: 0px;
            width: 100vw
        }

        #canvas {
            cursor: none;
            height: 100vh;
            width: 100vw
        }

        a,
        h1 {
            font-family: 'Rubik', sans-serif;
            font-weight: 400;
            margin: 0px
        }

        a {
            backdrop-filter: blur(3px);
            background-color: rgba(255, 255, 255, 0.1)
        }

        a:hover,
        a:focus,
        #youtube-link:hover,
        #youtube-link:focus {
            background-color: rgba(255, 255, 255, 0.25)
        }

        #youtube-link {
            bottom: 0px;
            left: 145px;
            margin: 20px;
            transform: none;
            top: auto
        }

        #inspiration-link {
            border-radius: 4px;
            bottom: 0px;
            color: white;
            display: inline-block;
            height: 20px;
            line-height: 20px;
            left: 0px;
            margin: 20px 15px;
            padding: 10px 15px;
            pointer-events: all;
            position: absolute;
            text-align: center;
            text-decoration: none;
            z-index: 2
        }
    </style>
</head>

<body>
    <canvas id="canvas"></canvas>
</body>
<script>
    const Color = { White: "255, 255, 255", Steel: "96, 125, 139", Red: "239, 83, 80", Orange: "255, 160, 0", Yellow: "253, 216, 53", Green: "42, 252, 152", Blue: "41, 121, 255", Indigo: "57, 73, 171", Violet: "103, 58, 183" }
    const State = {
        balloons: [],
        colors: [Color.Red, Color.Orange, Color.Yellow, Color.Green, Color.Blue, Color.Indigo, Color.Violet],
        context: null,
        index: 0,
        params: {
            balloon: { frequency: 600, size: 4 },
            duration: { slow: 500, medium: 250, fast: 100 },
            reticle: { accent: Color.White, color: Color.Steel, radius: 40, thickness: 2 }
        },
        position: { x: 0, y: 0 },
        splatter: { color: Color.White, lines: [] },
        timestamps: { clickAt: null, popAt: null }
    }
    const N = {
        clamp: (min, value, max) => Math.min(Math.max(min, value), max), rand: (min, max) => Math.floor(Math.random() * (max - min + 1) + min)
    }
    const Mapper = {
        angle: (start, end) => ({
            start: start || 0, end: end || (2 * Math.PI)
        }),
        balloon: color => {
            const { balloon } = State.params, { canvas } = State.context; const height = balloon.size * 100, width = height * 0.8; return { color, createdAt: new Date().getTime(), duration: N.rand(3000, 7000), id: Math.random(), poppedAt: null, position: Mapper.position(N.rand(width / 2, canvas.width - (width / 2)), canvas.height + height) }
        },
        circle: (position, color, fill, radius, thickness, angle) => ({
            position, color, fill, radius, thickness, angle: angle || Mapper.angle()
        }),
        line: (x1, y1, x2, y2, color, thickness) => ({
            start: Mapper.position(x1, y1), end: Mapper.position(x2, y2), color, thickness
        }),
        position: (x, y) => ({ x, y }),
        rgb: value => value.substring(0, 3) === "rgb" ? value : `rgb(${value})`,
        rgba: (value, alpha) => `rgba(${value},${alpha})`
    }
    const Canvas = {
        draw: () => {
            const { context } = State; const height = context.canvas.clientHeight, width = context.canvas.clientWidth; context.canvas.height = height; context.canvas.width = width; const size = 4, factor = width / 2000; State.params.balloon.size = N.clamp(2, size * factor, 4); State.context.clearRect(0, 0, width, height)
        },
        circle: params => {
            const { context } = State;
            const { angle, position } = params;
            context.beginPath();
            context.arc(position.x, position.y, params.radius, angle.start, angle.end);
            if (params.color && params.thickness) {
                context.lineWidth = params.thickness;
                context.strokeStyle = Mapper.rgb(params.color);
                context.stroke()
            }
            if (params.fill) {
                context.fillStyle = Mapper.rgb(params.fill); context.fill()
            }
        },
        in: path => {
            const { context, position } = State;
            return context.isPointInPath(path, position.x, position.y)
        },
        line: params => {
            const { context } = State;
            const { start, end, color, thickness } = params;
            context.beginPath();
            context.moveTo(start.x, start.y);
            context.lineTo(end.x, end.y);
            context.lineWidth = thickness;
            context.strokeStyle = Mapper.rgb(color);
            context.lineCap = "round"; context.stroke()
        },
        lines: lines => lines.forEach(line => Canvas.line(line))
    }
    const Animate = {
        on: (initial, final, speed, timestamp) => {
            const { duration } = State.params;
            return Animate.value(initial,
                final,
                duration[speed] || duration.medium,
                State.timestamps[timestamp])
        },
        onclick: (initial, final, speed) => Animate.on(initial, final, speed, "clickAt"),
        onpop: (initial, final, speed) => Animate.on(initial, final, speed, "popAt"),
        percent: (duration, timestamp) => {
            if (timestamp === null) return 1;
            const now = new Date().getTime(),
                diff = now - timestamp;
            return diff / duration
        }, value: (initial, final, duration, timestamp) => {
            const percent = Animate.percent(duration, timestamp);
            if (percent >= 1 || initial === final)
                return final;
            const diff = final - initial;
            return initial + (diff * percent)
        }
    }
    const Reticle = {
        borders: position => {
            const { timestamps } = State, {
                accent, color, radius, thickness } = State.params.reticle; for (let i = 0.05; i < 2; i += 0.5) { const angle = Mapper.angle(Math.PI * i, Math.PI * (0.4 + i)), params = Mapper.circle(position, color, null, radius, thickness, angle); Canvas.circle(params) } for (let i = 0.1; i < 2; i += 0.5) { const angle = Mapper.angle(Math.PI * i, Math.PI * (0.3 + i)), color = Mapper.rgba(accent, Animate.onclick(0, 1)), r = Animate.onclick(radius * 1.5, radius * 0.75), params = Mapper.circle(position, color, null, r, thickness, angle); Canvas.circle(params) }
        },
        draw: () => {
            Reticle.borders(State.position);
            Reticle.eye(State.position);
            const lines = Reticle.lines();
            Reticle.overlays(lines)
        },
        eye: position => {
            const { accent } = State.params.reticle;
            const color = Mapper.rgba(accent, Animate.onclick(0, 1)),
                radius = Animate.onclick(4, 2);
            Canvas.circle(Mapper.circle(position, null, color, radius))
        },
        line: (x1, y1, x2, y2) => {
            const { color, thickness } = State.params.reticle;
            return Mapper.line(x1, y1, x2, y2, color, thickness)
        },
        lines: () => {
            const { position } = State, { height, width } = State.context.canvas;
            const offset = State.params.reticle.radius * 0.25;
            const lines = [Reticle.line(position.x, 0, position.x, position.y - offset),
            Reticle.line(width, position.y, position.x + offset, position.y),
            Reticle.line(position.x, height, position.x, position.y + offset),
            Reticle.line(0, position.y, position.x - offset, position.y)];
            Canvas.lines(lines);
            return lines
        },
        overlay: (line, start, color) => {
            const { accent } = State.params.reticle;
            return { ...line, start, color: Mapper.rgba(accent, 0.75) }
        }, overlays: lines => {
            const { height, width } = State.context.canvas;
            const l0 = lines[0],
                l1 = lines[1],
                l2 = lines[2],
                l3 = lines[3];
            const overlays = [Reticle.overlay(l0, Mapper.position(l0.end.x, Animate.onclick(0, l0.end.y - 1, "slow"))),
            Reticle.overlay(l1, Mapper.position(Animate.onclick(width, l1.end.x + 1, "slow"), l1.end.y)),
            Reticle.overlay(l2, Mapper.position(l2.end.x, Animate.onclick(height, l2.end.y + 1, "slow"))),
            Reticle.overlay(l3, Mapper.position(Animate.onclick(0, l3.end.x - 1, "slow"), l3.end.y))];
            Canvas.lines(overlays)
        }
    }
    const Splatter = {
        draw: () => {
            const { position, splatter } = State;
            const lines = splatter.lines.map(line => {
                const start = Mapper.position(line.start.x + position.x, line.start.y + position.y),
                    end = Mapper.position(line.end.x + position.x, line.end.y + position.y);
                const color = Mapper.rgba(line.color, Animate.onpop(50, 0) / 100)
                return Mapper.line(Animate.onpop(start.x, end.x, "fast"),
                    Animate.onpop(start.y, end.y, "fast"),
                    end.x, end.y, color, line.thickness)
            });
            Canvas.lines(lines)
        },
        generate: color => {
            const { radius } = State.params.reticle;
            const count = 16;
            let lines = [];
            for (let i = 0; i < count; i++) {
                if (i % 4 !== 0) {
                    const angle = (Math.PI * 2) * (i / 16);
                    const length = N.rand(radius * 0.25, radius),
                        r = radius * 1.5;
                    const cos = Math.cos(angle),
                        sin = Math.sin(angle);
                    lines.push(Mapper.line(cos * r, sin * r, cos * (length + r), sin * (length + r), color, 2))
                }
            } State.splatter = { color, lines }
        }
    }
    const Balloon = {
        body: (size, position) => {
            const { context } = State;
            const rHeight = size.height * 0.5,
                rWidth = size.width * 0.5;
            const path = new Path2D();
            const a = {
                x: position.x, y: position.y - rHeight
            };
            path.moveTo(a.x, a.y);
            const b = {
                x: position.x + rWidth, y: position.y
            };
            const abDiff = {
                x: Math.abs(a.x - b.x), y: Math.abs(a.y - b.y)
            };
            path.bezierCurveTo(b.x - (abDiff.x * 0.1), a.y, b.x + (abDiff.x * 0.07), b.y - (abDiff.y * 0.36), b.x, b.y);
            const c = {
                x: position.x, y: position.y + rHeight
            };
            const bcDiff = {
                x: Math.abs(b.x - c.x), y: Math.abs(b.y - c.y)
            };
            path.bezierCurveTo(b.x - (bcDiff.x * 0.05), b.y + (bcDiff.y * 0.4), c.x + (bcDiff.x * 0.5), c.y, c.x, c.y);
            const d = { x: position.x - rWidth, y: position.y };
            const cdDiff = {
                x: Math.abs(c.x - d.x), y: Math.abs(c.y - d.y)
            };
            path.bezierCurveTo(c.x - (cdDiff.x * 0.5), c.y, d.x + (cdDiff.x * 0.05), d.y + (cdDiff.y * 0.4), d.x, d.y);
            path.bezierCurveTo(d.x - (abDiff.x * 0.07), d.y - (abDiff.y * 0.36), d.x + (abDiff.x * 0.1), a.y, a.x, a.y);
            const e = {
                x: a.x + (abDiff.x * 0.3), y: a.y + (abDiff.y * 0.2)
            };
            path.moveTo(e.x, e.y);
            const f = {
                x: b.x - (abDiff.x * 0.2), y: b.y - (abDiff.y * 0.2)
            };
            const efDiff = {
                x: Math.abs(e.x - f.x), y: Math.abs(e.y - f.y)
            };
            path.quadraticCurveTo(e.x + (efDiff.x * 0.9), e.y + (efDiff.y * 0.25), f.x, f.y);
            const g = {
                x: c.x - (cdDiff.x * 0.2), y: c.y - (cdDiff.y * 0.2)
            };
            path.moveTo(g.x, g.y);
            const h = {
                x: d.x + (cdDiff.x * 0.45), y: d.y + (cdDiff.y * 0.5)
            };
            const ghDiff = {
                x: Math.abs(g.x - h.x), y: Math.abs(g.y - h.y)
            };
            path.quadraticCurveTo(g.x - (ghDiff.x * 0.6), g.y - (ghDiff.y * 0.25), h.x, h.y);
            return path
        },
        check: (balloon, now) => {
            const visible = now - balloon.createdAt <= balloon.duration,
                unpopped = balloon.poppedAt === null || now - balloon.poppedAt <= State.params.duration.fast;
            return visible && unpopped
        }, color: (balloon, opacity) => {
            const alpha = balloon.poppedAt ? Animate.onclick(opacity * 100, 0, "fast") / 100 : opacity; return Mapper.rgba(balloon.color, alpha)
        },
        draw: balloon => {
            const { context, timestamps } = State,
                { height, width } = State.context.canvas;
            const { color, position } = balloon;
            const size = Balloon.size(balloon);
            context.lineWidth = size.thickness;
            context.strokeStyle = Balloon.color(balloon, 1);
            context.fillStyle = Balloon.color(balloon, 0.1);
            context.lineCap = "round";
            const body = Balloon.body(size, position),
                knot = Balloon.knot(size, position),
                tail = Balloon.tail(size, position);
            if (Canvas.in(body) || Canvas.in(knot)) {
                context.fillStyle = Balloon.color(balloon, 0.25);
                const now = new Date().getTime();
                if (now - timestamps.clickAt <= 20) {
                    Balloon.pop(balloon, now)
                }
            } context.stroke(body);
            context.fill(body);
            context.stroke(knot);
            context.fill(knot); context.stroke(tail)
        }, filter: () => {
            const now = new Date().getTime();
            return State.balloons.filter(balloon => Balloon.check(balloon, now))
        }, generate: () => {
            const { colors } = State,
                { frequency } = State.params.balloon;
            const color = State.colors[State.index++ % colors.length];
            State.balloons.push(Mapper.balloon(color));
            setTimeout(() => requestAnimationFrame(Balloon.generate), frequency)
        }, knot: (size, position) => {
            const { context } = State, { height, base } = size;
            const path = new Path2D();
            const a1 = {
                x: position.x, y: position.y + (height * 0.5)
            };
            const a2 = { x: a1.x + (base * 20), y: a1.y + (base * 10) },
                a3 = {
                    x: a1.x - (base * 20),
                    y: a1.y + (base * 10)
                };
            path.moveTo(a1.x, a1.y);
            path.bezierCurveTo(a2.x, a2.y, a3.x, a3.y, a1.x, a1.y);
            return path
        }, pop: (balloon, poppedAt) => {
            Splatter.generate(balloon.color);
            State.timestamps.popAt = poppedAt;
            State.balloons = State.balloons.map(b => {
                if (b.id === balloon.id && b.poppedAt === null) {
                    b.poppedAt = poppedAt
                }
                return b
            })
        },
        release: () => {
            const { height, width } = State.context.canvas, { balloon: params } = State.params;
            State.balloons = Balloon.filter();
            for (let balloon of State.balloons) {
                const y = Animate.value(balloon.position.y, params.size * 100 * -1, balloon.duration, balloon.createdAt);
                const position = { ...balloon.position, y };
                Balloon.draw({ ...balloon, position })
            }
        }, size: balloon => {
            const size = { base: State.params.balloon.size };
            if (balloon && balloon.poppedAt) {
                size.base = size.base * (Animate.onclick(100, 150, "fast") / 100)
            }
            const height = size.base * 100;
            size.height = height;
            size.width = height * 0.8;
            size.thickness = size.base * 1.2
            return size
        },
        tail: (size, position) => {
            const { height, base } = size;
            const path = new Path2D();
            const a = { x: position.x, y: position.y + (height * 0.5) + (base * 8) };
            path.moveTo(a.x, a.y);
            const b = { x: a.x, y: a.y + (base * 30) };
            const abDiff = { x: Math.abs(a.x - b.x), y: Math.abs(a.y - b.y) };
            path.bezierCurveTo(a.x - (base * 6), a.y + (base * 12), b.x + (base * 12), b.y - (base * 10), b.x, b.y);
            return path
        }
    }
    const App = {
        draw: () => {
            Canvas.draw();
            Balloon.release();
            Reticle.draw();
            Splatter.draw();
            window.requestAnimationFrame(App.draw)
        }
    }
    window.onload = () => {
        State.context = document.getElementById("canvas").getContext("2d");
        App.draw();
        Balloon.generate();
        State.position = Mapper.position(State.context.canvas.width / 2, State.context.canvas.height / 2)
    }
    window.onmousemove = e => {
        State.position.x = e.clientX; State.position.y = e.clientY
    }
    window.onmousedown = () => {
        State.timestamps.clickAt = new Date().getTime()
    }
</script>

</html>

预览    |    下载

打气球   下载

感谢各位的阅读,更多文章请前往九天小窝

标签:const,游戏,color,balloon,State,HTML,context,position,气球
From: https://blog.csdn.net/as1278/article/details/142727186

相关文章

  • 获取字符串的在html页面上的宽度并且若文字过长则缩小字体填充
    某个页面有这样一个需求:一个固定宽度的div,若文字过长,则缩小字体填充。看到同事采用的是用php的GD库的imagettfbbox函数来计算文字的宽度。imagettfbbox(float $size,float $angle,string $font_filename,string $string,array $options=[]): array|false 取得使用Tru......
  • 群雄逐鹿,谁主沉浮?三国战纪H5游戏详细图文架设教程
    前言这次给大家带来的是三国题材的游戏三国战纪H5,最近本人也是迷上了三国,那真是男人的浪漫,建议所有真男人都看看三国演义,有点跑题了--,游戏BUG很少,超级耐玩,架设也很简单,对服务器要求不高,快来体验一下吧~本文讲解三国战纪H5游戏架设教程,免费提供游戏链接、后台、源码、教程......
  • html面试题总结
    文章目录1.什么是DOCTYPE,有何作用2.H5有哪些新的元素和新特性3.cookie、sessionStorage和localStorage的区别4.script、scriptasync和scriptdefer的区别5.行内元素有哪些?块级元素有哪些?空(void)元素有哪些?6.页面导入样式时,使用link和@import有什么区别7.title和h1......
  • 2018-8-10-win10-uwp-商业游戏-1.2.1
    titleauthordateCreateTimecategorieswin10uwp商业游戏1.2.1lindexi2018-08-1019:16:50+08002018-2-1317:23:3+0800Win10UWP上一个游戏已经告诉大家如何写多个游戏,现在继续写这个无聊的游戏。希望大家在看这篇文章之前先看win10uwp商业游戏,在这个文章告诉了大家如何创建......
  • Unity游戏通用框架——事件的订阅和发布(观察者模式)
    在游戏开发的基本思想中,逻辑与表现的分离极为重要,相互之间并不关心具体实现,只注册对应的事件,有事件发生时才调用相应的函数事件管理器usingSystem.Collections;usingSystem.Collections.Generic;publicclassevent_manager:Singletom<event_manager>{public......
  • 前端Vue中如何将Div元素内容转换为图片?html2canvas库助你轻松实现!
    前端在Vue3中,如果你需要将一个div元素的内容转换成图片,可以使用一个高效且流行的库——html2canvas。这个库能够帮助你将DOM元素渲染为Canvas,并进一步将Canvas转换为图片,非常适合在Vue项目中使用。原文可查看在线演示地址:张苹果博客一,安装html2canvasnpminst......
  • 最新多平台多功能挂机,小游戏,福袋,短剧掘金助手
    项目介绍:带养标签功能,标签的好差类型能直接影响搜易,感兴趣的可以多研究研究自动挂机做任务,24小时无人值守,软件内置第三方项目,可以提高单机搜易,正版软件包更新软件支持全自动:小游戏类.福袋类型,短剧掘金类。脚本功能:设定任务次数后,执行广告视频任务,开宝箱,单平台实现双重......
  • 菲律宾与马来西亚游戏产业对比:火盾云如何助力游戏外包服务
    随着全球游戏产业的不断扩展,东南亚地区的游戏市场也呈现出蓬勃发展的态势。菲律宾与马来西亚作为该地区的两个重要国家,在游戏产业的发展上各具特色。与此同时,游戏外包服务在这些国家也逐渐成为一种趋势,而火盾云(Huodunyun)作为网络安全和加速领域的领军企业,能够为游戏外包业务提供全......
  • 最新多平台多功能挂机,小游戏,福袋,短剧掘金助手
    项目介绍:带养标签功能,标签的好差类型能直接影响搜易,感兴趣的可以多研究研究自动挂机做任务,24小时无人值守,软件内置第三方项目,可以提高单机搜易,正版软件包更新软件支持全自动:小游戏类.福袋类型,短剧掘金类。脚本功能:设定任务次数后,执行广告视频任务,开宝箱,单平台实现双重......
  • 5、HTML 常用标签 - 第三集
    通过学习,我们的人生被赋予了更多的选择,创造了更多的可能性。文章目录前言1.表单域form1.1标签描述1.2示例代码1.3运行截图2.表单元素input2.1常用“输入框”类型2.2常用“选择框”类型2.3常用“按钮”类型专栏附录前言在HTML中,一个完整的表单通常......