飞机大战java_Java飞机大战
FlyingObject,作為飛行物的父類,這里的飛行物指的就是敵機,小蜜蜂,子彈,英雄機
package com.tarena.shoot;
import java.awt.image.BufferedImage;
//飛行物類
public abstract class FlyingObject {
protected BufferedImage image; //圖片
protected int width; //寬
protected int height; //高
protected int x; //坐標
protected int y;
// 飛行物走一步
public abstract void step();
//檢查是否出界,,返回true表示已越界
public abstract boolean outOfBounds();
public boolean shootBy(Bullet b) {
return false;
};
}
敵機類,圖片取自ShootGame的入口類(在下面),打掉一個敵機得五分,敵機只能向下走
圖片在主類 ShootGame中靜態(tài)引入了
package com.tarena.shoot;
import java.util.Random;
//敵機
public class Airplane extends FlyingObject implements Enemy{
private int speed = 2; //走的步數(shù)
public Airplane() { // 全部都在構造方法中初始化數(shù)據
image = ShootGame.airplane;
width = image.getWidth(); //獲取圖片的寬
height = image.getHeight(); //獲取圖片的高
Random rand = new Random(); //隨機數(shù)對象
x = rand.nextInt(ShootGame.WIDTH-this.width+1);
y = -this.height;
}
public int getScore() {
return 5; //打掉一個敵機得5分
}
// 重寫
public void step() {
y+=speed;
}
// 重寫是否越界函數(shù)---越界刪除該對象
public boolean outOfBounds() {
if(y>ShootGame.HEIGHT) {
return true;
}else {
return false;
}
}
// 敵人被子彈射擊----也就是檢測圖片與圖片之間檢測碰撞
public boolean shootBy(Bullet bullet) {
int x1 = this.x;
int x2 = this.x + this.width;
int y1 = this.y;
int y2 = this.y + this.height;
// x在 x1 和 x2 之間,y在y1和y2之間,既是碰撞了
if(bullet.xx1 && bullet.yy1) {
return true;
}else {
return false;
}
}
}
Bee,小蜜蜂類,也是敵人的一種,和上面敵機不同的是,小蜜蜂還會橫著走,碰到邊界反彈,打掉一個小蜜蜂還會獲得獎勵,也就是繼承的接口 Award
package com.tarena.shoot;
import java.util.Random;
public class Bee extends FlyingObject implements Award{
private int xSpeed = 1; //X坐標速度
private int ySpeed = 2; //Y坐標速度
private int awardType; //獎勵的類型 0/1
public Bee() {
image = ShootGame.bee;
width = image.getWidth();
height = image.getHeight();
Random rand = new Random();
x = rand.nextInt(ShootGame.WIDTH - this.width+1);
y = -this.height;
awardType = rand.nextInt(2); //0到1之間
int xSymbol = rand.nextInt(2);
if(xSymbol == 0) {
xSpeed = -xSpeed;
}
}
public int getType() {
return awardType; //返回獎勵類型 0/1 在ShootGame 中根據0、1判斷獎勵的類型分別調用方法
}
// 重寫
public void step() {
y+=ySpeed;
x+=xSpeed;
if(x>ShootGame.WIDTH-this.width || x<=0) {
xSpeed = -xSpeed;
}
}
// 重寫是否越界函數(shù)
public boolean outOfBounds() {
if(y>ShootGame.HEIGHT) {
return true;
}else {
return false;
}
}
// 敵人被子彈射擊
public boolean shootBy(Bullet bullet) {
int x1 = this.x;
int x2 = this.x + this.width;
int y1 = this.y;
int y2 = this.y + this.height;
// x在 x1 和 x2 之間,y在y1和y2之間,既是碰撞了
if(bullet.xx1 && bullet.yy1) {
return true;
}else {
return false;
}
}
}
Award,,獎勵,被小蜜蜂繼承
package com.tarena.shoot;
public interface Award {
public int DOUBLE_FILE = 0; //火力值
public int LIFE = 1; //命
// 獲取獎勵
public int getType();
}
Enemy類,作為敵人(敵機+小蜜蜂)的公共接口,得分
package com.tarena.shoot;
public interface Enemy {
// 得分
public int getScore();
}
主角英雄機,Hero,有動態(tài)效果,兩張圖片來回切換
package com.tarena.shoot;
import java.awt.image.BufferedImage;
//英雄機
public class Hero extends FlyingObject {
private int life; //命
private int doubleFire=0; //火力值
private BufferedImage[] images; //兩張圖片切換
private int index; //協(xié)助圖片切換
public Hero() {
image = ShootGame.hero0;
width = image.getWidth(); //獲取圖片的寬
height = image.getHeight(); //獲取圖片的高
x = 150; //x:初始值
y = 400; //y:初始值
life = 3; //默認3條命
images = new BufferedImage[] {ShootGame.hero0,ShootGame.hero1};
// 初始化images,兩張圖片
index = 0; //協(xié)助切換
}
// 重寫
public void step() {
index++;
image = images[index/10%images.length]; //每100個毫秒切換一次
}
// 英雄機發(fā)射子彈
public Bullet[] shoot() {
int xStep = this.width/4;
int yStep = 20;
if(doubleFire > 0) {
Bullet[] bs = new Bullet[2];
bs[0] = new Bullet(this.x+1*xStep,this.y-yStep);
bs[1] = new Bullet(this.x+3*xStep,this.y-yStep);
doubleFire -=2; //發(fā)射一次雙倍火力減2
return bs;
}else {
Bullet[] bs = new Bullet[1];
bs[0] = new Bullet(this.x+2*xStep,this.y-yStep);
return bs;
}
}
// 英雄機隨著鼠標移動
public void moveTo(int x, int y) {
this.x = x - this.width/2; //使得英雄機中心對準在鼠標
this.y = y - this.height/2;
}
// 重寫是否越界函數(shù)
public boolean outOfBounds() {
return false; //永不越界
}
// 加命
public void addLife() {
life++;
}
// 返回命
public int getLife() {
return life;
}
public void reduceLife() {
life--;
}
// 加火力
public void addDoubleFire() {
doubleFire += 40;
}
public void zeroDoubleFire() {
doubleFire = 0;
}
// 英雄機撞敵人,,true表示碰撞
public boolean hit(FlyingObject obj) {
int x1 = this.x - this.width/2;
int x2 = this.x + this.width/2;
int y1 = this.y - this.height/2;
int y2 = this.y + this.height/2;
if(obj.xx1 && obj.yy1) {
return true;
}else {
return false;
}
}
}
Bullet類,子彈類,也是飛行物的一種,它的x,y都是跟隨英雄機
package com.tarena.shoot;
import java.util.Random;
public class Bullet extends FlyingObject{
private int speed = 3;
public Bullet(int x,int y) {
image = ShootGame.bullet;
width = image.getWidth(); //獲取圖片的寬
height = image.getHeight(); //獲取圖片的高
Random rand = new Random(); //隨機數(shù)對象
this.x = x; //x跟隨英雄機
this.y = y; //y跟隨英雄機
}
// 重寫
public void step() {
y-=speed;
}
// 重寫是否越界函數(shù)
public boolean outOfBounds() {
if(y
return true;
}else {
return false;
}
}
}
主類ShootGame,整個入口類,在這其中引入了所有的靜態(tài)資源(圖片),整個程序是用JFrame和JPanel來寫的
檢測鼠標事件,設定了一個定時器,畫面是由定時器不斷調用repaint方法刷新重畫的
package com.tarena.shoot;
import java.awt.Component;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Arrays;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.Color;
import java.awt.Font;
//主程序類
public class ShootGame extends JPanel {
public static final int WIDTH = 400; //窗口寬
public static final int HEIGHT = 654; //窗口高
public static final int START = 0; //啟動狀態(tài)
public static final int RUNNING = 1; //運行狀態(tài)
public static final int PAUSE = 2; //停止狀態(tài)
public static final int GAME_OVER = 3;//結束狀態(tài)
private int state = START; //當前狀態(tài)
public static BufferedImage background;
public static BufferedImage start;
public static BufferedImage pause;
public static BufferedImage gameover;
public static BufferedImage airplane;
public static BufferedImage bee;
public static BufferedImage bullet;
public static BufferedImage hero0;
public static BufferedImage hero1;
private Hero hero = new Hero();
private FlyingObject[] flyings = {}; //敵人數(shù)組
private Bullet[] bullets = {}; //子彈數(shù)組
static { //初始化靜態(tài)資源(圖片)
try {
background = ImageIO.read(ShootGame.class.getResource("background.png"));
start= ImageIO.read(ShootGame.class.getResource("start.png"));
pause = ImageIO.read(ShootGame.class.getResource("pause.png"));
gameover = ImageIO.read(ShootGame.class.getResource("gameover.png"));
airplane = ImageIO.read(ShootGame.class.getResource("airplane.png"));
bee = ImageIO.read(ShootGame.class.getResource("bee.png"));
bullet = ImageIO.read(ShootGame.class.getResource("bullet.png"));
hero0 = ImageIO.read(ShootGame.class.getResource("hero0.png"));
hero1 = ImageIO.read(ShootGame.class.getResource("hero1.png"));
}catch(Exception e) {
e.printStackTrace();
}
}
// 隨機生成敵人對象
public FlyingObject nextOne() {
Random rand = new Random();
int type = rand.nextInt(2);
if(type == 0) {
return new Bee();
}else {
return new Airplane();
}
}
// 敵人入場
int flyEnteredIndex = 0;
public void enterAction() {
//生成敵人對象,將對象添加到flyings數(shù)組中
flyEnteredIndex++;
if(flyEnteredIndex%40==0) {
FlyingObject one = nextOne();
flyings = Arrays.copyOf(flyings, flyings.length+1);
flyings[flyings.length-1] = one;
}
}
// 飛行物走一步
public void stepAction() {
hero.step();
for(int i=0; i
flyings[i].step();
}
for(int i=0; i
bullets[i].step();
}
}
// 子彈入場---英雄機發(fā)射子彈
int shootIndex = 0;
public void shootAction() {
shootIndex++;
if(shootIndex%30 == 0) {
Bullet[] bs = hero.shoot();
// System.arraycopy(bs,0, bullets, bullets.length-bs.length, bs.length);
bullets = Arrays.copyOf(bullets, bullets.length+bs.length);
for(int i=0; i
bullets[bullets.length-bs.length+i] = bs[i];
}
}
}
// 刪除越界的敵人,,敵機,小蜜蜂,子彈
public void outOfBoundsAction() {
// 刪除越界的敵機,小蜜蜂
int index = 0;
FlyingObject[] flyingLives = new FlyingObject[flyings.length];
for(int i=0; i
if(!flyings[i].outOfBounds()) {
flyingLives[index] = flyings[i];
index++;
}
}
flyings = Arrays.copyOf(flyingLives, index);
// 刪除越界的子彈
index = 0;
Bullet[] bulletLives = new Bullet[bullets.length];
for(int i=0; i
if(!bullets[i].outOfBounds()) {
bulletLives[index] = bullets[i];
index++;
}
}
bullets = Arrays.copyOf(bulletLives, index);
}
// 所有子彈與所有敵人(敵機+小蜜蜂)的碰撞
public void bangAction() {
for(int a=0; a
bang(a);
}
}
// 一個子彈與所有敵人的碰撞
int score = 0;
public void bang(int a) {
Bullet b = bullets[a];
int bangPoint = -1;
for(int i=0; i
if(flyings[i].shootBy(b)) {
bangPoint = i;
break;
}
}
if(bangPoint != -1) {
FlyingObject obj = flyings[bangPoint];
if(obj instanceof Enemy) {
Enemy one = (Enemy)obj;
score += one.getScore();
}
if(obj instanceof Award) {
Award two = (Award)obj;
int type = two.getType();
switch(type) {
case Award.DOUBLE_FILE:
hero.addDoubleFire();
break;
case Award.LIFE:
hero.addLife();
break;
}
}
FlyingObject l = flyings[flyings.length-1]; //最后一個敵人(敵機,小蜜蜂)
flyings[bangPoint] = l; //放到被撞物體的位置上
flyings = Arrays.copyOf(flyings, flyings.length-1); //直接縮容
Bullet ll = bullets[bullets.length-1]; //最后一個子彈
bullets[a] = ll; //放到被撞子彈的位置上
bullets = Arrays.copyOf(bullets, bullets.length-1); //直接縮容
}
}
// 檢查游戲是否結束
public void checkGameOverAction() {
for(int i=0; i
if(hero.hit(flyings[i])) {
//碰撞
hero.reduceLife();
hero.zeroDoubleFire();
if(hero.getLife() <= 0) {
state = GAME_OVER;
}
FlyingObject l = flyings[flyings.length-1]; //最后一個敵人(敵機,小蜜蜂)
flyings[i] = l; //放到被撞物體的位置上
flyings = Arrays.copyOf(flyings, flyings.length-1); //直接縮容
}
}
}
int num=0;
public void action() {
MouseAdapter l = new MouseAdapter() {
// 鼠標移動事件
public void mouseMoved(MouseEvent e) {
if(state == RUNNING) {
hero.moveTo(e.getX(), e.getY());
}
}
// 鼠標點擊事件
public void mouseClicked(MouseEvent e) {
switch(state) { //根據不同的狀態(tài)做不同的處理
case START: //啟動狀態(tài)時變?yōu)檫\行狀態(tài)
state = RUNNING;
break;
case GAME_OVER: //游戲結束狀態(tài)時變?yōu)閱訝顟B(tài)
score = 0; //清空數(shù)據
hero = new Hero();
flyings = new FlyingObject[0];
bullets = new Bullet[0];
state = START;
break;
}
}
//鼠標移入事件
public void mouseEntered(MouseEvent e) {
if(state == PAUSE) {
state = RUNNING;
}
}
//鼠標移出事件
public void mouseExited(MouseEvent e) {
if(state == RUNNING) {
state = PAUSE;
}
}
};
this.addMouseListener(l); //處理鼠標操作事件
this.addMouseMotionListener(l); //處理鼠標滑動事件
Timer timer = new Timer(); //創(chuàng)建定時器對象
int interval = 10; //時間間隔 (以毫秒為單位)
timer.schedule(new TimerTask() {
public void run() {
if(state == RUNNING) {
enterAction();
stepAction();
shootAction();
outOfBoundsAction(); //刪除越界的敵人
bangAction(); //子彈和敵人碰撞
}
checkGameOverAction(); //英雄機和敵人的碰撞
repaint();
}
}, interval,interval);
}
// 重寫paint() g:畫筆
public void paint(Graphics g) {
g.drawImage(background,0, 0, null);
paintHero(g);
paintFlyingObjects(g);
paintBullets(g);
paintScoreandLife(g);
paintState(g); //畫狀態(tài)
}
//畫英雄機對象
public void paintHero(Graphics g) {
g.drawImage(hero.image, hero.x, hero.y,null);
}
//畫敵人對象
public void paintFlyingObjects(Graphics g) {
for(int i=0; i
g.drawImage(flyings[i].image,flyings[i].x,flyings[i].y,null);
}
}
//畫子彈對象
public void paintBullets(Graphics g) {
for(int i=0; i
g.drawImage(bullets[i].image, bullets[i].x, bullets[i].y, null);
}
}
//畫分和得命
public void paintScoreandLife(Graphics g) {
g.setColor(new Color(0xFF0000)); //設置畫筆紅色
g.setFont(new Font(Font.SANS_SERIF, Font.BOLD,24)); //設置字體樣式
g.drawString("SCORE: "+score, 10, 25);
g.drawString("LIFE: "+hero.getLife(), 10, 45);
}
public void paintState(Graphics g) {
switch(state) { //根據狀態(tài)的不同來畫不同的圖片
case START:
g.drawImage(start, 0, 0, null);
break;
case PAUSE:
g.drawImage(pause, 0, 0, null);
break;
case GAME_OVER:
g.drawImage(gameover, 0, 0, null);
break;
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Fly"); //窗口
ShootGame game = new ShootGame(); //面板
frame.add(game); //將面板添加到窗口上
frame.setSize(WIDTH, HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true); //1.設置窗口可見,2.盡快調用paint()方法
game.action(); //啟動程序的執(zhí)行
}
}
看看效果圖啊,玩起來也是不錯的。。。。
這個是開始界面,鼠標點擊事件觸發(fā)開始游戲。。
pic1.png
游戲界面,計分,得命
pic2.png
鼠標移出框外時觸發(fā) mouseExited 事件,暫停
pic3.png
游戲結束
pic4.png
編程經驗:
先看功能是否是某個對象的行為
若是對象的行為,則寫在相應的類中
若不是對象的行為,則寫在ShooGame中
總結
以上是生活随笔為你收集整理的飞机大战java_Java飞机大战的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: rman备份恢复命令之switch
- 下一篇: java 8 io_Java IO8:I