打飛機小游戲

?xml version="1.0" encoding="UTF-8"?

importjava.util.Random;

publicclassAirplaneextendsFlyingObjectimplementsEnemy {

???privateintspeed = 3;?//移動步驟


???/** 初始化數(shù)據(jù) */

???publicAirplane(){

???????this.image = ShootGame.airplane;

??????? width = image.getWidth();

??????? height = image.getHeight();

??????? y = -height;?????????

??????? Random rand =newRandom();

??????? x = rand.nextInt(ShootGame.WIDTH - width);

??? }


???/** 獲取分?jǐn)?shù) */

???@Override

???publicintgetScore() {?

???????return5;

??? }

???/** //越界處理 */

???@Override

???public?booleanoutOfBounds() { ?

???????returny>ShootGame.HEIGHT;

??? }

???/** 移動 */

???@Override

???publicvoidstep() { ?

??????? y += speed;

??? }

}

/**

?* 獎勵

?*/?

publicinterfaceAward {?

???intDOUBLE_FIRE= 0;?//雙倍火力?

???intLIFE= 1; ?//1條命?

???/** 獲得獎勵類型(上面的0或1) */?

???intgetType();?

}?

importjava.util.Random;?


/** 蜜蜂 */?

publicclassBeeextendsFlyingObjectimplementsAward{?

???privateintxSpeed = 1; ?//x坐標(biāo)移動速度?

???privateintySpeed = 2; ?//y坐標(biāo)移動速度?

???privateintawardType;???//獎勵類型?


???/** 初始化數(shù)據(jù) */?

???publicBee(){?

???????this.image = ShootGame.bee;?

??????? width = image.getWidth();?

??????? height = image.getHeight();?

??????? y = -height;?

??????? Random rand =newRandom();?

??????? x = rand.nextInt(ShootGame.WIDTH - width);?

??????? awardType = rand.nextInt(2); ?//初始化時給獎勵?

??? }?


???/** 獲得獎勵類型 */?

???publicintgetType(){?

???????returnawardType;?

??? }?


???/** 越界處理 */?

???@Override?

???publicbooleanoutOfBounds() {?

???????returny>ShootGame.HEIGHT;?

??? }?


???/** 移動,可斜著飛 */?

???@Override?

???publicvoidstep() {???????

??????? x += xSpeed;?

??????? y += ySpeed;?

???????if(x > ShootGame.WIDTH-width){???

??????????? xSpeed = -1;?

??????? }?

???????if(x < 0){?

??????????? xSpeed = 1;?

??????? }?

??? }?

}

/**

?* 子彈類:是飛行物

?*/?

publicclassBulletextendsFlyingObject {?

???privateintspeed= 3;?//移動的速度?


???/** 初始化數(shù)據(jù) */?

???publicBullet(intx,inty){?

???????this.x=x;?

???????this.y=y;?

???????this.image= ShootGame.bullet;?

??? }?


???/** 移動 */?

???@Override?

???publicvoidstep(){ ???

???????y-=speed;?

??? }?


???/** 越界處理 */?

???@Override?

???publicbooleanoutOfBounds() {?

???????returny<-height;?

??? }?


}?

/**

?* 敵人,可以有分?jǐn)?shù)

?*/?

publicinterfaceEnemy {?

???/** 敵人的分?jǐn)?shù)? */?

???intgetScore();?

}?

importjava.awt.image.BufferedImage;?


/**

?* 飛行物(敵機,蜜蜂,子彈,英雄機)

?*/?

