飞机大战 java源代码DOC.docx

上传人:b****5 文档编号:4476315 上传时间:2022-12-01 格式:DOCX 页数:23 大小:18.54KB
下载 相关 举报
飞机大战 java源代码DOC.docx_第1页
第1页 / 共23页
飞机大战 java源代码DOC.docx_第2页
第2页 / 共23页
飞机大战 java源代码DOC.docx_第3页
第3页 / 共23页
飞机大战 java源代码DOC.docx_第4页
第4页 / 共23页
飞机大战 java源代码DOC.docx_第5页
第5页 / 共23页
点击查看更多>>
下载资源
资源描述

飞机大战 java源代码DOC.docx

《飞机大战 java源代码DOC.docx》由会员分享,可在线阅读,更多相关《飞机大战 java源代码DOC.docx(23页珍藏版)》请在冰豆网上搜索。

飞机大战 java源代码DOC.docx

飞机大战java源代码DOC

packagecom;

import

import

import

import

import

import

import

import

publicclassPlane{

ImagefeijiImage=null;

intx=300;

inty=700;

intlifeCount=5;

publicPlane(){

try{

feijiImage=

.getResourceAsStream("images/feiji.png"));

}catch(IOExceptione){

e.printStackTrace();

}

}

publicvoiddraw(Graphicsg){

//画飞机图片

g.drawImage(feijiImage,x,y,null);

//飞机移动

this.move();

//血条

if(lifeCount>0){

g.setColor(Color.WHITE);

g.fillRect(20,80,100,10);

g.setColor(Color.red);

g.fillRect(20,80,(100/5)*lifeCount,10);

g.setColor(Color.blue);

g.setFont(newFont("幼圆",Font.BOLD,30));

g.drawString("Score:

"+Play01.count,20,60);

}

}

publicvoidmove(){

if(isUP&&!

isDown&&!

isLeft&&!

isRight){

//上

y=y-5;

}elseif(!

isUP&&isDown&&!

isLeft&&!

isRight){

//下

y=y+5;

}elseif(!

isUP&&!

isDown&&isLeft&&!

isRight){

//左

x=x-5;

}elseif(!

isUP&&!

isDown&&!

isLeft&&isRight){

//右

x=x+5;

}elseif(isUP&&!

isDown&&isLeft&&!

isRight){

//左上

x=x-5;

y=y-5;

}elseif(!

isUP&&isDown&&isLeft&&!

isRight){

//左下

x=x-5;

y=y+5;

}elseif(isUP&&!

isDown&&!

isLeft&&isRight){

//右上

x=x+5;

y=y-5;

}elseif(!

isUP&&isDown&&!

isLeft&&isRight){

//右下

x=x+5;

y=y+5;

}

}

booleanisUP=false;

booleanisDown=false;

booleanisLeft=false;

booleanisRight=false;

//摁下

publicvoidkeyPressed(KeyEvente){

intkeyCode=e.getKeyCode();

if(keyCode==KeyEvent.VK_RIGHT||keyCode==KeyEvent.VK_D){

isRight=true;

}elseif(keyCode==KeyEvent.VK_LEFT||keyCode==KeyEvent.VK_A){

isLeft=true;

}elseif(keyCode==KeyEvent.VK_UP||keyCode==KeyEvent.VK_W){

isUP=true;

}elseif(keyCode==KeyEvent.VK_DOWN||keyCode==KeyEvent.VK_S){

isDown=true;

}

}

//放开

publicvoidkeyReleased(KeyEvente){

intkeyCode=e.getKeyCode();

if(keyCode==KeyEvent.VK_RIGHT||keyCode==KeyEvent.VK_D){

isRight=false;

}elseif(keyCode==KeyEvent.VK_LEFT||keyCode==KeyEvent.VK_A){

isLeft=false;

}elseif(keyCode==KeyEvent.VK_UP||keyCode==KeyEvent.VK_W){

isUP=false;

}elseif(keyCode==KeyEvent.VK_DOWN||keyCode==KeyEvent.VK_S){

isDown=false;

}

}

publicRectanglegetRectangle(){

returnnewRectangle(x,y,feijiImage.getWidth(null),feijiImage.getHeight(null));

}

}

packagecom;

publicclassPlaneStatus{

publicstaticintroleNum=1;

publicstaticintplayStatus=0;//0游戏开始前1第一关-1游戏结束

}

packagecom;

import

import

import

import

import

import

