1. 程式人生 > >java版飛機大戰程式碼

java版飛機大戰程式碼

java版飛機大戰程式碼

前言

很久之前整理的小東西,這個是從我個人部落格網站拷過來的地址
下面是原始碼

Plane

packagecom;;
importjava.awt.Color;;
importjava.awt.Font;;
importjava.awt.Graphics;;
importjava.awt.Image;;
importjava.awt.Rectangle;
importjava.
awt.event.Key; importjava.io.IOExceptio;imppackage com; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Image; import java.awt.Rectangle; import java.awt.event.KeyEvent; import java.io.IOException; import javax.imageio.ImageIO; public class Plane { Image feijiImage =
null; int x = 300; int y = 700; int lifeCount=5; public Plane() { try { feijiImage = ImageIO.read(Plane.class.getClassLoader() .getResourceAsStream("images/feiji.png")); } catch (IOException e) { e.printStackTrace(); } } public void draw(Graphics g) { // 畫飛機圖片 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(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){ 下 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){ 左上 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; } // // // // // // // } boolean isUP = false; boolean isDown = false; boolean isLeft = false; boolean isRight = false; // 摁下 public void keyPressed(KeyEvent e) { } 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; } // 放開 public void keyReleased(KeyEvent e) { int keyCode = e.getKeyCode(); if (keyCode == KeyEvent.VK_RIGHT || keyCode == KeyEvent.VK_D) { isRight=false; } else if (keyCode == KeyEvent.VK_LEFT || keyCode == KeyEvent.VK_A) { 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; } } public Rectangle getRectangle(){ return new Rectangle(x,y,feijiImage.getWidth(null),feijiImage.getHeight(null)); } }

PlaneStatus類

package com;
public class PlaneStatus {
public static int roleNum = 1;
public static int playStatus = 0;// 0 遊戲開始前 1 第一關 -1 遊戲結束
}

Power類

package com;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Power {
Image PowerImage = null;
int x = 20;
int y = 0;
public Power() {
this.x=(int)(Math.random()*540)+20;
try {
PowerImage = ImageIO.read(Gift.class.getClassLoader()
.getResourceAsStream("images/power.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
public void draw(Graphics g) {
// 畫血瓶圖片
g.drawImage(PowerImage, x, y, null);
// 血瓶移動
y++;
}
public Rectangle getRectangle(){
return new Rectangle(x,y,PowerImage.getWidth(null),PowerImage.getHeight(null)); }
}

play01類

package com;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.util.ArrayList; import java.util.List;
import javax.imageio.ImageIO;
public class Play01 {
static int count=0;
Image bgImage = null;
// 戰機 Plane plane = new Plane(); // 戰機子彈 List<MyZiDan> mzds = new ArrayList<MyZiDan>(); // 敵機 List<Diji> dijis = new ArrayList<Diji>(); // 敵機子彈 //List<DijiZiDan> dijizidans = new ArrayList<DijiZiDan>(); // 血瓶 List<Gift> gifts = new ArrayList<Gift>(); List<Power> powers = new ArrayList<Power>(); public Play01() { try { bgImage = ImageIO.read(Play01.class.getClassLoader() .getResourceAsStream("images/bg_01.jpg")); } catch (IOException e) { e.printStackTrace(); } } int bgY1 = 0; int bgY2 = -600; int fireTime = 0; boolean flag=false; public void draw(Graphics g) { // 畫背景圖片 g.drawImage(bgImage, 0, bgY1, null); bgY1 += 5; 


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;;


bgY1 = 0; } g.drawImage(bgImage, 0, bgY2, null); bgY2 += 5; if (bgY2 >= 0) { bgY2 = -600; } // 清理戰機子彈 for (int i = 0; i < mzds.size(); i++) { MyZiDan myZidan = mzds.get(i); if (myZidan.x > 800) { mzds.remove(i); } } // 新增子彈 if (isFire1 == true && flag==false) { if (fireTime % 8 == 0) { mzds.add(new MyZiDan(plane.x + 25, plane.y + 0)); } fireTime++; } if (isFire1== true && flag==true) { if (fireTime % 4 == 0) { } mzds.add(new MyZiDan(plane.x +8, plane.y +0)); mzds.add(new MyZiDan(plane.x +52, plane.y +0)); } fireTime++; // 畫戰機子彈 for (int i = 0; i < mzds.size(); i++) { MyZiDan myZidan = mzds.get(i); myZidan.draw(g); } // 清理敵機 for (int i = 0; i < dijis.size(); i++) { Diji dj = dijis.get(i); if (dj.x < -100) { dijis.remove(i); } } // 抽獎:新增敵機
dijis.add(new Diji()); } // 抽獎:新增血瓶 if ((int) (Math.random() * 1000) == 5) { gifts.add(new Gift()); } for (int i = 0; i < gifts.size(); i++) { Gift gift = gifts.get(i); gift.draw(g); } // // 抽獎:新增power if ((int) (Math.random() * 1000) == 5) { powers.add(new Power()); } for (int i = 0; i < powers.size(); i++) { Power power = powers.get(i); power.draw(g); } // 畫敵機 for (int i = 0; i < dijis.size(); i++) { Diji dj = dijis.get(i); dj.draw(g); } // 畫飛機 plane.draw(g); // 判斷戰機相撞 for (int i = 0; i < dijis.size(); i++) { // 先得到每一個敵機 Diji dj = 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; }
} // 判斷戰機和血瓶相撞 for (int i = 0; i < gifts.size(); i++) { } // 先得到每一個血瓶 Gift gift = gifts.get(i); Rectangle r1 = gift.getRectangle(); Rectangle r2 = plane.getRectangle(); if (r1.intersects(r2)) { gifts.remove(i); } if (plane.lifeCount <5) { } plane.lifeCount = plane.lifeCount + 1; //// 判斷戰機和Power相撞 for (int i = 0; i < powers.size(); i++) { // 先得到每一個power Power power = powers.get(i); Rectangle r1 = power.getRectangle(); Rectangle r2 = plane.getRectangle(); if (r1.intersects(r2)) { powers.remove(i); flag=true; } } // 判斷敵機在中彈 for (int i = 0; i < mzds.size(); i++) { // 得到每個戰機子彈 MyZiDan myzidan = mzds.get(i); Rectangle r1 = myzidan.getRectangle(); for (int j = 0; j < dijis.size(); j++) { // 每一個敵機 Diji diji = dijis.get(j); Rectangle r2 = diji.getRectangle(); if (r1.intersects(r2)) { mzds.remove(i); dijis.remove(j); count++; }
}
} } boolean isFire1 = false; boolean isFire2 = false; public void keyPressed(KeyEvent e) { plane.keyPressed(e); if (e.getKeyCode() == KeyEvent.VK_J) { isFire1 = true; } /*if (e.getKeyCode() == KeyEvent.VK_K) { isFire2 = true; }*/ } public void keyReleased(KeyEvent e) { 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(int i=0;i<dijis.size();i++) dijis.remove(i); } }

