package main;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.plaf.FontUIResource;
import common.GameApp;
import common.MusicPlayer;
/*
* 画布类
*/
public class GamePanel extends JPanel implements ActionListener {
GamePanel gamePanel=this;
private JFrame mainFrame=null;
JMenuBar jmb = null;
BufferedImage bg=null;//背景图片
MusicPlayer musicBg=null;
private int gameHeight=0;
private int gameWidth=0;
public List tempImages = new ArrayList();//临时图片
public BufferedImage tempBufferedImage = null;//临时图片对象
public HashMap imageMap = new HashMap();//图片Map对象
private HashMap enemy1boomImageMap = new HashMap();//敌机爆炸图片Map对象
private HashMap enemy2boomImageMap = new HashMap();//敌机爆炸图片Map对象
private HashMap enemy3boomImageMap = new HashMap();//敌机爆炸图片Map对象
private HashMap enemy4boomImageMap = new HashMap();//敌机爆炸图片Map对象
public List enemyListMap = new ArrayList();
public HashMap mypalneBoomImageMap= new HashMap();//我方飞机爆炸图片Map对象
public MyPlane myPlane=null;//我的飞机
public List enemyList = new ArrayList();
public List bulletList = new ArrayList();
public static boolean startFlag=true;
public static boolean nextEnd=false;
public static boolean nextWin=false;
public int totalCount=1000;
public int curCount = 0;
//构造里面初始化相关参数
public GamePanel(JFrame frame){
this.setLayout(null);
mainFrame = frame;
//创建按钮
initMenu();
//初始化图片
initImage();
//初始化自己的飞机
initMyPlane();
//初始化敌机
initEnemyPlane();
//添加鼠标事件监控
createMouseListener();
mainFrame.setVisible(true);
//主线程启动
new Thread(new RefreshThread()).start();
//开启背景音乐
musicBg=new MusicPlayer("/music/bg.wav");
musicBg.loop(-1);
ready();
}
//定时创建敌机
private void initEnemyPlane() {
new Thread(new Runnable() {
@Override
public void run() {
while (startFlag) {
createEnemyPlane();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
//创建敌机
private void createEnemyPlane() {
EnemyPlane enemyPlane = new EnemyPlane(this);
enemyList.add(enemyPlane);
}
//创建自己飞机
private void initMyPlane() {
myPlane = new MyPlane(300, 530, 132, 86, this);
}
//准备
private void ready() {
tempBufferedImage = (BufferedImage)imageMap.get("ready");
tempImages.add(tempBufferedImage);
//1秒后关闭图片
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
tempImages.remove(tempBufferedImage);
}
}).start();
}
//鼠标事件的创建
private void createMouseListener() {
MouseAdapter mouseAdapter = new MouseAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
int x = e.getX();
int y = e.getY();
if(myPlane==null) return ;
if(myPlane.isCanMove()){
myPlane.move(x,y);
return;
}
//
if(myPlane.isPoint(x,y)){
myPlane.setCanMove(true);
}
}
};
addMouseMotionListener(mouseAdapter);
addMouseListener(mouseAdapter);
}
//游戏结束
public void gameOver() {
nextEnd = true;//表示下一步执行线程后要全部停止
tempBufferedImage = (BufferedImage)imageMap.get("lost");
tempImages.add(tempBufferedImage);
}
public void gameWin(){
nextWin = true;//表示下一步执行线程后要全部停止
tempBufferedImage = (BufferedImage)imageMap.get("win");
tempImages.add(tempBufferedImage);
}
//游戏结束
public void realGameEnd(int type) {
startFlag=false;//停止线程
musicBg.stop();
EnemyPlane enemyPlane=null;
for (int i = 0; i < enemyList.size(); i++) {
enemyPlane = (EnemyPlane)enemyList.get(i);
if(enemyPlane!=null){
enemyPlane.setAlive(false);
enemyPlane=null;
}
}
enemyList.clear();
Bullet bullet=null;
for (int i = 0; i < bulletList.size(); i++) {
bullet = (Bullet)bulletList.get(i);
if(bullet!=null){
bullet.setAlive(false);
bullet=null;
}
}
//type=1 则不需要提示
/* if(type!=1){
if(nextWin){
musicWin=new MusicPlayer("/music/win.wav");
musicWin.play();
}else {
musicLost=new MusicPlayer("/music/lost.wav");
musicLost.play();
}
}*/
}
//重新开始游戏
private void restart() {
realGameEnd(1);
//参数重置
startFlag=true;
nextEnd=false;
nextWin=false;
curCount=0;
if(myPlane!=null){
myPlane.clear();
myPlane=null;//我的飞机
}
EnemyPlane enemyPlane=null;
for (int i = 0; i < enemyList.size(); i++) {
enemyPlane = (EnemyPlane)enemyList.get(i);
if(enemyPlane!=null){
enemyPlane.clear();
}
}
enemyList.clear();
Bullet bullet=null;
for (int i = 0; i < bulletList.size(); i++) {
bullet = (Bullet)bulletList.get(i);
if(bullet!=null){
bullet.clear();
}
}
bulletList.clear();
tempImages.clear();//临时图片清除
tempBufferedImage=null;//临时图片的清空
//初始化自己的飞机
initMyPlane();
//初始化敌机
initEnemyPlane();
new Thread(new RefreshThread()).start(); // 线程启动
//开启背景音乐
musicBg=new MusicPlayer("/music/bg.wav");
musicBg.loop(-1);
ready();
}
//初始图片
private void initImage(){
List commonList = new ArrayList();
commonList.add("bg.jpg");
commonList.add("myplane1.png");
commonList.add("enemy1.png");
commonList.add("enemy2.png");
commonList.add("enemy3.png");
commonList.add("enemy4.png");
commonList.add("bullet.png");
commonList.add("ready.png");
commonList.add("win.png");
commonList.add("lost.png");
imageMap = GameApp.getImageMapByIcon("/images/",commonList);//加载普通图片
List enemy1List = new ArrayList();
for (int i = 1; i <= 6; i++) {
enemy1List.add("enemy1boom"+i+".png");
}
enemy1boomImageMap = GameApp.getImageMapByIcon("/images/enemy1boom/",enemy1List);//加载飞机爆炸的图片
enemyListMap.add(enemy1boomImageMap);
List enemy2List = new ArrayList();
for (int i = 1; i <= 6; i++) {
enemy2List.add("enemy2boom"+i+".png");
}
enemy2boomImageMap = GameApp.getImageMapByIcon("/images/enemy2boom/",enemy2List);//加载飞机爆炸的图片
enemyListMap.add(enemy2boomImageMap);
List enemy3List = new ArrayList();
for (int i = 1; i <= 6; i++) {
enemy3List.add("enemy3boom"+i+".png");
}
enemy3boomImageMap = GameApp.getImageMapByIcon("/images/enemy3boom/",enemy3List);//加载飞机爆炸的图片
enemyListMap.add(enemy3boomImageMap);
List enemy4List = new ArrayList();
for (int i = 1; i <= 6; i++) {
enemy4List.add("enemy4boom"+i+".png");
}
enemy4boomImageMap = GameApp.getImageMapByIcon("/images/enemy4boom/",enemy4List);//加载飞机爆炸的图片
enemyListMap.add(enemy4boomImageMap);
List mypalneBoomList = new ArrayList();
for (int i = 1; i <= 6; i++) {
mypalneBoomList.add("myplane1boom"+i+".png");
}
my
没有合适的资源?快使用搜索试试~ 我知道了~
基于java swing实现的飞机大战游戏源代码,带GUI界面
共122个文件
png:84个
class:19个
java:10个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 130 浏览量
2024-08-02
14:14:53
上传
评论
收藏 12.33MB ZIP 举报
温馨提示
基于java swing实现的飞机大战游戏源代码,带GUI界面
资源推荐
资源详情
资源评论
收起资源包目录
基于java swing实现的飞机大战游戏源代码,带GUI界面 (122个子文件)
GamePanel.class 10KB
EnemyPlane.class 5KB
MyPlane.class 4KB
Bullet.class 4KB
GameApp.class 2KB
MyPlane$1.class 1KB
RandomColor.class 1KB
MusicPlayer.class 1KB
EnemyPlane$1.class 1KB
Bullet$1.class 1KB
GamePanel$RefreshThread.class 1KB
GamePanel$3.class 1018B
MyPlane$2.class 974B
GamePanel$2.class 961B
EnemyPlane$2.class 877B
GamePanel$1.class 856B
Main.class 655B
GameFrame.class 645B
Plane.class 411B
plane.iml 474B
GamePanel.java 12KB
EnemyPlane.java 5KB
MyPlane.java 4KB
Bullet.java 3KB
GameApp.java 2KB
RandomColor.java 833B
MusicPlayer.java 786B
GameFrame.java 492B
Plane.java 319B
Main.java 270B
bg.jpg 88KB
bg.jpg 88KB
enemy4boom5.png 100KB
enemy4boom5.png 100KB
enemy4boom4.png 86KB
enemy4boom4.png 86KB
enemy4.png 83KB
enemy4.png 83KB
enemy4boom3.png 81KB
enemy4boom3.png 81KB
enemy4boom2.png 78KB
enemy4boom2.png 78KB
enemy4boom1.png 76KB
enemy4boom1.png 76KB
enemy4boom6.png 52KB
enemy4boom6.png 52KB
enemy1boom6.png 26KB
enemy1boom6.png 26KB
enemy2boom5.png 24KB
enemy2boom5.png 24KB
enemy2boom4.png 23KB
enemy2boom4.png 23KB
myplane1boom9.png 23KB
myplane1boom9.png 23KB
enemy2.png 22KB
enemy2.png 22KB
enemy2boom3.png 22KB
enemy2boom3.png 22KB
enemy2boom2.png 21KB
enemy2boom2.png 21KB
enemy2boom1.png 21KB
enemy2boom1.png 21KB
myplane1boom8.png 21KB
myplane1boom8.png 21KB
myplane1boom4.png 20KB
myplane1boom4.png 20KB
myplane1boom2.png 20KB
myplane1boom2.png 20KB
enemy3boom5.png 20KB
enemy3boom5.png 20KB
myplane1boom6.png 20KB
myplane1boom6.png 20KB
myplane1boom7.png 20KB
myplane1boom7.png 20KB
myplane1boom1.png 20KB
myplane1boom1.png 20KB
myplane1boom5.png 19KB
myplane1boom5.png 19KB
myplane1.png 19KB
myplane1.png 19KB
myplane1boom3.png 18KB
myplane1boom3.png 18KB
enemy3boom4.png 18KB
enemy3boom4.png 18KB
enemy1boom5.png 17KB
enemy1boom5.png 17KB
lost.png 17KB
lost.png 17KB
enemy3boom3.png 17KB
enemy3boom3.png 17KB
enemy3boom2.png 16KB
enemy3boom2.png 16KB
enemy3boom1.png 15KB
enemy3boom1.png 15KB
enemy1boom4.png 15KB
enemy1boom4.png 15KB
enemy3.png 14KB
enemy3.png 14KB
enemy1boom3.png 13KB
enemy1boom3.png 13KB
共 122 条
- 1
- 2
资源评论
yanglamei1962
- 粉丝: 2532
- 资源: 838
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功