java飞机大战源代码
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
// 放开
public voidkeyReleased(KeyEvente) {
intkeyCode=e.getKeyCode();
if (keyCode==KeyEvent.VK_RIGHT||keyCode==KeyEvent.VK_D) {
isRight=false;
} else if (keyCode==KeyEvent.VK_LEFT||keyCode==KeyEvent.VK_A) {
}
package com;
importjava.awt.Graphics;
importjava.awt.Image;
importjava.awt.Rectangle;
importjava.awt.event.KeyEvent;
importjava.util.ArrayList; importjava.util.List;
.getResourceAsStream("images/feiji.png"));
} catቤተ መጻሕፍቲ ባይዱh (IOExceptione) {
e.printStackTrace();
}
}
public void draw(Graphics g) {
//画飞机图片
g.drawImage(feijiImage, x, y, null);
bgY1 = 0; }g.drawImage(bgImage, 0, bgY2, null); bgY2 += 5; if (bgY2 >= 0) { bgY2 = -600; } // 清理战机子弹 for (inti= 0;i<mzds.size();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<mzds.size();i++) {MyZiDanmyZidan=mzds.get(i);myZidan.draw(g); } // 清理敌机 for (inti= 0;i<dijis.size();i++) {Dijidj=dijis.get(i); if (dj.x< -100) {dijis.remove(i); } } // 抽奖:添加敌机
// 飞机移动
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.drawImage(PowerImage, x, y, null);
//血瓶移动
y++;
}
public RectanglegetRectangle(){
return new Rectangle(x,y,PowerImage.getWidth(null),PowerImage.getHeight(null)); }
importjavax.imageio.ImageIO;
public class Play01 {
staticintcount=0;
ImagebgImage= null;
//战机Planeplane= new Plane(); //战机子弹List<MyZiDan>mzds= newArrayList<MyZiDan>(); //敌机List<Diji>dijis= newArrayList<Diji>(); //敌机子弹//List<DijiZiDan>dijizidans= newArrayList<DijiZiDan>(); //血瓶List<Gift> gifts = newArrayList<Gift>(); List<Power> powers = newArrayList<Power>(); public Play01() { try {bgImage=ImageIO.read(Play01.class.getClassLoader() .getResourceAsStream("images/bg_01.jpg")); } catch (IOExceptione) {e.printStackTrace(); } }intbgY1 = 0;intbgY2 = -600;intfireTime= 0;booleanflag=false; public void draw(Graphics g) { //画背景图片g.drawImage(bgImage, 0,bgY1, null); bgY1 += 5;
try {
PowerImage=ImageIO.read(Gift.class.getClassLoader()
.getResourceAsStream("images/power.png"));
} catch (IOExceptione) {
e.printStackTrace();
}
}
public void draw(Graphics g) {
importjava.io.IOException;
importjavax.imageio.ImageIO;
public class Power {
ImagePowerImage= null;
intx = 20;
inty = 0;
public Power() {
this.x=(int)(Math.random()*540)+20;
dijis.add(newDiji()); } // 抽奖:添加血瓶if ((int) (Math.random() * 1000) == 5) {gifts.add(new Gift()); } for (inti= 0;i<gifts.size();i++) { Giftgift=gifts.get(i);gift.draw(g); } // // 抽奖:添加power if ((int) (Math.random() * 1000) == 5) {powers.add(new Power()); } for (inti= 0;i<powers.size();i++) { Powerpower=powers.get(i);power.draw(g); } // 画敌机 for (inti= 0;i<dijis.size();i++) {Dijidj=dijis.get(i);dj.draw(g); } //画飞机plane.draw(g); // 判断战机相撞 for (inti= 0;i<dijis.size();i++) { // 先得到每一个敌机Dijidj=dijis.get(i); Rectangle r1 =dj.getRectangle(); Rectangle r2 =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; }
importjavax.imageio.ImageIO;
public class Plane {
ImagefeijiImage= null;
intx = 300;
inty = 700;
intlifeCount=5;
public Plane() {
try {
feijiImage=ImageIO.read(Plane.class.getClassLoader()
}
}
public RectanglegetRectangle(){
return new Rectangle(x,y,feijiImage.getWidth(null),feijiImage.getHeight(null)); }
}
package com;
public classPlaneStatus{
packagecom;;importjava.awt.Color;;importjava.awt.Font;;importjava.awt.Graphics;;importjava.awt.Image;;importjava.awt.Rectangle;importjava.awt.event.Key;importjava.io.IOExceptio;imppackagecom;
} if (keyCode==KeyEvent.VK_RIGHT||keyCode==KeyEvent.VK_D) {isRight=true; } else if (keyCode==KeyEvent.VK_LEFT||keyCode==KeyEvent.VK_A) {isLeft=true; } else if (keyCode==KeyEvent.VK_UP||keyCode==KeyEvent.VK_W) {isUP=true; } else if (keyCode==KeyEvent.VK_DOWN||keyCode==KeyEvent.VK_S) {isDown=true; }
importjava.awt.Color;
importjava.awt.Font;
importjava.awt.Graphics;
importjava.awt.Image;
importjava.awt.Rectangle;
importjava.awt.event.KeyEvent;
importjava.io.IOException;
下
y=y+5;
}else if(!isUP&& !isDown&&isLeft&& !isRight){
左
x=x-5;
}else if(!isUP&& !isDown&& !isLeft&&isRight){
右
x=x+5;
}else if(isUP&& !isDown&&isLeft&& !isRight){
bgY1=0;}g.drawImage(bgIm;dijis.add(newDiji());}//;}//判断战机和血瓶相撞for(inti=0;i;}}booleanisFire1=false; b;packagecom;;importjava.awt.Color;;importjava.awt.Font;;importjava.awt.Graphics;;
左上
x=x-5;
y=y-5;
}else if(!isUP&&isDown&&isLeft&& !isRight){
左下
x=x-5;
y=y+5;
}else if(isUP&& !isDown&& !isLeft&&isRight){
右上
x=x+5;
y=y-5;
}else if(!isUP&&isDown&& !isLeft&&isRight){
右下
x=x+5;
y=y+5;
} // // // // // // //
}booleanisUP= false;booleanisDown= false;booleanisLeft= false;booleanisRight= false; // 摁下 public voidkeyPressed(KeyEvente) {
public staticintroleNum= 1;
public staticintplayStatus= 0;// 0 游戏开始前 1 第一关 -1 游戏结束
}
package com;
importjava.awt.Graphics;
importjava.awt.Image;
importjava.awt.event.KeyEvent;
isLeft=false;
} else if (keyCode==KeyEvent.VK_UP||keyCode==KeyEvent.VK_W) {
isUP=false;
} else if (keyCode==KeyEvent.VK_DOWN||keyCode==KeyEvent.VK_S) {isDown=false;
g.setColor(Color.blue);
g.setFont(new Font("幼圆",Font.BOLD, 30));
g.drawString("Score:"+Play01.count, 20, 60);
}
}
//
public void move(){ if(isUP&& !isDown&& !isLeft&& !isRight){ 上 y=y-5; }else if(!isUP&&isDown&& !isLeft&& !isRight){
public voidkeyReleased(KeyEvente) {
intkeyCode=e.getKeyCode();
if (keyCode==KeyEvent.VK_RIGHT||keyCode==KeyEvent.VK_D) {
isRight=false;
} else if (keyCode==KeyEvent.VK_LEFT||keyCode==KeyEvent.VK_A) {
}
package com;
importjava.awt.Graphics;
importjava.awt.Image;
importjava.awt.Rectangle;
importjava.awt.event.KeyEvent;
importjava.util.ArrayList; importjava.util.List;
.getResourceAsStream("images/feiji.png"));
} catቤተ መጻሕፍቲ ባይዱh (IOExceptione) {
e.printStackTrace();
}
}
public void draw(Graphics g) {
//画飞机图片
g.drawImage(feijiImage, x, y, null);
bgY1 = 0; }g.drawImage(bgImage, 0, bgY2, null); bgY2 += 5; if (bgY2 >= 0) { bgY2 = -600; } // 清理战机子弹 for (inti= 0;i<mzds.size();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<mzds.size();i++) {MyZiDanmyZidan=mzds.get(i);myZidan.draw(g); } // 清理敌机 for (inti= 0;i<dijis.size();i++) {Dijidj=dijis.get(i); if (dj.x< -100) {dijis.remove(i); } } // 抽奖:添加敌机
// 飞机移动
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.drawImage(PowerImage, x, y, null);
//血瓶移动
y++;
}
public RectanglegetRectangle(){
return new Rectangle(x,y,PowerImage.getWidth(null),PowerImage.getHeight(null)); }
importjavax.imageio.ImageIO;
public class Play01 {
staticintcount=0;
ImagebgImage= null;
//战机Planeplane= new Plane(); //战机子弹List<MyZiDan>mzds= newArrayList<MyZiDan>(); //敌机List<Diji>dijis= newArrayList<Diji>(); //敌机子弹//List<DijiZiDan>dijizidans= newArrayList<DijiZiDan>(); //血瓶List<Gift> gifts = newArrayList<Gift>(); List<Power> powers = newArrayList<Power>(); public Play01() { try {bgImage=ImageIO.read(Play01.class.getClassLoader() .getResourceAsStream("images/bg_01.jpg")); } catch (IOExceptione) {e.printStackTrace(); } }intbgY1 = 0;intbgY2 = -600;intfireTime= 0;booleanflag=false; public void draw(Graphics g) { //画背景图片g.drawImage(bgImage, 0,bgY1, null); bgY1 += 5;
try {
PowerImage=ImageIO.read(Gift.class.getClassLoader()
.getResourceAsStream("images/power.png"));
} catch (IOExceptione) {
e.printStackTrace();
}
}
public void draw(Graphics g) {
importjava.io.IOException;
importjavax.imageio.ImageIO;
public class Power {
ImagePowerImage= null;
intx = 20;
inty = 0;
public Power() {
this.x=(int)(Math.random()*540)+20;
dijis.add(newDiji()); } // 抽奖:添加血瓶if ((int) (Math.random() * 1000) == 5) {gifts.add(new Gift()); } for (inti= 0;i<gifts.size();i++) { Giftgift=gifts.get(i);gift.draw(g); } // // 抽奖:添加power if ((int) (Math.random() * 1000) == 5) {powers.add(new Power()); } for (inti= 0;i<powers.size();i++) { Powerpower=powers.get(i);power.draw(g); } // 画敌机 for (inti= 0;i<dijis.size();i++) {Dijidj=dijis.get(i);dj.draw(g); } //画飞机plane.draw(g); // 判断战机相撞 for (inti= 0;i<dijis.size();i++) { // 先得到每一个敌机Dijidj=dijis.get(i); Rectangle r1 =dj.getRectangle(); Rectangle r2 =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; }
importjavax.imageio.ImageIO;
public class Plane {
ImagefeijiImage= null;
intx = 300;
inty = 700;
intlifeCount=5;
public Plane() {
try {
feijiImage=ImageIO.read(Plane.class.getClassLoader()
}
}
public RectanglegetRectangle(){
return new Rectangle(x,y,feijiImage.getWidth(null),feijiImage.getHeight(null)); }
}
package com;
public classPlaneStatus{
packagecom;;importjava.awt.Color;;importjava.awt.Font;;importjava.awt.Graphics;;importjava.awt.Image;;importjava.awt.Rectangle;importjava.awt.event.Key;importjava.io.IOExceptio;imppackagecom;
} if (keyCode==KeyEvent.VK_RIGHT||keyCode==KeyEvent.VK_D) {isRight=true; } else if (keyCode==KeyEvent.VK_LEFT||keyCode==KeyEvent.VK_A) {isLeft=true; } else if (keyCode==KeyEvent.VK_UP||keyCode==KeyEvent.VK_W) {isUP=true; } else if (keyCode==KeyEvent.VK_DOWN||keyCode==KeyEvent.VK_S) {isDown=true; }
importjava.awt.Color;
importjava.awt.Font;
importjava.awt.Graphics;
importjava.awt.Image;
importjava.awt.Rectangle;
importjava.awt.event.KeyEvent;
importjava.io.IOException;
下
y=y+5;
}else if(!isUP&& !isDown&&isLeft&& !isRight){
左
x=x-5;
}else if(!isUP&& !isDown&& !isLeft&&isRight){
右
x=x+5;
}else if(isUP&& !isDown&&isLeft&& !isRight){
bgY1=0;}g.drawImage(bgIm;dijis.add(newDiji());}//;}//判断战机和血瓶相撞for(inti=0;i;}}booleanisFire1=false; b;packagecom;;importjava.awt.Color;;importjava.awt.Font;;importjava.awt.Graphics;;
左上
x=x-5;
y=y-5;
}else if(!isUP&&isDown&&isLeft&& !isRight){
左下
x=x-5;
y=y+5;
}else if(isUP&& !isDown&& !isLeft&&isRight){
右上
x=x+5;
y=y-5;
}else if(!isUP&&isDown&& !isLeft&&isRight){
右下
x=x+5;
y=y+5;
} // // // // // // //
}booleanisUP= false;booleanisDown= false;booleanisLeft= false;booleanisRight= false; // 摁下 public voidkeyPressed(KeyEvente) {
public staticintroleNum= 1;
public staticintplayStatus= 0;// 0 游戏开始前 1 第一关 -1 游戏结束
}
package com;
importjava.awt.Graphics;
importjava.awt.Image;
importjava.awt.event.KeyEvent;
isLeft=false;
} else if (keyCode==KeyEvent.VK_UP||keyCode==KeyEvent.VK_W) {
isUP=false;
} else if (keyCode==KeyEvent.VK_DOWN||keyCode==KeyEvent.VK_S) {isDown=false;
g.setColor(Color.blue);
g.setFont(new Font("幼圆",Font.BOLD, 30));
g.drawString("Score:"+Play01.count, 20, 60);
}
}
//
public void move(){ if(isUP&& !isDown&& !isLeft&& !isRight){ 上 y=y-5; }else if(!isUP&&isDown&& !isLeft&& !isRight){