over類

package com;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.io.IOException;
import javax.imageio.ImageIO;
// 遊戲結束
public class Over {
Image obg=null;
Image ng=null;
public Over() {
try {
obg
ImageIO.read(Before.class.getClassLoader().getResourceAsStream("images/obg.jpg")); ng
ImageIO.read(Before.class.getClassLoader().getResourceAsStream("images/ng.png")); } catch (IOException e) {
e.printStackTrace();
}
}
public void draw(Graphics g){
g.drawImage(obg, 0, 0, null);
g.drawImage(ng, 120,100, null);
g.setColor(Color.black);
g.setFont(new Font("幼圓", Font.BOLD, 40));
g.drawString("遊戲結束啦!",200 ,600 );
}
public void keyPressed(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
}
}

MainFrame主類

package com;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image; = =
importjava.awt.event.Key;importjava.awt.event.Key;importjavax.swing.JFrame;publicclassMainFrameexte;Beforebefore=newBefore();Play01play01=newPlay01();Overover=newOver();;publicMain


import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
public class MainFrame extends JFrame {
Before before =new Before();
Play01 play01 = new Play01();
Over over = new Over();
public MainFrame() {
//
//
//
//
//
//
//
設 置標題 this.setTitle(" 讓子彈飛一會~"); 設定大小 this.setSize(600, 800); 定位居中 this.setLocationRelativeTo(null); 新增關閉操作 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 禁止重置大小z this.setResizable(false); 新增鍵盤監聽器 KeyListener --> KeyAdapter this.addKeyListener(new KeyAdapter(){ @Override public void keyPressed(KeyEvent e) { if(PlaneStatus.playStatus == 0){ before.keyPressed(e); }else if(PlaneStatus.playStatus ==1){ play01.keyPressed(e); }else if(PlaneStatus.playStatus==-1){ over.keyPressed(e); } } @Override public void keyReleased(KeyEvent e) { if(PlaneStatus.playStatus == 0){ before.keyReleased(e); }else if(PlaneStatus.playStatus ==1){ play01.keyReleased(e); }else if(PlaneStatus.playStatus==-1){ over.keyReleased(e); } } });
//
//
//
啟 動一個執行緒:每隔20 毫秒執行一次 new Thread(){ public void run(){ while(true){ MainFrame.this.repaint(); try { Thread.sleep(20); } catch (InterruptedException e) { e.printStackTrace(); } } } }.start(); 顯示 this.setVisible(true); } Image bufferImage =null; 這個方法也是20毫秒執行一次 public void paint(Graphics g){ if(bufferImage==null){ bufferImage = this.createImage(600, 800); } Graphics g4Image = bufferImage.getGraphics(); g4Image.setColor(Color.BLACK); g4Image.fillRect(0, 0, 600, 800); this.draw(g4Image); g.drawImage(bufferImage, 0, 0, null); } public void draw(Graphics g){ if<