演示视频
JAVA练手小游戏——飞机大战
代码实现
AirPlane类
public class AirPlan extends Role implements Enemy {
private int speed = 3;
/**
* 初始化数据
*/
public AirPlan() {
Random rand = new Random();
this.bfImg = Resources.airplane;
this.width = bfImg.getWidth();
this.height = bfImg.getHeight();
this.y = -height;
this.x = rand.nextInt(Resources.WIDTH - width);
}
/**
* 获取分数
*/
public int getScore() {
return 5;
}
/**
* //越界处理
*/
@Override
public boolean outOfBounds() {
return y > Resources.HEIGHT;
}
/**
* 移动
*/
@Override
public void step() {
y += speed;
}
}
Bee类
public class Bee extends Role implements Award {
private int xSpeed = 1;
private int ySpeed = 2;
private int awardType;
public Bee() {
this.bfImg = Resources.bee;
width = bfImg.getWidth();
height = bfImg.getHeight();
y = -height;
Random rand = new Random();
x = rand.nextInt(Resources.WIDTH - width);
awardType = rand.nextInt(2);
}
/**
* 获得奖励类型
*/
public int getType() {
return awardType;
}
/**
* 越界处理
*/
@Override
public boolean outOfBounds() {
return y > Resources.HEIGHT;
}
/**
* 移动,可斜着飞
*/
@Override
public void step() {
x += xSpeed;
y += ySpeed;
if (x > Resources.WIDTH - width) {
xSpeed = -1;
}
if (x < 0) {
xSpeed = 1;
}
}
}
Bullet类
public class Bullet extends Role {
private int speed = 3;
/**
* 初始化数据
*/
public Bullet(int x, int y) {
this.x = x;
this.y = y;
this.bfImg = Resources.bullet;
}
/**
* 移动
*/
@Override
public void step() {
y -= speed;
}
/**
* 越界处理
*/
@Override
public boolean outOfBounds() {
return y < -height;
}
}
Hero类
public class Hero extends Role {
private BufferedImage[] images = {};
private int index = 0;
private int doubleFire;
private int life;
/**
* 初始化数据
*/
public Hero() {
life = 3;
doubleFire = 0;
images = new BufferedImage[]{Resources.hero0, Resources.hero1};
bfImg = Resources.hero0;
width = bfImg.getWidth();
height = bfImg.getHeight();
x = 150;
y = 400;
}
/**
* 设置双倍火力
*/
public void setDoubleFire(int doubleFire) {
this.doubleFire = doubleFire;
}
/**
* 增加火力
*/
public void addDoubleFire() {
doubleFire = 40;
}
/**
* 增命
*/
public void addLife() { //增命
life++;
}
/**
* 减命
*/
public void subtractLife() { //减命
life--;
}
/**
* 获取命
*/
public int getLife() {
return life;
}
/**
* 当前物体移动了一下,相对距离,x,y鼠标位置
*/
public void moveTo(int x, int y) {
this.x = x - width / 2;
this.y = y - height / 2;
}
/**
* 越界处理
*/
@Override
public boolean outOfBounds() {
return false;
}
/**
* 发射子弹
*/
public ArrayList<Bullet> shoot() {
ArrayList<Bullet> bullets = new ArrayList<>();
int xStep = width / 4;
int yStep = 20;
if (doubleFire > 0) {
bullets.add(new Bullet(x + xStep, y - yStep));
bullets.add(new Bullet(x + 3 * xStep, y - yStep));
return bullets;
} else {
bullets.add(new Bullet(x + 2 * xStep, y - yStep));
return bullets;
}
}
/**
* 移动
*/
@Override
public void step() {
if (images.length > 0) {
bfImg = images[index++ / 10 % images.length];
}
}
/**
* 英雄机碰撞
*/
public boolean hit(Role other) {
int x1 = other.x - this.width / 2;
int x2 = other.x + this.width / 2 + other.width;
int y1 = other.y - this.height / 2;
int y2 = other.y + this.height / 2 + other.height;
int herox = this.x + this.width / 2;
int heroy = this.y + this.height / 2;
return herox > x1 && herox < x2 && heroy > y1 && heroy < y2;
}
}
Role类
public abstract class Role {
protected int x;
protected int y;
protected int width;
protected int height;
protected BufferedImage bfImg;
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 BufferedImage getImage() {
return bfImg;
}
public void setImage(BufferedImage image) {
this.bfImg = image;
}
/**
* 检查是否出界
*
* @return true 出界与否
*/
public abstract boolean outOfBounds();
/**
* 飞行物移动一步
*/
public abstract void step();
/**
* 是否被击中
*
* @param bullet
* @return
*/
public boolean shootBy(Bullet bullet) {
boolean flag = false;
int bulletX = bullet.getX();
int bulletY = bullet.getY();
if (this.x < bulletX && bulletX < (this.x + this.width)
&& this.y < bulletY && bulletY < (this.y + this.height)) {
flag = true;
}
return flag;
}
}
StateEnum
public enum StateEnum {
START, RUNNING, PAUSE, OVER
}
Resource类
public class Resources {
public static final int WIDTH = 400;
public static final int HEIGHT = 654;
public static BufferedImage background;
public static BufferedImage start;
public static BufferedImage airplane;
public static BufferedImage bee;
public static BufferedImage bullet;
public static BufferedImage hero0;
public static BufferedImage hero1;
public static BufferedImage pause;
public static BufferedImage gameover;
/**
* 加载资源
* @param resourceDir
*/
public void load(String resourceDir) {
try {
background = ImageIO.read(this.getClass().getClassLoader().
getResourceAsStream(resourceDir + "background.png"));
start = ImageIO.read(this.getClass().getClassLoader().
getResourceAsStream(resourceDir + "start.png"));
airplane = ImageIO.read(this.getClass().getClassLoader().
getResourceAsStream(resourceDir + "airplane.png"));
bee = ImageIO.read(this.getClass().getClassLoader().
getResourceAsStream(resourceDir + "bee.png"));
bullet = ImageIO.read(this.getClass().getClassLoader().
getResourceAsStream(resourceDir + "bullet.png"));
hero0 = ImageIO.read(this.getClass().getClassLoader().
getResourceAsStream(resourceDir + "hero0.png"));
hero1 = ImageIO.read(this.getClass().getClassLoader().
getResourceAsStream(resourceDir + "hero1.png"));
pause = ImageIO.read(this.getClass().getClassLoader().
getResourceAsStream(resourceDir + "pause.png"));
gameover = ImageIO.read(this.getClass().getClassLoader().
getResourceAsStream(resourceDir + "gameover.png"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
PlaneWarFarme类
public class PlaneWarGameFrame extends JPanel {
// 分数值与生命值坐标
private static final int SCORE_X = 10;
private static final int SCORE_Y = 25;
private static final int LIFE_X = 10;
private static final int LIFE_Y = 45;
// 游戏资源
private RulesGame rulesGame;
private ArrayList<Role> shootObjects;
private ArrayList<Bullet> bullets;
private Hero hero;
public PlaneWarGameFrame(RulesGame rulesGame) {
this.rulesGame = rulesGame;
this.shootObjects = rulesGame.getShootObjects();
this.hero = rulesGame.getHero();
this.bullets = rulesGame.getBullets();
JFrame jFrame = new JFrame("飞机大战");
jFrame.add(this);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setSize(Resources.WIDTH, Resources.HEIGHT);
jFrame.setAlwaysOnTop(true);
jFrame.setLocationRelativeTo(null);
jFrame.setVisible(true);
MouseHandler mouseHandler = new MouseHandler(rulesGame);
this.addMouseListener(mouseHandler);
this.addMouseMotionListener(mouseHandler);
}
/**
* repaint方法会调用paint方法,
*/
@Override
public void paint(Graphics g) {
super.paint(g);
paintBackground(g);
paintHero(g);
paintShootObjects(g);
paintBullets(g);
paintScore(g);
paintState(g);
}
/**
* 画游戏状态
*/
public void paintState(Graphics g) {
switch (STATE) {
case START:
g.drawImage(Resources.start, 0, 0, null);
break;
case PAUSE:
g.drawImage(Resources.pause, 0, 0, null);
break;
case OVER:
g.drawImage(Resources.gameover, 0, 0, null);
break;
case RUNNING:
break;
}
}
/**
* 画分数
*/
public void paintScore(Graphics g) {
Font font = new Font(Font.SANS_SERIF, Font.BOLD, 14);
g.setColor(new Color(0x3A3B3B));
g.setFont(font);
g.drawString("SCORE:" + rulesGame.getScore(), SCORE_X, SCORE_Y);
g.drawString("LIFE:" + hero.getLife(), LIFE_X, LIFE_Y);
}
/**
* 画子弹
*/
public void paintBullets(Graphics g) {
Iterator<Bullet> iterator = bullets.iterator();
while (iterator.hasNext()) {
Bullet b = iterator.next();
g.drawImage(b.getImage(), b.getX() - b.getWidth() / 2,
b.getY(), null);
}
}
private void paintHero(Graphics g) {
g.drawImage(hero.getImage(), hero.getX(),
hero.getY(), null);
}
private void paintBackground(Graphics g) {
g.drawImage(Resources.background,
0, 0, null);
}
/**
* 画飞行物
*/
private void paintShootObjects(Graphics g) {
Iterator<Role> iterator = shootObjects.iterator();
while (iterator.hasNext()) {
Role shootObj = iterator.next();
g.drawImage(shootObj.getImage(), shootObj.getX(),
shootObj.getY(), null);
}
}
}
MainAPP类
public class MainApp {
public static void main(String[] args) {
PlanWarGameFactory planWarGameFactory = new PlanWarGameFactory();
Game game = planWarGameFactory.creatGame();
game.start();
}
}