JavaScript实现球球大作战

35 篇文章 0 订阅
3 篇文章 0 订阅

题目:用js实现一个球球游戏,也就是,当球遇到障碍物的时候,障碍物会消失,并且会相应的加一分,要是没有接到球就会结束游戏

在这里插入图片描述

案例分析:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>球球游戏</title>
    <style>
        body {
            margin: 0px;
            width: 1920px;
            height: 800px;
        }

        #ball {
            width: 100px;
            height: 100px;
            background-color: pink;
            border-radius: 50%;
            position: absolute;
            margin-top: 30px;
        }

        #board {
            width: 200px;
            height: 20px;
            background-color: #371cff;
            position: absolute;
        }

        #score {
            width: 150px;
            height: 20px;
            margin-top: 0px;
            background-color: skyblue;
            margin: 0 auto;
        }
    </style>
</head>
<body>
<div id="ball"></div>
<div id="board"></div>
<div id="score">得分:</div>

<script>
    let directH = "right"; // 水平方向
    let directV = "bottom"; // 垂直方向
    let speed = 2;
    let boardSpeed = 20;
    var sum = 0;

    window.onload = function () {
        creatTree()
        // 改变下面板子的位置
        var board = document.querySelector("#board");
        var w = document.documentElement.clientWidth || document.body.clientWidth;
        var h = document.documentElement.clientHeight || document.body.clientHeight;
        board.style.top = (h - 20) + "px";
        board.style.left = "0px";
        document.onkeydown = function () {
            let which = event.keyCode || event.which;
            let left = board.style.left == "" ? 0 : parseInt(board.style.left);
            if (which == 37) {
                if (left - boardSpeed > 0)
                    board.style.left = (left - boardSpeed) + "px";
            } else if (which == 39) {
                if (left + boardSpeed + 200 < w)
                    board.style.left = (left + boardSpeed) + "px";
            }
        }
        intervalId = setInterval(move, 10);

    }

    //障碍物创建
    //这里也可以用随机数来产生障碍物
    function creatTree() {
        for (let i = 0; i < 10; i++) {
            var treeDiv = document.createElement("div");
            treeDiv.className = "tree"
            treeDiv.style.position = "absolute"
            treeDiv.style.backgroundColor = "red"
            treeDiv.style.width = "50px";
            treeDiv.style.height = "50px";
            let left = 50;
            let right = i * 200;
            treeDiv.style.marginTop = left + "px";
            treeDiv.style.marginLeft = right + "px";
            document.body.appendChild(treeDiv)
        }
    }


    //球球移动方法
    function move() {
        let left = ball.style.left == "" ? 0 : parseInt(ball.style.left);
        let top = ball.style.top == "" ? 0 : parseInt(ball.style.top);
        if (directH == "left") {
            left -= speed;
        } else {
            left += speed;
        }

        if (directV == "top") {
            top -= speed;
        } else {
            top += speed;
        }
        ball.style.left = left + "px";
        ball.style.top = top + "px";

        var w = document.documentElement.clientWidth || document.body.clientWidth;
        var h = document.documentElement.clientHeight || document.body.clientHeight;
        if (directH == "left" && left <= 0) {
            directH = "right";
        }
        if (directH == "right" && left + 100 >= w) {
            directH = "left";
        }

        if (directV == "top" && top <= 0) {
            directV = "bottom";
        }
        //反弹 改变方向
        var board = document.querySelector("#board");
        let boardLeft = board.style.left == "" ? 0 : parseInt(board.style.left);
        if (left + 50 >= boardLeft && left + 50 <= boardLeft + 200) {
            if (directV == "bottom" && top + 100 >= h - 40) {
                directV = "top";
            }
        } else if (Math.pow((boardLeft - left - 50), 2) + Math.pow((h - 20 - top - 50), 2) <= 2500) { // 碰到左边的点
            directH = "left";
            directV = "top";
        } else if (Math.pow((boardLeft + 200 - left - 50), 2) + Math.pow((h - 20 - top - 50), 2) <= 2500) { // 碰到左边的点
            directH = "right";
            directV = "top";
        } else if (directV == "bottom" && top + 100 >= h) {
            alert("game over!!!");
            clearInterval(intervalId);
        }
        //碰到障碍物的操作
        var trees = document.querySelectorAll(".tree");
        var score = document.querySelector("#score");

        for (let i = 0; i < trees.length; i++) {
            let treeTop = parseInt(trees[i].style.marginTop) + 25;
            let treeLeft = parseInt(trees[i].style.marginLeft) + 25;
            let ballTop = parseInt(ball.style.top) + 50;
            let ballLeft = parseInt(ball.style.left) + 50;
            //每一个矩形和园的最远距离
            let distanceMax = Math.sqrt(800) + 50;
            //实际矩形和圆心的距离
            let distanceZhen = Math.sqrt(Math.pow((ballTop - treeTop), 2) + Math.pow((ballLeft - treeLeft), 2));
            if (distanceZhen <= distanceMax) {//意味着挨着了  就消失
                if ((ballLeft < treeLeft) || ballLeft > treeLeft) {
                    if (directH == "right") {
                        directH = "left";
                    } else if (directH == "left") {
                        directH = "right";
                    }
                    sum++;
                    console.log(sum)
                    score.innerHTML = "得分:" + sum;
                    trees[i].remove();
                } else if ((ballTop > treeTop) || (ballTop < treeTop)) {
                    if (directV == "top") {
                        directV = "button";
                    } else if (directV == "button") {
                        directV = "top";
                    }
                    sum++;
                    console.log(sum)
                    score.innerHTML = "得分:" + sum;
                    trees[i].remove();
                }
            }
        }
    }

</script>
</body>

</html>

注意:这里最需要注意的就是球和障碍物相撞的判断,我利用的是球心和障碍物中心两者之间的距离来判断的,最远的相撞点肯定就是障碍物角和球相撞的时候,所以只要两者中心之间的距离小于等于这个最大距离就代表碰到了,就会消失

关于JavaScript的详细介绍,可以点击这里JavaScript详细介绍、了解、使用查看我的另外一篇具体js的详解

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值