publicclassPower{

ImagePowerImage=null;

intx=20;

inty=0;

publicPower(){

this.x=(int)(Math.random()*540)+20;

try{

PowerImage=

.getResourceAsStream("images/power.png"));

}catch(IOExceptione){

e.printStackTrace();

}

}

publicvoiddraw(Graphicsg){

//画血瓶图片

g.drawImage(PowerImage,x,y,null);

//血瓶移动

y++;

}

publicRectanglegetRectangle(){

returnnewRectangle(x,y,PowerImage.getWidth(null),PowerImage.getHeight(null));

}

}

packagecom;

import

import

import

import

import

import

import

import

//第一关

publicclassPlay01{

staticintcount=0;

ImagebgImage=null;

//战机

Planeplane=newPlane();

//战机子弹

Listmzds=newArrayList();

//敌机

Listdijis=newArrayList();

//敌机子弹

//Listdijizidans=newArrayList();

//血瓶

Listgifts=newArrayList();

Listpowers=newArrayList();

publicPlay01(){

try{

bgImage=

.getResourceAsStream("images/bg_01.jpg"));

}catch(IOExceptione){

e.printStackTrace();

}

}

intbgY1=0;

intbgY2=-600;

intfireTime=0;

booleanflag=false;

publicvoiddraw(Graphicsg){

//画背景图片

g.drawImage(bgImage,0,bgY1,null);

bgY1+=5;

if(bgY1<=600){

bgY1=0;

}

g.drawImage(bgImage,0,bgY2,null);

bgY2+=5;

if(bgY2>=0){

bgY2=-600;

}

//清理战机子弹

for(inti=0;i

MyZiDanmyZidan=mzds.get(i);

if(myZidan.x>800){

mzds.remove(i);

}

}

//添加子弹

if(isFire1==true&&flag==false){

if(fireTime%8==0){

mzds.add(newMyZiDan(plane.x+25,plane.y+0));

}

fireTime++;

}

if(isFire1==true&&flag==true){

if(fireTime%4==0){

mzds.add(newMyZiDan(plane.x+8,plane.y+0));

mzds.add(newMyZiDan(plane.x+52,plane.y+0));

}

fireTime++;

}

//画战机子弹

for(inti=0;i

MyZiDanmyZidan=mzds.get(i);

myZidan.draw(g);

}

//清理敌机

for(inti=0;i

Dijidj=dijis.get(i);

if(dj.x<-100){

dijis.remove(i);

}

}

//抽奖:

添加敌机

if((int)(Math.random()*20)==5){

dijis.add(newDiji());

}

//抽奖:

添加血瓶

if((int)(Math.random()*1000)==5){

gifts.add(newGift());

}

for(inti=0;i

Giftgift=gifts.get(i);

gift.draw(g);

}

//

//抽奖:

添加power

if((int)(Math.random()*1000)==5){

powers.add(newPower());

}

for(inti=0;i

Powerpower=powers.get(i);

power.draw(g);

}

//画敌机

for(inti=0;i

Dijidj=dijis.get(i);

dj.draw(g);

}

//画飞机

plane.draw(g);

//判断战机相撞

for(inti=0;i

//先得到每一个敌机

Dijidj=dijis.get(i);

Rectangler1=dj.getRectangle();

Rectangler2=plane.getRectangle();

if(r1.intersects(r2)){

dijis.remove(i);

plane.lifeCount=plane.lifeCount-1;

if(plane.lifeCount>0)

flag=false;

if(plane.lifeCount<1){PlaneStatus.playStatus=-1;}

}

}

//判断战机和血瓶相撞

for(inti=0;i

//先得到每一个血瓶

Giftgift=gifts.get(i);

Rectangler1=gift.getRectangle();

Rectangler2=plane.getRectangle();

if(r1.intersects(r2)){

gifts.remove(i);

if(plane.lifeCount<5){

plane.lifeCount=plane.lifeCount+1;

}

}

}

////判断战机和Power相撞

for(inti=0;i

//先得到每一个power

Powerpower=powers.get(i);

Rectangler1=power.getRectangle();

Rectangler2=plane.getRectangle();

if(r1.intersects(r2)){

powers.remove(i);

flag=true;

}

}

//判断敌机在中弹

for(inti=0;i

//得到每个战机子弹

MyZiDanmyzidan=mzds.get(i);

Rectangler1=myzidan.getRectangle();

for(intj=0;j

//每一个敌机

Dijidiji=dijis.get(j);

Rectangler2=diji.getRectangle();

if(r1.intersects(r2)){

mzds.remove(i);

dijis.remove(j);

count++;

}

}

}

}

