|
|
你好!飞机大作战是一款飞行射击类游戏,比较复杂一些。不过我可以帮你做一个非常简单的飞机射击小游戏的代码示例,适合在网页上运行,功能包括:用键盘左右移动飞机,按空格发射子弹,简单的敌机和碰撞检测。
你可以复制下面的代码到一个HTML文件里打开玩玩看:
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="UTF-8" />
- <title>简易飞机大作战</title>
- <style>
- body {
- margin: 0;
- background: #000;
- overflow: hidden;
- color: white;
- font-family: Arial, sans-serif;
- text-align: center;
- }
- canvas {
- background: #111;
- display: block;
- margin: 20px auto;
- border: 2px solid #0f0;
- }
- </style>
- </head>
- <body>
- <h1>简易飞机大作战</h1>
- <canvas id="gameCanvas" width="400" height="600"></canvas>
- <p>左右方向键控制飞机,空格射击</p>
- <script>
- const canvas = document.getElementById('gameCanvas');
- const ctx = canvas.getContext('2d');
- // 飞机属性
- const planeWidth = 40;
- const planeHeight = 40;
- let planeX = (canvas.width - planeWidth) / 2;
- const planeY = canvas.height - planeHeight - 10;
- const planeSpeed = 5;
- // 子弹数组
- const bullets = [];
- const bulletWidth = 5;
- const bulletHeight = 10;
- const bulletSpeed = 7;
- // 敌机数组
- const enemies = [];
- const enemyWidth = 40;
- const enemyHeight = 40;
- const enemySpeed = 2;
- // 控制状态
- let isLeftPressed = false;
- let isRightPressed = false;
- let isSpacePressed = false;
- // 分数
- let score = 0;
- // 创建敌机
- function createEnemy() {
- const x = Math.random() * (canvas.width - enemyWidth);
- enemies.push({x, y: -enemyHeight});
- }
- // 监听键盘按下
- window.addEventListener('keydown', function(e) {
- if (e.code === 'ArrowLeft') isLeftPressed = true;
- else if (e.code === 'ArrowRight') isRightPressed = true;
- else if (e.code === 'Space') isSpacePressed = true;
- });
- // 监听键盘松开
- window.addEventListener('keyup', function(e) {
- if (e.code === 'ArrowLeft') isLeftPressed = false;
- else if (e.code === 'ArrowRight') isRightPressed = false;
- else if (e.code === 'Space') isSpacePressed = false;
- });
- // 画飞机
- function drawPlane(){
- ctx.fillStyle = '#0f0';
- // 简单画个三角形当飞机
- ctx.beginPath();
- ctx.moveTo(planeX + planeWidth/2, planeY);
- ctx.lineTo(planeX, planeY + planeHeight);
- ctx.lineTo(planeX + planeWidth, planeY + planeHeight);
- ctx.closePath();
- ctx.fill();
- }
- // 画子弹
- function drawBullets(){
- ctx.fillStyle = '#ff0';
- bullets.forEach(bullet => {
- ctx.fillRect(bullet.x, bullet.y, bulletWidth, bulletHeight);
- });
- }
- // 画敌机
- function drawEnemies(){
- ctx.fillStyle = '#f00';
- enemies.forEach(enemy => {
- ctx.fillRect(enemy.x, enemy.y, enemyWidth, enemyHeight);
- });
- }
- // 更新位置
- function update(){
- // 移动飞机
- if(isLeftPressed){
- planeX -= planeSpeed;
- if(planeX < 0) planeX = 0;
- }
- if(isRightPressed){
- planeX += planeSpeed;
- if(planeX > canvas.width - planeWidth) planeX = canvas.width - planeWidth;
- }
- // 发射子弹
- if(isSpacePressed && (bullets.length === 0 || Date.now() - bullets[bullets.length-1].time > 300)){
- bullets.push({x: planeX + planeWidth/2 - bulletWidth/2, y: planeY, time: Date.now()});
- }
- // 子弹飞行
- for(let i=bullets.length-1; i>=0; i--){
- bullets[i].y -= bulletSpeed;
- if(bullets[i].y + bulletHeight < 0){
- bullets.splice(i,1);
- }
- }
- // 敌机飞行
- for(let i=enemies.length-1; i>=0; i--){
- enemies[i].y += enemySpeed;
- if(enemies[i].y > canvas.height){
- enemies.splice(i,1); // 敌机飞出屏幕消失
- }
- }
- // 子弹和敌机碰撞检测
- for(let i=enemies.length-1; i>=0; i--){
- let enemy = enemies[i];
- for(let j=bullets.length-1; j>=0; j--){
- let bullet = bullets[j];
- if(bullet.x < enemy.x + enemyWidth &&
- bullet.x + bulletWidth > enemy.x &&
- bullet.y < enemy.y + enemyHeight &&
- bullet.y + bulletHeight > enemy.y){
- // 碰撞了
- enemies.splice(i, 1);
- bullets.splice(j, 1);
- score += 10;
- break;
- }
- }
- }
- // 敌机撞飞机检测 - 游戏结束判定
- for(let i=0; i<enemies.length; i++){
- let enemy = enemies[i];
- if(planeX < enemy.x + enemyWidth &&
- planeX + planeWidth > enemy.x &&
- planeY < enemy.y + enemyHeight &&
- planeY + planeHeight > enemy.y){
- alert('游戏结束!你的得分是:' + score);
- document.location.reload();
- }
- }
- }
- // 画分数
- function drawScore(){
- ctx.fillStyle='white';
- ctx.font = '20px Arial';
- ctx.fillText('得分: ' + score, 10, 30);
- }
- // 主循环
- function gameLoop(){
- ctx.clearRect(0,0,canvas.width,canvas.height);
- update();
- drawPlane();
- drawBullets();
- drawEnemies();
- drawScore();
- requestAnimationFrame(gameLoop);
- }
- // 每秒生成一个敌机
- setInterval(createEnemy, 1000);
- gameLoop();
- </script>
- </body>
- </html>
复制代码
**说明:**
- 用左右箭头控制飞机左右移动。
- 按空格键可以发射黄色子弹。
- 红色块表示敌机从上往下飞。
- 子弹打中敌机会消失并加分。
- 敌机碰到飞机游戏结束,会弹窗显示得分并重新加载页面。
这个游戏代码很简单,没有音效和复杂的图形,但能帮你初步了解如何使用画布(Canvas)做游戏。
如果你想让飞机更漂亮,或者加上更多功能,随时告诉我,我可以帮你一步步改进哦!祝你学习编程开心!????✈️???? |
|