创意编程:用 CSS 和 HTML js 打造飞机大战游戏实现飞机移动

可通过键盘 wasd来操作 也可以更具鼠标移动 来操作飞机 如果只想要其中一个功能 删掉对应的代码即可 代码处有详细备注

<html>

<head>

    <title>飞机大战</title>

    <script>

        // 飞机动画 弹道路径 敌机躲闪动画//

    </script>

</head>

<body>

    <div class="scene">

        <div>

            <div class="enimy1" style="left:100px"></div>

            <div class="enimy2" style="left:600px"></div>

            <div class="enimy3" style="left:810px"></div>

            <div class="enimy4" style="left:320px"></div>

            <div class="enimy5" style="left:460px"></div>

            <div class="enimy6" style="left:500px"></div>

        </div>

        <div>

            <div class="bullet" style="left:300px;bottom: 100px"></div>

            <div class="bullet" style="left:200px;bottom: 200px"></div>

            <div class="bullet" style="left:250px;bottom: 300px"></div>

            <div class="bullet" style="left:350px;bottom: 400px"></div>

            <div class="bullet" style="left:400px;bottom: 500px"></div>

            <div class="bullet" style="left:600px;bottom: 600px"></div>

            <div class="bullet" style="left:700px;bottom: 700px"></div>

            <div class="bullet" style="left:800px;bottom: 800px"></div>

            <div class="bullet" style="left:900px;bottom: 900px"></div>

            <div class="bullet" style="left:400px;bottom: 750px"></div>

            <div class="bullet" style="left:200px;bottom: 650px"></div>

        </div>

        <div id="plane"></div>

    </div>

    <script>

        // 获取plane元素

        var planeElement = document.getElementById('plane');

        var planeSpeed = 5; // 飞机移动速度

// 监听键盘按下事件

document.addEventListener('keydown', function (event) {

    // 获取当前飞机的位置

    var currentLeft = parseFloat(planeElement.style.left) || 0;

    var currentTop = parseFloat(planeElement.style.top) || 0;

    // 根据按键调整飞机位置

    switch (event.key) {

        case 'w':

            planeElement.style.top = currentTop - planeSpeed + 'px';

            break;

        case 's':

            planeElement.style.top = currentTop + planeSpeed + 'px';

            break;

        case 'a':

            planeElement.style.left = currentLeft - planeSpeed + 'px';

            break;

        case 'd':

            planeElement.style.left = currentLeft + planeSpeed + 'px';

            break;

    }

});

        // 监听鼠标移动事件

        document.addEventListener('mousemove', function (event) {

            // 获取鼠标坐标

            var mouseX = event.clientX;

            var mouseY = event.clientY;

            // 将坐标值设置给plane元素的style的left和top属性

            planeElement.style.left = mouseX - planeElement.offsetWidth / 2 + 'px';

            planeElement.style.top = mouseY - planeElement.offsetHeight / 2 + 'px';

        });

    </script>

</body>

<style>

    /* class selector 类选择器 */

    .scene {

        width: 100%;

        height: 100%;

        background-image: url('C:/科技/XK.jpg'); /* 替换为实际飞机的图片路径 */

        background-size: cover; /* 将背景图等比例放大或缩小,以填满整个元素 */

    background-position: center; /* 将背景图居中 */

    background-repeat: no-repeat; /* 不重复背景图 */

     

    }

    /* tag selector 类选择器 */

    div{

        border-radius: 5px;

    }

    /* id selector id选择器 */

    #plane {

        width: 32px;

        height: 24px;

        background-image: url('C:/科技/FJ2.png'); /* 替换为实际飞机的图片路径 */

    background-size: cover;

        position: absolute;

        bottom: 20px;

        left: 400px;

       

    }

    .bullet {

        width: 2px;

        height: 10px;

        background-image: url('C:/科技/ZD.jpg'); /* 替换为实际飞机的图片路径 */

    background-size: cover;

        position: absolute;

        bottom: 300px;

        left: 400px;

    }

    .enimy1{

        width: 30px;

        height: 40px;

        background-image: url('C:/科技/DJ.png'); /* 替换为实际飞机的图片路径 */

    background-size: cover;

        position: absolute;

        top: 100px;

        left: 300px;

    }

    .enimy2{

        width: 30px;

        height: 40px;

        background-image: url('C:/科技/DJ2.png'); /* 替换为实际飞机的图片路径 */

    background-size: cover;

        position: absolute;

        top: 100px;

        left: 300px;

    }

    .enimy3{

        width: 60px;

        height: 30px;

        background-image: url('C:/科技/DJ3.png'); /* 替换为实际飞机的图片路径 */

    background-size: cover;

        position: absolute;

        top: 100px;

        left: 300px;

    }

    .enimy4{

        width: 40px;

        height: 60px;

        background-image: url('C:/科技/DJ4.png'); /* 替换为实际飞机的图片路径 */

    background-size: cover;

        position: absolute;

        top: 100px;

        left: 300px;

    }

    .enimy5{

        width: 20px;

        height: 60px;

        background-image: url('C:/科技/DJ5.png'); /* 替换为实际飞机的图片路径 */

    background-size: cover;

        position: absolute;

        top: 100px;

        left: 300px;

    }

    .enimy6{

        width: 50px;

        height: 80px;

        background-image: url('C:/科技/DJ6.png'); /* 替换为实际飞机的图片路径 */

    background-size: cover;

        position: absolute;

        top: 100px;

        left: 300px;

    }

</style>


 

</html>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值