publicabstractclassFlyingObject {?

???protectedintx;???//x坐標(biāo)?

???protectedinty;???//y坐標(biāo)?

???protectedintwidth;???//寬?

???protectedintheight; ?//高?

???protectedBufferedImageimage; ?//圖片?


???publicintgetX() {?

???????returnx;?

??? }?


???publicvoidsetX(intx) {?

???????this.x=x;?

??? }?


???publicintgetY() {?

???????returny;?

??? }?


???publicvoidsetY(inty) {?

???????this.y=y;?

??? }?


???publicintgetWidth() {?

???????returnwidth;?

??? }?


???publicvoidsetWidth(intwidth) {?

???????this.width=width;?

??? }?


???publicintgetHeight() {?

???????returnheight;?

??? }?


???publicvoidsetHeight(intheight) {?

???????this.height=height;?

??? }?


???publicBufferedImage getImage() {?

???????returnimage;?

??? }?


???publicvoidsetImage(BufferedImageimage) {?

???????this.image=image;?

??? }?


???/**

???? * 檢查是否出界

???? *@returntrue 出界與否

???? */?

???publicabstractbooleanoutOfBounds();?


???/**

???? * 飛行物移動一步

???? */?

???publicabstractvoidstep();?


???/**

???? * 檢查當(dāng)前飛行物體是否被子彈(x,y)擊(shoot)中

???? *@paramBullet 子彈對象

???? *@returntrue表示被擊中了

???? */?

???publicbooleanshootBy(Bulletbullet){?

???????intx=bullet.x;?//子彈橫坐標(biāo)?

???????inty=bullet.y;?//子彈縱坐標(biāo)?

???????returnthis.x

??? }?


}?

importjava.awt.image.BufferedImage;?


/**

?* 英雄機:是飛行物

?*/?

publicclassHeroextendsFlyingObject{?


???privateBufferedImage[]images= {};?//英雄機圖片?

???privateintindex= 0;???????????????//英雄機圖片切換索引?


???privateintdoubleFire; ?//雙倍火力?

???privateintlife; ?//命?


???/** 初始化數(shù)據(jù) */?

???publicHero(){?

???????life= 3; ?//初始3條命?

???????doubleFire= 0; ?//初始火力為0?

???????images=newBufferedImage[]{ShootGame.hero0, ShootGame.hero1};//英雄機圖片數(shù)組?

???????image= ShootGame.hero0; ?//初始為hero0圖片?

???????width=image.getWidth();?

???????height=image.getHeight();?

???????x= 150;?

???????y= 400;?

??? }?


???/** 獲取雙倍火力 */?

???publicintisDoubleFire() {?

???????returndoubleFire;?

??? }?


???/** 設(shè)置雙倍火力 */?

???publicvoidsetDoubleFire(intdoubleFire) {?

???????this.doubleFire=doubleFire;?

??? }?


???/** 增加火力 */?

???publicvoidaddDoubleFire(){?

???????doubleFire= 40;?

??? }?


???/** 增命 */?

???publicvoidaddLife(){?//增命?

???????life++;?

??? }?


???/** 減命 */?

???publicvoidsubtractLife(){ ?//減命?

???????life--;?

??? }?


???/** 獲取命 */?

???publicintgetLife(){?

???????returnlife;?

??? }?


???/** 當(dāng)前物體移動了一下,相對距離,x,y鼠標(biāo)位置? */?

???publicvoidmoveTo(intx,inty){ ???

???????this.x=x-width/2;?

???????this.y=y-height/2;?

??? }?


???/** 越界處理 */?

???@Override?

???publicbooleanoutOfBounds() {?

???????returnfalse;???

??? }?


???/** 發(fā)射子彈 */?

???publicBullet[] shoot(){ ???

???????intxStep=width/4;?????//4半?

???????intyStep= 20;?//步?

???????if(doubleFire>0){?//雙倍火力?

??????????? Bullet[]bullets=newBullet[2];?

???????????bullets[0] =newBullet(x+xStep,y-yStep);?//y-yStep(子彈距飛機的位置)?

???????????bullets[1] =newBullet(x+3*xStep,y-yStep);?

???????????returnbullets;?

??????? }else{?????//單倍火力?

??????????? Bullet[]bullets=newBullet[1];?

???????????bullets[0] =newBullet(x+2*xStep,y-yStep);???

???????????returnbullets;?

??????? }?

??? }?


???/** 移動 */?

???@Override?

???publicvoidstep() {?

???????if(images.length>0){?

???????????image=images[index++/10%images.length];?//切換圖片hero0,hero1?

??????? }?

??? }?


???/** 碰撞算法 */?

???publicbooleanhit(FlyingObjectother){?


???????intx1=other.x-this.width/2; ???????????????//x坐標(biāo)最小距離?

???????intx2=other.x+this.width/2 +other.width; ?//x坐標(biāo)最大距離?

???????inty1=other.y-this.height/2;???????????????//y坐標(biāo)最小距離?

???????inty2=other.y+this.height/2 +other.height;//y坐標(biāo)最大距離?


???????intherox=this.x+this.width/2; ?????????????//英雄機x坐標(biāo)中心點距離?

???????intheroy=this.y+this.height/2;?????????????//英雄機y坐標(biāo)中心點距離?


???????returnherox>x1&&heroxy1&&heroy

??? }?


}?