booleanisFire1=false;

booleanisFire2=false;

publicvoidkeyPressed(KeyEvente){

plane.keyPressed(e);

if(e.getKeyCode()==KeyEvent.VK_J){

isFire1=true;

}

/*if(e.getKeyCode()==KeyEvent.VK_K){

isFire2=true;

}*/

}

publicvoidkeyReleased(KeyEvente){

plane.keyReleased(e);

if(e.getKeyCode()==KeyEvent.VK_J){

isFire1=false;

fireTime=0;

}

/*if(e.getKeyCode()==KeyEvent.VK_K){

isFire2=false;

fireTime=0;

}*/

if(e.getKeyCode()==KeyEvent.VK_P){

for(inti=0;i

dijis.remove(i);

}

}

}

packagecom;

import

import

import

import

import

import

import

//游戏结束

publicclassOver{

Imageobg=null;

Imageng=null;

publicOver(){

try{

obg=

ng=

}catch(IOExceptione){

e.printStackTrace();

}

}

publicvoiddraw(Graphicsg){

g.drawImage(obg,0,0,null);

g.drawImage(ng,120,100,null);

g.setColor(Color.black);

g.setFont(newFont("幼圆",Font.BOLD,40));

g.drawString("游戏结束啦!

",200,600);

}

publicvoidkeyPressed(KeyEvente){

}

publicvoidkeyReleased(KeyEvente){

}

}

packagecom;

import

import

import

import

import

import

publicclassMainFrameextendsJFrame{

Beforebefore=newBefore();

Play01play01=newPlay01();

Overover=newOver();

publicMainFrame(){

//设置标题

this.setTitle("让子弹飞一会~");

//设置大小

this.setSize(600,800);

//定位居中

this.setLocationRelativeTo(null);

//添加关闭操作

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//禁止重置大小z

this.setResizable(false);

//添加键盘监听器

//KeyListener-->KeyAdapter

this.addKeyListener(newKeyAdapter(){

@Override

publicvoidkeyPressed(KeyEvente){

if(PlaneStatus.playStatus==0){

before.keyPressed(e);

}elseif(PlaneStatus.playStatus==1){

play01.keyPressed(e);

}elseif(PlaneStatus.playStatus==-1){

over.keyPressed(e);

}

}

@Override

publicvoidkeyReleased(KeyEvente){

if(PlaneStatus.playStatus==0){

before.keyReleased(e);

}elseif(PlaneStatus.playStatus==1){

play01.keyReleased(e);

}elseif(PlaneStatus.playStatus==-1){

over.keyReleased(e);

}

}

});

//启动一个线程:

每隔20毫秒执行一次

newThread(){

publicvoidrun(){

while(true){

try{

Thread.sleep(20);

}catch(InterruptedExceptione){

e.printStackTrace();

}

}

}

}.start();

//显示

this.setVisible(true);

}

ImagebufferImage=null;

//这个方法也是20毫秒执行一次

publicvoidpaint(Graphicsg){

if(bufferImage==null){

bufferImage=this.createImage(600,800);

}

Graphicsg4Image=bufferImage.getGraphics();

g4Image.setColor(Color.BLACK);

g4Image.fillRect(0,0,600,800);

this.draw(g4Image);

g.drawImage(bufferImage,0,0,null);

}

publicvoiddraw(Graphicsg){

if(PlaneStatus.playStatus==0){

before.draw(g);

}elseif(PlaneStatus.playStatus==1){

play01.draw(g);

}elseif(PlaneStatus.playStatus==-1){

over.draw(g);

}

}

publicstaticvoidmain(String[]args){

newMainFrame();

}

}

packagecom;

import

import

import

import

import

publicclassMyZiDan{

ImagezidanImage=null;

intx;

inty;

publicMyZiDan(intx,inty){

this.x=x;

this.y=y;

try{

zidanImage=

.getResourceAsStream("images/zidan.png"));

}catch(IOExceptione){

e.printStackTrace();

}

}

publicvoiddraw(Graphicsg){

//画我军子弹图片

g.drawImage(zidanImage,x,y,null);

//我的飞机子弹移动速度

y-=20;

}

publicRectanglegetRectangle(){

returnnewRectangle(x,y,zidanImage.getWidth(null),zidanImage.getHeight(null));

}

}

packagecom;

import

import

import

import

import

import

publicclassDiji{

ImagedijiImage=null;

inty=-20;

intx=10;//20~760

intr;

publicDiji(

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高中教育 > 高中教育

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1