原生JS --飞机大战

这篇博客介绍了如何使用原生JavaScript编写一款简单的飞机大战游戏。游戏包括地图、飞机、敌人和子弹四个主要部分,玩家可以通过键盘控制飞机移动和射击。代码中定义了Map、Flyer、Diren和Zidan四个类,分别对应游戏的不同元素,并实现了游戏的基本功能,如飞机移动、射击、敌人生成和碰撞检测。
摘要由CSDN通过智能技术生成

闲时 3小时制作飞机大战,可以自己调整参数进行游戏,  整体分为4大类,地图,飞机,敌人,子弹,代码如下

 

<!DOCTYPE html>

<html lang="en">

 

<head>

    <meta charset="UTF-8">

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

    <title>飞机大战</title>

</head>

 

<body>

    总积分:<span id="score">0</span>

    上下左右,J射击K炸弹

</body>

 

</html>

<script>

    class Map {

        // 地图类

        static status = false  // 开始游戏开关

        static height = 400;

        static width = 400;

        static score = 0

        static map = null

        static isBound = null

        static changeScore(bool, num = 1) {

            // 地图分数

            if (bool) {

                Map.score += num

            } else {

                Map.score -= num

            }

            document.getElementById('score').innerText = Map.score

        }

        constructor() {

            Map.status = true

            this._initMap()

            this.getBound()

        }

        _initMap() {

            // 初始化地图

            document.body.style.overflow = 'hidden'

            let div = document.createElement('div')

            div.style = `height:${Map.height}px;width:${Map.width}px;

        border:1px solid;margin:100px auto;`

            this.div = div

            Map.map = this

            document.body.appendChild(div)

        }

        getBound() {

            // 地图边界

            let top = this.div.offsetTop

            let bottom = this.div.offsetTop + this.div.offsetHeight

            let left = this.div.offsetLeft

            let right = this.div.offsetLeft + this.div.offsetWidth

            Map.isBound = { top, bottom, left, right }

            return { top, bottom, left, right }

        }

        pause() {

            // 游戏状态

            Map.status = !Map.status

        }

    }

    class Flyer {

        static height = 10;

        static width = 10;

        static speed = 3;

        static imgUrl = ''

        static boomTime = 3

        constructor() {

            this.map = Map.map

            this.isBound = Map.isBound

  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值