飛機大戰
1.主要功能
英雄機受玩家滑鼠控制進行移動并發出子彈對敵人進行攻擊,打中敵機會加分,打中小心心會隨機獲得獎勵,獎勵型別分為三種:英雄機生命值加一,英雄機雙倍火力,英雄機三倍火力(要達到三倍火力首先需要達到雙倍火力才行),當英雄機撞到敵機時,火力加成會消失,當分數達到50的時候會出現boss,boss會進行移動并且發射子彈,擊敗boss會判定游戲勝利,當英雄機撞到敵機,小心心或者被boss發射的子彈擊中時,生命值會減一,當生命值為零的時候會判定游戲失敗,英雄機如果撞到了boss那就直接白給,游戲期間用滑鼠進行控制,點擊滑鼠游戲開始,將滑鼠移動至游戲界面外可以暫停游戲,
2.效果展示圖

3.分析
1. 素材準備
在開發之前需要先去下載一些素材圖片,包括背景圖,英雄機圖,敵機圖,子彈圖,boss圖,開始,暫停,勝利以及結束圖片等,
2.設計構思
- 首先添加開始,勝利,結束等游戲畫面
- 設計獎勵介面以及得分介面添加獎勵與得分的方法
- 設計飛行物父類,在類中添加相關的方法
- 設計敵機類以及小心心類,分別執行得分介面與獎勵介面,并且繼承飛行物父類
- 設計英雄機類,boss類,英雄機子彈類以及boss子彈類分別繼承飛行物父類
- 再設計實作類,對他們進行初始化等操作
- 滑鼠監聽控制英雄機的移動軌跡
3.運用到的知識
- 陣列
- swing組件:圖形界面工具
- 滑鼠監聽
- 面向物件:封裝繼承多型
- ImageIO流
4.代碼展示
測驗類代碼出現了一點小BUG,已經標出,按理來說這樣寫當判定游戲結束或者勝利時會先清理現場,然后再修改為啟動狀態,但是我這個游戲結束后還是戰后現場,界面上boss機發射出來的子彈并不能清除,這就很迷惑,
獎勵介面
package shoot;
//小心心:獎勵
public interface Award {
int TRIPLE_FIRE = 2;//三倍火力
int DOUBLE_FIRE = 1;//雙倍火力
int LIFE =0 ;//1條命
public int getType();//獲取獎勵型別
}
得分介面
package shoot;
//敵機:得分
public interface Enemy {
public int getScope();
}
飛行物類
package shoot;
import java.awt.image.BufferedImage;
//飛行物類 父類
public abstract class FlyingObject {
protected int width;//寬
protected int height;//高
protected int x;//x坐標
protected int y;//y坐標
protected BufferedImage image;//圖片
//走步
public abstract void step();
//敵人(小飛機,小心心)被子彈打
public boolean shootBy(Bullet b){
int x = b.x;//子彈的x和y
int y = b.y;
//子彈x在心的x和心的x加寬之間
//并且
//子彈y在心的y和心的y加高之間
return x>this.x && x<this.x+width
&&
y>this.y && y<this.y+height;
}
//檢測是否出界
public abstract boolean outofBounds();
}
敵機類
package shoot;
import java.util.Random;
//小飛機
public class Airplane extends FlyingObject
implements Enemy {
private int speed = 2;//敵機移動的步數
//得分
public int getScope() {
return 5;
}
public Airplane(){
image = ShootGame.airplane;
width = image.getWidth();
height = image.getHeight();
y = -height;
Random r = new Random();
x = r.nextInt(ShootGame.WIDTH - width);
}
//敵機走步
public void step() {
y += speed;
}
//重寫出界
public boolean outofBounds() {
return y>ShootGame.HEIGHT;
}
}
獎勵類
package 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;//獎勵型別
//獲取獎勵
public int getType() {
return awardType;
}
//初始化實體變數
public Bee(){
image = ShootGame.bee;
width = image.getWidth();//獲取當前圖片的寬
height = image.getHeight();//獲取當前圖片的高
y = -height;
//x = (int)(Math.random()*(ShootGame.WIDTH - width));
Random rand = new Random();
x = rand.nextInt(ShootGame.WIDTH - width);
awardType = rand.nextInt(4);//0,1,2隨機生成
}
//小心心走步
public void step() {
x += xSpeed;
y += ySpeed;
if(x<0){
xSpeed = 1;//往右
}
if(x>ShootGame.WIDTH - width){
xSpeed = -1;//往左
}
}
//重寫出界
public boolean outofBounds() {
return y>ShootGame.HEIGHT;//y大于面板的高
}
}
英雄機類
package shoot;
import java.awt.image.BufferedImage;
//英雄機
public class Hero extends FlyingObject{
private BufferedImage[] images = {};//存放英雄機圖片陣列
private int index;//下標
private int doubleFire;//雙倍火力
protected int tripleFire;//三倍火力
public int life;//命
//初始化實體變數
public Hero(){
image = ShootGame.hero0;
width = image.getWidth();
height = image.getHeight();
x = 250;
y = 700;
doubleFire = 0;//單倍火力
tripleFire = 0;//單倍火力
life = 3;//命3條
images = new BufferedImage[]{ShootGame.hero0,ShootGame.hero1};
}
//英雄機走步
public void step() {
int num = index++/10%images.length;
image = images[num];
}
//發射子彈
public Bullet[] shoot(){
int xstep = this.width/4;//4半
int ystep = 20;
if(doubleFire<=0&&tripleFire>0){
tripleFire=0;
life++;
Bullet[] bullets = new Bullet[1];
bullets[0] = new Bullet(this.x+2*xstep,this.y-ystep);
return bullets;
}else if(tripleFire>0&&doubleFire>0){
Bullet[] bullets = new Bullet[3];
bullets[0] = new Bullet(this.x+1*xstep,this.y-ystep);
bullets[1] = new Bullet(this.x+3*xstep,this.y-ystep);
bullets[2] = new Bullet(this.x+2*xstep,this.y-ystep);
return bullets;
}else if(doubleFire > 0&&tripleFire<=0){//雙倍火力
Bullet[] bullets = new Bullet[2];
bullets[0] = new Bullet(this.x+1*xstep,this.y-ystep);
bullets[1] = new Bullet(this.x+3*xstep,this.y-ystep);
return bullets;
}else{//單倍火力
Bullet[] bullets = new Bullet[1];
bullets[0] = new Bullet(this.x+2*xstep,this.y-ystep);
return bullets;
}
}
//移動 傳入的是滑鼠的x和y
public void moveTo(int x,int y){
this.x = x-this.width/2;
this.y = y-this.height/2;
}
//添加三倍火力
public void addTripleFire(){
tripleFire+=40;
}
//添加雙倍火力
public void addDoubleFire(){
doubleFire += 40;
}
//添加命
public void addLife(){
life++;
}
//獲取命
public int getLife(){
return life;
}
//重寫出界
public boolean outofBounds() {
return false;//永不出界
}
//判斷英雄機與敵人是否發生碰撞
//other:敵人
public boolean hit(FlyingObject other){
int x1 = other.x-this.width/2;
int x2 = other.x+other.width+this.width/2;
int y1 = other.y-this.height/2;
int y2 = other.y+other.height+this.height/2;
int heroX = this.x+this.width/2;
int heroY = this.y+this.height/2;
return heroX>x1 && heroX<x2
&&
heroY>y1 && heroY<y2;
}
//英雄機撞到boss
public boolean bosshit(Boss bo){
int X1=bo.x-this.width/2;
int X2=bo.x+bo.width+this.width/2;
int Y1=bo.y-this.height/2;
int Y2=bo.y+bo.height+this.height/2;
int HeroX=this.x+this.width/2;
int HeroY=this.y+this.height/2;
return HeroX>X1&&HeroX<X2
&&
HeroY>Y1&&HeroY<Y2;
}
//boss子彈打中英雄機
public boolean heroShootBy(BossBullet a){
int x = a.x;//子彈的x和y
int y = a.y;
//子彈x在英雄機的x和英雄機的x加寬之間
//并且
//子彈y在英雄機的y和英雄機的y加高之間
return x>this.x && x<this.x+width
&&
y>this.y && y<this.y+height;
}
//減命
public void subLife(){
life--;
}
public void setDoubleFire(int doubleFire){
this.doubleFire = doubleFire;
}
}
英雄機子彈類
package shoot;
//子彈
public class Bullet extends FlyingObject {
private int speed = 5;//移動走步
public Bullet(int x,int y){
image = ShootGame.bullet;
width = image.getWidth();
height = image.getHeight();
this.x = x;//跟隨英雄機坐標
this.y = y;
}
//子彈走步
public void step() {
y -= speed;
}
//重寫出界
public boolean outofBounds() {
return y<-height;
}
}
boss類
package shoot;
import java.awt.image.BufferedImage;
import java.util.Random;
//import java.util.Random;
public class Boss extends FlyingObject{
protected BufferedImage image=ShootGame.boss;//圖片
protected int Xspeed=2;
protected int Yspeed=1;
public int bossLife=1000;
protected int width=image.getWidth();//寬
protected int height=image.getHeight();//高
Random bx=new Random();
protected int x=bx.nextInt(ShootGame.WIDTH-width);//x坐標
protected int y=-height;//y坐標
//private BufferedImage image=ShootGame.boss;//圖片
// public Boss(){
// image=ShootGame.boss;
// width=image.getWidth();
// height=image.getHeight();
// x=100;
// y=100;
// bossLife=100;
// }
//boss被子彈打
public boolean bossShootBy(Bullet b){
int x = b.x;//子彈的x和y
int y = b.y;
//子彈x在boss的x和boss的x加寬之間
//并且
//子彈y在boss的y和boss的y加高之間
return x>this.x && x<this.x+width
&&
y>this.y && y<this.y+height;
}
//boss打出子彈
public BossBullet[] bossshoot(){
int x_step = this.width/4;//4半
int y_step = this.height-40;
BossBullet[] Bullets = new BossBullet[2];
Bullets[0] = new BossBullet(this.x+1*x_step,this.y+y_step);
Bullets[1] = new BossBullet(this.x+3*x_step,this.y+y_step);
//Bullets[2] = new BossBullet(this.x+2*x_step,this.y+y_step);
return Bullets;
}
@Override
public void step() {//boss移動
y+=Yspeed;
x+=Xspeed;
if(y<=0){
Yspeed=1;
}
if(y>=height){
Yspeed=-1;
}
if(x<=0){
Xspeed=2;
}
if(x>=ShootGame.WIDTH-width){
Xspeed=-2;
}
}
@Override
public boolean outofBounds() {//檢測出界
// TODO Auto-generated method stub
return false;
}
}
boss子彈類
package shoot;
import java.awt.image.BufferedImage;
public class BossBullet extends FlyingObject {
private int B_Bulspeed=2;//設定Boss子彈速度
public BossBullet(int x,int y){
image=ShootGame.bossbullet;
width=image.getWidth();
height=image.getHeight();
this.x=x;//跟隨boss坐標
this.y=y;
}
@Override
public void step() {
// TODO Auto-generated method stub
y+=B_Bulspeed;
}
@Override
public boolean outofBounds() {
// TODO Auto-generated method stub
return y>ShootGame.HEIGHT+height;
}
}
測驗類
package shoot;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Arrays;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.imageio.ImageIO;
//游戲主界面,主類
public class ShootGame extends JPanel{
public static final int WIDTH = 600;//游戲界面的寬
public static final int HEIGHT = 900;//游戲界面的高
public static BufferedImage background;
public static BufferedImage airplane;
public static BufferedImage bee;
public static BufferedImage bullet;
public static BufferedImage gameover;
public static BufferedImage hero0;
public static BufferedImage hero1;
public static BufferedImage start;
public static BufferedImage pause;
public static BufferedImage boss;
public static BufferedImage bossbullet;
public static BufferedImage win;
public Hero hero = new Hero();//英雄級物件
public Bullet[] bullets = {};//子彈陣列
public BossBullet[] Bullets={};//boss子彈陣列
public FlyingObject[] flyings = {};//敵人
public Boss bosses=new Boss();//boss
//定時器
private Timer timer;
//時間間隔(毫秒)
private int intervar = 10;
public int score = 0;//紀錄分數
private int state;//狀態
public static final int START = 0;
public static final int RUNNING = 1;
public static final int PAUSE = 2;
public static final int GAME_OVER = 3;
public static final int WIN=4;
//加載靜態資源
static{
try {
background = ImageIO.read(ShootGame.class.getResource("background.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"));
gameover = ImageIO.read(ShootGame.class.getResource("gameover.png"));
hero0 = ImageIO.read(ShootGame.class.getResource("hero0.png"));
hero1 = ImageIO.read(ShootGame.class.getResource("hero1.png"));
start = ImageIO.read(ShootGame.class.getResource("start.png"));
pause = ImageIO.read(ShootGame.class.getResource("pause.png"));
boss=ImageIO.read(ShootGame.class.getResource("boss.png"));
bossbullet=ImageIO.read(ShootGame.class.getResource("bossbullet.png"));
win=ImageIO.read(ShootGame.class.getResource("win.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
//重寫繪制方法
//g就是畫筆
public void paint(Graphics g){
g.drawImage(background,0,0,null);
paintHero(g);//英雄
paintBullets(g);//子彈
paintFlyingObjects(g);//敵人
paintScore(g);//分數
paintState(g);//狀態
paintBoss(g);//boss
paintBosslife(g);//boss生命值
paintB_Bullet(g);//畫boss子彈
}
//畫游戲狀態
public void paintState(Graphics g){
switch(state){
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;
case WIN://勝利
//
g.drawImage(win,0,0,null);
}
}
//畫分數
public void paintScore(Graphics g){
int x = 10;
int y = 25;
//設定字體
g.setFont(new Font(Font.SANS_SERIF,Font.BOLD,20));
//設定字體顏色
g.setColor(new Color(0xFF0000));
g.drawString("SCORE:"+score, x, y);//畫分數
g.drawString("LIFE:"+hero.getLife(), x, y+20);//畫英雄生命值
}
//畫boss生命值
public void paintBosslife(Graphics g){
int x=420;
int y=25;
//設定字體
g.setFont(new Font(Font.SANS_SERIF,Font.BOLD,20));
//設定字體顏色
g.setColor(new Color(0xFF00));//畫字體顏色
if(bosses.y>=0){
g.drawString("BOSS LIFE:"+bosses.bossLife,x,y);//畫boss生命值
}
}
//畫boss
public void paintBoss(Graphics g){
g.drawImage(bosses.image, bosses.x,bosses.y, null);
}
//畫boss子彈
public void paintB_Bullet(Graphics g){
for(int j=0;j<Bullets.length;j++){
BossBullet bb=Bullets[j];
g.drawImage(bb.image, bb.x, bb.y, null);
}
}
//畫英雄機
public void paintHero(Graphics g){
g.drawImage(hero.image,hero.x,hero.y,null);
}
//畫子彈
public void paintBullets(Graphics g){
for(int i=0;i<bullets.length;i++){
Bullet b = bullets[i];
g.drawImage(b.image,b.x,b.y,null);
}
}
//畫敵人
public void paintFlyingObjects(Graphics g){
for(int i=0;i<flyings.length;i++){
FlyingObject f = flyings[i];
g.drawImage(f.image,f.x,f.y,null);
}
}
//啟動執行操作
public void action(){
//滑鼠事件配接器
MouseAdapter l = new MouseAdapter() {
//重寫,(滑鼠移動)方法
public void mouseMoved(MouseEvent e){
if(state == RUNNING){
int x = e.getX();//得到滑鼠x
int y = e.getY();//得到滑鼠y
hero.moveTo(x, y);
}
}
///
/**
*游戲結束或勝利后,boss機已經發射出來的子彈會留在界面上不能消失
*這就很搞心態,按理來說這樣寫游戲結束后螢屏上的飛行物就會消失
*/
//重寫,滑鼠點擊
public void mouseClicked(MouseEvent e) {
switch(state){
case START:
state = RUNNING;
break;
case WIN:
case GAME_OVER://游戲結束歸零
state = START;
hero = new Hero();
bosses = new Boss();
flyings = new FlyingObject[0];
Bullets = new BossBullet[0];
bullets = new Bullet[0];
score = 0;
//state = START;
break;
}
}
///
//重寫 滑鼠移入
public void mouseEntered(MouseEvent e) {
if(state == PAUSE){
state = RUNNING;
}
}
//重寫 滑鼠移出
public void mouseExited(MouseEvent e) {
if(state != GAME_OVER){
state = PAUSE;
}
}
};
//給當前畫板添加滑鼠滑動偵聽
this.addMouseMotionListener(l);
//給當前畫板添加滑鼠點擊偵聽
this.addMouseListener(l);
//創建定時器物件
timer = new Timer();
//定時觸發
timer.schedule(new TimerTask(){
//重寫run方法 ,定時執行的任務
public void run() {
if(state == RUNNING){
enterAction();//飛行物入場---new物件
stepAction();//飛行物走步
shootAction();//子彈入場
if(bosses.y>=0){
B_shootAction();//boss子彈入場
}
heroBangAction();//子彈打中英雄機
bangAction();//子彈打敵人
outofBoundsAction();//洗掉出界的飛行物
checkGameOverAction();//檢測游戲是否結束
checkWinAction();//檢測游戲是否勝利
}
//重構畫板
repaint();
}
},intervar,intervar);
}
//檢測游戲是否結束
public void checkGameOverAction(){
if(isGameOver()){//判斷游戲是否結束
state = GAME_OVER;
}
}
//判斷是否結束
public boolean isGameOver(){
for(int i=0;i<flyings.length;i++){
int index = -1;//記錄撞上飛行物的索引
FlyingObject obj = flyings[i];
if(hero.hit(obj)){//撞上
hero.subLife();//減命
hero.setDoubleFire(0);//設定火力
index = i;//記錄索引
}
if(index != -1){//有撞上
FlyingObject t = flyings[index];
flyings[index] = flyings[flyings.length-1];
flyings[flyings.length-1] = t;
flyings = Arrays.copyOf(flyings,flyings.length-1);
}
}
if(hero.bosshit(bosses)){//判斷boss是否與英雄機相撞
hero.life=0;//英雄機死亡
}
return hero.getLife()<=0;
}
//檢測游戲勝利
public void checkWinAction(){
if(isWIN()){
state=WIN;
}
}
//判斷游戲勝利
public boolean isWIN(){
return bosses.bossLife<=0;
}
//洗掉出界的飛行物
public void outofBoundsAction(){
int index = 0;//下標
FlyingObject[] flyingLives = new FlyingObject[flyings.length];
for(int i=0;i<flyings.length;i++){
FlyingObject f = flyings[i];//得到的每個敵人
if(!f.outofBounds()){//若不出界
flyingLives[index++] = f;
}
}
flyings = Arrays.copyOf(flyingLives,index);
//洗掉越界子彈
index = 0;
Bullet[] bulletsLives = new Bullet[bullets.length];
for(int i=0;i<bullets.length;i++){
Bullet b = bullets[i];
if(!b.outofBounds()){
bulletsLives[index++] = b;
}
}
bullets = Arrays.copyOf(bulletsLives,index);
//洗掉越界boss子彈
index=0;
BossBullet[] BulletsLives=new BossBullet[Bullets.length];
for(int i=0;i<BulletsLives.length;i++){
BossBullet a=Bullets[i];
if(!a.outofBounds()){
BulletsLives[index++]=a;
}
}
}
//子彈打敵人
int flag=0;
public void bangAction(){
for(int i=0;i<bullets.length;i++){
Bullet b = bullets[i];//每一個子彈
flag=i;
bang(b);//判斷子彈和敵人撞擊
bossbang(b);//判斷子彈和boss撞擊
}
}
//boss子彈列印雄機
int B_flag=0;
public void heroBangAction(){
for(int i=0;i<Bullets.length;i++ ){
BossBullet a=Bullets[i];//每一顆boss子彈
B_flag=i;
if(bosses.y>=0){
herobang(a);
}
}
}
//判斷boss子彈與英雄機撞擊
public void herobang(BossBullet a){
if(hero.heroShootBy(a)){//是否被擊中
hero.subLife();//英雄機生命值減一
BossBullet heroone=Bullets[B_flag];//洗掉擊中后的子彈
Bullets[B_flag]=Bullets[Bullets.length-1];
Bullets[Bullets.length-1]=heroone;
Bullets=Arrays.copyOf(Bullets, Bullets.length-1);
}
}
//判斷子彈與boss撞擊
public void bossbang(Bullet b){
if(bosses.bossLife>0&&score>=50){
if(bosses.bossShootBy(b)){//boss是否被擊中
bosses.bossLife-=10;
Bullet bossone=bullets[flag];//洗掉擊中后的子彈
bullets[flag]=bullets[bullets.length-1];
bullets[bullets.length-1]=bossone;
bullets=Arrays.copyOf(bullets, bullets.length-1);
}
}else{
bosses.y=-bosses.height;
}
}
//判斷子彈和敵人撞擊
public void bang(Bullet b){
int index = -1;
//遍歷所有敵人
for(int j=0;j<flyings.length;j++){
FlyingObject obj =flyings[j];//每一個敵人
if(obj.shootBy(b)){//是否撞上
index = j;//紀錄被擊中的敵人的下標
break;
}
}
if(index != -1){//有擊中的敵人
FlyingObject one = flyings[index];//擊中的敵人
//洗掉子彈
Bullet m=bullets[flag];
bullets[flag]=bullets[bullets.length-1];
bullets[bullets.length-1]=m;
bullets=Arrays.copyOf(bullets, bullets.length-1);
//洗掉敵人
FlyingObject t = flyings[index];
flyings[index] = flyings[flyings.length-1];
flyings[flyings.length-1] = t;
flyings = Arrays.copyOf(flyings,flyings.length-1);//縮容
//得分或的獎勵
if(one instanceof Enemy){//若為敵機
Enemy e = (Enemy)one;
score += e.getScope();//設定加分
}else if(one instanceof Award){//若為獎勵
Award a = (Award)one;
int type = a.getType();
switch(type){//判斷獎勵型別
case 3:
case Award.DOUBLE_FIRE:
hero.addDoubleFire();//雙倍火力
break;
case Award.TRIPLE_FIRE:
hero.addTripleFire();//三倍火力
break;
case Award.LIFE:
hero.addLife();//增加命
break;
}
}
}
}
int shootIndex = 0;
int B_shootIndex=0;
//boss子彈入場
public void B_shootAction(){//每十毫秒調一次
B_shootIndex++;//每調一次自增1
if(B_shootIndex%50==0){//調30次發射一次
BossBullet[] B_bs=bosses.bossshoot();//boss發射出來的子彈
Bullets=Arrays.copyOf(Bullets, Bullets.length+B_bs.length);
System.arraycopy(B_bs, 0,Bullets, Bullets.length-B_bs.length,B_bs.length );
}
}
//子彈入場
public void shootAction(){//每10毫秒調一次
shootIndex++;//每調一次自增1
if(shootIndex % 25 == 0){//調25次才進來一次
Bullet[] bs = hero.shoot();//英雄機發射出來的子彈
//擴容長度等于原陣列長度加上bs的實際長度
bullets = Arrays.copyOf(bullets,bullets.length+bs.length);
System.arraycopy(bs,0,bullets,bullets.length-bs.length,bs.length);
}
}
//隨機生成敵人(敵機,小心心)
public static FlyingObject nextOne(){
Random r = new Random();
int type = r.nextInt(10);
//只有6,7,8,9,10的時候生成小心心
if(type >5){
return new Bee();
}else{//其他則生成小飛機
return new Airplane();
}
}
int flyingIndex = 0;//飛行物入場計數
//飛行物入場
public void enterAction(){//每10毫秒調一次
flyingIndex++;//調一次自增1
if(flyingIndex % 80 == 0 ){//走30次,10*30 300毫秒
FlyingObject obj = nextOne();//隨機生成的敵人
flyings = Arrays.copyOf(flyings,flyings.length+1);//擴容
flyings[flyings.length-1] = obj;//將生成的敵人放入擴容后的陣列最后一位
}
}
//飛行物走步
public void stepAction(){
//敵人(小心心,小飛機)
for(int i=0;i<flyings.length;i++){
flyings[i].step();
}
//子彈走步
for(int i=0;i<bullets.length;i++){
bullets[i].step();
}
//boss子彈走步
for(int i=0;i<Bullets.length;i++){
Bullets[i].step();
}
if(score>=30){
bosses.step();
}
hero.step();
}
public static void main(String[] args) {
//畫框
JFrame frame = new JFrame("FLY shoot");
ShootGame game = new ShootGame();//畫板
frame.add(game); //將畫板嵌入畫框上
frame.setSize(WIDTH,HEIGHT);//大小
frame.setAlwaysOnTop(true);//總在最上
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//默認關閉
frame.setLocationRelativeTo(null);//初始表單位置
frame.setVisible(true);//顯示,盡快呼叫paint()方法
game.action();//界面動起來
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/236671.html
標籤:其他
