package FlyHero;
import java.util.Random;
public class Airplane extends FlyingObject implements Enemy{
private int speed = 2;
public Airplane(){
this.image = ShootGame.airplane;
width = image.getWidth();
height = image.getHeight();
y = -height;
Random rand = new Random();
x = rand.nextInt(ShootGame.WIDTH - width);
}
@Override
public void step(){
y += speed;
}
@Override
public int getScore(){
return 3;
}
}
//-----------------------------------------------------------------------------------------------------------------------
package FlyHero;
public interface Award {
int DOUBLE_FIRE = 0;
int LIFE = 1;
int getType();
}
//-----------------------------------------------------------------------------------------------------------------------
package FlyHero;
import java.util.Random;
public class Bee extends FlyingObject implements Award{
private int xSpeed = 1;
private int ySpeed = 2;
private int awardType;
public Bee(){
this.image = ShootGame.bee;
width = image.getWidth();
height = image.getHeight();
y = -height;
Random rand = new Random();
x = rand.nextInt(ShootGame.WIDTH - width);
awardType = rand.nextInt(2);
}
@Override
public void step(){
x += xSpeed;
y += ySpeed;
if(x>ShootGame.WIDTH-width){
xSpeed = -1;
}
if(x<0){
xSpeed = 1;
}
}
public int getType(){
return awardType;
}
}
//-----------------------------------------------------------------------------------------------------------------------
package FlyHero;
import java.util.Random;
public class Boss extends FlyingObject implements Enemy{
private int awardType;
private int xSpeed = 1;
private int ySpeed = 2;
private int life;
static int i;
@Override
public void step(){
x += xSpeed;
y = 10;
if(x>ShootGame.WIDTH-width){
xSpeed = -1;
}
if(x<0){
xSpeed = 1;
}
i = x;
}
public Boss(){
life = 100;
this.image = ShootGame.boss0;
width = image.getWidth();
height = image.getHeight();
y = this.y+100;
Random rand = new Random();
// x = this.x;
awardType = rand.nextInt(2);
}
public BossBullet[] shoot(){
int xStep = this.width/4;
int yStep = 20;
BossBullet[] bossbullets = new BossBullet[1];
bossbullets[0] = new BossBullet(this.i+2*xStep-16,this.y-yStep);
return bossbullets;
}
@Override
public int getScore(){
return 50;
}
public int getType(){
return awardType;
}
public boolean die(){
return false;
}
public void subtractLife(){
life--;
}
public int getLife(){
return life;
}
public boolean hit(FlyingObject 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;
}
}
//-----------------------------------------------------------------------------------------------------------------------
package FlyHero;
public class BossBullet extends FlyingObject{
private int ySpeed = 4;
public BossBullet(int x,int y) {
this.x = x;
this.y = y;
this.image = ShootGame.bossbullet;
}
@Override
public void bulletsStep(){
y += ySpeed;
}
@Override
public void step() {
// TODO Auto-generated method stub
}
}
//-----------------------------------------------------------------------------------------------------------------------
package FlyHero;
public class Bullet extends FlyingObject{
private int ySpeed = 3;
public Bullet(int x,int y) {
this.x = x;
this.y = y;
this.image = ShootGame.bullet;
}
@Override
public void step(){
y -= ySpeed;
}
}
//-----------------------------------------------------------------------------------------------------------------------
package FlyHero;
public interface Enemy {
int getScore();
}
//-----------------------------------------------------------------------------------------------------------------------
package FlyHero;
import java.awt.image.BufferedImage;
public abstract class FlyingObject {
protected int x;
protected int y;
protected int width;
protected int height;
protected BufferedImage 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 BufferedImage getImage() {
return image;
}
public void setImage(BufferedImage image) {
this.image = image;
}
public abstract void step();
public boolean shootBy(Bullet b){
int x = b.x;
int y = b.y;
return x>this.x&&x<this.x+width&&y>this.y&&y<this.y+height;
}
public void bulletsStep() {
// TODO Auto-generated method stub
}
}
//-----------------------------------------------------------------------------------------------------------------------
package FlyHero;
public class Hero extends FlyingObject{
private double Fire;
private int life;
private int doubleFire;
public Hero(){
life = 3;
Fire = 0;
image = ShootGame.hero0;
width = image.getWidth();
height = image.getHeight();
x = 150;
y = 400;
}
public Bullet[] shoot(){
int xStep = this.width/4;
int yStep = 20;
if(doubleFire > 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;
}
}
public void moverTo(int x,int y){
this.x = x - width/2;
this.y = y - height/2;
}
public double isDoubleFire(){
return doubleFire;
}
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;
}
public boolean hit(FlyingObject 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;
}
@Override
public void step() {
}
}
//-----------------------------------------------------------------------------------------------------------------------
package FlyHero;
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.util.Arrays;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ShootGame extends JPanel{
public static final int WIDTH = 1080;
public static final int HEIGHT =1920;
public static BufferedImage background;
public static BufferedImage start;
public static BufferedImage gameover;
public static BufferedImage pause;
public static BufferedImage airplane;
public static BufferedImage bee;
public static BufferedImage bullet;
public static BufferedImage bossbullet;
public static BufferedImage hero0;
public static BufferedImage boss0;
private FlyingObject[] flyings = {};
private Bullet[] bullets = {};
private BossBullet[] bossbullets = {};
private Hero hero = new Hero();
private Boss boss = new Boss();
private Timer timer;
private int interval = 10;
private int shootIndex = 0;
private static int score = 0;
private int state;
private static final int START = 0;
private static final int RUNNING = 1;
private static final int PAUSE = 2;
private static final int GAME_OVER = 3;
static {
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"));
bossbullet = ImageIO.read(ShootGame.class.getResource("bossbullet.png"));
hero0 = ImageIO.read(ShootGame.class.getResource("hero0.png"));
pause = ImageIO.read(ShootGame.class.getResource("pause.png"));
gameover = ImageIO.read(ShootGame.class.getResource("gameover.png"));
boss0 = ImageIO.read(ShootGame.class.getResource("boss.png"));
}catch (Exception e) {
e.printStackTrace();
}
}
public void paintBoss(Graphics g){
g.drawImage(boss.getImage(),boss.getX(),boss.getY(),null);
}
public void paintHero(Graphics g) { //绘制hero
g.drawImage(hero.getImage(), hero.getX(), hero.getY(), null);
}
public void paintBullets(Graphics g) { //绘制bullet
for (int i = 0; i < bullets.length; i++) {
Bullet b = bullets[i];
g.drawImage(b.getImage(), b.x, b.y,null);
System.out.println("paintBullets运行");
}
}
public void paintBossBullets(Graphics g) { //绘制bullet
for (int i = 0; i < bossbullets.length; i++) {
BossBullet b2 = bossbullets[i];
g.drawImage(b2.getImage(), b2.x, b2.y,null);
System.out.println("paintBossBullets运行");
}
}
public void paintFlyingObjects(Graphics g) { //绘制飞行物
for (int i = 0; i < flyings.length; i++) {
FlyingObject f = flyings[i];
g.drawImage(f.getImage(), f.getX(), f.getY(), null);
}
}
public void paintScore(Graphics g) { //分数
//Font font = new Font(Font.SANS_SERIF, Font.BOLD, 14);
g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 14));
g.setColor(new Color(0x3a3b3b));
g.drawString("SCORE:"+score, 10, 25);
g.drawString("LIFE:"+hero.getLife(), 10, 45);
}
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;
}
}
@Override
public void paint(Graphics g) { //绘制画面
g.drawImage(background, 0, 0, null);
paintHero(g);
//--------------------------------------------------------------------------------------------------------------------//
//判断flyings[]数组中是否含有boss 否 不执行 是 执行
if(score%20<=10 && score%20>0 && boss instanceof FlyingObject){
paintBossBullets(g);
}
paintBullets(g);
paintFlyingObjects(g);
paintScore(g);
paintState(g);
}
//工厂方法
public static FlyingObject nextOne() { //判断出现的是蜜蜂 敌机 还是Boss
Random random = new Random();
int type = random.nextInt(5);
if(type == 0 && score<20){
return new Bee();
}else if(score%20<=10 && score%20>0){
return new Boss();
}else{
return new Airplane();
}
}
int flyEnteredIndex = 0;
public void enterAction(){ //将nextone()方法里面判断的敌机种类存储到flying数组中
flyEnteredIndex++;
if(flyEnteredIndex%120==0){
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();
}
if(score%20<=10 && score%20>0){
for (int i = 0; i < bossbullets.length; i++){
bossbullets[i].bulletsStep();
}
}
}
public void action(){
MouseAdapter l = new MouseAdapter(){
@Override
public void mouseMoved(MouseEvent e){
int x = e.getX();
int y = e.getY();
hero.moverTo(x,y);
}
@Override
public void mouseEntered(MouseEvent e){
if (state == PAUSE) {
state = RUNNING;
}
}
@Override
public void mouseExited(MouseEvent e) {
if (state == RUNNING) {
state = PAUSE;
}
}
@Override
public void mouseClicked(MouseEvent e) {
switch (state) {
case START:
state = RUNNING;
break;
case GAME_OVER:
flyings = new FlyingObject[0];
bullets = new Bullet[0];
hero = new Hero();
score = 0;
state = START;
break;
}
}
};
this.addMouseListener(l);
this.addMouseMotionListener(l);
timer = new Timer();
timer.schedule(new TimerTask() { //计时器
@Override
public void run() {
if (state == RUNNING) {
enterAction(); //创建新的airplane对象,并存储在flyings数组中
stepAction(); //给数组中的每个airplane和bullet运动路线
shootAction(); //飞机发射bullet,且规定bullet出现的位置,bullet的运动路线在bullet类中给出(自减y轴坐标)
bangAction(); //判断bullet和airplane是否碰撞
//outOfBoundsAction(); // 删除越界飞行物及子弹
checkGameOverAction(); //检查游戏是否结束
}
repaint();
}
}, interval, interval);
}
//-------------------------------------------------------------------------------------------------//
public void shootAction(){
shootIndex++;
if(shootIndex % 30 == 0){
BossBullet[] bs2 = boss.shoot();
bossbullets = Arrays.copyOf(bossbullets, bossbullets.length+bs2.length);
System.arraycopy(bs2, 0, bossbullets, bossbullets.length - bs2.length, bs2.length);
}
if(shootIndex % 30 == 0){
Bullet[] bs = hero.shoot();
bullets = Arrays.copyOf(bullets, bullets.length+bs.length);
System.arraycopy(bs, 0, bullets, bullets.length - bs.length, bs.length);
}
}
public void bangAction(){
for (int i = 0; i < bullets.length; i++) {
Bullet b = bullets[i];
bang(b);
}
}
public void bang(Bullet b){
int index = -1;
for (int i = 0; i < flyings.length; i++) {
FlyingObject obj = flyings[i];
if(obj.shootBy(b)){
index = i;
break;
}
}
if(index != -1){ //删去被击中的敌机
FlyingObject one = flyings[index];
FlyingObject t = flyings[index];
flyings[index] = flyings[flyings.length-1];
flyings[flyings.length-1] = t;
flyings = Arrays.copyOf(flyings, flyings.length-1);
// for(int i=0; i<bossbullets.length; i++){
// BossBullet t2 = bossbullets[i];
// bossbullets[i] = bossbullets[bossbullets.length-1];
// bossbullets[bossbullets.length-1] = t2;
// bossbullets = Arrays.copyOf(bossbullets, bossbullets.length-1);
// }
if(one instanceof Enemy){
Enemy e = (Enemy)one;
score += e.getScore();
}else if(one instanceof Award){
Award a = (Award)one;
int type = a.getType();
switch (type) {
case Award.DOUBLE_FIRE:
hero.addDoubleFire();
break;
case Award.LIFE:
hero.addLife();
break;
}
}
}
}
public boolean isGameOver() {
for (int i = 0; i < flyings.length; i++) {
int index = -1;
FlyingObject obj = flyings[i];
if (hero.hit(obj)) {
hero.subtractLife();
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);
}
}
return hero.getLife() <= 0;
}
public void checkGameOverAction() {
if (isGameOver()) {
state = GAME_OVER;
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Fly");
ShootGame game = new ShootGame();
frame.add(game);
frame.setSize(WIDTH, HEIGHT);
frame.setAlwaysOnTop(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setIconImage(new ImageIcon("icon.png").getImage());
frame.setLocationRelativeTo(null);
frame.setVisible(true);
game.action();
System.out.println("游戏开始。。。");
}
}
//-----------------------------------------------------------------------------------------------------------------------