惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

K
Kaspersky official blog
Martin Fowler
Martin Fowler
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
V
Visual Studio Blog
博客园_首页
Engineering at Meta
Engineering at Meta
The Cloudflare Blog
MongoDB | Blog
MongoDB | Blog
Blog — PlanetScale
Blog — PlanetScale
T
The Blog of Author Tim Ferriss
雷峰网
雷峰网
D
Docker
博客园 - 司徒正美
S
SegmentFault 最新的问题
M
MIT News - Artificial intelligence
博客园 - 叶小钗
博客园 - 三生石上(FineUI控件)
U
Unit 42
J
Java Code Geeks
A
About on SuperTechFans
N
Netflix TechBlog - Medium
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
Security Affairs
I
Intezer
Cisco Talos Blog
Cisco Talos Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
B
Blog RSS Feed
P
Privacy & Cybersecurity Law Blog
T
Tenable Blog
T
Threatpost
H
Hacker News: Front Page
G
Google Developers Blog
博客园 - 【当耐特】
Hugging Face - Blog
Hugging Face - Blog
Apple Machine Learning Research
Apple Machine Learning Research
L
Lohrmann on Cybersecurity
大猫的无限游戏
大猫的无限游戏
Google DeepMind News
Google DeepMind News
A
Arctic Wolf
S
Secure Thoughts
GbyAI
GbyAI
NISL@THU
NISL@THU
S
Security @ Cisco Blogs
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Webroot Blog
Webroot Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
O
OpenAI News
Spread Privacy
Spread Privacy
Application and Cybersecurity Blog
Application and Cybersecurity Blog

资源分享 – 魔帆博客

解锁iOS侧载自由:使用sideStore轻松搞定ipa签名 | 魔帆博客 安装有 RootMagiskXposedPlay 的 WSA 安卓子系统 | 魔帆博客 双重验证 Authy 导出所有 TOTP token 和 golang 配置 GOPROXY 解决网络问题 | 魔帆博客 安卓电池充电保护-智能充电/温控切断(Root方案) | 魔帆博客 Redshift——Linux 下 f.lux 代替品 | 魔帆博客 作品:媒体音量磁贴 | 魔帆博客 推荐:turnoff.us 漫画汉化 | 魔帆博客 【软件】XML文本解析器 | 魔帆博客 饭饭街优惠卷线报1.1版(快来抢11红包) | 魔帆博客 【软件】Mac OS环境下最强虚拟机软件--Parallels Desktop | 魔帆博客 魔帆幻听 - 天天动听停止服务修复版 | 魔帆博客 魔帆暴走表情制作神器 - 聊天斗图/撩妹必备神器! | 魔帆博客 作品:魔帆资源纵横:搞笑段子/图片/视频收集获取工具(人气必备) | 魔帆博客 魔帆VIP会员获取神器 支持迅雷爱奇艺乐视等 | 魔帆博客 来让文件夹变得扑朔迷离吧:CLSID加密软体 | 魔帆博客 QQ广告屏蔽器 | 魔帆博客 【开源】自己写的机器人插件源码部分开源 | 魔帆博客 QQXML扫描仪 2.0 全面支持修改! | 魔帆博客 腾讯QQ8.1去广告显IP增强版【可选安装】 | 魔帆博客
【源码】打猎游戏 html 纯原生代码 | 魔帆博客
Sonui · 2021-10-05 · via 资源分享 – 魔帆博客

使用 html + JavaScript 制作打猎游戏,纯原生代码

//取范围内随机数
function randomNum(minNum, maxNum) {
    switch (arguments.length) {
        case 1:
            return parseInt(Math.random() * minNum + 1, 10);
        case 2:
            return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
    }
}

class Prey {
    /**
     * 初始化猎物对象并加入到body中
     * name string 猎物名
     * id string 唯一id
     * enclosure object 围栏范围
     * img string 图片路径
     * speed int 速度
     */
    join(name, id, enclosure, img, speed) {
        this.name = name;
        this.enclosure = enclosure;
        this.id = id;
        let pic = '<img title="' + name + '" id="' + id + '" src="' + img + '" onclick="pipipi()"></img>';
        document.getElementById("game").innerHTML = document.getElementById("game").innerHTML + pic;
        let obj = document.getElementById(id);
        obj.style.left = randomNum(this.enclosure.x1, this.enclosure.x2) + 'px';
        obj.style.top = randomNum(this.enclosure.y1, this.enclosure.y2) + 'px';
        this.speed = speed;
        this.setTarget();
    }
    
