您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 其它行业文档 > 飞机大战JAVA实例(JAVA300集的例子自己改进的)
packagemao.plane;importjava.awt.Color;importjava.awt.Font;importjava.awt.Graphics;importjava.awt.Image;importjava.awt.event.KeyAdapter;importjava.awt.event.KeyEvent;importjava.util.ArrayList;importjava.util.Date;importmao.util.Constant;importmao.util.GameUtil;importmao.util.MyFrame;publicclassPlaneGameFrameextendsMyFrame{/****/privatestaticfinallongserialVersionUID=1L;Imagefeiji=GameUtil.getImage(images/feiji.jpg);Imagebackground=GameUtil.getImage(images/background.jpg);Imagep1=GameUtil.getImage(images/1.png);Imagep2=GameUtil.getImage(images/2.png);Imagep3=GameUtil.getImage(images/3.png);Imagep4=GameUtil.getImage(images/4.png);Imagebp=GameUtil.getImage(images/BossPlane.jpg);ArrayListBulletbulletList1=newArrayList();ArrayListBulletbulletList2=newArrayList();ArrayListBulletbulletList3=newArrayList();ArrayListTrackBullettrackbulletList1=newArrayList();ArrayListBulletbulletList=newArrayList();ArrayListTrackBullettrackbulletList=newArrayList();ArrayListBigBulletbigBulletList=newArrayList();Planep=newPlane(Constant.WINDOW_WIDTH/2-36,Constant.WINDOW_HEIGHT-72-Constant.downdistance,5,images/feiji.jpg);BossPlanebplane=newBossPlane(Constant.WINDOW_WIDTH/2-98,-82,3,images/BossPlane.jpg);BigBulletbb=newBigBullet(495,142,Math.PI/2);BigBulletbb1=newBigBullet(460,142,Math.PI/2);BigBulletbb2=newBigBullet(530,142,Math.PI/2);DatestartTime;//界面开始的时间DatestartTime1;//游戏开始的时间DateendTime;//飞机死亡的时间DatenowTime;//现在的时间DatecollisionTime=newDate();DatecollisionTimeNow;longtimefirst=24625;longnow_startTime;//开始到现在的时间longperiod;//生存的时间单位毫秒doublepbAngle;//跟踪子弹逼近飞机的角度inttbflag=-1;intflagb1=-1;intcollisionNum=0;//代表飞机被碰的次数intbulletNum=20;//子弹的数目inttrackbulletNum=4;//跟踪子弹的数目intbigBulletNum=4;//大子弹的数量intk;//大子弹到达指定位置的次数intcontainNum=6;//每个大子弹包含子弹的数目intcontainNum2=80;intenterNum=0;//按下enter键的次数booleanbigbullet=true;booleanboss=false;//判断是否是boss关booleanflag=false;//此标志位表示游戏是否开始booleanflag1=true;//开始的文字是否开始publicvoidpaint(Graphicsg){g.drawImage(background,0,0,null);if(flag1){printInfo(g,有个小女孩她叫樱桃小丸子,30,Font.BOLD,200,100,Color.YELLOW,宋体);printInfo(g,她是一个:,30,Font.BOLD,400,160,Color.YELLOW,宋体);printInfo(g,羞涩腼腆,30,Font.BOLD,400,220,Color.YELLOW,宋体);printInfo(g,敢爱敢恨,30,Font.BOLD,400,280,Color.YELLOW,宋体);printInfo(g,热情开朗,30,Font.BOLD,400,340,Color.YELLOW,宋体);printInfo(g,温婉善良,30,Font.BOLD,400,400,Color.YELLOW,宋体);printInfo(g,乐于助人,30,Font.BOLD,400,460,Color.YELLOW,宋体);printInfo(g,勤学好问,30,Font.BOLD,400,520,Color.YELLOW,宋体);printInfo(g,的女孩,30,Font.BOLD,400,580,Color.YELLOW,宋体);printInfo(g,使用说明:,20,Font.BOLD,100,300,Color.GREEN,宋体);printInfo(g,请按enter键开始,20,Font.BOLD,100,340,Color.GREEN,幼圆);printInfo(g,W表示小丸子向上,20,Font.BOLD,100,380,Color.GREEN,宋体);printInfo(g,A表示小丸子向左,20,Font.BOLD,100,420,Color.GREEN,宋体);printInfo(g,S表示小丸子向下,20,Font.BOLD,100,460,Color.GREEN,宋体);printInfo(g,D表示小丸子向右,20,Font.BOLD,100,500,Color.GREEN,宋体);printInfo(g,通过W,A,S,D,控制小丸子的方向来躲避子弹,15,Font.BOLD,100,540,Color.GREEN,宋体);}if(flag){nowTime=newDate();now_startTime=nowTime.getTime()-startTime.getTime();p.draw(g);if(p.life){//以下为画血量的程序switch(collisionNum){case0:g.drawImage(p1,952,676,null);break;case1:g.drawImage(p2,952,676,null);break;case2:g.drawImage(p3,952,676,null);break;case3:g.drawImage(p4,952,676,null);break;default:break;}//20s后进入boss关if(now_startTime20000){boss=true;bplane.draw(g);if(now_startTime22000){for(inti=0;ibigBulletNum;i++){if(now_startTime22000+i*2000&&bigbullet==true){BigBulletbigbullet=bigBulletList.get(i);bigbullet.draw(g);if(now_startTime24625+i*2000){switch(i){case0:for(intj=0;jcontainNum;j++){trackbulletList1.add(newTrackBullet());TrackBullettb=trackbulletList1.get(j);if(p.getY()=tb.getY()){pbAngle=Math.PI/6-Math.acos((p.getX()+6-tb.getX())/(Math.sqrt(Math.pow(p.getX()+6-tb.getX(),2)+Math.pow(p.getY()+5-tb.getY(),2))))-Math.PI/6;}if(p.getY()tb.getY()){pbAngle=Math.PI/6+Math.acos((p.getX()+6-tb.getX())/(Math.sqrt(Math.pow(p.getX()+6-tb.getX(),2)+Math.pow(p.getY()+5-tb.getY(),2))))-Math.PI/6;}tb.setAngle(pbAngle);tb.draw(g);//检测飞机和子弹的碰撞booleanlif=p.getRect().intersects(tb.getRect());//检测子弹与飞机是否碰撞if(lif&&collisionNum=3){collisionTimeNow=newDate();//现在飞机碰撞的时间if((collisionTimeNow.getTime()-collisionTime.getTime()1000&&collisionNum=1)||collisionNum==0){collisionNum++;collisionTime=newDate();if(collisionNum==4){p.setLife(false);endTime=newDate();//飞机死亡的时间}}}}break;case1:for(intj=0;jcontainNum;j++){bulletList1.add(newBullet(460,400,Math.PI*2/containNum*j));Bulletb=bulletList1.get(j);b.draw(g);//检测飞机和子弹的碰撞booleanlif=p.getRect().intersects(b.getRect());//检测子弹与飞机是否碰撞if(lif&&collisionNum=3){collisionTimeNow=newDate();if((collisionTimeNow.getTime()-collisionTime.getTime()500&&collisionNum=1)||collisionNum==0){collisionNum++;collisionTime=newDate();if(collisionNum==4){p.setLife(false);endTime=newDate();}//飞机死掉}}}break;case2:for(intj=0;jcontainNum;j++){bulletList2.add(newBullet(530,400,Math.PI*2/containNum*j));Bulletb=bulletList2.get(j);b.draw(g);//检测飞机和子弹的碰撞booleanlif=p.getRect().intersects(b.getRect());//检测子弹与飞机是否碰撞if(lif&&collisionN
本文标题:飞机大战JAVA实例(JAVA300集的例子自己改进的)
链接地址:https://www.777doc.com/doc-4214178 .html