小游戏Matlab飞机大战(简易版)

举报
nimo的小舔狗 发表于 2022/05/10 22:47:09 2022/05/10
【摘要】 文末附链接    上代码: function air_battle global game; global bkg; global line; global planeown; global planeopp; global bulletlist; global bullet global ...

文末附链接 

 

上代码:

function air_battle
global game;
global bkg;
global line;
global planeown;
global planeopp;

global bulletlist;
global bullet

global ControlTimer;

global Mainfig;
global Mainaxes;

global DrawBkgHdl;
global DrawLineHdl;
global DrawPlaneOwnHdl;
global DrawPlaneOppHdl;
global DrawBulletHdl;
init()
    function init(~,~)
        ControlTimer=0;
        bulletlist=[];
        
        bkg.CData=imread('bkg1.jpg');
        bkg.Size=size(bkg.CData);
        
        [line.CData,~,line.AlpData]=imread('line2.png');
        line.Size=size(line.CData);
        
        
        [planeown.CData,~,planeown.AlpData]=imread('planeown.png');
        planeown.Size=size(planeown.CData);
        planeown.Pos=[bkg.Size(2)/2-planeown.Size(2)/2,40];
        [planeopp.CData,~,planeopp.AlpData]=imread('planeopp.png');
        planeopp.Size=size(planeopp.CData);
        planeopp.Pos=[bkg.Size(2)/2-planeopp.Size(2)/2,750];
        
        for i=1:6
            [temp_CData,~,temp_AlpData]=imread(['bullet',num2str(i),'.png']);
            bullet.(['CData',num2str(i)])=temp_CData;
            bullet.(['AlpData',num2str(i)])=temp_AlpData;
            bullet.Size(i,:)=size(temp_CData);
        end
        
        Mainfig=figure('units','pixels','position',[550 50 bkg.Size(1,[2,1])./1.2],...
                       'Numbertitle','off','menubar','none','resize','off',...
                       'name','air battle2.0');
        Mainaxes=axes('parent',Mainfig,'position',[0 0 1 1],...
                    'XLim', [0 bkg.Size(2)],...
                    'YLim', [0 bkg.Size(1)],...
                    'NextPlot','add',...
                    'layer','bottom',...
                    'Visible','on',...
                    'XTick',[], ...
                    'YTick',[]);
       DrawBkgHdl=image([0 bkg.Size(2)],[0 bkg.Size(1)],flipud(bkg.CData));
       DrawLineHdl=image([0 bkg.Size(2)],[0 line.Size(1)]+180,flipud(line.CData),'alphaData',flipud(line.AlpData).*0.5);
       DrawPlaneOwnHdl=image([0 planeown.Size(2)]+planeown.Pos(1),[0 planeown.Size(1)]+planeown.Pos(2),...
                             flipud(planeown.CData),...
                             'alphaData',flipud(planeown.AlpData));
       DrawPlaneOppHdl=image([0 planeopp.Size(2)]+planeopp.Pos(1),[0 planeopp.Size(1)]+planeopp.Pos(2),...
                             flipud(planeopp.CData),...
                             'alphaData',flipud(planeopp.AlpData));
       
       fps = 50;                                    
       game = timer('ExecutionMode', 'FixedRate', 'Period',1/fps, 'TimerFcn', @airgame);
       set(gcf,'tag','co','CloseRequestFcn',@clo);
       function clo(~,~),stop(game),delete(findobj('tag','co'));clf,close,end 
       start(game)
       set(gcf,'WindowButtonMotionFcn',@planown_move)
       set(gcf,'WindowButtonDownFcn',@planown_shoot)
       set(gcf,'KeyPressFcn',@shoot_by_key)  
    end

    function airgame(~,~)
        ControlTimer=ControlTimer+1;
        temp_bkg=flipud([bkg.CData;bkg.CData]);
        set(DrawBkgHdl,'CData',...
            temp_bkg(mod(ControlTimer,bkg.Size(1))+1:mod(ControlTimer,bkg.Size(1))+bkg.Size(1),:,:)); 
        
        planeopp.Pos(2)=planeopp.Pos(2)-1.5;
        set(DrawPlaneOppHdl,'XData',[0 planeopp.Size(2)]+planeopp.Pos(1),'YData',[0 planeopp.Size(1)]+planeopp.Pos(2));
        if ~isempty(bulletlist)
            for i=length(bulletlist):-1:1
                temp_num=bulletlist(i);
                set(DrawBulletHdl(temp_num),'YData',get(DrawBulletHdl(temp_num),'YData')+4);
                temp_y=get(DrawBulletHdl(temp_num),'YData');
                if temp_y(1)>766
                    bulletlist(bulletlist==temp_num)=[];
                    delete(DrawBulletHdl(temp_num));
                else
                    temp_judge_pos(1)=mean(get(DrawBulletHdl(temp_num),'XData'));
                    temp_judge_pos(2)=mean(get(DrawBulletHdl(temp_num),'YData'))-20;
                    if(planeopp.Pos(1)<=temp_judge_pos(1))&&...
                      (temp_judge_pos(1)<=planeopp.Size(2)+planeopp.Pos(1))&&...
                      (planeopp.Pos(2)<=temp_judge_pos(2))&&...
                      (temp_judge_pos(2)<=planeopp.Size(1)+planeopp.Pos(2))
                        bulletlist(bulletlist==temp_num)=[];
                        delete(DrawBulletHdl(temp_num));
                        planeopp.Pos=[randi(floor(bkg.Size(2)-planeopp.Size(2)/2)),750];
                    end
                end
            end
        end
        judge()
    end

    function planown_move(~,~)
        xy=get(gca,'CurrentPoint');
        temp_x=xy(1,1);%temp_y=xy(1,2);
        planeown.Pos(1)=temp_x-planeown.Size(2)/2;
        set(DrawPlaneOwnHdl,'XData',[0 planeown.Size(2)]+planeown.Pos(1),...
                            'YData',[0 planeown.Size(1)]+planeown.Pos(2)); 
    end

    function planown_shoot(~,~)
        temp_num=getMax(bulletlist);
        DrawBulletHdl(temp_num)=image([0 bullet.Size(1,2)]+planeown.Pos(1)+planeown.Size(2)/2-bullet.Size(1,2)/2,...
                                      [0 bullet.Size(1,1)]+planeown.Pos(2)+planeown.Size(1)/2,...
                             flipud(bullet.CData1),...
                             'alphaData',flipud(bullet.AlpData1));   
        bulletlist=[bulletlist;temp_num];
    end

    function shoot_by_key(~,event)
        switch event.Key
            case 'a'
                temp_num=getMax(bulletlist);
                DrawBulletHdl(temp_num)=image([0 bullet.Size(1,2)]+planeown.Pos(1)+planeown.Size(2)/2-bullet.Size(1,2)/2,...
                                      [0 bullet.Size(1,1)]+planeown.Pos(2)+planeown.Size(1)/2,...
                             flipud(bullet.CData1),...
                             'alphaData',flipud(bullet.AlpData1));   
                bulletlist=[bulletlist;temp_num];
            case 's'
                temp_num=getMax(bulletlist);
                DrawBulletHdl(temp_num)=image(30+[0 bullet.Size(2,2)/2]+planeown.Pos(1)+planeown.Size(2)/2-bullet.Size(2,2)/4,...
                                      [0 bullet.Size(2,1)/2]+planeown.Pos(2)+planeown.Size(1)/2,...
                             flipud(bullet.CData2),...
                             'alphaData',flipud(bullet.AlpData2));   
                bulletlist=[bulletlist;temp_num];
                temp_num=getMax(bulletlist);
                DrawBulletHdl(temp_num)=image(-30+[0 bullet.Size(2,2)/2]+planeown.Pos(1)+planeown.Size(2)/2-bullet.Size(2,2)/4,...
                                      [0 bullet.Size(2,1)/2]+planeown.Pos(2)+planeown.Size(1)/2,...
                             flipud(bullet.CData2),...
                             'alphaData',flipud(bullet.AlpData2));   
                bulletlist=[bulletlist;temp_num];
            case 'd'
                temp_num=getMax(bulletlist);
                DrawBulletHdl(temp_num)=image([0 bullet.Size(1,2)]+planeown.Pos(1)+planeown.Size(2)/2-bullet.Size(1,2)/2,...
                                      [0 bullet.Size(1,1)]+planeown.Pos(2)+planeown.Size(1)/2,...
                             flipud(bullet.CData1),...
                             'alphaData',flipud(bullet.AlpData1));  
                bulletlist=[bulletlist;temp_num];
                temp_num=getMax(bulletlist);
                DrawBulletHdl(temp_num)=image([0 bullet.Size(3,2)]+planeown.Pos(1)+planeown.Size(2)/2-bullet.Size(3,2)/2,...
                                      30+[0 bullet.Size(3,1)]+planeown.Pos(2)+planeown.Size(1)/2,...
                             flipud(bullet.CData3),...
                             'alphaData',flipud(bullet.AlpData3));   
                bulletlist=[bulletlist;temp_num];
            case 'f'
                temp_num=getMax(bulletlist);
                DrawBulletHdl(temp_num)=image(30+[0 bullet.Size(4,2)/2]+planeown.Pos(1)+planeown.Size(2)/2-bullet.Size(4,2)/4,...
                                      30+[0 bullet.Size(4,1)/2]+planeown.Pos(2)+planeown.Size(1)/2,...
                             flipud(bullet.CData4),...
                             'alphaData',flipud(bullet.AlpData4));   
                bulletlist=[bulletlist;temp_num];
                temp_num=getMax(bulletlist);
                DrawBulletHdl(temp_num)=image(-30+[0 bullet.Size(4,2)/2]+planeown.Pos(1)+planeown.Size(2)/2-bullet.Size(4,2)/4,...
                                      30+[0 bullet.Size(4,1)/2]+planeown.Pos(2)+planeown.Size(1)/2,...
                             flipud(bullet.CData4),...
                             'alphaData',flipud(bullet.AlpData4));   
                bulletlist=[bulletlist;temp_num];
                temp_num=getMax(bulletlist);
                DrawBulletHdl(temp_num)=image(30+[0 bullet.Size(4,2)/2]+planeown.Pos(1)+planeown.Size(2)/2-bullet.Size(4,2)/4,...
                                      [0 bullet.Size(4,1)/2]+planeown.Pos(2)+planeown.Size(1)/2,...
                             flipud(bullet.CData4),...
                             'alphaData',flipud(bullet.AlpData4));   
                bulletlist=[bulletlist;temp_num];
                temp_num=getMax(bulletlist);
                DrawBulletHdl(temp_num)=image(-30+[0 bullet.Size(4,2)/2]+planeown.Pos(1)+planeown.Size(2)/2-bullet.Size(4,2)/4,...
                                      [0 bullet.Size(4,1)/2]+planeown.Pos(2)+planeown.Size(1)/2,...
                             flipud(bullet.CData4),...
                             'alphaData',flipud(bullet.AlpData4));   
                bulletlist=[bulletlist;temp_num];
        end
        
        
    end

    function judge(~,~)
        temp_y=min(get(DrawPlaneOppHdl,'YData'));
        if temp_y<=180
            stop(game)
            set(gcf,'WindowButtonMotionFcn',[])
            set(gcf,'WindowButtonDownFcn',[])
            set(gcf,'KeyPressFcn',[]) 
            buttonName=questdlg('You lose. What do you mean to do?','You lose','close','restart','close');
            switch buttonName
                case 'restart',delete(gcf),init()
                case 'close',delete(gcf)
            end
        end
    end


%==========================================================================
    function num=getMax(list)
        if isempty(list)
            num=1;
        else
            num=max(list)+1;
        end
    end
end

完整项目链接:matlab飞机大战小游戏-其他文档类资源-CSDN下载

文章来源: blog.csdn.net,作者:渣渣ye,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/yyfloveqcw/article/details/123836984

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。