|
|
好呀!我来帮你写一个简单的“飞机大战”小游戏代码,用的是网页编程中的HTML和JavaScript。你可以把它放到电脑上的一个文件里,保存为 `index.html`,然后用浏览器打开就能玩啦!
这是一个简单版的飞机大战游戏,内容包括:
- 你的飞机可以用左右方向键移动
- 按空格键发射子弹击打敌机
- 敌机会从上方不断出现并向下移动
- 撞到敌机或者敌机到底游戏结束
---
- <!DOCTYPE html>
- <html>
- <head>
- <title>简易飞机大战</title>
- <style>
- body {
- margin: 0;
- overflow: hidden; /* 不显示滚动条 */
- background: black;
- }
- #gameCanvas {
- background: #111;
- display: block;
- margin: 0 auto;
- border: 2px solid white;
- }
- </style>
- </head>
- <body>
- <canvas id="gameCanvas" width="400" height="600"></canvas>
- <script>
- // 获取画布和上下文
- const canvas = document.getElementById('gameCanvas');
- const ctx = canvas.getContext('2d');
- const planeWidth = 40;
- const planeHeight = 40;
- let score = 0;
- let gameOver = false;
- // 玩家飞机对象
- const player = {
- x: canvas.width / 2 - planeWidth / 2,
- y: canvas.height - planeHeight - 10,
- width: planeWidth,
- height: planeHeight,
- speed: 5,
- moveLeft: false,
- moveRight: false,
- };
- // 子弹数组
- const bullets = [];
- // 敌机数组
- const enemies = [];
- // 生成敌机
- function createEnemy() {
- const enemyWidth = 40;
- const enemyHeight = 40;
- const x = Math.random() * (canvas.width - enemyWidth);
- const y = -enemyHeight;
- const speed = 2 + Math.random() * 2;
- enemies.push({x, y, width: enemyWidth, height: enemyHeight, speed});
- }
- // 绘制玩家飞机
- function drawPlayer() {
- ctx.fillStyle = 'cyan';
- // 用三角形画飞机
- ctx.beginPath();
- ctx.moveTo(player.x + player.width/2, player.y);
- ctx.lineTo(player.x, player.y + player.height);
- ctx.lineTo(player.x + player.width, player.y + player.height);
- ctx.closePath();
- ctx.fill();
- }
- // 绘制子弹
- function drawBullets() {
- ctx.fillStyle = 'yellow';
- bullets.forEach(b => {
- ctx.fillRect(b.x, b.y, b.width, b.height);
- });
- }
- // 绘制敌机
- function drawEnemies() {
- ctx.fillStyle = 'red';
- enemies.forEach(e => {
- ctx.fillRect(e.x, e.y, e.width, e.height);
- });
- }
- // 移动玩家飞机
- function movePlayer() {
- if (player.moveLeft && player.x > 0) {
- player.x -= player.speed;
- }
- if (player.moveRight && player.x + player.width < canvas.width) {
- player.x += player.speed;
- }
- }
- // 移动子弹
- function moveBullets() {
- for(let i = bullets.length -1; i >= 0; i--) {
- bullets[i].y -= bullets[i].speed;
- if (bullets[i].y < 0) {
- bullets.splice(i, 1); // 把超出画面顶部的子弹删掉
- }
- }
- }
- // 移动敌机
- function moveEnemies() {
- for(let i = enemies.length -1; i >= 0; i--) {
- enemies[i].y += enemies[i].speed;
- if (enemies[i].y > canvas.height) {
- // 敌机到底,算作游戏失败
- gameOver = true;
- }
- }
- }
- // 碰撞检测函数
- function isColliding(a, b) {
- return !(
- a.x + a.width < b.x ||
- a.x > b.x + b.width ||
- a.y + a.height < b.y ||
- a.y > b.y + b.height
- );
- }
- // 检测子弹和敌机碰撞
- function checkBulletEnemyCollision() {
- for(let i = enemies.length -1; i >= 0; i--) {
- for(let j = bullets.length -1; j >= 0; j--) {
- if(isColliding(enemies[i], bullets[j])) {
- // 击中了敌机
- enemies.splice(i,1);
- bullets.splice(j,1);
- score += 10;
- break; // 一个敌机被一个子弹击中后跳出内层循环
- }
- }
- }
- }
- // 检测玩家和敌机碰撞
- function checkPlayerEnemyCollision() {
- for(let i = 0; i < enemies.length; i++) {
- if(isColliding(player, enemies[i])) {
- gameOver = true;
- break;
- }
- }
- }
- // 显示分数
- function drawScore() {
- ctx.fillStyle = 'white';
- ctx.font = '20px Arial';
- ctx.fillText('得分: '+score, 10, 30);
- }
- // 显示游戏结束字样
- function drawGameOver() {
- ctx.fillStyle = 'white';
- ctx.font = '40px Arial';
- ctx.textAlign = 'center';
- ctx.fillText('游戏结束', canvas.width / 2, canvas.height / 2);
- ctx.font = '20px Arial';
- ctx.fillText('得分: '+score, canvas.width / 2, canvas.height / 2 + 40);
- }
- // 主循环
- function gameLoop() {
- if (gameOver) {
- drawGameOver();
- return;
- }
- // 清空画布
- ctx.clearRect(0, 0, canvas.width, canvas.height);
- movePlayer();
- moveBullets();
- moveEnemies();
-
- checkBulletEnemyCollision();
- checkPlayerEnemyCollision();
- drawPlayer();
- drawBullets();
- drawEnemies();
- drawScore();
- requestAnimationFrame(gameLoop);
- }
- // 控制按键
- document.addEventListener('keydown', function(e) {
- if(e.code === 'ArrowLeft') {
- player.moveLeft = true;
- } else if(e.code === 'ArrowRight') {
- player.moveRight = true;
- } else if(e.code === 'Space') {
- // 发射子弹
- bullets.push({
- x: player.x + player.width/2 - 2,
- y: player.y,
- width:4,
- height:10,
- speed:7,
- });
- }
- });
- document.addEventListener('keyup', function(e) {
- if(e.code === 'ArrowLeft') {
- player.moveLeft = false;
- } else if(e.code === 'ArrowRight') {
- player.moveRight = false;
- }
- });
- // 每秒生成一个敌机
- setInterval(() => {
- if(!gameOver) {
- createEnemy();
- }
- }, 1000);
- // 开始游戏
- gameLoop();
- </script>
- </body>
- </html>
复制代码
---
###怎么玩?
1. 左右方向键控制飞机左右移动
2. 空格键发射子弹
3. 尽量用子弹打掉红色的敌机,不让敌机碰到你的飞机或者飞到底部
4. 游戏结束时会显示“游戏结束”和你的得分
---
有没有听明白呀?这个游戏其实就是用画布不停地画图,动画就是让东西的位置一帧一帧地改变,看起来像在动。飞机、子弹和敌机都是用简单的颜色和形状表示的,容易理解。
如果你想换飞机样子或增加新功能,可以再问我,我帮你改哦!祝你玩得开心!????✈️???? |
|