    //重新设置终点
    setTarget() {
        let obj = document.getElementById(this.id);
        let rect = obj.getBoundingClientRect();
        let w = window.innerWidth ||
            document.documentElement.clientWidth ||
            document.body.clientWidth;
        let h = window.innerHeight ||
            document.documentElement.clientHeight ||
            document.body.clientHeight;
        w = w - obj.clientWidth;
        h = h - obj.clientHeight;
        this.target = {
            x: randomNum(this.enclosure.x1, this.enclosure.x2 > w ? w : this.enclosure.x2),
            y: randomNum(this.enclosure.y1, this.enclosure.y2 > h ? h : this.enclosure.y2)
        };
        let x = Math.abs(this.target.x - rect.left);
        let y = Math.abs(this.target.y - rect.top);
        let z = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)); //直角三角形求斜边

        //xy步进值,带方向
        this.runSpeed = {
            x: Math.floor(Math.ceil(this.speed) / z * x * (this.target.x > rect.left ? 1 : -1)),
            y: Math.floor(Math.ceil(this.speed) / z * y * (this.target.y > rect.top ? 1 : -1))
        };

    }

    //运动
    motion() {
        /*
        运动算法两种思路
        一种设置目标点,定向移动,同时设计围栏,撞上围栏重新生成目标点
        或者直接左右移动,要简单很多
        下面是多方向移动算法
        */
        let toX, toY;
        let obj = document.getElementById(this.id);
        let rect = obj.getBoundingClientRect();
        let w = window.innerWidth ||
            document.documentElement.clientWidth ||
            document.body.clientWidth;
        let h = window.innerHeight ||
            document.documentElement.clientHeight ||
            document.body.clientHeight;
        if (
            rect.x - this.runSpeed.x <= this.enclosure.x1 || rect.x + this.runSpeed.x >= this.enclosure.x2 ||
            rect.y - this.runSpeed.y <= this.enclosure.y1 || rect.y + this.runSpeed.y >= this.enclosure.y2 || //判断是否逃出围栏
            rect.x - this.runSpeed.x <= 0 || rect.x - this.runSpeed.x >= w ||
            rect.y - this.runSpeed.y <= 0 || rect.y - this.runSpeed.y >= h || //判断是否逃出浏览器窗口范围
            Math.abs(this.target.x - rect.x) <= Math.abs(this.runSpeed.x) ||
            Math.abs(this.target.y - rect.y) <= Math.abs(this.runSpeed.y) //到达终点
        ) {
            //即将逃出围栏,重新设定目标点
            this.setTarget();
        }

        //console.log(obj, rect);
        toX = Math.floor(rect.left + this.runSpeed.x);
        toY = Math.floor(rect.top + this.runSpeed.y);
        //console.log(this.id, obj.style.left, toX, toY);
        obj.style.left = toX + "px";
        obj.style.top = toY + "px";
    }

    //死亡
    die() {
        document.getElementById(this.id).remove();
    }
}
//定于全局变量
let animal = new Array();

//body加载完成后加入猎物并创建时钟
window.onload = function () {
    for (let i = 0; i < 10; i++) {
        animal.splice(0, 0, new Prey());
        animal[0].join(
            "bird",
            "bird" + i, {
                x1: 100,
                x2: 1000,
                y1: 200,
                y2: 500
            }, "./1.gif",
            5);
    }
    animal.splice(0, 0, new Prey());
    animal[0].join(
        "pig",
        "pig0", {
            x1: 50,
            x2: 500,
            y1: 50,
            y2: 1000
        }, "./2.jpeg",
        30);
    setInterval("run()", 20);
}

function run() {
    //console.log(animal.length);
    for (let i = 0; i < animal.length; i++) {
        animal[i].motion();
    }
}

function pipipi(from) {
    let e = window.event;
    for (let i = 0; i < animal.length; i++) {
        if (animal[i].id == e.target.id) {
            //死亡
            console.log(i);
            alert("你抓住了" + animal[i].name);
            animal[i].die();
            animal.splice(i, 1);
        }
    }
}
<!DOCTYPE html>
<html lang="zh-cn">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>打猎游戏</title>
    <script src="./game.js"></script>
    <style>
        img{
            position: absolute;
        }
    </style>
</head>

<body>
    <div id="game">
    </div>
</body>
</html>