用HTML5制作精美战机游戏

这篇具有很好参考价值的文章主要介绍了用HTML5制作精美战机游戏。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

每天要被大学老师催H5作业👏🏻👏🏻👏🏻

不如看看本文,代码齐全,直接用来做参考案例👌🏻  

干货满满不看后悔👍👍👍

代码和图片压缩包完整下载链接---战机游戏下载

📝个人主页→数据挖掘博主ZTLJQ的主页

用HTML5制作精美战机游戏

个人推荐python学习系列:

☄️爬虫JS逆向系列专栏 - 爬虫逆向教学

☄️python系列专栏 - 从零开始学python


以下是游戏画面

用HTML5制作精美战机游戏

 话不多说直接上代码!

<!DOCTYPE html>
<html>
<head>
    <title>ZT战机</title>
    <style>
        canvas {
            border: 1px solid #000;
            display: block;
            margin: 0 auto;
        }
    </style>
</head>
<body>
    <button onclick="startGame()">开始游戏</button>
    <button onclick="endGame()">结束游戏</button>
    <canvas id="gameCanvas" width="800" height="800"></canvas>

    <script>
        var game = {
            canvas: document.getElementById("gameCanvas"),
            context: null,
            player: null,
            bullets: [],
            enemies: [],
            score: 0,
            gameover: true,

            init: function() {
                this.context = this.canvas.getContext("2d");
                this.player = new Player(this.canvas.width / 2, this.canvas.height - 100);

                window.addEventListener("keydown", function(event) {
                    game.handleKeydown(event);
                });
                window.addEventListener("keyup", function(event) {
                    game.handleKeyup(event);
                });

                this.gameLoop();
            },

            startGame: function() {
                if (this.gameover) {
                    this.score = 0;
                    this.gameover = false;
                    this.bullets = [];
                    this.enemies = [];

                    this.spawnEnemies();

                    this.gameLoop();
                }
            },

            endGame: function() {
                this.gameover = true;
            },

            gameLoop: function() {
                if (!this.gameover) {
                    this.update();
                    this.draw();

                    requestAnimationFrame(function() {
                        game.gameLoop();
                    });
                }
            },

            update: function() {
                this.player.update();

                for (var i = this.bullets.length - 1; i >= 0; i--) {
                    var bullet = this.bullets[i];
                    bullet.update();
                    if (bullet.y < 0 || bullet.hit) {
                        this.bullets.splice(i, 1);
                    }
                }

                for (var i = this.enemies.length - 1; i >= 0; i--) {
                    var enemy = this.enemies[i];
                    enemy.update();

                    if (enemy.checkCollision(this.player)) {
                        this.gameover = true;
                    }

                    for (var j = this.bullets.length - 1; j >= 0; j--) {
                        var bullet = this.bullets[j];
                        if (bullet.checkCollision(enemy)) {
                            this.bullets.splice(j, 1);
                            this.enemies.splice(i, 1);
                            this.score += 10;
                            this.increaseDifficulty();
                            break;
                        }
                    }
                }
            },

            draw: function() {
                this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);

                // 绘制游戏场景背景图
                var backgroundImage = new Image();
                backgroundImage.src = "background.jpg";
                this.context.drawImage(backgroundImage, 0, 0, this.canvas.width, this.canvas.height);

                this.player.draw(this.context);

                for (var i = 0; i < this.bullets.length; i++) {
                    var bullet = this.bullets[i];
                    bullet.draw(this.context);
                }

                for (var i = 0; i < this.enemies.length; i++) {
                    var enemy = this.enemies[i];
                    enemy.draw(this.context);
                }

                this.context.fillStyle = "#000";
                this.context.font = "20px Arial";
                this.context.fillText("Score: " + this.score, 10, 30);

                if (this.gameover) {
                    this.context.fillStyle = "#000";
                    this.context.font = "40px Arial";
                    this.context.fillText("Game Over", this.canvas.width / 2 - 100, this.canvas.height / 2);
                }
            },

            handleKeydown: function(event) {
                if (event.keyCode === 37) {
                    this.player.moveLeft();
                } else if (event.keyCode === 39) {
                    this.player.moveRight();
                } else if (event.keyCode === 32) {
                    if (!this.gameover) {
                        var bullet = this.player.shoot();
                        this.bullets.push(bullet);
                    }
                }
            },

            handleKeyup: function(event) {
                if (event.keyCode === 37 || event.keyCode === 39) {
                    this.player.stopMove();
                }
            },

            spawnEnemies: function() {
                if (!this.gameover) {
                    var enemyCount = Math.floor(Math.random() * 3) + 1; // 随机生成 1 到 3 个敌人

                    for (var i = 0; i < enemyCount; i++) {
                        var x = Math.random() * (this.canvas.width - 50);
                        var y = Math.random() * -200; // 从屏幕上方随机生成

                        // 创建不同种类的敌机
                        var enemyType = Math.random();
                        var enemy;

                        if (enemyType < 0.5) {
                            enemy = new Enemy1(x, y);
                        } else {
                            enemy = new Enemy2(x, y);
                        }

                        this.enemies.push(enemy);
                    }

                    setTimeout(function() {
                        game.spawnEnemies();
                    }, 2000); // 每隔 2 秒生成一波敌人
                }
            },

            increaseDifficulty: function() {
                if (this.score % 50 === 0) {
                    for (var i = 0; i < this.enemies.length; i++) {
                        this.enemies[i].speed += 0.5;
                    }
                }
            }
        };

        function Player(x, y) {
            this.x = x;
            this.y = y;
            this.width = 80;
            this.height = 60;
            this.speed = 8;
            this.isMovingLeft = false;
            this.isMovingRight = false;
            this.image = new Image();
            this.image.src = "player.png";
        }

        Player.prototype.update = function() {
            if (this.isMovingLeft) {
                this.moveLeft();
            } else if (this.isMovingRight) {
                this.moveRight();
            }
        };

        Player.prototype.draw = function(context) {
            context.drawImage(this.image, this.x, this.y, this.width, this.height);
        };

        Player.prototype.moveLeft = function() {
            if (this.x > 0) {
                this.x -= this.speed;
            }
        };

        Player.prototype.moveRight = function() {
            if (this.x + this.width < game.canvas.width) {
                this.x += this.speed;
            }
        };

        Player.prototype.stopMove = function() {
            this.isMovingLeft = false;
            this.isMovingRight = false;
        };

        Player.prototype.shoot = function() {
            var bullet = new Bullet(this.x + this.width / 2, this.y);
            return bullet;
        };

        function Bullet(x, y) {
            this.x = x;
            this.y = y;
            this.width = 10;
            this.height = 30;
            this.speed = 5;
            this.hit = false;
            this.image = new Image();
            this.image.src = "bullet.png";
        }

        Bullet.prototype.update = function() {
            this.y -= this.speed;
        };

        Bullet.prototype.draw = function(context) {
            context.drawImage(this.image, this.x, this.y, this.width, this.height);
        };

        Bullet.prototype.checkCollision = function(object) {
            if (
                this.x < object.x + object.width &&
                this.x + this.width > object.x &&
                this.y < object.y + object.height &&
                this.y + this.height > object.y
            ) {
                this.hit = true;
                return true;
            }
            return false;
        };

        function Enemy1(x, y) {
            this.x = x;
            this.y = y;
            this.width = 80;
            this.height = 60;
            this.speed = Math.random() * 2 + 1;
            this.image = new Image();
            this.image.src = "enemy1.png";
        }

        Enemy1.prototype.update = function() {
            this.y += this.speed;
        };

        Enemy1.prototype.draw = function(context) {
            context.drawImage(this.image, this.x, this.y, this.width, this.height);
        };

        Enemy1.prototype.checkCollision = function(object) {
            if (
                this.x < object.x + object.width &&
                this.x + this.width > object.x &&
                this.y < object.y + object.height &&
                this.y + this.height > object.y
            ) {
                return true;
            }
            return false;
        };

        function Enemy2(x, y) {
            this.x = x;
            this.y = y;
            this.width = 80;
            this.height = 60;
            this.speed = Math.random() * 2 + 1;
            this.image = new Image();
            this.image.src = "enemy2.png";
        }

        Enemy2.prototype.update = function() {
            this.y += this.speed;
        };

        Enemy2.prototype.draw = function(context) {
            context.drawImage(this.image, this.x, this.y, this.width, this.height);
        };

        Enemy2.prototype.checkCollision = function(object) {
            if (
                this.x < object.x + object.width &&
                this.x + this.width > object.x &&
                this.y < object.y + object.height &&
                this.y + this.height > object.y
            ) {
                return true;
            }
            return false;
        };

        var startGame = function() {
            game.startGame();
        };

        var endGame = function() {
            game.endGame();
        };

        game.init();
    </script>
