java-飞机大战(源代码)
.
·
今天来更新我的飞机大战了,是参考尚学堂写的,有需要的小伙伴可以直接来取,关于state=2时以及state=3时的运行时可能不太优化,下周我会更新代码的.
1.整个游戏的主窗口以及游戏方法
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
//-----飞机大战的游戏状态: 0.未开始 1.游戏中 2.通关失败 3.通关成功-----
public class planeWin extends JFrame {
//添加背景图片
static ImageIcon backGround=new ImageIcon("D:\\idea.java\\plane\\background.png");
//添加子弹图片
static ImageIcon shall=new ImageIcon("D:\\idea.java\\plane\\myBullet.png");
//添加敌方boss图片
static ImageIcon boss=new ImageIcon("D:\\idea.java\\plane\\blueEnemyPlane.png");
//添加我方飞机图片
static ImageIcon myPlane=new ImageIcon("D:\\idea.java\\plane\\myPlane.png");
//添加敌方飞机的图片
static ImageIcon enemyPlane=new ImageIcon("D:\\idea.java\\plane\\enemyPlane.png");
//添加飞机一个爆炸图片
static ImageIcon explore=new ImageIcon("D:\\idea.java\\plane\\explore.png");
//添加敌方boss的子弹
static ImageIcon bossShall=new ImageIcon("D:\\idea.java\\plane\\bossShall.png");
//添加灰色飞机图片
static ImageIcon geryPlane=new ImageIcon("D:\\idea.java\\plane\\greyEnemyPlane.png");
//创建集合
//整个物体的集合
ArrayList<gameObj> gameObjList=new ArrayList<>();
//子弹物体的集合
ArrayList<shallObj> shallObjList=new ArrayList<>();
//创建敌方飞机的集合
ArrayList<enemyPlaneObj> enemyPlaneObjList=new ArrayList<>();
//创建消失集合
ArrayList<gameObj> removeList=new ArrayList<>();
//给敌方boss添加集合
ArrayList<bossShallObj> bossShallList=new ArrayList<>();
//添加爆炸集合的集合
ArrayList<exploreObj> exploreObjList=new ArrayList<>();
//创建灰色飞机的集合
ArrayList<geryPlaneObj> geryEnemyPlaneList=new ArrayList<>();
//解决图片闪动,实现双缓存
Image offScreenImage=null;
//创建背景类的对象
bgObj bg=new bgObj(backGround.getImage(),0,-2000,2);
//创建我方飞机的对象
public planeObj myplane=new planeObj(myPlane.getImage(),296,500,37,41,0,this);
//创建敌方BOSS
bossObj bossobj=null;
//得到图片更新的次数
int count=1;
//统计敌方飞机生成的数量
int enemyNumber=0;
//设置分数
public static int score=0;
//设置游戏初始状态为0
public static int state=0;
//创建窗口
public void JFrame(){
//设置标题
this.setTitle("飞机大战 v1.0");
//设置窗口关闭事件
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//设置画布大小
this.setSize(600,600);
//设置窗口居中
this.setLocationRelativeTo(null);
//设置窗口可见
this.setVisible(true);
gameObjList.add(bg);
gameObjList.add(myplane);
//添加鼠标点击事件
this.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent m){
if(m.getButton()==1&&state==0){
state=1;
repaint();
}
}
});
//添加空格鼠标按键,实现游戏的暂停功能
this.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==32){
switch (state){
case 0:
state=1;
break;
case 1:
state=0;
break;
default:
}
}
}
});
//创建时间管理计时器
new Thread(() -> {
while(true){
if(state==1){
creatObj();
repaint();
}
try {
Thread.sleep(25);
} catch (InterruptedException e){
e.printStackTrace();
}
}
}).start();
}
@Override
public void paint(Graphics g){
if(offScreenImage==null){
offScreenImage=createImage(600,600);
}
Graphics gImage=offScreenImage.getGraphics();
gImage.fillRect(0,0,600,600);
//游戏未开始
if(state==0){
backGround.paintIcon(this,gImage,0,0);
myPlane.paintIcon(this,gImage,296,500);
shall.paintIcon(this,gImage,309,450);
boss.paintIcon(this,gImage,230,80);
drawWord(gImage,"游戏开始",254,300,30,Color.yellow);
}
//游戏进行中
else if(state==1){
gameObjList.addAll(exploreObjList);
for(int i=0;i<gameObjList.size();i++){
gameObjList.get(i).paintSelf(gImage);
}
gameObjList.removeAll(removeList);
}
//游戏通关失败
else if(state==2){
backGround.paintIcon(this,gImage,0,0);
explore.paintIcon(this,gImage, myplane.getX()-25,myplane.getY()-34);
drawWord(gImage,"GAME OVER",225,300,40,Color.red);
}
//游戏通过成功
else if(state==3){
backGround.paintIcon(this,gImage,0,0);
drawWord(gImage,"挑战成功!",230,300,40,Color.green);
}
drawWord(gImage,"Score: "+score,30,80,20,Color.white);
g.drawImage(offScreenImage,0,0,null);
count++;
}
void creatObj(){
//创建我方子弹
if(count%15==0) {
shallObj newShallObj = new shallObj(shall.getImage(), myplane.getX() + 5, myplane.getY() - 16, 30, 30, 5, this);
gameObjList.add(newShallObj);
shallObjList.add(newShallObj);
}
//创建敌方飞机
if(count%20==0) {
enemyPlaneObj newEnemyObj=new enemyPlaneObj(enemyPlane.getImage(),(int)(Math.random()*12)*50,0,49,36,5,this);
gameObjList.add(newEnemyObj);
enemyPlaneObjList.add(newEnemyObj);
enemyNumber++;
}
//创建敌方灰色飞机
if(count%50==0){
geryPlaneObj newGeryPlane=new geryPlaneObj(geryPlane.getImage(),(int)(Math.random()*12)*50,0,44,67,6,this) ;
gameObjList.add(newGeryPlane);
geryEnemyPlaneList.add(newGeryPlane);
}
//创建boss的集合
if(count%20==0&&bossobj!=null){
bossShallObj newBossShall=new bossShallObj(bossShall.getImage(), bossobj.getX()+75,bossobj.getY()+150,40,40,5,this);
gameObjList.add(newBossShall);
bossShallList.add(newBossShall);
}
//创建boss
if(enemyNumber>=15&&bossobj==null){
bossobj=new bossObj(boss.getImage(),50,50,150,100,4,this);
gameObjList.add(bossobj);
}
}
public void drawWord(Graphics gImage,String str,int x,int y,int size,Color color) {
gImage.setColor(color);
gImage.setFont(new Font("仿宋", Font.BOLD, size));
gImage.drawString(str, x, y);
}
public static void main(String[] args) {
planeWin win=new planeWin();
win.JFrame();
}
}
2.类:
1.整个游戏中的物体需要继承的父类
import java.awt.*;
public class gameObj {
Image img;
int x;
int y;
int width;
int height;
double speed;
planeWin frame;
public gameObj() {
}
public gameObj(Image img, int x, int y, int width, int height, double speed, planeWin frame) {
this.img = img;
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.speed = speed;
this.frame = frame;
}
public gameObj(Image img, int x, int y, double speed) {
this.img = img;
this.x = x;
this.y = y;
this.speed = speed;
}
public gameObj(int x, int y) {
this.x = x;
this.y = y;
}
public Image getImg() {
return img;
}
public void setImg(Image img) {
this.img = img;
}
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 double getSpeed() {
return speed;
}
public void setSpeed(double speed) {
this.speed = speed;
}
public planeWin getFrame() {
return frame;
}
public void setFrame(planeWin frame) {
this.frame = frame;
}
public void paintSelf(Graphics gImage){
gImage.drawImage(img,x,y,null);
}
//用于碰撞事件
public Rectangle getRct(){
return new Rectangle(x,y,width,height);
}
}
2.背景类
import java.awt.*;
public class bgObj extends gameObj {
public bgObj() {
}
public bgObj(Image img, int x, int y, int width, int height, double speed, planeWin frame) {
super(img, x, y, width, height, speed, frame);
}
public bgObj(Image img, int x, int y, double speed) {
super(img, x, y, speed);
}
@Override
public void paintSelf(Graphics gImage) {
super.paintSelf(gImage);
y+=2;
//实现背景的反复应用
if(y>=0){
y=-2000;
}
}
}
3.我方飞机类
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class planeObj extends gameObj{
public planeObj() {
}
public planeObj(Image img, int x, int y, int width, int height, double speed, planeWin frame) {
super(img, x, y, width, height, speed, frame);
//添加鼠标移动事件控制飞机的移动,随鼠标移动而移动
this.frame.addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
//让飞机的坐标跟随鼠标的位置
planeObj.super.x=e.getX()-11;
planeObj.super.y=e.getY()-16;
}
});
}
@Override
public void paintSelf(Graphics gImage) {
super.paintSelf(gImage);
}
@Override
public Rectangle getRct() {
return super.getRct();
}
}
4.敌方飞机1类
import java.awt.*;
public class enemyPlaneObj extends gameObj{
public enemyPlaneObj() {
}
public enemyPlaneObj(Image img, int x, int y, int width, int height, double speed, planeWin frame) {
super(img, x, y, width, height, speed, frame);
}
@Override
public void paintSelf(Graphics gImage) {
gImage.drawImage(img, x, y, null);
y+=speed;
if(y>600){
this.setX(-300);
this.setY(300);
frame.removeList.add(this);
}
if(frame.myplane.getRct().intersects(x,y,this.getWidth()/2,this.getHeight()/2)){
planeWin.state =2;
frame.repaint();
}
//遍历整个集合里面的类
for(shallObj shallObj: frame.shallObjList){
//如果发生碰撞
if(this.getRct().intersects(shallObj.getRct())){
//添加爆炸类
exploreObj NewexploreObj=new exploreObj(x,y);
frame.exploreObjList.add(NewexploreObj);
frame.removeList.add(NewexploreObj);
this.x=-100;
this.y=100;
shallObj.setX(-200);
shallObj.setY(200);
frame.removeList.add(this);
frame.removeList.add(shallObj);
planeWin.score+=1;
}
}
}
@Override
public Rectangle getRct() {
return super.getRct();
}
}
5.敌方飞机2类
import java.awt.*;
public class geryPlaneObj extends gameObj{
int life=2;
public geryPlaneObj(Image img, int x, int y, int width, int height, double speed, planeWin frame) {
super(img, x, y, width, height, speed, frame);
}
@Override
public void paintSelf(Graphics gImage) {
super.paintSelf(gImage);
y+=speed;
if(y>600){
this.setX(-300);
this.setY(300);
frame.removeList.add(this);
}
if(frame.myplane.getRct().intersects(this.getRct())){
planeWin.state=2;
frame.repaint();
}
for(shallObj shallObj: frame.shallObjList){
if(this.getRct().intersects(shallObj.getRct())){
life--;
}
if(life<=0){
//添加爆炸类
exploreObj NewexploreObj=new exploreObj(x,y);
frame.exploreObjList.add(NewexploreObj);
frame.removeList.add(NewexploreObj);
this.x=-100;
this.y=100;
shallObj.setX(-200);
shallObj.setY(200);
frame.removeList.add(this);
frame.removeList.add(shallObj);
planeWin.score+=2;
}
}
}
@Override
public Rectangle getRct() {
return super.getRct();
}
}
6.我方子弹类
import java.awt.*;
public class shallObj extends gameObj{
public shallObj() {
}
public shallObj(Image img, int x, int y, int width, int height, double speed, planeWin frame) {
super(img, x, y, width, height, speed, frame);
}
@Override
public void paintSelf(Graphics gImage) {
gImage.drawImage(img, x, y, null);
y-=speed;
if(y<0){
this.setX(-400);
this.setY(400);
frame.removeList.add(this);
}
}
@Override
public Rectangle getRct() {
return super.getRct();
}
}
7.敌方boss类
import java.awt.*;
public class bossObj extends gameObj{
int life=10;
public bossObj(Image img, int x, int y, int width, int height, double speed, planeWin frame) {
super(img, x, y, width, height, speed, frame);
}
@Override
public void paintSelf(Graphics gImage) {
super.paintSelf(gImage);
if(x<-10||x>490){
speed=-speed;
}
x+=speed;
if(frame.myplane.getRct().intersects(x,y,getWidth()/2,getHeight()/2)){
planeWin.state=2;
frame.repaint(100);
}
for(shallObj shallObj: frame.shallObjList){
if(this.getRct().intersects(shallObj.getRct())){
life--;
shallObj.setX(-200);
shallObj.setY(200);
frame.removeList.add(shallObj);
}
if(life<=0){
this.setX(-500);
this.setY(200);
planeWin.state=3;
frame.repaint();
}
gImage.setColor(Color.white);
gImage.fillRect(10,40,100,10);
gImage.setColor(Color.red);
gImage.fillRect(10,40,life*100/10,10);
}
}
@Override
public Rectangle getRct() {
return super.getRct();
}
}
8.敌方boss子弹类
import java.awt.*;
public class bossShallObj extends gameObj{
public bossShallObj(Image img, int x, int y, int width, int height, double speed, planeWin frame) {
super(img, x, y, width, height, speed, frame);
}
@Override
public void paintSelf(Graphics gImage) {
super.paintSelf(gImage);
y+=speed;
if(y>600){
this.setX(-300);
this.setY(300);
frame.removeList.add(this);
}
// 检测与 bossShallObj 的碰撞
for (bossShallObj shallObj : frame.bossShallList) {
if (shallObj.getRct().intersects(frame.myplane.getX()+frame.myplane.width/2,frame.myplane.getY()+frame.myplane.height/2,frame.myplane.width/4,frame.myplane.height /4)) {
// 如果与玩家飞机相交,设置游戏状态为 2
planeWin.state = 2;
frame.repaint();
}
}
}
@Override
public Rectangle getRct() {
return super.getRct();
}
}
9.爆炸图类
import java.awt.*;
public class exploreObj extends gameObj{
int pictureNumber=0;
static Image[] exploreImages=new Image[16];
static {
for (int i=0;i<15;i++){
exploreImages[i]=Toolkit.getDefaultToolkit().getImage("D:\\idea.java\\plane\\img_"+(17+i)+".png");
}
}
public exploreObj(int x, int y) {
super(x, y);
}
@Override
public void paintSelf(Graphics gImage) {
//循环16次添加爆炸图片
if(pictureNumber<15){
img=exploreImages[pictureNumber];
super.paintSelf(gImage);
pictureNumber++;
}
}
}
写到
更多推荐
已为社区贡献1条内容
所有评论(0)