package main;
import java.applet.Applet;
import java.applet.AudioClip;
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.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
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 {
/**
*
*/
private static final long serialVersionUID = 9158805858745581422L;
public static final int WIDTH = 400; // 窗口的宽
public static final int HEIGHT = 654; // 窗口的高
// 静态资源
public static BufferedImage background; // 背景图
public static BufferedImage start; // 开始图
public static BufferedImage pause; // 暂停图
public static BufferedImage gameover; // 游戏结束图
public static BufferedImage airplane; // 敌机图
public static BufferedImage bee; // 蜜蜂图
public static BufferedImage bullet; // 子弹图
public static BufferedImage hero0; // 英雄机0图
public static BufferedImage hero1; // 英雄机1图
public static AudioClip music;
public static final int START = 0;
public static final int RUNNING = 1;
public static final int PAUSE = 2;
public static final int GAME_OVER = 3;
private int state = 0; // 当前状态
private Hero hero = new Hero(); // 英雄机
private Bullet[] bullets = {}; // 子弹数组
private FlyingObject[] flyings = {}; // 敌人数组
private Timer timer;
private int intervel = 10; // 间隔时间:单位--毫秒
// 静态块
static {
try {
background = ImageIO.read(ShootGame.class.getResource("background.png"));
start = ImageIO.read(ShootGame.class.getResource("start.png"));
pause = ImageIO.read(ShootGame.class.getResource("pause.png"));
gameover = ImageIO.read(ShootGame.class.getResource("gameover.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"));
hero0 = ImageIO.read(ShootGame.class.getResource("hero0.png"));
hero1 = ImageIO.read(ShootGame.class.getResource("hero1.png"));
URL musicPath = ShootGame.class.getResource("game_music.wav");
music = Applet.newAudioClip(musicPath);
} catch (Exception e) {
e.printStackTrace();
}
}
public static FlyingObject nextOne() {
Random rand = new Random();
int type = rand.nextInt(20); // 生成0到19的随机数
if (type == 0) { // 随机数为0,返回bee;否则返回敌机
return new Bee();
} else {
return new Airplane();
}
}
int flyEnteredIndex = 0;
// 敌人登场
public void enterAction() {// 10毫秒走一次
flyEnteredIndex++; // 每10毫秒增1
if (flyEnteredIndex % 40 == 0) {
FlyingObject obj = nextOne();
flyings = Arrays.copyOf(flyings, flyings.length + 1);
flyings[flyings.length - 1] = obj;// 将敌人赋值给flyings数组的最后一个元素
}
}
public void stepAction() { // 10毫秒走一次
hero.step(); // 英雄机走一步
int num = time1 / 15;
for (int i = 0; i < flyings.length; i++) {
for(int j = 0; j <= num; j++) {
flyings[i].step(); // 敌人走一步
}
}
for (int i = 0; i < bullets.length; i++) {
for(int j = 0; j <= num/2; j++) {
bullets[i].step(); // 子弹走一步
}
}
}
int shootIndex = 0;
public void shootAction() { // 10毫秒走一次
shootIndex++; // 每10毫秒增1
if (shootIndex % 30 == 0) { // 300毫秒发射一次子弹
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 outOfBoundsAction() {
int index = 0;
FlyingObject[] flyingLives = new FlyingObject[flyings.length];
for (int i = 0; i < flyings.length; i++) {
FlyingObject f = flyings[i];
if (!f.outOfBounds()) {
flyingLives[index] = f;// 不越界,将其装入flyingLives[]数组中
index++;
}
}
flyings = Arrays.copyOf(flyingLives, index);
index = 0;
Bullet[] bulletsLives = new Bullet[bullets.length];
for (int i = 0; i < bullets.length; i++) {
Bullet bs = bullets[i];
if (!bs.outOfBounds()) {
bulletsLives[index] = bs;// 不越界,将其装入flyingLives[]数组中
index++;
}
}
bullets = Arrays.copyOf(bulletsLives, index);
}
int score = 0; // 得分
static double time = 0.00;//时间
static int time1 = 0;
// 所有子弹与所有敌人撞
public void bangAction() {
for (int i = 0; i < bullets.length; i++) {
bang(bullets[i]);
}
}
// 一个子弹与所有敌人撞
public void bang(Bullet b) {
int index = -1;// 被撞敌人的索引
for (int i = 0; i < flyings.length; i++) {// 遍历所有的敌人
if (flyings[i].shootBy(b)) {// 判断是否撞上
index = i; // 记录被撞敌人的索引
break;
}
}
if (index != -1) {// 撞上了
FlyingObject one = flyings[index];
if (one instanceof Enemy) {
Enemy e = (Enemy) one;
score += e.getScore();
}
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;
}
}
// 被撞敌人与flyings数组中的最后一个元素交换
FlyingObject t = flyings[index];
flyings[index] = flyings[flyings.length - 1];
flyings[flyings.length - 1] = t;
// 缩容,删除随后一个元素---即被撞的对象
flyings = Arrays.copyOf(flyings, flyings.length - 1);
}
}
public void checkGameOverAction() {
if (isGameOver()) { // 结束游戏
state = GAME_OVER;
time1 = 0;
time = 0;
}
}
public boolean isGameOver() {
for (int i = 0; i < flyings.length; i++) { // 撞上了,
if (hero.hit(flyings[i])) {
hero.subtractLife(); // 生命减1
hero.setDoubleFire(0); // 火力值清零
// 相撞之后,交换缩容
FlyingObject t = flyings[i];
flyings[i] = flyings[flyings.length - 1];
flyings[flyings.length - 1] = t;
flyings = Arrays.copyOf(flyings, flyings.length - 1);
}
}
return hero.getLife() <= 0; // 英雄机的命<=0,游戏结束
}
// 启动执行代码
public void action() {
MouseAdapter l = new MouseAdapter() {
public void mouseMoved(MouseEvent e) {
if (state == RUNNING) { // 运行状态下执行
int x = e.getX(); // 鼠标Y坐标
int y = e.getY(); // 鼠标X坐标
hero.moveTo(x, y); // 英雄机随着鼠标移动而移动
}
}
// 鼠标的点击事件
public void mouseClicked(MouseEvent e) {
switch (state) {
case PAUSE:
state = RUNNING;
music.stop();
music.loop();
break;
case RUNNING:
state = PAUSE;
music.stop();
break;
case START:
state = RUNNING;
music.stop();
music.loop();
break;
case GAME_OVER:
hero = new Hero();// 清理现场
flyings = new FlyingObject[0];
bullets = new Bullet[0];
score = 0;
state = START;
music.stop();
break;
}
}
public void mouseEntered(MouseEvent e) {
if (state == PAUSE) {
state = RUNNING;
}
}
public void mouseExited(MouseEvent e) {
if (state == RUNNING) {
state = PAUSE;
}
}
};
this.addMouseListener(l); // 处理鼠标操作事件
this.addMouseMotionListener(l);// 处理鼠标移动事件
timer = new Timer(); // 创建定时器对象
timer.schedule(new TimerTask() {
public void run() { // 定时干的那个事--10毫秒走一次
if (state == RUNNING) { // 运行状态下执行
enterAction();
stepAction();// 飞行物走一步
shootAction();// 子弹入场
outOfBoundsAction();// 删除越界飞行物
bangAction(); // 子弹与敌人撞
基于Java的飞机大战游戏的设计与实现.rar
需积分: 46 147 浏览量
2020-02-25
23:02:37
上传
评论 4
收藏 3.29MB RAR 举报
程序员DD
- 粉丝: 1112
- 资源: 99
最新资源
- vb题库管理系统设计(源代码+论文).rar
- [信息办公]PHP Classifieds v7.3_classifieds.rar
- [检测统计]北雨访问记数器系统_bycount.rar
- HonoCMS 内容发布系统 v1.0_honocms_cn_CMS程序开发模板(使用说明+源代码+html).zip
- 手机在线点餐模板.zip
- [搜索链接]Page Rank查询_pagerank.rar
- matlab imu和视觉里程计 kalman滤波器 进行融合.rar
- matlab 该模拟器由构成IEEE802.11a协议物理层的多个部分集成
- mmexport1713552623466.jpg
- matlab Physical layer of the IEEE 802.11ac simulator.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