importjava.awt.Font;?

importjava.awt.Color;?

importjava.awt.Graphics;?

importjava.awt.event.MouseAdapter;?

importjava.awt.event.MouseEvent;?

importjava.util.Arrays;?

importjava.util.Random;?

importjava.util.Timer;?

importjava.util.TimerTask;?

importjava.awt.image.BufferedImage;?


importjavax.imageio.ImageIO;?

importjavax.swing.ImageIcon;?

importjavax.swing.JFrame;?

importjavax.swing.JPanel;?


publicclassShootGameextendsJPanel {?

???publicstaticfinalintWIDTH= 400;// 面板寬?

???publicstaticfinalintHEIGHT= 654;// 面板高?

???/** 游戲的當(dāng)前狀態(tài): START RUNNING PAUSE GAME_OVER */?

???privateintstate;?

???privatestaticfinalintSTART= 0;?

???privatestaticfinalintRUNNING= 1;?

???privatestaticfinalintPAUSE= 2;?

???privatestaticfinalintGAME_OVER= 3;?


???privateintscore= 0;// 得分?

???privateTimertimer;// 定時器?

???privateintintervel= 1000 / 100;// 時間間隔(毫秒)?


???publicstaticBufferedImagebackground;?

???publicstaticBufferedImagestart;?

???publicstaticBufferedImageairplane;?

???publicstaticBufferedImagebee;?

???publicstaticBufferedImagebullet;?

???publicstaticBufferedImagehero0;?

???publicstaticBufferedImagehero1;?

???publicstaticBufferedImagepause;?

???publicstaticBufferedImagegameover;?


???privateFlyingObject[]flyings= {};// 敵機數(shù)組?

???privateBullet[]bullets= {};// 子彈數(shù)組?

???privateHerohero=newHero();// 英雄機?


???static{// 靜態(tài)代碼塊,初始化圖片資源?

???????try{?

???????????background= ImageIO.read(ShootGame.class?

??????????????????? .getResource("background.png"));?

???????????start= ImageIO.read(ShootGame.class.getResource("start.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"));?

???????????pause= ImageIO.read(ShootGame.class.getResource("pause.png"));?

???????????gameover= ImageIO?

??????????????????? .read(ShootGame.class.getResource("gameover.png"));?

??????? }catch(Exceptione) {?

???????????e.printStackTrace();?

??????? }?

??? }?


???/** 畫 */?

???@Override?

???publicvoidpaint(Graphicsg) {?

???????g.drawImage(background, 0, 0,null);// 畫背景圖?

??????? paintHero(g);// 畫英雄機?

??????? paintBullets(g);// 畫子彈?

??????? paintFlyingObjects(g);// 畫飛行物?

??????? paintScore(g);// 畫分?jǐn)?shù)?

??????? paintState(g);// 畫游戲狀態(tài)?

??? }?


???/** 畫英雄機 */?

???publicvoidpaintHero(Graphicsg) {?

???????g.drawImage(hero.getImage(),hero.getX(),hero.getY(),null);?

??? }?


???/** 畫子彈 */?

???publicvoidpaintBullets(Graphicsg) {?

???????for(inti= 0;i

??????????? Bulletb=bullets[i];?

???????????g.drawImage(b.getImage(),b.getX() -b.getWidth() / 2,b.getY(),?

???????????????????null);?

??????? }?

??? }?


???/** 畫飛行物 */?

???publicvoidpaintFlyingObjects(Graphicsg) {?

???????for(inti= 0;i

??????????? FlyingObjectf=flyings[i];?

???????????g.drawImage(f.getImage(),f.getX(),f.getY(),null);?

??????? }?

??? }?


???/** 畫分?jǐn)?shù) */?

???publicvoidpaintScore(Graphicsg) {?

???????intx= 10;// x坐標(biāo)?

???????inty= 25;// y坐標(biāo)?

??????? Fontfont=newFont(Font.SANS_SERIF, Font.BOLD, 22);// 字體?

???????g.setColor(newColor(0xFF0000));?

???????g.setFont(font);// 設(shè)置字體?

???????g.drawString("SCORE:"+score,x,y);// 畫分?jǐn)?shù)?

???????y=y+20;// y坐標(biāo)增20?

???????g.drawString("LIFE:"+hero.getLife(),x,y);// 畫命?

??? }?


???/** 畫游戲狀態(tài) */?

???publicvoidpaintState(Graphicsg) {?

???????switch(state) {?

???????caseSTART:// 啟動狀態(tài)?

???????????g.drawImage(start, 0, 0,null);?

???????????break;?

???????casePAUSE:// 暫停狀態(tài)?

???????????g.drawImage(pause, 0, 0,null);?

???????????break;?

???????caseGAME_OVER:// 游戲終止?fàn)顟B(tài)?

???????????g.drawImage(gameover, 0, 0,null);?

???????????break;?

??????? }?

??? }?


???publicstaticvoidmain(String[]args) {?

??????? JFrameframe=newJFrame("Fly");?

??????? ShootGamegame=newShootGame();// 面板對象?

???????frame.add(game);// 將面板添加到JFrame中?

???????frame.setSize(WIDTH,HEIGHT);// 設(shè)置大小?

???????frame.setAlwaysOnTop(true);// 設(shè)置其總在最上?

???????frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 默認(rèn)關(guān)閉操作?

???????frame.setIconImage(newImageIcon("images/icon.jpg").getImage());// 設(shè)置窗體的圖標(biāo)?

???????frame.setLocationRelativeTo(null);// 設(shè)置窗體初始位置?

???????frame.setVisible(true);// 盡快調(diào)用paint?


???????game.action();// 啟動執(zhí)行?

??? }?


???/** 啟動執(zhí)行代碼 */?

???publicvoidaction() {?

???????// 鼠標(biāo)監(jiān)聽事件?

??????? MouseAdapterl=newMouseAdapter() {?

???????????@Override?

???????????publicvoidmouseMoved(MouseEvente) {// 鼠標(biāo)移動?

???????????????if(state==RUNNING) {// 運行狀態(tài)下移動英雄機--隨鼠標(biāo)位置?

???????????????????intx=e.getX();?

???????????????????inty=e.getY();?

???????????????????hero.moveTo(x,y);?

??????????????? }?

??????????? }?


???????????@Override?

???????????publicvoidmouseEntered(MouseEvente) {// 鼠標(biāo)進入?

???????????????if(state==PAUSE) {// 暫停狀態(tài)下運行?

???????????????????state=RUNNING;?

??????????????? }?

??????????? }?


???????????@Override?

???????????publicvoidmouseExited(MouseEvente) {// 鼠標(biāo)退出?

???????????????if(state==RUNNING) {// 游戲未結(jié)束,則設(shè)置其為暫停?

???????????????????state=PAUSE;?

??????????????? }?

??????????? }?


???????????@Override?

???????????publicvoidmouseClicked(MouseEvente) {// 鼠標(biāo)點擊?

???????????????switch(state) {?

???????????????caseSTART:?

???????????????????state=RUNNING;// 啟動狀態(tài)下運行?

???????????????????break;?

???????????????caseGAME_OVER:// 游戲結(jié)束,清理現(xiàn)場?

???????????????????flyings=newFlyingObject[0];// 清空飛行物?

???????????????????bullets=newBullet[0];// 清空子彈?

???????????????????hero=newHero();// 重新創(chuàng)建英雄機?

???????????????????score= 0;// 清空成績?

???????????????????state=START;// 狀態(tài)設(shè)置為啟動?

???????????????????break;?

??????????????? }?

??????????? }?

??????? };?

???????this.addMouseListener(l);// 處理鼠標(biāo)點擊操作?

???????this.addMouseMotionListener(l);// 處理鼠標(biāo)滑動操作?


???????timer=newTimer();// 主流程控制?

???????timer.schedule(newTimerTask() {?

???????????@Override?

???????????publicvoidrun() {?

???????????????if(state==RUNNING) {// 運行狀態(tài)?

??????????????????? enterAction();// 飛行物入場?

??????????????????? stepAction();// 走一步?

??????????????????? shootAction();// 英雄機射擊?

??????????????????? bangAction();// 子彈打飛行物?

??????????????????? outOfBoundsAction();// 刪除越界飛行物及子彈?

??????????????????? checkGameOverAction();// 檢查游戲結(jié)束?

??????????????? }?

??????????????? repaint();// 重繪,調(diào)用paint()方法?

??????????? }?


??????? },intervel,intervel);?

??? }?


???intflyEnteredIndex= 0;// 飛行物入場計數(shù)?


???/** 飛行物入場 */?

???publicvoidenterAction() {?

???????flyEnteredIndex++;?

???????if(flyEnteredIndex% 40 == 0) {// 400毫秒生成一個飛行物--10*40?

??????????? FlyingObjectobj= nextOne();// 隨機生成一個飛行物?

???????????flyings= Arrays.copyOf(flyings,flyings.length+ 1);?

???????????flyings[flyings.length- 1] =obj;?

??????? }?

??? }?


???/** 走一步 */?

???publicvoidstepAction() {?

???????for(inti= 0;i

??????????? FlyingObjectf=flyings[i];?

???????????f.step();?

??????? }?


???????for(inti= 0;i

??????????? Bulletb=bullets[i];?

???????????b.step();?

??????? }?

???????hero.step();// 英雄機走一步?

??? }?


???/** 飛行物走一步 */?

???publicvoidflyingStepAction() {?

???????for(inti= 0;i

??????????? FlyingObjectf=flyings[i];?

???????????f.step();?

??????? }?

??? }?


???intshootIndex= 0;// 射擊計數(shù)?


???/** 射擊 */?

???publicvoidshootAction() {?

???????shootIndex++;?

???????if(shootIndex% 30 == 0) {// 300毫秒發(fā)一顆?

??????????? Bullet[]bs=hero.shoot();// 英雄打出子彈?

???????????bullets= Arrays.copyOf(bullets,bullets.length+bs.length);// 擴容?

??????????? System.arraycopy(bs, 0,bullets,bullets.length-bs.length,?

???????????????????bs.length);// 追加數(shù)組?

??????? }?

??? }?


???/** 子彈與飛行物碰撞檢測 */?

???publicvoidbangAction() {?

???????for(inti= 0;i

??????????? Bulletb=bullets[i];?

??????????? bang(b);// 子彈和飛行物之間的碰撞檢查?

??????? }?

??? }?


???/** 刪除越界飛行物及子彈 */?

???publicvoidoutOfBoundsAction() {?

???????intindex= 0;// 索引?

??????? FlyingObject[]flyingLives=newFlyingObject[flyings.length];// 活著的飛行物?

???????for(inti= 0;i

??????????? FlyingObjectf=flyings[i];?

???????????if(!f.outOfBounds()) {?

???????????????flyingLives[index++] =f;// 不越界的留著?

??????????? }?

??????? }?

???????flyings= Arrays.copyOf(flyingLives,index);// 將不越界的飛行物都留著?


???????index= 0;// 索引重置為0?

??????? Bullet[]bulletLives=newBullet[bullets.length];?

???????for(inti= 0;i

??????????? Bulletb=bullets[i];?

???????????if(!b.outOfBounds()) {?

???????????????bulletLives[index++] =b;?

??????????? }?

??????? }?

???????bullets= Arrays.copyOf(bulletLives,index);// 將不越界的子彈留著?

??? }?


???/** 檢查游戲結(jié)束 */?

???publicvoidcheckGameOverAction() {?

???????if(isGameOver()==true) {?

???????????state=GAME_OVER;// 改變狀態(tài)?

??????? }?

??? }?


???/** 檢查游戲是否結(jié)束 */?

???publicbooleanisGameOver() {?


???????for(inti= 0;i

???????????intindex= -1;?

??????????? FlyingObjectobj=flyings[i];?

???????????if(hero.hit(obj)) {// 檢查英雄機與飛行物是否碰撞?

???????????????hero.subtractLife();// 減命?

???????????????hero.setDoubleFire(0);// 雙倍火力解除?

???????????????index=i;// 記錄碰上的飛行物索引?

??????????? }?

???????????if(index!= -1) {?

??????????????? FlyingObjectt=flyings[index];?

???????????????flyings[index] =flyings[flyings.length- 1];?

???????????????flyings[flyings.length- 1] =t;// 碰上的與最后一個飛行物交換?


???????????????flyings= Arrays.copyOf(flyings,flyings.length- 1);// 刪除碰上的飛行物?

??????????? }?

??????? }?


???????returnhero.getLife() <= 0;?

??? }?


???/** 子彈和飛行物之間的碰撞檢查 */?

???publicvoidbang(Bulletbullet) {?

???????intindex= -1;// 擊中的飛行物索引?

???????for(inti= 0;i

??????????? FlyingObjectobj=flyings[i];?

???????????if(obj.shootBy(bullet)) {// 判斷是否擊中?

???????????????index=i;// 記錄被擊中的飛行物的索引?

???????????????break;?

??????????? }?

??????? }?

???????if(index!= -1) {// 有擊中的飛行物?

??????????? FlyingObjectone=flyings[index];// 記錄被擊中的飛行物?


??????????? FlyingObjecttemp=flyings[index];// 被擊中的飛行物與最后一個飛行物交換?

???????????flyings[index] =flyings[flyings.length- 1];?

???????????flyings[flyings.length- 1] =temp;?


???????????flyings= Arrays.copyOf(flyings,flyings.length- 1);// 刪除最后一個飛行物(即被擊中的)?


???????????// 檢查one的類型(敵人加分,獎勵獲取)?

???????????if(oneinstanceofEnemy) {// 檢查類型,是敵人,則加分?

??????????????? Enemye= (Enemy)one;// 強制類型轉(zhuǎn)換?

???????????????score+=e.getScore();// 加分?

??????????? }else{// 若為獎勵,設(shè)置獎勵?

??????????????? Awarda= (Award)one;?

???????????????inttype=a.getType();// 獲取獎勵類型?

???????????????switch(type) {?

???????????????caseAward.DOUBLE_FIRE:?

???????????????????hero.addDoubleFire();// 設(shè)置雙倍火力?

???????????????????break;?

???????????????caseAward.LIFE:?

???????????????????hero.addLife();// 設(shè)置加命?

???????????????????break;?

??????????????? }?

??????????? }?

??????? }?

??? }?


???/**

???? * 隨機生成飛行物

???? *?

???? *@return飛行物對象

???? */?

???publicstaticFlyingObject nextOne() {?

??????? Randomrandom=newRandom();?

???????inttype=random.nextInt(20);// [0,20)?

???????if(type< 4) {?

???????????returnnewBee();?

??????? }else{?

???????????returnnewAirplane();?

??????? }?

??? }?


}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容