主函数ShootGame
初始化游戏原始背景图片,游戏人物图片,游戏开始结束图片;构建产生敌人算法;产生英雄机算法;发射子弹算法;判断是否相撞算法(子弹与敌机相撞,英雄机与敌机相撞);击落敌人奖励算法;绘制游戏中人物算法;鼠标控制移动算法;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ShootGame extends JPanel implements MouseMotionListener,MouseListener{
//继承JPanel类
public static final int width = 400;
public static final int height = 600;
//设置对象图片名称(与源文件名称一致)
public static Image background;
public static Image airplane;
public static Image bee;
public static Image bullet;
public static Image gameover;
public static Image hero0;
public static Image hero1;
public static Image pause;
public static Image start;
//定义游戏的四种状态
public static final int START = 0; //开始状态
public static final int RUNNING = 1; //运行状态
public static final int PAUSE = 3; //暂停状态
public static final int GAMEOVER =4; //结束状态
int state =START;
//创建对象-----------------------------------------------------------------------------------------------
Hero hero = new Hero(); //创建英雄机对象
Bullet[] bullets = {}; //子弹(众多),定义子弹数组
FlyingObject[] flyings = {}; //小敌机(众多),定义小敌机数组
//ShootGame无惨构造方法(给创建对象初始化)
/*public ShootGame() {
flyings = new FlyingObject[2];
//多态 夫妇
flyings[0] = new Airplane();//小敌机
flyings[1] = new Bee();//小蜜蜂
bullets = new Bullet[2];
bullets[0] = new Bullet(130,250);
bullets[1] = new Bullet(100,120);
}*/
//静态代码块内容加载
static {
try {
//导入图片文件-----------------------------------------------------------------------------------
//检测英雄机生命值方法------------------------------------------------------------------------------------
public void checkGame() {
if(hero.life <= 0) {
state = GAMEOVER;
}
}
//游戏中的各种行为方法-----------------------------------------------------------------------------------
public void action() {
//添加定时器(每隔多久出现一次)
Timer timer = new Timer();//utill包下
//安排指定任务从指定的延迟后开始进行重复的固定延迟执行
timer.schedule(new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
if(state == RUNNING) { //处于运行状态执行操作
enterAction(); //敌人入场
stepAction(); //设置对象的移动方式
shootAction(); //英雄机发射子弹
hitAction(); //子弹与敌人相撞
duangAction(); //英雄机与敌人撞
checkGame(); //检测英雄机生命值
}
repaint(); //重新绘制(产生新的小敌机)
}}, 10,10);
}
//绘制子弹
paintBullet(g);
//绘制英雄机
paintHero(g);
//绘制分值
paintScore(g);
//绘制状态图
paintState(g);
}
//绘制英雄机方法-----------------------------------------------------------------------------------------
public void paintHero(Graphics g) {
// TODO Auto-generated method stub
g.drawImage(hero.image, hero.x, hero.y, null);
}
//绘制子弹方法-------------------------------------------------------------------------------------------
public void paintBullet(Graphics g) {
// TODO Auto-generated method stub
for(int i = 0; i<bullets.length; i++) {
g.drawImage(bullets[i].image, bullets[i].x, bullets[i].y, null);
}
}
//绘制敌人方法-------------------------------------------------------------------------------------------
public void paintFlying(Graphics g) {
// TODO Auto-generated method stub
for(int i = 0; i<flyings.length; i++) {
g.drawImage(flyings[i].image, flyings[i].x, flyings[i].y, null);
}
}
//主方法
public static void main(String[] args) {
//飞机大战窗体制作
JFrame jf = new JFrame("飞机大战");
jf.setSize(width, height); //设置窗体宽高
jf.setLocationRelativeTo(null); //窗体居中
jf.setVisible(true); //窗体可见
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//导入背景图片
ShootGame game = new ShootGame(); //创建ShootGame对象
jf.add(game); //在窗体中添加背景图片
game.action(); //游戏中的各种操作
jf.addMouseMotionListener(game); //添加监听器(对对象实时监控)
jf.addMouseListener(game); //添加监听器(对鼠标实时监控)
}
@Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseMoved(MouseEvent e) { //鼠标移动
// TODO Auto-generated method stub
if(state == RUNNING) { //在运行状态下执行操作
hero.x =e.getX()-hero.width/2; //鼠标的具体坐标(x,y)
hero.y =e.getY()-hero.height/2; //根据鼠标的位置确定英雄机的位置
}
}
//鼠标单击启动
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
if(state == START) {
state = RUNNING; //开始运行
}
if(state == GAMEOVER) {
state = START; //返回开始状态
hero = new Hero();
flyings = new FlyingObject[] {};
bullets = new Bullet[] {};
score = 0;
}
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
//鼠标进入
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
if(state == PAUSE) {
state = RUNNING; //修改为运行
}
}
//鼠标退出
@Override
public void