java-实现飞机大战小游戏
初学Java,看完同学写的飞机大战,感觉内容有点多了,不过别人写的真挺好,希望高手给予点意见
package view;import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Map;import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;import subclass.model.Bee;
import subclass.model.Bullet;
import subclass.model.Enemy;
import subclass.model.Hero;@SuppressWarnings("serial")
public class ShootGame extends JPanel {public static final int WIDTH = 400; // 窗体宽度public static final int HEIGHT = 650; // 窗体高度public static final String READY = "READY"; //游戏准备符号public static final String PLAY = "PLAY"; //游戏开始符号public static final String PAUSE = "PAUSE"; //游戏暂停符号public static final int ADD = 1; //飞机的射速public static final int RATE = -50; //每增加一倍数减少的毫秒public static BufferedImage bee;public static BufferedImage bg;public static BufferedImage bullet;public static BufferedImage enemy;public static BufferedImage hero1;public static BufferedImage hero2;public static BufferedImage over;public static BufferedImage pause;public static BufferedImage icon;public static BufferedImage start;private String gameSate = null; //设置游戏状态private final boolean START = true; //设置所有线程的循环条件private final int ShootRate = 4; //最大射速private final int timeBG = 10; // 每10毫秒更新一次画面private int timeEnemy = 1000; // 每1000MS生成一个敌机private int timeBee = 500; // 每10MS生成一个蜜蜂private int enemyNum = 9; // 允许画面内所存在的敌机数量private int bulletNum = 18; // 允许画面内所存在的子弹数量private int beeNum = 10; // 允许画面内所存在的蜜蜂数量private final static String name = "The plane of thunder";public static Hero hero;public Bee beeO;private JFrame frame;private Thread tadBG;private Thread tadBullet;private Thread tadEnemy;private Thread tadBee;//创建对象集合private Map<Integer, Bullet> bulletMap = new HashMap<>();private Map<Integer, Bullet> bulletRight = new HashMap<>();private Map<Integer, Bullet> bulletLeft = new HashMap<>();private Map<Integer, Enemy> enemyMap = new HashMap<>();// 使图片加载到内存中static {try {bee = ImageIO.read(ShootGame.class.getResource("/img/bee.png"));bg = ImageIO.read(ShootGame.class.getResource("/img/bg.png"));bullet = ImageIO.read(ShootGame.class.getResource("/img/bullet.png"));enemy = ImageIO.read(ShootGame.class.getResource("/img/enemy.png"));hero1 = ImageIO.read(ShootGame.class.getResource("/img/hero1.png"));hero2 = ImageIO.read(ShootGame.class.getResource("/img/hero2.png"));over = ImageIO.read(ShootGame.class.getResource("/img/over.png"));pause = ImageIO.read(ShootGame.class.getResource("/img/pause.png"));icon = ImageIO.read(ShootGame.class.getResource("/img/icon.png"));start = ImageIO.read(ShootGame.class.getResource("/img/start.png"));} catch (Exception e) {e.printStackTrace();}}/*** 初始化飞机和窗体布局以及所有线程* */public ShootGame() {this.setGameSate(READY);this.setFrame(this.geteFrame());hero = new Hero();tadBG = new Thread(this.new RunBg());tadBullet = new Thread(this.new RunBullet());tadEnemy = new Thread(this.new RunEnemy());tadBee = new Thread(this.new RunBee());tadBee.start();tadEnemy.start();tadBG.start();tadBullet.start();} /*** FunName: getFrame Description:初始化窗口并返回一个窗口* @return JFrame* @param null*/public JFrame geteFrame() {JFrame frame = new JFrame(name);frame.setVisible(true);frame.setResizable(false);frame.setSize(WIDTH, HEIGHT);frame.setIconImage(ShootGame.icon);frame.setLocationRelativeTo(null); // 调整窗口的相对位置(null)frame.setAlwaysOnTop(true); // 使窗口始终保持在桌面最上方frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);frame.setContentPane(this);frame.addMouseMotionListener(this.new Mouse());frame.addKeyListener(this.new Key());return frame;}//主方法public static void main(String[] args) {@SuppressWarnings("unused")ShootGame game = new ShootGame();}public int getShootRate() {return this.ShootRate;}public void setBulletNum(int bulletNum) {this.bulletNum = getBulletNum() + bulletNum;}public int getBulletNum() {return this.bulletNum;}public String getGameSate() {return gameSate;}public void setGameSate(String gameSate) {this.gameSate = gameSate;}public JFrame getFrame() {return frame;}public void setFrame(JFrame frame) {this.frame = frame;}/*** FunName: vCollision Description:检测各个图像的体积碰撞* * @return null* @param null*/public void bulletCollision(Rectangle enemy, Map<Integer, Bullet> bullet, int num) {for (int num2 = 0; num2 < bulletNum; num2++) {// 判断当前键值是否存在Bullet的实例if (bullet.get(num2) != null) {Bullet bu = bullet.get(num2);// 创建Rectangle的实例 buRec 使 bu 图像矩阵化Rectangle buRec = new Rectangle(bu.getX(), bu.getY(), bu.getWidth(), bu.getHeight());// 判断 enmyRec 图像与 buRec 图像的面积是否存在交集,若存在交集则发生碰撞if (enemy.intersects(buRec)) {hero.setScore(1);// 移除当前键值所映射的对象enemyMap.remove(num);bullet.remove(num2);break;}}}}public void beeCollision(Rectangle bee, Map<Integer, Bullet> bullet) {/* 遍历Bullet所有实例与beeO的碰撞检测 */for (int num = 0; num < bulletNum; num++) {// 判断当前键值是否存在Bullet的实例if (bullet.get(num) != null) {Bullet bu = bullet.get(num);// 创建Rectangle的实力 buRec 使 bu 图像矩阵化Rectangle buRec = new Rectangle(bu.getX(), bu.getY(), bu.getWidth(), bu.getHeight());// 判断 buRec 图像与 beRec 图像的面积是否存在交集,若存在交集则发生碰撞if (bee.intersects(buRec)) {if (beeO != null) {//对beeO所携带的奖励检测switch (beeO.getAwardType()) {case Bee.DOUBLE_FIRE: {if (hero.getShootMode().equals(Hero.SINGLE_SHOT)) {setBulletNum(bulletNum);hero.setShootMode(Hero.DOUBLE_SHOT);} else {hero.setScore(1);}break;}case Bee.THREE_FIRE: {if (hero.getShootMode().equals(Hero.DOUBLE_SHOT)) {hero.setShootMode(Hero.THREE_SHOT);setBulletNum(bulletNum);} else {hero.setScore(1);}break;}case Bee.LIFE: {hero.setLife(Bee.LIFE);break;}case Bee.SHOT_SPEED: {if (hero.getShootRate() < getShootRate()) {hero.setShootRate(ADD);hero.setFireRate(RATE);}break;}default: {break;}}beeO = null;bullet.remove(num);break;}}}}}public void vCollision() {// 创建Rectangle的实例 heRec 使 hero 图像矩阵化Rectangle heRec = new Rectangle(hero.getX(), hero.getY(), hero.getWidth(), hero.getHeight());/* 遍历Enemy所有实例与Bullet所有实的碰撞检测 */for (int num = 0; num < enemyNum; num++) {// 检测当前键值所映射的Enemy实例是否存在if (enemyMap.get(num) != null) {Enemy en = enemyMap.get(num);// 创建Rectangle的实力 enmyRec 使 en 图像矩阵化Rectangle enmyRec = new Rectangle(en.getX(), en.getY(), en.getWidth(), en.getHeight());bulletCollision(enmyRec, bulletMap, num);bulletCollision(enmyRec, bulletRight, num);bulletCollision(enmyRec, bulletLeft, num);}}/* 遍历Enemy所有实例与hero的碰撞检测 */for (int num = 0; num < enemyNum; num++) {// 检测当前键值所映射的Enemy实例是否存在if (enemyMap.get(num) != null) {Enemy en = enemyMap.get(num);// 创建Rectangle的实力 enmyRec 使 en 图像矩阵化Rectangle enmyRec = new Rectangle(en.getX(), en.getY(), en.getWidth(), en.getHeight());// 判断 hero 是否存在if (enmyRec.intersects(heRec)) {enemyMap.remove(num);hero.setLife(-1);if (hero.getLife() < 1) {hero = null;}break;} else {continue;}}}// 判断Bee是否有实例化if (beeO != null) {// 创建Rectangle的实力 beRec 使 beeO 图像矩阵化Rectangle beRec = new Rectangle(beeO.getX(), beeO.getY(), beeO.getWidth(), beeO.getHeight());beeCollision(beRec, bulletRight);beeCollision(beRec, bulletLeft);beeCollision(beRec, bulletMap);}}/*** Description:此类作为全图像更新类*/public class RunBg implements Runnable {/*** FunName: run* * @return null* @param null*/public void run() {while (START) {try {// 每秒更新全图像的次数Thread.sleep(timeBG);} catch (Exception e) {e.printStackTrace();}// 判断飞机和游戏的状态若飞机存在且游戏处于PLAY状态则进行碰撞检测再重绘画if (getGameSate().equals(READY)) {repaint();} else if (ShootGame.hero != null && getGameSate().equals(PLAY)) {vCollision();repaint();} else if (getGameSate().equals(PAUSE) && ShootGame.hero != null) {repaint();} else {break;}}}}/*** Description:此类作为实例化Bullet类*/public class RunBullet implements Runnable {/*** FunName: run* * @return null* @param null*/public void run() {while (START) {try {// 每秒实例化Bullet类的次数if (hero != null) {Thread.sleep(hero.getFireRate());}} catch (Exception e) {e.printStackTrace();}if (getGameSate().equals(PAUSE)) {continue;} else if (getGameSate().equals(PLAY)) {setBullet_Single();setBullet_Double();}}}}/*** Description:此类作为实例化Enemy类*/public class RunEnemy implements Runnable {/*** FunName: run* * @return null* @param null*/public void run() {while (START) {try {// 每秒实例化Enemy类的次数Thread.sleep(timeEnemy);} catch (Exception e) {e.printStackTrace();}if (getGameSate().equals(PAUSE)) {continue;} else if (getGameSate().equals(PLAY)) {setEnemy();}}}}/*** Description:此类作为实例化Bee类*/public class RunBee implements Runnable {/*** FunName: run* * @return null* @param null*/public void run() {while (START) {try {// 每秒实例化Bee类的次数Thread.sleep(timeBee);} catch (Exception e) {e.printStackTrace();}if (getGameSate().equals(PAUSE)) {continue;} else if (getGameSate().equals(PLAY)) {setBee();}}}}/*** FunName: setBullet* * @Description: 创建Bullet对象并用HashMap集合储存* @return null* @param null*/public void setBullet_Single() {// bulletNum:限制实例化Bullet类的数量for (int num = 0; num < bulletNum; num++) {// 判断当前键值是否存在Bullet的实例if (bulletMap.get(num) != null) {// 判断子弹是否越过上边界if (bulletMap.get(num).getY() < 0) {// 实例化新的Bullet类使此对象代替当前键值所映射的对象if (hero != null) {if (hero.getShootMode().equals(Hero.SINGLE_SHOT)|| hero.getShootMode().equals(Hero.THREE_SHOT)) {bulletMap.replace(num, new Bullet());break;} else {bulletMap.remove(num);break;}}} else {continue;}} else {// 实例化新的Bullet类并添加到当前键值if (hero != null) {if (hero.getShootMode().equals(Hero.SINGLE_SHOT) || hero.getShootMode().equals(Hero.THREE_SHOT)) {bulletMap.put(num, new Bullet());break;}}}}}public void setBullet_Double() {for (int num = 0; num < bulletNum; num++) {if (bulletRight.get(num) != null) {if (bulletRight.get(num).getY() < 0) {if (hero != null) {if (hero.getShootMode().equals(Hero.DOUBLE_SHOT)|| hero.getShootMode().equals(Hero.THREE_SHOT)) {bulletRight.replace(num, new Bullet());bulletRight.get(num).setRight();break;} else {bulletRight.remove(num);break;}} else {continue;}}} else {if (hero != null) {if (hero.getShootMode().equals(Hero.DOUBLE_SHOT) || hero.getShootMode().equals(Hero.THREE_SHOT)) {bulletRight.put(num, new Bullet());bulletRight.get(num).setRight();break;}}}}for (int num = 0; num < bulletNum; num++) {if (bulletLeft.get(num) != null) {if (bulletLeft.get(num).getY() < 0) {if (hero != null) {if (hero.getShootMode().equals(Hero.DOUBLE_SHOT)|| hero.getShootMode().equals(Hero.THREE_SHOT)) {bulletLeft.replace(num, new Bullet());bulletLeft.get(num).setLeft();break;} else {bulletLeft.remove(num);break;}}} else {continue;}} else {if (hero != null) {if (hero.getShootMode().equals(Hero.DOUBLE_SHOT) || hero.getShootMode().equals(Hero.THREE_SHOT)) {bulletLeft.put(num, new Bullet());bulletLeft.get(num).setLeft();break;}}}}}/*** FunName: setEnemy* * @Description: 创建Enemy对象并用HashMap集合储存* @return null* @param null*/public void setEnemy() {// enemyNum:限制实例化Bullet类的数量for (int num = 0; num < enemyNum; num++) {// 判断当前键值是否存在Enemy的实例if (enemyMap.get(num) != null) {// 判断敌机是否越过下边界if (enemyMap.get(num).getY() > ShootGame.HEIGHT) {// 实例化新的Enemy类使此对象代替当前键值所映射的对象enemyMap.replace(num, new Enemy());break;} else {continue;}} else {// 实例化新的Enemy类并添加到当前键值enemyMap.put(num, new Enemy());break;}}}/*** FunName: setBee* * @Description: 创建Bee对象* @return null* @param null*/public void setBee() {// beeNum:限制实例化Bullet类的数量for (int num = 0; num < beeNum; num++) {// 判断beeO是否存在if (beeO != null) {// 判断beeO是否越过下边界if (beeO.getY() > ShootGame.HEIGHT) {beeO = null;break;} else {continue;}} else {beeO = new Bee().getBee();break;}}}public class Mouse implements MouseMotionListener {public void mouseDragged(MouseEvent e) {}public void mouseMoved(MouseEvent e) {/* 判断hero是否存在 */if (ShootGame.hero != null && getGameSate().equals(PLAY)) {hero.setX(e.getX());// 设置X位置,鼠标所在的X位置作为参数hero.setY(e.getY());// 设置Y位置,鼠标所在的Y位置作为参数hero.move();}}}public class Key implements KeyListener {public void keyTyped(KeyEvent e) {}public void keyPressed(KeyEvent e) {switch (e.getKeyChar()) {case KeyEvent.VK_SPACE: {// 判断目前游戏的状态if (getGameSate().equals(PAUSE)) {setGameSate(PLAY);} else if (getGameSate().equals(PLAY)) {setGameSate(PAUSE);}break;}case KeyEvent.VK_ENTER: {if (getGameSate().equals(READY)) {setGameSate(PLAY);}break;}case KeyEvent.VK_ESCAPE: {System.exit(0);}default: {break;}}}public void keyReleased(KeyEvent e) {}}public void paint(Graphics g) {if (hero != null && (getGameSate().equals(PLAY) || getGameSate().equals(READY))) {if (getGameSate().equals(PLAY)) {g.drawImage(ShootGame.bg, 0, 0, null);} else if (getGameSate().equals(READY)) {g.drawImage(ShootGame.start, 0, 0, null);}} else if (getGameSate().equals(PAUSE)) {g.drawImage(ShootGame.pause, 0, 0, null);} else if (ShootGame.hero == null) {paintOver(g);} else {}if (ShootGame.hero != null) {paintEnemy(g);paintBullet(g);paintRight(g);paintLeft(g);paintBee(g);paintHero(g);paintSocer(g);}}public void paintOver(Graphics g) {g.drawImage(ShootGame.over, 0, 0, null);}public void paintHero(Graphics g) {if (hero != null) {g.drawImage(hero.getImage(), hero.getX(), hero.getY(), null);}}public void paintSocer(Graphics g) {g.setColor(Color.RED);g.setFont(new Font("宋体", Font.BOLD, 16));if (hero != null) {g.drawString("Socer:" + hero.getScore(), 10, 30);g.drawString("Life:" + hero.getLife(), 10, 50);g.setFont(new Font("宋体", Font.BOLD, 13));g.drawString("FireRate:" + hero.getShootRate() + "X", 10, 70);g.drawString("ShootMode:" + hero.getShootMode(), 10, 90);}}public void paintEnemy(Graphics g) {for (int num = 0; num < enemyNum; num++) {// 判断当前键值是否存在Enemy的实例if (enemyMap.get(num) != null) {g.drawImage(this.enemyMap.get(num).getImage(), this.enemyMap.get(num).getX(),this.enemyMap.get(num).getY(), null);// 若hero存在且游戏状态为PLAY进行绘画if (hero != null && getGameSate().equals(PLAY)) {enemyMap.get(num).move();} else if (getGameSate().equals(PAUSE)) {continue;}}}}/*** FunName:paintBullet 此方法计算Bullet类所有实例的坐标并绘画出来* * @return null* @param g* 调用Graphics 绘画图像*/public void paintBullet(Graphics g) {for (int num = 0; num < bulletNum; num++) {// 判断当前键值是否存在Bullet的实例if (this.bulletMap.get(num) != null) {g.drawImage(bulletMap.get(num).getImage(), bulletMap.get(num).getX(), bulletMap.get(num).getY(), null);// 若hero存在且游戏状态为PLAY进行绘画if (hero != null && getGameSate().equals(PLAY)) {bulletMap.get(num).move();} else if (getGameSate().equals(PAUSE)) {continue;}}}}public void paintRight(Graphics g) {for (int num = 0; num < bulletNum; num++) {if (this.bulletRight.get(num) != null) {g.drawImage(bulletRight.get(num).getImage(), bulletRight.get(num).getX(), bulletRight.get(num).getY(),null);if (hero != null && getGameSate().equals(PLAY)) {this.bulletRight.get(num).setY(this.bulletRight.get(num).getY() - this.bulletRight.get(num).getYSpeed());} else if (getGameSate().equals(PAUSE)) {continue;}}}}public void paintLeft(Graphics g) {for (int num = 0; num < bulletNum; num++) {if (this.bulletLeft.get(num) != null) {g.drawImage(bulletLeft.get(num).getImage(), bulletLeft.get(num).getX(), bulletLeft.get(num).getY(),null);if (hero != null && getGameSate().equals(PLAY)) {this.bulletLeft.get(num).setY(this.bulletLeft.get(num).getY() - this.bulletLeft.get(num).getYSpeed());} else if (getGameSate().equals(PAUSE)) {continue;}}}}public void paintBee(Graphics g) {for (int num = 0; beeO != null; num++) {if (num > 0) {break;} else {g.drawImage(beeO.getImage(), beeO.getX(), beeO.getY(), null);// 若hero存在且游戏状态为PLAY进行绘画if (hero != null && getGameSate().equals(PLAY)) {beeO.move();} else if (getGameSate().equals(PAUSE)) {continue;}}}}
}
package controller;public interface Award {public int DOUBLE_FIRE=0;public int THREE_FIRE=3;public int LIFE=1;public int SHOT_SPEED=2;public void setAward(int num);
}
package controller;
public interface Award {
public int DOUBLE_FIRE=0;
public int THREE_FIRE=3;
public int LIFE=1;
public int SHOT_SPEED=2;
public void setAward(int num);
}
package controller;public interface Score {
public void setScore(int scoer);
public String getScore();
}
下面是飞行物的父类,和各个子类
/*** */
package superclass.model;import java.awt.image.BufferedImage;/*** @author Liu**/
public class FlyingObject {
protected int width = 0;
protected int height = 0;
protected int x = 0;
protected int y = 0;
protected BufferedImage imgSrc = null;
protected int ySpeed = 0;
protected int xSpeed = 0; public BufferedImage getImage() {
return imgSrc;
} public void setWidth(int width) {
this.width = width;
} public void setHeight(int height) {
this.height = height;
} public void setX(int x) {
this.x = x;
} public void setY(int y) {
this.y = y;
} public int getWidth() {
return this.width;
} public int getHeight() {
return this.height;
} public int getX() {
return this.x;
} public int getY() {
return this.y;
} public int getXSpeed() {
return xSpeed;
} public void setXSpeed(int xSpeed) {
this.xSpeed = xSpeed;
} public int getYSpeed() {
return ySpeed;
} public void setYSpeed(int ySpeed) {
this.ySpeed = ySpeed;
}}
package subclass.model;import java.util.Random;import controller.Award;
import controller.Flying;
import superclass.model.FlyingObject;
import view.ShootGame;public class Bee extends FlyingObject implements Award,Flying{
private int xSpeed;
private int ySpeed;
private int awardType = 0;
private String arrwo = "right"; public Bee() {
this.imgSrc = ShootGame.bee;
this.width = this.imgSrc.getWidth();
this.height = this.imgSrc.getHeight();
this.y = -this.height-20;
this.x = new Random().nextInt(ShootGame.WIDTH - this.width + 1);
this.setAward(new Random().nextInt(5) - 1);
this.setXSpeed(3);
this.setYSpeed(2);
} public int getYSpeed() {
return ySpeed;
} public void setYSpeed(int ySpeed) {
this.ySpeed = ySpeed;
} public int getXSpeed() {
return this.xSpeed;
} public void setXSpeed(int xSpeed) {
this.xSpeed = xSpeed;
} public Bee getBee() {
if (new Random().nextInt(7000) == 5) {
return new Bee();
} else {
return null;
}
} @Override
public void setAward(int num) {
switch (num) {
case Award.DOUBLE_FIRE: {
setAwardType(DOUBLE_FIRE);
break;
}
case Award.LIFE: {
setAwardType(LIFE);
break;
}
case Award.SHOT_SPEED: {
setAwardType(SHOT_SPEED);
break;
}
case Award.THREE_FIRE: {
setAwardType(THREE_FIRE);
break;
}
default: {
break;
}
}
} public String getArrow() {
return this.arrwo;
} public void setArrow(String arrwo) {
this.arrwo = arrwo;
} public int getAwardType() {
return awardType;
} public void setAwardType(int awardType) {
this.awardType = awardType;
} @Override
public void move() {
this.setY(this.getY() + this.getYSpeed());
// 判断Bee的对象是否碰撞到右边框
if (this.getX() > ShootGame.WIDTH - this.getImage().getWidth()) {
this.setArrow("right");//
// 判断Bee的对象是否碰撞到右边框
} else if (this.getX() < 0) {
this.setArrow("left");
}
// 判断beeO在X轴的方向
if (this.getArrow().equals("right")) {
this.setX(this.getX() - this.getXSpeed());
} else {
this.setX(this.getX() + this.getXSpeed());
}
}
}
package subclass.model;import controller.Flying;
import superclass.model.FlyingObject;
import view.ShootGame;public class Bullet extends FlyingObject implements Flying{ public Bullet() {
this.imgSrc = ShootGame.bullet;
this.width = this.imgSrc.getWidth();
this.height = this.imgSrc.getHeight();
if (ShootGame.hero != null) {
this.x = (ShootGame.hero.getX() + ShootGame.hero.getWidth() / 2) - this.getWidth() / 2;
this.y = ShootGame.hero.getY() - this.getHeight();
}
this.setYSpeed(3);
} public void setRight() {
if (ShootGame.hero != null) {
this.x=this.getX()+ShootGame.hero.getWidth()/4+10;
this.y=this.getY()+30;
}
} public void setLeft() {
if (ShootGame.hero != null) {
this.x=this.getX()-ShootGame.hero.getWidth()/4-10;
this.y=this.getY()+30;
}
} public int getX() {
return this.x;
} public int getY() {
return this.y;
} public int getWidth() {
return this.width;
} public int getHeight() {
return this.height;
} public int getYSpeed() {
return this.ySpeed;
} public void setYSpeed(int speed) {
this.ySpeed = speed;
} public void move() {
this.setY(this.getY() - this.getYSpeed());
}
}
package subclass.model;import java.util.Random;import controller.Flying;
import superclass.model.FlyingObject;
import view.ShootGame;public class Enemy extends FlyingObject implements Flying{
public static Integer speed;
public Enemy() {
this.imgSrc=ShootGame.enemy;
this.width=this.imgSrc.getWidth();
this.height=this.imgSrc.getHeight();
this.y=-this.height-20;
this.x=new Random().nextInt(ShootGame.WIDTH-this.width+1);
this.setYSpeed(2);
}
public int getX() {
return this.x;
}
public int getYSpeed() {
return this.ySpeed;
} public void setYSpeed(int speed) {
this.ySpeed = speed;
} public Integer getSpeed() {
return speed;
} public void setSpeed(int speed) {
Enemy.speed = speed;
} @Override
public void move() {
this.setY(this.getY() + this.getYSpeed());
}
}
package subclass.model;import java.awt.image.BufferedImage;import controller.Flying;
import controller.Score;
import superclass.model.FlyingObject;
import view.ShootGame;public class Hero extends FlyingObject implements Score,Flying {
public static final String SINGLE_SHOT = "SINGLE";
public static final String DOUBLE_SHOT = "DOUBLE";
public static final String THREE_SHOT = "THREE";
private String shootMode = SINGLE_SHOT;
private Integer fireRate = 600;
private Integer shootRate=1;
public BufferedImage[] imgSrc;
private int index;
private BufferedImage img;
private Integer scoer = 0;
private Integer life = 1; public Hero() {
this.imgSrc = new BufferedImage[2];
this.imgSrc[0] = ShootGame.hero1;
this.imgSrc[1] = ShootGame.hero2;
this.img=imgSrc[0];
this.life = 1;
this.shootRate=1;
this.width = this.imgSrc[0].getWidth();
this.height = this.imgSrc[0].getHeight();
this.setShootMode(SINGLE_SHOT);
this.fireRate=600;
this.y = ShootGame.HEIGHT - this.height - 30;
this.x = ShootGame.WIDTH / 2 - this.width / 2;
} public BufferedImage getImage() {
return this.img;
} public void setX(int x) {
this.x = x - this.getWidth() / 2;
} public void setY(int y) {
this.y = y - this.getHeight() / 2 - 5;
} public String getScore() {
return this.scoer.toString();
} public void setScore(int scoer) {
this.scoer += scoer;
} public Integer getLife() {
return life;
} public void setLife(Integer life) {
this.life += life;
} public String getShootMode() {
return shootMode;
} public void setShootMode(String shootMode) {
this.shootMode = shootMode;
} public Integer getFireRate() {
return fireRate;
} public void setFireRate(Integer fireRate) {
this.fireRate += fireRate;
} public Integer getShootRate() {
return shootRate;
} public void setShootRate(Integer shootRate) {
this.shootRate += shootRate;
} public void move() {
int num=index++%this.imgSrc.length;
this.img=this.imgSrc[num];
}
}
看完他的代码首先觉得,线程其实用一个就可以实现了,想知到线程多了会对内存有多大影响???
自己写的时候是用数组去存储各个对象,还想想问问CSDN上的大神们,这种小项目使用数组和HashMap<>()那个更好???
有什么区别
下篇文章把自己的修改好再发出来
java-实现飞机大战小游戏相关推荐
- 【Java】Java基础飞机大战小游戏完整代码
Java基础飞机大战小游戏完整代码 先来展示一下代码实现结果图 主函数ShootGame 初始化游戏原始背景图片,游戏人物图片,游戏开始结束图片:构建产生敌人算法:产生英雄机算法:发射子弹算法:判断是 ...
- java实现飞机大战小游戏——————【附素材、源码、逐行注释讲解】
java飞机小游戏,实现了以下功能: 1.我方小飞机可以通过鼠标移动来控制. 2.蜜蜂(必须打死而不是碰到,并且碰到蜜蜂会失去生命值)分为两种奖励 获得生命值或者子弹翻3倍. 3.当鼠标移动到界面外可 ...
- java.swing 飞机大战小游戏
上午没事刷到网上最近炒热了一些简单的小游戏和爱心代码,单身8个月了,对爱心代码不是很感冒,所以想蹭个热度,写一个飞机大站来玩玩. 首先,分析小游戏需要那些技术与怎么设计: 窗体,因为是java小游戏, ...
- 基于Java语言在窗体上实现飞机大战小游戏
全套资料下载地址:https://download.csdn.net/download/sheziqiong/85594271 项目介绍 飞机大战:用 Java 语言在窗体上实现飞机大战小游戏,运行程 ...
- 【Java代码实现飞机大战小游戏】简单理解
飞机大战 飞机大战小游戏历经10天完成,主要用到的就是我们面向对象部分的知识:类,封装,继承,多态,静态代码块等等内容+swing部分内容.所以即使你是java小白,也不用担心欧! 游戏说明:游戏有3 ...
- 点击list view中一行内容可以在combox中显示_java版飞机大战小游戏详细教程(零基础小白也可以分分钟学会!)...
一:游戏展示 飞机大战小游戏我们都玩过,通过移动飞机来打敌机,这里给大家展示一下游戏成果:呜呜呜由于gif只能上传5M大小,所以就不能给大家展示操作了,如果大家有兴趣可以自己自己做出来再玩哟. 这里面 ...
- python 飞机大战小游戏
飞机大战小游戏,这里需要下载pygame模块 这是需要的素材,需要的自取: 上代码: import time import pygame from pygame.locals import *#检测事 ...
- 飞机大战小游戏(超详细)
偷学Python之最后的项目二:飞机大战小游戏(超详细) 古之立大事者,不惟有超世之才,亦必有坚忍不拔之志.--苏轼 甜甜先说 这次用Python中的pygame模块来完成一个飞机大战的小游戏:基本思 ...
- matlab飞机大战小游戏(第二版)
第一版链接:https://blog.csdn.net/slandarer/article/details/88025006 游戏截图: ------------------------ 游戏动图: ...
- 使用小程序制作一个飞机大战小游戏
此文主要基于微信小程序制作一个飞机大战小游戏,上手即用,操作简单. 一.创建小程序 二.页面实现 三.代码块 一.创建小程序 访问微信公众平台,点击账号注册. 选择小程序,并在表单填写所需的各项信息进 ...
最新文章
- 无需用户输入,Adobe提出自动生成高质量合成图像新方法
- 微软全球执行副总裁沈向洋:你给自己的定位是什么,你就会得到什么
- ASCII码、ISO8859-1、Unicode、GBK和UTF-8 的区别
- (*长期更新)软考网络工程师学习笔记——数据链路层与网络层的相关计算题
- redis 的bitmap 开源包 bitmapist的应用
- python时间倒计时显示屏厂家_python 实现倒计时功能(gui界面)
- 如何学好初中计算机,初中生怎么学习方法好 十大方法告诉你
- mysql 文卷_mysql数据库试卷
- (32)FPGA面试技能提升篇(EMMC)
- Keras及其前端配置
- java下载什么软件有题库_java题库app
- Qt编程环境下学习《OpenGL编程指南(原书第九版)》(一)
- magisk mask面具自动打卡
- 倍福--实现和西门子的profinet
- eigen 在线手册
- 程序猿生存指南-57 故友来京
- C语言实现通过日期计算这是一年中的第几天
- iOS APP调取短信 发送消息给其他人
- 抖音关键词排名优化技巧,手把手教你怎样优化抖音关键词
- 42-Map集合遍历键找值方式-键值对对象Entry-键值对方式遍历