打飞机小游戏


<!DOCTYPE html>
<html onselectstart="return false">
    <head>
        <meta charset="UTF-8" />
        <meta name="Anthor" content="苏苏少年" />
        <title>Title</title>
        <style>
            *{ margin:0; padding:0; font-family:"Microsoft yahei",serif;}
            li{ list-style-type: none;}
            body{
                overflow: hidden;
                user-select: none;
            }
            #box{
                position: relative;
                width: 512px;
                height: 768px;
                margin: 20px auto;
            }
            #map{
                overflow: hidden;
                position: absolute;
                top: 0;
                left: 0;
                width: 100%;
                height: 100%;
                background: url("img/bg_1.jpg");
            }
            #level{
                position: absolute;
                top: 0;
                left: 0;
                z-index: 1;
                width: 100%;
                height: 100%;
            }
            #level h1{
                font-size: 40px;
                padding-top: 60px;
                padding-bottom: 150px;
                line-height: 60px;
                text-align: center;
                color: #fff;
            }
            #level p{
                margin: 30px auto;
                width: 200px;
                height: 35px;
                line-height: 35px;
                text-align: center;
                background: #fff;
                font-weight: bolder;
                cursor: pointer;
            }
            #level p:hover{
                background: pink;
                color: #fff;
            }
            #map .plane,#map .biu,#map .enemy,#map .boom,#map .boom2{
                position: absolute;
            }
            #map .plane{
                z-index: 8;
            }
            #map .biu{
                z-index: 10;
            }
            #map .boom2{
                z-index: 11;
                animation: bling 2s 1;
                animation-fill-mode: forwards;
            }
            #map .enemy{
                z-index: 9;
            }
            #map .boom{
                z-index: 7;
                animation: fade .8s 1;
                animation-fill-mode: forwards;
            }
            @keyframes fade {
                from{ opacity: 1; }
                to{ opacity: 0; }
            }
            @keyframes bling {
                0%{ opacity: 1; }
                20%{ opacity: 0; }
                40%{opacity: 1; }
                60%{opacity: 0;}
                80%{opacity: 1; }
                100%{opacity: 0;}
            }
            #score{
                display: none;
                position: absolute;
                top:10px;
                left:10px;
                color: #fff;
                line-height: 20px;
                font-size: 14px;
                font-weight: bold;
                z-index:20;
            }
            #restart{
                display: none;
                position: absolute;
                top: 0;
                left: 0;
                width: 100%;
                height: 100%;
                z-index:30;
            }
            #restart p{
                width: 300px;
                height: 40px;
                line-height: 20px;
                margin: 140px auto;
                color: #fff;
            }

            #restart p span{
                display: block;
                font-weight : bolder;
                font-size: 22px;
                text-align: center;
            }
            #restart .p1 span{
                color: red;
            }
            #restart .p2 span{
                color: #ffa80c;
            }
            #restart .p3{
                font-family: "楷体";
                font-size: 20px;
                width: 100px;
                height: 35px;
                background: rgb(255,255,255);
                background: rgba(255,255,255,.8);
                color: #000;
                font-weight: bolder;
                line-height: 35px;
                text-align: center;
                border-radius: 3px;
                cursor: pointer;
            }
            #restart .p3:hover{
                background: white;
            }
        </style>
    </head>
    <body>
        <div id="box">
            <div id="level">
                <h1>打飞机v1.0</h1>
                <p>简单</p>
                <p>中等</p>
                <p>困难</p>
                <p style="color: #f00">WM附体</p>
            </div>
            <div id="map">
                <div id="BiuAll"></div>
            </div>
            <div id="score">0</div>
            <div id="restart">
                <p class="p1">您的最终得分是:<span>0</span></p>
                <p class="p2">获得称号:<span>抠脚侠</span></p>
                <p class="p3">重新开始</p>
            </div>
        </div>

        <script>

            window.requestAnimationFrame = window.requestAnimationFrame || function (fn) {return setTimeout(fn,1000/60)};
            window.cancelAnimationFrame = window.cancelAnimationFrame || clearTimeout;

            //全局变量
            var oBox = document.getElementById("box"),
                oScore = document.getElementById("score"),
                oRe = document.getElementById("restart"),
                oLevel = document.getElementById("level"),
                oMap = document.getElementById("map"),
                oBiuAll = document.getElementById("BiuAll"),
                allBiu = oBiuAll.children,
                allReChild = oRe.children,
                boxOffsetTop = oBox.offsetTop,
                boxOffsetLeft = oBox.offsetLeft;

            //启动
            exe();

            //初始选择难度界面的点击事件
            function exe(){

                //难度选择
                var aP = oLevel.getElementsByTagName("p");
                for (var i = 0,length=aP.length; i < length; i++) {
                    (function(i){
                        aP[i].onclick = function (e) {
                            e = e || window.event;
                            startGame(i , {
                                x : e.clientX - boxOffsetLeft,
                                y : e.clientY - boxOffsetTop
                            });//第一个实参为序号 ,第二个实参为存储着鼠标距离map边缘距离的json
                        }
                    })(i);
                }

                //restart按钮
                allReChild[2].onclick = function (ev) {
                    cancelAnimationFrame(oMap.bgTimer);//停止背景滚动
                    oRe.style.display = "none";
                    oLevel.style.display = "block";
                    oScore.innerHTML = 0;
                    oMap.innerHTML = "<div id='BiuAll'></div>";
                    oBiuAll = document.getElementById("BiuAll");
                    allBiu = oBiuAll.children;
                };
            }

            //开始游戏
            function startGame(level , pos){

                clearMap(); //执行 隐藏和清理

                MapBg(level); //执行 Map背景相关操作
                var p = plane(level , pos); //执行 创建我军
                enemy(level , p); //执行 创建敌军
                //enemy(level , plane(level , pos));
                oBox.score = 0;//得分清零
            }

            //隐藏和清理
            function clearMap(){
                oScore.style.display = "block";
                oLevel.style.display = "none";//隐藏关卡选择框
            }

            //Map背景选择与运动
            function MapBg(level) {
                oMap.style.backgroundImage = "url('img/bg_"+(level+1)+".jpg')";

                (function m(){
                    oMap.bgPosY = oMap.bgPosY || 0;
                    oMap.bgPosY ++;
                    oMap.style.backgroundPositionY = oMap.bgPosY + 'px';
                    oMap.bgTimer = requestAnimationFrame(m);
                })();
            }

            //创建我军
            function plane(level , pos) {
                //创建我军图片
                var oImg = new Image();//document.createElement("img");
                oImg.src = "img/plane_0.png";
                oImg.width = 70;
                oImg.height = 70;
                oImg.className = "plane";
                oImg.style.left = pos.x - oImg.width/2 + 'px';
                oImg.style.top = pos.y - oImg.height/2 + 'px';
                oMap.appendChild(oImg);

                //边界值
                var leftMin = -oImg.width/2,
                    leftMax = oMap.clientWidth - oImg.width/2,
                    topMin = 0,
                    topMax = oMap.clientHeight - oImg.height/2;

                //加入mousemove事件
                document.onmousemove = function (ev) {
                    ev = ev || window.event;
                    //获取飞机实时坐标,并限制边界值
                    var left = ev.clientX - boxOffsetLeft - oImg.width/2;
                    var top = ev.clientY - boxOffsetTop - oImg.height/2;
                    left = Math.max(leftMin,left);
                    left = Math.min(leftMax,left);
                    top = Math.max(topMin,top);
                    top = Math.min(topMax,top);
                    //赋值
                    oImg.style.left = left + 'px';
                    oImg.style.top = top + 'px';
                };

                //调用子弹函数
                fire(oImg,level);

                return oImg;
            }

            //我军子弹
            function fire(oImg , level){
                oBox.biuInterval = setInterval(function () {
                    if ( oBox.score >= 500 ){
                        createBiu(true , -1);
                        createBiu(true , 1);
                    }else{
                        createBiu();
                    }
                } , [100 , 200 , 200 , 15][level]);

                function createBiu(index , d){
                    //创建子弹
                    var oBiu = new Image();
                    oBiu.src = "img/fire.png";
                    oBiu.width = 30;
                    oBiu.height = 30;
                    oBiu.className = "biu";

                    var left = oImg.offsetLeft + oImg.width/2 - oBiu.width/2;
                    var top = oImg.offsetTop - oBiu.height + 5;

                    if ( index ){
                        left += oBiu.width*d
                    }

                    oBiu.style.left = left + "px";
                    oBiu.style.top = top + 'px';


                    oBiuAll.appendChild(oBiu);

                    //子弹运动
                    function m() {
                        if ( oBiu.parentNode ){
                            var top = oBiu.offsetTop - 20;
                            if ( top < -oBiu.height ){
                                oBiuAll.removeChild(oBiu);
                            }else{
                                oBiu.style.top = top + 'px';
                                requestAnimationFrame(m);
                            }
                        }
                    }
                    //将运动执行队列放后面,不然子弹会直接初始就在 top-50 的位置
                    setTimeout(function(){
                        requestAnimationFrame(m);
                    },50);
                }
            }

            //创建敌军
            function enemy(level , oPlane) {
                var w = oMap.clientWidth,
                    h = oMap.clientHeight;

                var speed = [5,6,8,8][level]; //敌军下落速度

                var num = 1;
                oBox.enemyIntetval = setInterval(function () {
                    var index = num%30?1:0;

                    //生成敌军
                    var oEnemy = new Image();
                    oEnemy.index = index;
                    oEnemy.HP = [20,1][index];
                    oEnemy.speed = speed + (Math.random()*0.6 - 0.3)*speed;
                    oEnemy.speed *= index?1:0.5;
                    oEnemy.src = "img/enemy_"+["big","small"][index]+".png";
                    oEnemy.className = "enemy";
                    oEnemy.width = [104,54][index];
                    oEnemy.height = [80,40][index];
                    oEnemy.style.left = Math.random()*w - oEnemy.width/2 + 'px';
                    oEnemy.style.top = -oEnemy.height + 'px';
                    oMap.appendChild(oEnemy);
                    num ++;

                    //敌军运动
                    function m(){
                        if ( oEnemy.parentNode ){
                            var top = oEnemy.offsetTop;
                            top += oEnemy.speed;
                            if ( top >= h ){
                                oBox.score --; //漏掉飞机减分
                                oScore.innerHTML = oBox.score;
                                oMap.removeChild(oEnemy);
                            }else{
                                oEnemy.style.top = top + 'px';
                                //子弹碰撞检测
                                for (var i = allBiu.length - 1 ; i >= 0; i--) {
                                    var objBiu = allBiu[i];
                                    if ( coll(oEnemy , objBiu) ){
                                        oBiuAll.removeChild(objBiu);//移除子弹
                                        oEnemy.HP --;
                                        if ( !oEnemy.HP ){
                                            oBox.score += oEnemy.index?2:20; //打掉飞机加分
                                            oScore.innerHTML = oBox.score;
                                            boom(oEnemy.offsetLeft,oEnemy.offsetTop,oEnemy.width,oEnemy.height,index?0:2);//敌军爆炸图
                                            oMap.removeChild(oEnemy);//移除敌军
                                            return;
                                        }
                                    }
                                }
                                //我军碰撞检测
                                if ( oPlane.parentNode && coll(oEnemy,oPlane) ){
                                    boom(oEnemy.offsetLeft,oEnemy.offsetTop,oEnemy.width,oEnemy.height,index?0:2);//敌军爆炸图
                                    boom(oPlane.offsetLeft,oPlane.offsetTop,oPlane.width,oPlane.height,1);//我军爆炸图
                                    oMap.removeChild(oEnemy);//移除敌军
                                    oMap.removeChild(oPlane);//移除我军
                                    GameOver();
                                    return;
                                }
                                requestAnimationFrame(m);
                            }
                        }
                    }
                    requestAnimationFrame(m);
                },[350,150,120,40][level]);
            }

            //爆炸函数
            function boom(l,t,w,h,i){
                var oBoom = new Image();
                oBoom.src = "img/"+["boom_small","plane_0","boom_big"][i]+".png";
                oBoom.className = 'boom'+["","2",""][i];
                oBoom.width = w;
                oBoom.height = h;
                oBoom.style.left = l + "px";
                oBoom.style.top = t + 'px';
                oMap.appendChild(oBoom);
                setTimeout(function(){
                    oBoom.parentNode && oMap.removeChild(oBoom);
                },[1200,2500,1200][i]);
            }

            //两个物体 碰撞检测
            function coll( obj1 , obj2 ){
                var T1 = obj1.offsetTop,
                    B1 = T1+obj1.clientHeight,
                    L1 = obj1.offsetLeft,
                    R1 = L1+obj1.clientWidth;

                var T2 = obj2.offsetTop,
                    B2 = T2+obj2.clientHeight,
                    L2 = obj2.offsetLeft,
                    R2 = L2+obj2.clientWidth;

                return !( B1 < T2 || R1 < L2 || T1 > B2 || L1 > R2 );
            }

            //游戏结束
            function GameOver(){
                document.onmousemove = null; //清除移动事件
                clearInterval(oBox.biuInterval);//不再产生新子弹
                clearInterval(oBox.enemyIntetval);//不再产生新敌军
                restart();
            }

            //结算+重新开始
            function restart(){
                oScore.style.display = "none";

                var s = oBox.score;
                var honor;

                if ( s < -300 ){
                    honor = "闪避+MAX!!!";
                }else if ( s < 10 ){
                    honor = "菜得…算了我不想说了…";
                }else if ( s < 30 ){
                    honor = "抠脚侠!";
                }else if ( s < 100 ){
                    honor = "初级飞机大师";
                }else if ( s < 200 ){
                    honor = "渐入佳境";
                }else if ( s < 500 ){
                    honor = "中级飞机大师";
                }else if ( s < 1000 ){
                    honor = "高级飞机大师";
                }else if ( s < 5000 ){
                    honor = "终极飞机大师";
                }else{
                    honor = "孤独求败!";
                }

                oRe.style.display = "block";
                allReChild[0].children[0].innerHTML = s;
                allReChild[1].children[0].innerHTML = honor;
            }
        </script>
    </body>
