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

推荐订阅源

N
News and Events Feed by Topic
S
SegmentFault 最新的问题
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Last Week in AI
Last Week in AI
Jina AI
Jina AI
H
Help Net Security
C
Check Point Blog
aimingoo的专栏
aimingoo的专栏
MyScale Blog
MyScale Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Vercel News
Vercel News
L
LangChain Blog
Recorded Future
Recorded Future
F
Full Disclosure
Google DeepMind News
Google DeepMind News
Microsoft Security Blog
Microsoft Security Blog
I
InfoQ
GbyAI
GbyAI
B
Blog RSS Feed
T
The Blog of Author Tim Ferriss
Engineering at Meta
Engineering at Meta
A
About on SuperTechFans
M
MIT News - Artificial intelligence
爱范儿
爱范儿
V
V2EX
Microsoft Azure Blog
Microsoft Azure Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Y
Y Combinator Blog
B
Blog
WordPress大学
WordPress大学
Blog — PlanetScale
Blog — PlanetScale
W
WeLiveSecurity
MongoDB | Blog
MongoDB | Blog
Cloudbric
Cloudbric
N
News and Events Feed by Topic
The Cloudflare Blog
月光博客
月光博客
博客园 - 三生石上(FineUI控件)
有赞技术团队
有赞技术团队
D
DataBreaches.Net
博客园 - 【当耐特】
T
Troy Hunt's Blog
V
Visual Studio Blog
V2EX - 技术
V2EX - 技术
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 司徒正美
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google Online Security Blog
Google Online Security Blog
The GitHub Blog
The GitHub Blog

其他分享 – 魔帆博客

推荐:turnoff.us 漫画汉化 | 魔帆博客 【开源】自己写的机器人插件源码部分开源 | 魔帆博客 MAC OS X 系统镜像各版本下载 | 魔帆博客 万万没想到 西游篇 下载(2015) | 魔帆博客 一些linux发行版分享 | 魔帆博客 催泪电影《忠犬八公的故事》 | 魔帆博客 温暖人心电影《背影》 | 魔帆博客 Sublime Text 3 激活码分享 | 魔帆博客 魔帆云音乐源码-高仿天天动听的pjax在线音乐电台系统源码 | 魔帆博客
【源码】打猎游戏 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>