</body>
</html>

其中的background bullet这些图片你可用使用自己想要图片进行替换

 文章来源地址https://www.toymoban.com/news/detail-489022.html

到了这里,关于用HTML5制作精美战机游戏的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Html5版音乐游戏制作及分享(H5音乐游戏)

    这里实现了Html5版的音乐游戏的核心玩法。 游戏的制作借鉴了,很多经典的音乐游戏玩法,通过简单的代码将音乐的节奏与操作相结合。 可以通过手机进行游戏,准确点击下落时的目标,进行得分。 点击试玩 游戏内的下落数据是通过手打记录的,可能有些偏差哈。 1、Html中

    2023年04月17日
    浏览(27)
  • HTML5 Canvas API制作一个简单的猜字单机游戏

    这篇文章主要介绍了借助HTML5 Canvas API制作一个简单的猜字单机游戏的实例分享,游戏中每局会自动生成一个字母,玩家按键盘来猜测该字母是哪一个,需要的朋友可以参考下 HTML代码 !doctype html   html lang=\\\"en\\\"       head           meta charset=\\\"utf-8\\\" /           script 

    2024年02月16日
    浏览(32)
  • HTML5期末大作业:游戏网站设计与实现——基于bootstrap响应式游戏资讯网站制作HTML+CSS+JavaScript

    🎉精彩专栏推荐👇🏻👇🏻👇🏻 ✍️ 作者简介: 一个热爱把逻辑思维转变为代码的技术博主 💂 作者主页: 【主页——🚀获取更多优质源码】 🎓 web前端期末大作业: 【📚毕设项目精品实战案例 (1000套) 】 🧡 程序员有趣的告白方式:【💌HTML七夕情人节表白网页制作

    2024年01月17日
    浏览(40)
  • 【HTML5】登录页面制作简易版

    刚开始学习Java。文件的命名,讲道理应该以英文为主,但是英语又不好,所以只好用拼音,最痛苦的应该算是那些英语又不好,又想秀一下的程序员,一半英语一半拼音,如mainFangFa~~~你说看了糟心不糟心。 目录 1、form表单定义和用法 1.1input标签定义和用法 1.2label标签for属性

    2024年02月01日
    浏览(76)
  • HTML5网页制作 我的心灵小屋

    代码如下:

    2024年02月08日
    浏览(35)
  • HTML小游戏18 —— html5版街头霸王游戏(附完整源码)

    💂 网站推荐:【神级源码资源网】【摸鱼小游戏】 🤟 前端学习课程:👉【28个案例趣学前端】【400个JS面试题】 💅 想寻找共同学习交流、摸鱼划水的小伙伴,请点击【摸鱼学习交流群】 💬 免费且实用的计算机相关知识题库:👉进来逛逛 本节教程我会带大家使用 HTML 、

    2024年01月17日
    浏览(39)
  • HTML5 游戏开发实战 | 贪吃蛇

    在该游戏中,玩家操纵一条贪吃的蛇在长方形场地里行走,贪吃蛇按玩家所按的方向键折行,蛇头吃到食物(豆)后,分数加10分,蛇身会变长,如果贪吃蛇碰上墙壁或者自身的话,游戏就结束了(当然也可能是减去一条生命)。 贪吃蛇游戏的运行界面如上图所示。 把游戏画面看

    2024年02月11日
    浏览(37)
  • HTML小游戏25 —— HTML5拉杆子过关小游戏(附完整源码)

    本节教程我会带大家使用 HTML 、CSS和 JS 来制作一个HTML5拉杆子过关小游戏 🕹️ 本文已收录于🎖️100个HTML小游戏专栏: 100个H5游戏专栏 https://blog.csdn.net/qq_53544522/category_12064846.html 🎮 目前已有100+小游戏,源码在持续更新中,前100位订阅限时优惠,先到先得。 🐬 订阅专栏后

    2024年02月05日
    浏览(40)
  • HTML5 实现扑克翻牌游戏

     扑克翻牌游戏就是桌面24张牌,玩家翻到两张相同扑克牌则消去,如果时间2分钟到了,仍然没有成功则游戏失败。扑克翻牌游戏运行结果如图1。   Html5倒计时功能可以使用setTimeout()函数或者setInterval()函数来实现。 1.使用setTimeout实现倒计时功能 setTimeout()会在一个指定的延

    2024年02月13日
    浏览(25)
  • HTML5 游戏开发实战 | 黑白棋

     黑白棋,又叫反棋(Reversi)、奥赛罗棋(Othello)、苹果棋、翻转棋。黑白棋在西方和日本很流行。游戏通过相互翻转对方的棋子,最后以棋盘上谁的棋子多来判断胜负。黑白棋的棋盘是一个有8×8方格的棋盘。开始时在棋盘正中有两白两黑四个棋子交叉放置,黑棋总是先下子。

    2024年02月12日
    浏览(23)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包