</html>




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
飞机(Fighter)是一款有趣又益智的游戏。它可以培养游戏者的逻辑推理能力,适合各种年龄段的人玩。 该游戏原本是我读本科时在宿舍里和同学一起玩的。需要两人以上合作,互相布局互相“开炮”。非常好玩。这次,我把游戏改成计算机版本,(可选计算机自动布局和手动布局两种模式),可以单机玩,也可以由别人布局在由你玩。 1、游戏界面 游戏启动后左边为10×10的“天空”,其中暗藏了三架以“士”字形分布的飞机,其中红色块表示飞机头,灰色块表示其他机身部位,黑色块表示此格无飞机,为空。 主窗口右边顶端分布六个按钮,表示各种功能。 开始:开始一局新游戏。缺省状态为由计算机自动布局。 保存:表示保存当前游戏中的飞机布局。你在玩游戏时可能觉得某一次飞机的布局特别巧妙,很难推出飞机头所在。你就可以利用本功能把该飞机布局保存下来,发给你的朋友,考考他的推理能力。 载入:载入飞机布局的文件,载入完毕后直接开始。 布局:手动布局。请按“士”字形在10×10的“天空”中分布好飞机,然后点击“开始”。游戏将自动检测飞机布局是否正确。 选项:选项里面包含许多信息。“设置”栏包括是否打开音效,是否在桌面上创建快捷方式,以及选择老板键打开的文件。“鼠标操作”栏提示游戏的鼠标操作;“关于”栏说明游戏的一些版权信息。 退出:离开本游戏游戏界面右下脚为游戏信息区,显示游戏的一些提示信息。再下面是游戏结果显示区。 鼠标操作: 游戏进行中: 左键双击:发炮弹。在推断飞机特别是在机头的所在格后请果断“开炮”。 左键单机:游戏中的辅助功能。用于表示你推断所点击的那格有飞机。再次点击取消推测。 右键单击:游戏中的辅助功能。用于表示你推断所点击的那格没有飞机。再次点击取消推测。 游戏手动布局中: 左键单击:表示该格分布为飞机头。再次点击取消该格布局。 右键单击:表示该格分布为飞机的其他机身部位。再次点击取消该格布局。 键盘操作: Enter、Escape:退出游戏。退出之前需要确认。 F2:重新启动新游戏。 Q、q:老板键,哈哈。老板过来时按此键游戏自动变小为任务栏上的小图标,随后自动打开一个cpp文件(或txt文件,由你在“选项”对话框中指定)“假装”你刚才在编程序。 玩法: 点击“开始”进行新游戏(技巧:可以直接双击“天空”开始)。然后双击“天空”。你需要分析已发炮弹的命中情况来推断三架飞机的机头。只有击中飞机头,才算击落该飞机。你的任务就是用最少的炮弹击落三架飞机游戏进行过程中会自动记录已发炮弹数,已击落飞机和炮弹命中率。 三架飞机均被击落后,游戏根据炮弹数进行排行。若进入前十名,则跳出对话框输入你的姓名,进入排行榜。 作者信息: 本游戏为自由软件。源代码公开,需要者可以向作者email联系。若你有其他建议,务必发email至:xiaogi@sohu.com 也欢迎访问作者主页:http://xiaogi.nease.net
好的,让我们来编写一个简单的 Python 打飞机游戏吧。 在这个游戏中,玩家需要控制一个飞机躲避敌机的攻击并射击敌机。游戏界面由一个背景图和多个飞机组成,玩家可以通过键盘控制自己的飞机移动和射击。 下面是一个简单的 Python 打飞机游戏代码示例: ```python import pygame import random # 初始化游戏 pygame.init() # 游戏界面大小 screen_width = 480 screen_height = 800 # 创建游戏界面 screen = pygame.display.set_mode((screen_width, screen_height)) # 游戏标题 pygame.display.set_caption("打飞机游戏") # 加载游戏背景图 bg = pygame.image.load("background.png").convert() bg_rect = bg.get_rect() # 加载玩家飞机图 player_img = pygame.image.load("player.png").convert_alpha() player_rect = player_img.get_rect() # 加载敌机图 enemy_img = pygame.image.load("enemy.png").convert_alpha() # 加载子弹图 bullet_img = pygame.image.load("bullet.png").convert_alpha() # 玩家飞机初始位置 player_rect.left = screen_width // 2 - player_rect.width // 2 player_rect.top = screen_height - player_rect.height - 50 # 子弹列表 bullet_list = [] # 敌机列表 enemy_list = [] # 分数 score = 0 # 游戏是否结束 game_over = False # 游戏循环 while True: # 处理游戏事件 for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit() # 监听键盘事件 elif event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: # 创建新的子弹 bullet = {"rect": pygame.Rect(player_rect.centerx - 5, player_rect.top - 20, 10, 20), "speed": 10} bullet_list.append(bullet) # 移动玩家飞机 key_pressed = pygame.key.get_pressed() if key_pressed[pygame.K_LEFT]: player_rect.left -= 5 elif key_pressed[pygame.K_RIGHT]: player_rect.left += 5 elif key_pressed[pygame.K_UP]: player_rect.top -= 5 elif key_pressed[pygame.K_DOWN]: player_rect.top += 5 # 玩家飞机边界检测 if player_rect.left < 0: player_rect.left = 0 elif player_rect.right > screen_width: player_rect.right = screen_width elif player_rect.top < 0: player_rect.top = 0 elif player_rect.bottom > screen_height: player_rect.bottom = screen_height # 清除屏幕 screen.blit(bg, bg_rect) # 移动子弹 for bullet in bullet_list: bullet["rect"].top -= bullet["speed"] # 绘制子弹 for bullet in bullet_list: screen.blit(bullet_img, bullet["rect"]) # 移动敌机 for enemy in enemy_list: enemy["rect"].top += enemy["speed"] # 绘制敌机 for enemy in enemy_list: screen.blit(enemy_img, enemy["rect"]) # 生成新的敌机 if len(enemy_list) < 5 and random.randint(0, 50) == 0: enemy = {"rect": pygame.Rect(random.randint(0, screen_width - enemy_img.get_width()), -enemy_img.get_height(), enemy_img.get_width(), enemy_img.get_height()), "speed": random.randint(1, 5)} enemy_list.append(enemy) # 检测子弹和敌机是否碰撞 for bullet in bullet_list: for enemy in enemy_list: if bullet["rect"].colliderect(enemy["rect"]): bullet_list.remove(bullet) enemy_list.remove(enemy) score += 10 # 绘制玩家飞机 screen.blit(player_img, player_rect) # 绘制分数 score_text = pygame.font.SysFont(None, 36).render("Score: " + str(score), True, (255, 255, 255)) screen.blit(score_text, (10, 10)) # 检测敌机是否与玩家飞机相撞 for enemy in enemy_list: if player_rect.colliderect(enemy["rect"]): game_over = True # 显示游戏结束界面 if game_over: game_over_text = pygame.font.SysFont(None, 72).render("Game Over!", True, (255, 0, 0)) game_over_rect = game_over_text.get_rect() game_over_rect.centerx = screen.get_rect().centerx game_over_rect.centery = screen.get_rect().centery screen.blit(game_over_text, game_over_rect) pygame.display.update() pygame.time.wait(2000) pygame.quit() exit() # 刷新屏幕 pygame.display.update() ``` 注意:该示例代码只是一个简单的游戏示例,实际的游戏开发需要更多的功能和优化,如增加多种敌机类型、音效、游戏难度等。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值