Java打飞机小游戏
飛機大戰腳本組成
1.有一個所有物體的父物體GameObject,然后就是一堆物體繼承于他,等到他的屬性于函數。
2.窗口組件
3.工具腳本(負責做一些雜事和存放圖片)
代碼
GameObject
package object;
import utils.GameWindow;
import java.awt.*;
public class GameObject {
? ? public Image image;
? ? public int x;
? ? public int y;
? ? public int width;
? ? public int height;
? ? public double speed;
? ? public GameWindow fram;
? ? public Image getImage() {
? ? ? ? return image;
? ? }
? ? public void setImage(Image image) {
? ? ? ? this.image = image;
? ? }
? ? public int getX() {
? ? ? ? return x;
? ? }
? ? public void setX(int x) {
? ? ? ? this.x = x;
? ? }
? ? public int getY() {
? ? ? ? return y;
? ? }
? ? public void setY(int y) {
? ? ? ? this.y = y;
? ? }
? ? public int getWidth() {
? ? ? ? return width;
? ? }
? ? public void setWidth(int width) {
? ? ? ? this.width = width;
? ? }
? ? public int getHeight() {
? ? ? ? return height;
? ? }
? ? public void setHeight(int height) {
? ? ? ? this.height = height;
? ? }
? ? public double getSpeed() {
? ? ? ? return speed;
? ? }
? ? public void setSpeed(double speed) {
? ? ? ? this.speed = speed;
? ? }
? ? public GameWindow getFram() {
? ? ? ? return fram;
? ? }
? ? public void setFram(GameWindow fram) {
? ? ? ? this.fram = fram;
? ? }
? ? public GameObject(Image image, int x, int y, int width, int height, double speed, GameWindow fram) {
? ? ? ? this.image = image;
? ? ? ? this.x = x;
? ? ? ? this.y = y;
? ? ? ? this.width = width;
? ? ? ? this.height = height;
? ? ? ? this.speed = speed;
? ? ? ? this.fram = fram;
? ? }
? ? public GameObject(Image image, int x, int y, double speed) {
? ? ? ? this.image = image;
? ? ? ? this.x = x;
? ? ? ? this.y = y;
? ? ? ? this.speed = speed;
? ? }
? ? public GameObject(int x, int y, int width, int height) {
? ? ? ? this.x = x;
? ? ? ? this.y = y;
? ? ? ? this.width = width;
? ? ? ? this.height = height;
? ? }
? ? public GameObject(int x, int y) {
? ? ? ? this.x = x;
? ? ? ? this.y = y;
? ? }
? ? public GameObject() {//無參構造
? ? }
? ? public void paintSelf(Graphics gImage){
? ? ? ? gImage.drawImage(image,x,y,null);
? ? }
? ? public Rectangle getRec(){
? ? ? ? return new Rectangle(x,y,width,height);
? ? }
}
背景
package object;
import utils.GameWindow;
import java.awt.*;
public class bgObj extends GameObject {
? ? public bgObj(Image image, int x, int y, int width, int height, double speed, GameWindow fram) {
? ? ? ? super(image, x, y, width, height, speed, fram);
? ? }
? ? public bgObj(Image image, int x, int y, double speed) {
? ? ? ? super(image, x, y, speed);
? ? }
? ? public bgObj() {
? ? ? ? super();
? ? }
? ? @Override
? ? public Image getImage() {
? ? ? ? return super.getImage();
? ? }
? ? @Override
? ? public void paintSelf(Graphics gImage) {
? ? ? ? super.paintSelf(gImage);
? ? ? ? //y+=speed;
? ? }
}
玩家
package object;
import utils.GameWindow;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class Plane extends GameObject{
? ? @Override
? ? public Image getImage() {
? ? ? ? return super.getImage();
? ? }
? ? public Plane(Image image, int x, int y, int width, int height, double speed, GameWindow fram) {
? ? ? ? super(image, x, y, width, height, speed, fram);
? ? ? ? fram.addMouseMotionListener(new MouseAdapter() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void mouseMoved(MouseEvent e) {//跟隨鼠標移動
? ? ? ? ? ? ? ? Plane.super.x=e.getX()-11;//鼠標位置減去飛機位置的一半相當于鼠標在飛機中間
? ? ? ? ? ? ? ? Plane.super.y=e.getY()-16;
? ? ? ? ? ? }
? ? ? ? });
? ? }
? ? public Plane(Image image, int x, int y, double speed) {
? ? ? ? super(image, x, y, speed);
? ? }
? ? public Plane() {
? ? ? ? super();
? ? }
? ? @Override
? ? public void paintSelf(Graphics gImage) {
? ? ? ? gImage.drawImage(image,x,y,width,height,null);
? ? ? ? //與敵方boss碰撞死亡
? ? ? ? if (this.fram.bossObj!=null&&this.getRec().intersects(this.fram.bossObj.getRec())){
? ? ? ? ? ? this.fram.state=3;
? ? ? ? ? ? this.fram.repaint();
? ? ? ? }
? ? }
? ? @Override
? ? public Rectangle getRec() {
? ? ? ? return super.getRec();
? ? }
}
子彈
package object;
import utils.GameUtils;
import utils.GameWindow;
import java.awt.*;
public class shellObj extends GameObject{
? ? @Override
? ? public Image getImage() {
? ? ? ? return super.getImage();
? ? }
? ? public shellObj(Image image, int x, int y, int width, int height, double speed, GameWindow fram) {
? ? ? ? super(image, x, y, width, height, speed, fram);
? ? }
? ? public shellObj() {
? ? ? ? super();
? ? }
? ? @Override
? ? public void paintSelf(Graphics gImage) {
? ? ? ? gImage.drawImage(image,x,y,width,height,null);
? ? ? ? y-=speed;
? ? ? ? if (y<0){
? ? ? ? ? ? this.x=100;
? ? ? ? ? ? this.y=-100;
? ? ? ? ? ? GameUtils.removeObjList.add(this);
? ? ? ? }
? ? }
? ? @Override
? ? public Rectangle getRec() {
? ? ? ? return super.getRec();
? ? }
}
敵機
package object;
import utils.GameUtils;
import utils.GameWindow;
import java.awt.*;
public class enemyObj extends GameObject{
? ? @Override
? ? public Image getImage() {
? ? ? ? return super.getImage();
? ? }
? ? public enemyObj(Image image, int x, int y, int width, int height, double speed, GameWindow fram) {
? ? ? ? super(image, x, y, width, height, speed, fram);
? ? }
? ? public enemyObj() {
? ? ? ? super();
? ? }
? ? @Override
? ? public void paintSelf(Graphics gImage) {
? ? ? ? gImage.drawImage(image,x,y,width,height,null);
? ? ? ? y+=speed;
? ? ? ? if (this.getRec().intersects(this.fram.planeObj.getRec())){
? ? ? ? ? ? GameWindow.state=3;
? ? ? ? ? ? this.fram.repaint();//重畫一幀
? ? ? ? }
? ? ? ? for (shellObj item: GameUtils.shellObjList) {
? ? ? ? ? ? if (this.getRec().intersects(item.getRec())){
? ? ? ? ? ? ? ? ExplodeObj obj=new ExplodeObj(x,y,40,40);
? ? ? ? ? ? ? ? GameUtils.explodeObjList.add(obj);
? ? ? ? ? ? ? ? GameUtils.removeObjList.add(obj);
? ? ? ? ? ? ? ? item.setX(100);
? ? ? ? ? ? ? ? item.setY(-100);
? ? ? ? ? ? ? ? this.x=-100;
? ? ? ? ? ? ? ? this.y=-100;
? ? ? ? ? ? ? ? GameUtils.removeObjList.add(item);
? ? ? ? ? ? ? ? GameUtils.removeObjList.add(this);
? ? ? ? ? ? ? ? GameWindow.score++;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? if (y>600){
? ? ? ? ? ? this.x=-100;
? ? ? ? ? ? this.y=-100;
? ? ? ? ? ? GameUtils.removeObjList.add(this);
? ? ? ? }
? ? }
? ? @Override
? ? public Rectangle getRec() {
? ? ? ? return super.getRec();
? ? }
}
敵方boss
package object;
import utils.GameUtils;
import utils.GameWindow;
import java.awt.*;
public class BossObj extends GameObject{
? ? int life=10;
? ? @Override
? ? public Image getImage() {
? ? ? ? return super.getImage();
? ? }
? ? public BossObj(Image image, int x, int y, int width, int height, double speed, GameWindow fram) {
? ? ? ? super(image, x, y, width, height, speed, fram);
? ? }
? ? public BossObj() {
? ? ? ? super();
? ? }
? ? @Override
? ? public void paintSelf(Graphics gImage) {
? ? ? ? gImage.drawImage(image,x,y,width,height,null);
? ? ? ? if (x>485||x<-40){
? ? ? ? ? ? speed=-speed;
? ? ? ? }
? ? ? ? x+=speed;
? ? ? ? for (shellObj item: GameUtils.shellObjList) {
? ? ? ? ? ? if (this.getRec().intersects(item.getRec())){
? ? ? ? ? ? ? ? item.setX(100);
? ? ? ? ? ? ? ? item.setY(-100);
? ? ? ? ? ? ? ? GameUtils.removeObjList.add(item);
? ? ? ? ? ? ? ? life--;
? ? ? ? ? ? }
? ? ? ? ? ? if (life<=0){
? ? ? ? ? ? ? ? GameUtils.removeObjList.add(this);
? ? ? ? ? ? ? ? GameWindow.state=4;
? ? ? ? ? ? ? ? this.fram.repaint();//重畫一幀
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? //血條的白色背景
? ? ? ? gImage.setColor(Color.white);
? ? ? ? gImage.fillRect(20,40,100,10);
? ? ? ? //紅血條的繪制
? ? ? ? gImage.setColor(Color.red);
? ? ? ? gImage.fillRect(20,40,life*100/10,10);
? ? }
? ? @Override
? ? public Rectangle getRec() {
? ? ? ? return super.getRec();
? ? }
}
敵方子彈
package object;
import utils.GameUtils;
import utils.GameWindow;
import java.awt.*;
public class BulletObj extends GameObject{
? ? @Override
? ? public Image getImage() {
? ? ? ? return super.getImage();
? ? }
? ? public BulletObj(Image image, int x, int y, int width, int height, double speed, GameWindow fram) {
? ? ? ? super(image, x, y, width, height, speed, fram);
? ? }
? ? public BulletObj() {
? ? ? ? super();
? ? }
? ? @Override
? ? public void paintSelf(Graphics gImage) {
? ? ? ? gImage.drawImage(image,x,y,width,height,null);
? ? ? ? y+=speed;
? ? ? ? if (this.getRec().intersects(this.fram.planeObj.getRec())){
? ? ? ? ? ? this.fram.state=3;
? ? ? ? ? ? this.fram.repaint();
? ? ? ? }
? ? ? ? if (y>600){
? ? ? ? ? ? this.x=-300;
? ? ? ? ? ? this.y=-300;
? ? ? ? ? ? GameUtils.removeObjList.add(this);
? ? ? ? }
? ? }
? ? @Override
? ? public Rectangle getRec() {
? ? ? ? return super.getRec();
? ? }
}
敵方的爆炸效果
package object;
import utils.GameUtils;
import utils.GameWindow;
import java.awt.*;
public class ExplodeObj extends GameObject {
? ? //調用的幀數
? ? int count=0;
? ? Image explode=Toolkit.getDefaultToolkit().getImage("Images/boom.gif");
? ? @Override
? ? public Image getImage() {
? ? ? ? return super.getImage();
? ? }
? ? public ExplodeObj(Image image, int x, int y, int width, int height, double speed, GameWindow fram) {
? ? ? ? super(image, x, y, width, height, speed, fram);
? ? }
? ? public ExplodeObj(int x, int y) {
? ? ? ? super(x, y);
? ? }
? ? public ExplodeObj() {
? ? ? ? super();
? ? }
? ? public ExplodeObj(int x, int y, int width, int height) {
? ? ? ? super(x, y, width, height);
? ? }
? ? @Override
? ? public void paintSelf(Graphics gImage) {
? ? ? ? if (count<16){
? ? ? ? ? ? super.image=explode;
? ? ? ? ? ? gImage.drawImage(image,x,y,width,height,null);//要可以設置大小
? ? ? ? ? ? count++;
? ? ? ? }
? ? ? ? //super.paintSelf(gImage);
? ? }
? ? @Override
? ? public Rectangle getRec() {
? ? ? ? return super.getRec();
? ? }
}
工具類
package utils;
import object.*;
import javax.tools.Tool;
import java.util.*;
import java.awt.*;
import java.util.List;
public class GameUtils {
? ? //背景圖片
? ? public static Image bgImage = Toolkit.getDefaultToolkit().getImage("Images/backGroup.jpg");
? ? //BOSS圖片
? ? public static Image bossImage = Toolkit.getDefaultToolkit().getImage("Images/boss.png");
? ? //動態爆炸圖片
? ? public static Image boomImage = Toolkit.getDefaultToolkit().getImage("Images/boom.gif");
? ? //我方飛機爆炸圖片
? ? public static Image boom_2Image = Toolkit.getDefaultToolkit().getImage("Images/boom2.png");
? ? //敵方飛機爆炸圖片
? ? public static Image boom_3Image = Toolkit.getDefaultToolkit().getImage("Images/boom3.png");
? ? //己方飛機
? ? public static Image planeImage=Toolkit.getDefaultToolkit().getImage("Images/plane2.png");
? ? //我方子彈
? ? public static Image shellImage=Toolkit.getDefaultToolkit().getImage("Images/shell.png");
? ? //敵方飛機
? ? public static Image enemyImage=Toolkit.getDefaultToolkit().getImage("Images/enemy.png");
? ? //敵方子彈
? ? public static Image bulletImage=Toolkit.getDefaultToolkit().getImage("Images/bullet.png");
? ? //物體的集合
? ? public static List<GameObject> gameObjList=new ArrayList<>();
? ? //子彈的集合
? ? public static List<shellObj> shellObjList=new ArrayList();
? ? //敵方飛機集合
? ? public static List<enemyObj> enemyObjList=new ArrayList();
? ? //Boss子彈的集合
? ? public static List<BulletObj> bulletObjList=new ArrayList();
? ? //需要移除的目標的集合
? ? public static List<GameObject> removeObjList=new ArrayList<>();
? ? //敵機爆炸的集合
? ? public static List<ExplodeObj> explodeObjList=new ArrayList<>();
? ? //寫字
? ? public static void DrawWord(Graphics gGrafics,String word,Color color,int size,int x,int y){
? ? ? ? gGrafics.setColor(color);//設置字體顏色
? ? ? ? gGrafics.setFont(new Font("仿宋",Font.BOLD,size));
? ? ? ? gGrafics.drawString(word,x,y);
? ? }
}
窗口類
package utils;
import object.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class GameWindow extends JFrame {
? ? //0未開始,1游戲中,2暫停,3通關失敗,4,通關成功
? ? public static int state=0;
? ? //分數
? ? public static int score=0;
? ? //生成多少飛機后生成boss
? ? int num=100;
? ? int width=600;
? ? int height=600;
? ? //畫布重畫的次數
? ? int count=1;
? ? //敵機生成數目
? ? int enemy_count=0;
? ? //開雙緩存的圖片
? ? Image offImage=null;
? ? //背景
? ? bgObj bgOb =new bgObj(GameUtils.bgImage,0,0,2);
? ? //玩家飛機
? ? public Plane planeObj =new Plane(GameUtils.planeImage,100,100,30,40,0,this);
? ? //Boss
? ? public BossObj bossObj=null;
? ? public void Start(){
? ? ? ? this.setVisible(true);
? ? ? ? this.setSize(width,height);//大小
? ? ? ? this.setLocationRelativeTo(null);//設置在屏幕中間
? ? ? ? this.setTitle("飛機大戰之Boss決戰篇");
? ? ? ? //關掉后清理緩存
? ? ? ? this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
? ? ? ? //添加一個監聽器MouseAdapter,然后重寫他的方法
? ? ? ? this.addMouseListener(new MouseAdapter(){
? ? ? ? ? ? @Override
? ? ? ? ? ? public void mouseClicked(MouseEvent e) {
? ? ? ? ? ? ? ? if (e.getButton()==1&&state==0){
? ? ? ? ? ? ? ? ? ? state=1;
? ? ? ? ? ? ? ? ? ? repaint();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? });
? ? ? ? //添加一個鍵盤的監聽事件
? ? ? ? this.addKeyListener(new KeyAdapter() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void keyPressed(KeyEvent e) {
? ? ? ? ? ? ? ? if (e.getKeyCode()==' '){//按下空格就暫停,暫停就相當于不再重畫畫布
? ? ? ? ? ? ? ? ? ? switch (state){
? ? ? ? ? ? ? ? ? ? ? ? case 1:
? ? ? ? ? ? ? ? ? ? ? ? ? ? state=2;
? ? ? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? ? ? case 2:
? ? ? ? ? ? ? ? ? ? ? ? ? ? state=1;
? ? ? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? ? ? default:
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? });
? ? ? ? GameUtils.gameObjList.add(bgOb);
? ? ? ? GameUtils.gameObjList.add(planeObj);
? ? ? ? while (true){
? ? ? ? ? ? if (state==1||state==0){
? ? ? ? ? ? ? ? if (state==1){
? ? ? ? ? ? ? ? ? ? creatGameObj();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? repaint();
? ? ? ? ? ? }
? ? ? ? ? ? try {
? ? ? ? ? ? ? ? Thread.sleep(25);//每秒調用40次
? ? ? ? ? ? } catch (InterruptedException ex) {
? ? ? ? ? ? ? ? System.out.println("圖片解析出錯,錯誤原因:"+ex.getMessage());
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? @Override
? ? public void paint(Graphics g) {
? ? ? ? if (offImage==null){
? ? ? ? ? ? offImage=createImage(width,height) ;
? ? ? ? }
? ? ? ? Graphics gGraphic=offImage.getGraphics();//使用兩張圖片用雙緩存來畫防止閃爍
? ? ? ? gGraphic.fillRect(0,0,width,height);
? ? ? ? //游戲未開始時繪圖
? ? ? ? if (state==0){
? ? ? ? ? ? try {
? ? ? ? ? ? ? ? MediaTracker mediaTracker = new MediaTracker(new Container());
? ? ? ? ? ? ? ? mediaTracker.addImage(GameUtils.bgImage, 0);
? ? ? ? ? ? ? ? mediaTracker.addImage(GameUtils.bossImage, 1);
? ? ? ? ? ? ? ? mediaTracker.addImage(GameUtils.boomImage, 2);
? ? ? ? ? ? ? ? mediaTracker.waitForAll();
? ? ? ? ? ? ? ? gGraphic.drawImage(GameUtils.bgImage,0,0,null);//讓圖片加載完以后再顯示
? ? ? ? ? ? ? ? gGraphic.drawImage(GameUtils.bossImage,225,100,150,150,null);//boss
? ? ? ? ? ? ? ? gGraphic.drawImage(GameUtils.boomImage,250,380,100,100,null);//爆炸
? ? ? ? ? ? ? ? GameUtils.DrawWord(gGraphic,"點擊此處游戲開始",Color.blue,35,170,320);//文字標識
? ? ? ? ? ? } catch (InterruptedException ex) {
? ? ? ? ? ? ? ? System.out.println("圖片解析出錯,錯誤原因:"+ex.getMessage());
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? //游戲開始時繪圖
? ? ? ? else if (state==1){
? ? ? ? ? ? GameUtils.gameObjList.addAll(GameUtils.explodeObjList);
? ? ? ? ? ? //將物件集合中所有東西畫出
? ? ? ? ? ? for (int i=0;i<GameUtils.gameObjList.size();i++){
? ? ? ? ? ? ? ? GameUtils.gameObjList.get(i).paintSelf(gGraphic);
? ? ? ? ? ? }
? ? ? ? ? ? //分數顯示
? ? ? ? ? ? GameUtils.DrawWord(gGraphic,"得分: "+score+" 分",Color.green,30,30,100);//文字標識
? ? ? ? ? ? //將所有要移除的物件移除
? ? ? ? ? ? GameUtils.gameObjList.removeAll(GameUtils.removeObjList);
? ? ? ? }
? ? ? ? //暫停
? ? ? ? else if (state==2){
? ? ? ? ? ? //不做任何事就是暫停
? ? ? ? }
? ? ? ? //游戲結束
? ? ? ? else if (state==3){
? ? ? ? ? ? try {
? ? ? ? ? ? ? ? //讓場景保持不變
? ? ? ? ? ? ? ? for (int i=0;i<GameUtils.gameObjList.size();i++){
? ? ? ? ? ? ? ? ? ? GameUtils.gameObjList.get(i).paintSelf(gGraphic);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? //額外畫出一些字
? ? ? ? ? ? ? ? MediaTracker mediaTracker = new MediaTracker(new Container());
? ? ? ? ? ? ? ? mediaTracker.addImage(GameUtils.boom_2Image, 0);
? ? ? ? ? ? ? ? mediaTracker.waitForID(0);
? ? ? ? ? ? ? ? gGraphic.drawImage(GameUtils.boom_2Image,planeObj.getX()-20, planeObj.getY()-15, null);//爆炸
? ? ? ? ? ? ? ? GameUtils.DrawWord(gGraphic,"菜",Color.red,80,270,320);//文字標識
? ? ? ? ? ? } catch (InterruptedException ex) {
? ? ? ? ? ? ? ? System.out.println("圖片解析出錯,錯誤原因:"+ex.getMessage());
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? //游戲通關
? ? ? ? else if (state==4){
? ? ? ? ? ? try {
? ? ? ? ? ? ? ? //先讓不該出現的東西消失
? ? ? ? ? ? ? ? GameUtils.gameObjList.removeAll(GameUtils.removeObjList);
? ? ? ? ? ? ? ? //讓場景保持不變
? ? ? ? ? ? ? ? for (int i=0;i<GameUtils.gameObjList.size();i++){
? ? ? ? ? ? ? ? ? ? GameUtils.gameObjList.get(i).paintSelf(gGraphic);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? //額外畫出一些字
? ? ? ? ? ? ? ? MediaTracker mediaTracker = new MediaTracker(new Container());
? ? ? ? ? ? ? ? mediaTracker.addImage(GameUtils.boom_3Image, 0);
? ? ? ? ? ? ? ? mediaTracker.waitForID(0);
? ? ? ? ? ? ? ? gGraphic.drawImage(GameUtils.boom_3Image,bossObj.getX()+30, bossObj.getY(), null);//爆炸
? ? ? ? ? ? ? ? GameUtils.DrawWord(gGraphic,"雖然你贏了,但你還是菜",Color.red,30,140,320);//文字標識
? ? ? ? ? ? } catch (InterruptedException ex) {
? ? ? ? ? ? ? ? System.out.println("圖片解析出錯,錯誤原因:"+ex.getMessage());
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? g.drawImage(offImage,0,0,null);
? ? ? ? count++;
? ? }
? ? void creatGameObj(){
? ? ? ? //我方子彈的生成
? ? ? ? if (count%15==0){
? ? ? ? ? ? GameUtils.shellObjList.add(new shellObj(GameUtils.shellImage,planeObj.getX()+8, planeObj.getY()-16,14,29,5,this));
? ? ? ? ? ? GameUtils.gameObjList.add(GameUtils.shellObjList.get(GameUtils.shellObjList.size()-1));
? ? ? ? }
? ? ? ? //敵機的生成
? ? ? ? if (count%15==0){
? ? ? ? ? ? GameUtils.enemyObjList.add(new enemyObj(GameUtils.enemyImage,(int)(Math.random()*12)*50, 0,49,36,5,this));
? ? ? ? ? ? GameUtils.gameObjList.add(GameUtils.enemyObjList.get(GameUtils.enemyObjList.size()-1));
? ? ? ? ? ? enemy_count++;
? ? ? ? }
? ? ? ? //敵方子彈的生成
? ? ? ? if (bossObj!=null&&count%15==0){
? ? ? ? ? ? GameUtils.bulletObjList.add(new BulletObj(GameUtils.bulletImage,bossObj.getX()+76, bossObj.getY()+85,14,29,5,this));
? ? ? ? ? ? GameUtils.gameObjList.add(GameUtils.bulletObjList.get(GameUtils.bulletObjList.size()-1));
? ? ? ? }
? ? ? ? if (enemy_count>num&&bossObj==null){
? ? ? ? ? ? bossObj=new BossObj(GameUtils.bossImage,250,35,155,100,5,this);
? ? ? ? ? ? GameUtils.gameObjList.add(bossObj);
? ? ? ? }
? ? }
}
主程序
import object.*;
import utils.*;
public class PlanWarMain {
? ? public static void main(String[] args) {
? ? ? ? GameWindow gWin=new GameWindow();
? ? ? ? gWin.Start();
? ? }
}
效果圖
沒有長的可以循環的背景就隨便弄了個背景(當然,是懶得做,誒嘿)
?
?
?
?
?
總結
以上是生活随笔為你收集整理的Java打飞机小游戏的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 星球大战Java实验报告_一份被隐瞒多年
- 下一篇: 激光测距原理与方法