打怪升级js,随机产生一只苍蝇,点击消失

 这个是body部分,只有简单的东西

 

<h4>你的分数</h4>

    <span id="sorce">

        <!-- 放置的是你的分数 -->0

    </span>

    <button>开始</button>

 这个是js部分

<script>

        //设置一个变量表示分数,count表示计数

        var sorces=0,count=0;

        var btn1=document.querySelector('button');

        var kaishi;

        btn1.οnclick=function(){

            //调试代码的时候这个时间间隔可以设置短一点

            kaishi=window.setInterval("show()",1000);

        }

        function show(){

            count++;

            //产生随机坐标X,Y

            var left1= Math.random(0.2,0.8)*500;

            var top1=Math.random(0.2,0.8)*500;

            //为img绑定事件

            var img=document.createElement('img');

            //创建图片

            document.body.appendChild(img);

            img.setAttribute("src","https://static.ntimg.cn/original/images/zhaohang_logo.png");

            img.setAttribute("width",50+"px");

            img.setAttribute("style","position: absolute");

            img.style.left=left1+'px';

            img.style.top=top1+'px';

            img.setAttribute("onclick","remove(this)");

            if(count>10){

                var body=document.querySelector("body");

                alert("游戏结束");

                count=0;

                //刷新页面的方法清除苍蝇,感觉比较low就没用

                // window.location.reload();

                //调用函数把图片全删除,然后停止生成苍蝇

                removeall(body);

            }

        }

        function remove(bird){

            sorces++;

            count--;

            document.body.removeChild(bird);

            console.log("clear");

            document.getElementById('sorce').innerHTML=sorces;

        }

        //清除苍蝇函数

        function removeall(parent){

            while (parent.lastChild!=document.querySelector('button')) {

                parent.removeChild(parent.lastChild);

            }

            //关闭定时器,这样就不会继续产生苍蝇了

            clearInterval(kaishi);

            //将span标签重新设置成0,不然虽然数据是0,但是显示出来的东西还是上次的分数

            document.getElementById('sorce').innerHTML='0';

            sorces=0;

            count=0;

        }

    </script>

 全部代码为下面,可以复制先行调试理解

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Document</title>

</head>

<body>

    <h4>你的分数</h4>

    <span id="sorce">

        <!-- 放置的是你的分数 -->0

    </span>

    <button>开始</button>

    <script>

        //设置一个变量表示分数,count表示计数

        var sorces=0,count=0;

        var btn1=document.querySelector('button');

        var kaishi;

        btn1.οnclick=function(){

            //调试代码的时候这个时间间隔可以设置短一点

            kaishi=window.setInterval("show()",1000);

        }

        function show(){

            count++;

            //产生随机坐标X,Y

            var left1= Math.random(0.2,0.8)*500;

            var top1=Math.random(0.2,0.8)*500;

            //为img绑定事件

            var img=document.createElement('img');

            //创建图片

            document.body.appendChild(img);

            img.setAttribute("src","https://static.ntimg.cn/original/images/zhaohang_logo.png");

            img.setAttribute("width",50+"px");

            img.setAttribute("style","position: absolute");

            img.style.left=left1+'px';

            img.style.top=top1+'px';

            img.setAttribute("onclick","remove(this)");

            if(count>10){

                var body=document.querySelector("body");

                alert("游戏结束");

                count=0;

                //刷新页面的方法清除苍蝇,感觉比较low就没用

                // window.location.reload();

                //调用函数把图片全删除,然后停止生成苍蝇

                removeall(body);

            }

        }

        function remove(bird){

            sorces++;

            count--;

            document.body.removeChild(bird);

            console.log("clear");

            document.getElementById('sorce').innerHTML=sorces;

        }

        //清除苍蝇函数

        function removeall(parent){

            while (parent.lastChild!=document.querySelector('button')) {

                parent.removeChild(parent.lastChild);

            }

            //关闭定时器,这样就不会继续产生苍蝇了

            clearInterval(kaishi);

            //将span标签重新设置成0,不然虽然数据是0,但是显示出来的东西还是上次的分数

            document.getElementById('sorce').innerHTML='0';

            sorces=0;

            count=0;

        }

    </script>

</body>

</html>

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这里是一个简单的打怪升级游戏的示例代码: ``` #include <iostream> #include <string> #include <cstdlib> #include <ctime> using namespace std; int main() { srand(time(NULL)); // 初始化随机数生成器 int player_health = 100; // 玩家初始血量 int player_attack = 10; // 玩家攻击力 int enemy_health = 50; // 敌人初始血量 int enemy_attack = 5; // 敌人攻击力 int experience = 0; // 玩家经验值 int level = 1; // 玩家等级 while (player_health > 0) { // 玩家血量为0游戏结束 cout << "你的血量:" << player_health << endl; cout << "你的攻击力:" << player_attack << endl; cout << "当前经验值:" << experience << endl; cout << "当前等级:" << level << endl; cout << "-----------------------" << endl; cout << "遭遇敌人!" << endl; // 敌人战斗 while (enemy_health > 0) { int enemy_damage = rand() % enemy_attack + 1; cout << "敌人攻击了你,造成了" << enemy_damage << "点伤害。" << endl; player_health -= enemy_damage; if (player_health <= 0) { break; } int player_damage = rand() % player_attack + 1; cout << "你攻击了敌人,造成了" << player_damage << "点伤害。" << endl; enemy_health -= player_damage; if (enemy_health <= 0) { break; } } // 玩家胜利 if (enemy_health <= 0) { cout << "你打败了敌人!" << endl; int gained_experience = rand() % 10 + 1; cout << "你获得了" << gained_experience << "点经验值。" << endl; experience += gained_experience; if (experience >= level * 10) { // 经验值满10升一级 level++; player_attack += 5; // 每升一级攻击力增加5 cout << "恭喜你升到了" << level << "级!" << endl; } enemy_health = 50; // 敌人恢复满血 } // 玩家死亡 if (player_health <= 0) { cout << "你被敌人打败了,游戏结束。" << endl; break; } } return 0; } ``` 这个示例代码实现了一个简单的打怪升级游戏,玩家需要通过战斗来获取经验值,提升等级和攻击力,最终打败敌人。这个示例代码只是一个简单的实现,可以根据具体需求进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值