飞机大战实现实例:
接口:
package aircraft_war;
public interface AddScore {
int addScore();
}
package aircraft_war;
public interface Award {
void choice();
}
package aircraft_war;
public interface AwardType {
int ADD_FIRE=0;
int LIFE=1;
void choose();
}
父类:
package aircraft_war;
import java.awt.image.BufferedImage;
public abstract class BasicClass {
protected int blood;
protected int y;
protected int x;
protected int width;
protected int height;
protected BufferedImage img;
public BasicClass(){
}
/* private int blood;
public BasicClass(BufferedImage img,int y,int x){
this.img=img;
this.x=x;
this.y=y;
}*/
public int getBlood() {
return blood;
}
public void setBlood(int blood) {
this.blood = blood;
}
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 getImg() {
return img;
}
public void setImg(BufferedImage img) {
this.img = img;
}
protected abstract void move();
}
主函数:
package aircraft_war;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
public class Main extends JPanel {
private static int state=0;
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 BufferedImage bg=null;
static BufferedImage airPlane=null;
static BufferedImage airPlane0=null;
static BufferedImage airPlane1=null;
static BufferedImage bigPlane=null;
static BufferedImage bee=null;
static BufferedImage bullet=null;
static BufferedImage start=null;
static BufferedImage pause=null;
static BufferedImage gameover=null;
static BufferedImage hero0=null;
static BufferedImage hero1=null;
static BufferedImage boss0=null;
static BufferedImage boss1=null;
static {
try {
bg= ImageIO.read(Main.class.getResourceAsStream("Image/background.png"));
airPlane= ImageIO.read(Main.class.getResourceAsStream("Image/airplane.png"));
airPlane0= ImageIO.read(Main.class.getResourceAsStream("Image/airplane_ember0.png"));
airPlane1= ImageIO.read(Main.class.getResourceAsStream("Image/airplane_ember1.png"));
bigPlane= ImageIO.read(Main.class.getResourceAsStream("Image/bigplane.png"));
bee= ImageIO.read(Main.class.getResourceAsStream("Image/bee.png"));
hero0= ImageIO.read(Main.class.getResourceAsStream("Image/hero0.png"));
hero1= ImageIO.read(Main.class.getResourceAsStream("Image/hero1.png"));
bullet= ImageIO.read(Main.class.getResourceAsStream("Image/bullet.png"));
start= ImageIO.read(Main.class.getResourceAsStream("Image/start.png"));
pause= ImageIO.read(Main.class.getResourceAsStream("Image/pause.png"));
gameover= ImageIO.read(Main.class.getResourceAsStream("Image/gameover.png"));
boss0= ImageIO.read(Main.class.getResourceAsStream("Image/extraplane_n1.png"));
boss1= ImageIO.read(Main.class.getResourceAsStream("Image/extraplane_n2.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
Timer timer=new Timer();
HeroPlane hero=new HeroPlane();
ArrayList<Bullet> bull=new ArrayList<>();
ArrayList<BasicClass> flyings=new ArrayList();
//int count=0;
public void paintState(Graphics g) {
switch (state) {
case START: // 启动状态
g.drawImage(start, 0, 0, null);
break;
/*case RUNNING: // 运行状态
g.drawImage(bg, 0, 0, null);
break;*/
case PAUSE: // 暂停状态
g.drawImage(pause, 0, 0, null);
break;
case GAME_OVER: // 游戏终止状态
g.drawImage(gameover, 0, 0, null);
break;
}
}
public void action(){
MouseAdapter l = new MouseAdapter() {
@Override
public void mouseMoved(MouseEvent e) { // 鼠标移动
if (state == RUNNING) { // 运行状态下移动英雄机--随鼠标位置
/*int x = e.getX();
int y = e.getY();
hero.moveTo(x, y);*/
hero.setX(e.getX()-hero.width/2);
hero.setY(e.getY()-hero.height/2);
}
}
@Override
public void mouseEntered(MouseEvent e) { // 鼠标进入
if (state == PAUSE) { // 暂停状态下运行
state = RUNNING;
}
}
@Override
public void mouseExited(MouseEvent e) { // 鼠标退出
if (state != GAME_OVER&&state!=START) { // 游戏未结束,则设置其为暂停
state = PAUSE;
}
}
@Override
public void mouseClicked(MouseEvent e) { // 鼠标点击
switch (state) {
case START:
state = RUNNING; // 启动状态下运行
break;
case GAME_OVER: // 游戏结束,清理现场
flyings = new ArrayList(); // 清空飞行物
bull = new ArrayList(); // 清空子弹
hero = new HeroPlane(); // 重新创建英雄机
hero.score = 0; // 清空成绩
state = START; // 状态设置为启动
break;
}
}
};
this.addMouseListener(l); // 处理鼠标点击操作
this.addMouseMotionListener(l); // 处理鼠标滑动操作
timer.schedule(new TimerTask() {
@Override
public void run() {
if (state==1){
createFlying();
runFlying();
outFlying();
createBullet();
runBullet();
outBullet();
banBull();
banHero();
}
repaint();
}
}, 100, 42);
}
int count=0;
private void createBullet(){
count++;
if (count%10==0){
Bullet[] bs=hero.shoot();
for (int i = 0; i < bs.length; i++) {
bull.add(bs[i]);
}
}
}
private void paintBullet(Graphics g){
for (int i = 0; i < bull.size(); i++) {
Bullet n=bull.get(i);
g.drawImage(n.getImg(),n.getX(),n.getY(),this);
}
}
private void runBullet(){
for (int i = 0; i < bull.size(); i++) {
Bullet fly=bull.get(i);
fly.move();
}
}
private void outBullet(){
for (int i = 0; i < bull.size(); i++) {
Bullet fly=bull.get(i);
if (fly.getY()<0){
bull.remove(fly);
i--;//回到当前位置,因为remove后会自动补位
}
}
}
int flyIndex;
private void createFlying() {
flyIndex++;
if(flyIndex%20==0){
int ran=(int)(Math.random()*50);
BasicClass fly = null;
if (ran<5){
fly=new Bee();
}
else if (ran>=40&&ran!=7){
fly=new BigPlane();
}
else if(ran==7){
fly=new Boss();
}
else
{
fly=new AirPlane();
}
flyings.add(fly);
}
}
private void paintFlying(Graphics g){
for (int i = 0; i < flyings.size(); i++) {
BasicClass n=flyings.get(i);
g.drawImage(n.getImg(),n.getX(),n.getY(),this);
}
}
private void runFlying(){
for (int i = 0; i < flyings.size(); i++) {
BasicClass fly=flyings.get(i);
fly.move();
}
}
private void outFlying(){
for (int i = 0; i < flyings.size(); i++) {
BasicClass fly=flyings.get(i);
if (fly.getY()>HEIGHT){
flyings.remove(fly);
i--;//回到当前位置
}
}
}
//循环所有子弹
public void banBull(){
for (int i = 0; i < bull.size(); i++) {
Bullet bul=bull.get(i);
//将每一个子弹都与飞行物进行检测
if(ban(bul)==true){
bull.remove(i);
i--;
}
}
}
//循环出的子弹检测与所有飞行物的碰撞
public boolean ban(Bullet bul){
for (int i = 0; i < flyings.size(); i++) {
BasicClass fly=flyings.get(i);
if (fly.getY()+fly.getHeight()>=bul.getY()&&fly.getX()<+bul.getX()&&fly.getX()+fly.getWidth()>=bul.getX()&&fly.getY()<=bul.getY()){
fly.blood--;
//bull.remove(num);
if (fly.blood<=0){
if (fly instanceof Bee){//判断是不是蜜蜂类型
hero.choice();
/* hero.blood++;
if (hero.flag==1){
hero.flag=2;
}
else if (hero.flag==2){
hero.flag=3;
}*/
}
if (fly instanceof AddScore){
AddScore add= (AddScore)fly;
hero.score=hero.score+add.addScore();
}
/*
也可以下转后
AirPlane air=AirPlane(fly);
hero.score=air.score+hero.score;
*/
//fly.destory();
fly.getX();
fly.getY();
flyings.remove(i);
}
return true;
}
}
return false;
}
public void banHero(){
for (int i = 0; i < flyings.size(); i++) {
BasicClass fly=flyings.get(i);
if (fly.getY()+fly.getHeight()>=hero.getY()&&fly.getX()<=hero.getX()+hero.width&&fly.getX()+fly.getWidth()>=hero.getX()&&fly.getY()<=hero.getY()+hero.height){
fly.blood-=3;
hero.blood--;
//bull.remove(num);
if (fly.blood<=0){
flyings.remove(i);
}
if (hero.blood<=0){
state=GAME_OVER;
}
}
}
}
@Override
public void paint(Graphics g) {
super.paint(g);
//显示图片
g.drawImage(bg,0,0,this);
// g.drawImage(start,WIDTH/2-start.getWidth()/2,HEIGHT/2-start.getHeight()/2,this);
//drawBullet(g);
paintFlying(g);
paintBullet(g);
drawHeroPlane(g);
paintState(g);
//显示文字
Font font=new Font("黑体",Font.BOLD,15);
g.setFont(font);
Color c=Color.BLACK;
g.setColor(c);
g.drawString("Score:"+hero.score,5,20);
g.drawString("Life:"+hero.blood,5,50);
}
/*private void drawStart(Graphics g) {
g.drawImage(start,240-start.getWidth()/2,415-start.getHeight()/2,this);
}*/
/*private void drawBullet(Graphics g) {
for(int i = 0; i < bull.size(); i++) {
bull.get(i).move();
g.drawImage(bull.get(i).getImg(),bull.get(i).getX()-3,bull.get(i).getY(),this);
}
}*/
private void drawHeroPlane(Graphics g) {
hero.move();
g.drawImage(hero.getImg(),hero.getX(),hero.getY(),this);
}
public static final int WIDTH=410;
public static final int HEIGHT=700;
public static void main(String[] args) {
JFrame win =new JFrame();
Main main=new Main();
win.add(main);
main.action();
//z设置窗口大小
win.setSize(WIDTH,HEIGHT);
//读取屏幕尺寸
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
//移动窗口位置
win.setLocation((dim.width - win.getWidth()) / 2, 0);
win.setDefaultCloseOperation(3);
win.setVisible(true);
}
}
飞行物子类(Bee为奖励类,飞机击毁效果未添加):
package aircraft_war;
import java.awt.image.BufferedImage;
public class AirPlane extends BasicClass implements AddScore {
int speed;
int score;
@Override
public void move() {
//垂直向下运动y+=speed;
y+=speed;
}
/*BufferedImage[] imgs= {Main.airPlane0,Main.airPlane1};
public void destory(){
this.img=imgs[0];
this.img=imgs[1];
}*/
public AirPlane(){
img=Main.airPlane;
speed=5;
score=5;
width=img.getWidth();
height=img.getHeight();
blood=1;
x=(int)(Math.random()*(Main.WIDTH-width));
y=-height;
/*y=100;*/
}
@Override
public int addScore() {
return score;
}
}
package aircraft_war;
import java.awt.image.BufferedImage;
public class BigPlane extends BasicClass implements AddScore {
int speed;
int score;
@Override
public void move() {
y+=speed;
}
/*BufferedImage[] imgs= {Main.airPlane0,Main.airPlane1};
public void destory(){
this.img=imgs[0];
this.img=imgs[1];
}
*/
public BigPlane(){
img=Main.bigPlane;
speed=3;
score=50;
width=img.getWidth();
height=img.getHeight();
blood=5;
x=(int)(Math.random()*(Main.WIDTH-width));
y=-height;
//y=30;
}
@Override
public int addScore() {
return score;
}
}
package aircraft_war;
import javax.imageio.ImageIO;
import java.io.IOException;
public class Bee extends BasicClass{
int speed,speed_x;
//boolean flag=true;
@Override
public void move() {
//x++,y++如果达到阈值,x--,y++
//如果private setY(getY()+=speed);
if(x>=Main.WIDTH-width){
this.speed_x=-speed_x;
}
else if(x<=0){
this.speed_x=-speed_x;
}
y+=speed;
x+=speed_x;
}
public Bee(){
//super(20);
img=Main.bee;
speed=5;
speed_x=6;
width=img.getWidth();
height=img.getHeight();
blood=1;
x=(int)(Math.random()*(Main.WIDTH-width));
y=-height;
//y=25;
}
}
package aircraft_war;
import aircraft_war.BasicClass;
import aircraft_war.Main;
import java.awt.image.BufferedImage;
public class Boss extends BasicClass implements AddScore{
int speed;
int score;
int count;
BufferedImage[] imgs={Main.boss0,Main.boss1};
/*public void moveTo(int x,int y){
this.x = x - width/2;
this.y = y - height/2;
}*/
@Override
public void move() {
count++;
if (count%4==0){
this.img=imgs[1];
}
else{
this.img=imgs[0];
}
y+=speed;
}
public Boss(){
img=Main.boss0;
speed=3;
score=1000;
width=img.getWidth();
height=img.getHeight();
blood=21;
x=(int)(Math.random()*(Main.WIDTH-width));
y=-height;
//y=30;
}
@Override
public int addScore() {
return score;
}
}
package aircraft_war;
import javax.imageio.ImageIO;
import java.io.IOException;
public class Bullet extends BasicClass{
int speed;
@Override
public void move() {
this.y-=speed;
}
public Bullet(int x,int y){
//super(20);
img=Main.bullet;
speed=5;
width=img.getWidth();
height=img.getHeight();
blood=1;
this.x=x;
/*y=-height;*/
this.y=y;
}
}
package aircraft_war;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.sql.Array;
import java.util.AbstractList;
import java.util.ArrayList;
public class HeroPlane extends BasicClass implements Award{
int score;
Bullet[] bullets;
//ArrayList<Bullet> list=new ArrayList();
int count,choose=0;
BufferedImage[] imgs={Main.hero0,Main.hero1};
/*public void moveTo(int x,int y){
this.x = x - width/2;
this.y = y - height/2;
}*/
@Override
public void move() {
count++;
if (count%4==0){
this.img=imgs[1];
}
else{
this.img=imgs[0];
}
}
public void addBloot() {
this.blood++;
}
public void addFire() {
choose+=20;
if (this.flag==1){
this.flag=2;
}
else if (this.flag==2){
this.flag=3;
}
}
int flag=1;
public Bullet[] shoot(){
if (flag==1){
Bullet[] bs=new Bullet[1];
bs[0]=new Bullet(this.x+width/2,this.y);
return bs;
}
else if(flag==2){
choose--;
Bullet[] bs=new Bullet[2];
bs[0]=new Bullet(this.x+width/4,this.y);
bs[1]=new Bullet(this.x+width/4*3,this.y);
if (choose<=0){
flag=1;
}
return bs;
}
else {
choose--;
Bullet[] bs=new Bullet[3];
bs[0]=new Bullet(this.x+width/4,this.y);
bs[1]=new Bullet(this.x+width/2,this.y-7);
bs[2]=new Bullet(this.x+width/4*3,this.y);
if (choose<=20){
flag=2;
}
return bs;
}
}
public HeroPlane(){
img=Main.hero0;
score=0;
width=img.getWidth();
height=img.getHeight();
blood=3;
x=Main.WIDTH/2-img.getWidth()/2;
/*y=-height;*/
y=400;
}
@Override
public void choice() {
int num=(int)(Math.random()*10);
if (num<5){
addBloot();
}
else {
addFire();
}
}
}