专业的网站建设运营,如何制作聊天软件,传统外贸网站的seo运用,中天建设集团门户网站游戏界面使用JFrame和JPanel构建。背景图通过BG类绘制。英雄机和敌机在界面上显示并移动。子弹从英雄机发射并在屏幕上移动。游戏有四种状态#xff1a;READY、RUNNING、PAUSE、GAMEOVER。状态通过鼠标点击进行切换#xff1a;点击开始游戏#xff08;从READY变为RUNNINGREADY、RUNNING、PAUSE、GAMEOVER。状态通过鼠标点击进行切换点击开始游戏从READY变为RUNNING。点击重新开始游戏从GAMEOVER变为READY。鼠标移出窗口时游戏暂停从RUNNING变为PAUSE。鼠标移入窗口时游戏继续从PAUSE变为RUNNING。开局默认玩家生命值为3分数为0。每隔一定时间随机生成敌机小飞机、大飞机、小飞碟。敌机在屏幕上移动并与英雄机进行碰撞检测。英雄机可以发射子弹。子弹在屏幕上移动并与敌机进行碰撞检测。英雄机与敌机发生碰撞时英雄机会损失一条生命值。子弹与敌机发生碰撞时敌机会被摧毁玩家的分增加。当子弹与奖励机发生碰撞时奖励机会被摧毁玩家随机获得两张奖励生命值加1、双倍子弹。在屏幕左上角显示当前的生命值和得分。生命值减少到0时游戏进入结束状态。再次点击重新开始游戏。
1.图片素材百度网盘分享
链接https://pan.baidu.com/s/10QiTcddI_Zxw5jbn8fmOpA?pwdhuan 提取码huan 2.主页面world.java
package plane.gzeu;import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Arrays;
import java.util.Timer;
import java.util.TimerTask;public class World extends JPanel {//ImageIcon类保存图片的路径-----读取图片
// ImageIcon icon new ImageIcon(images\\background.png);
// BG bg new BG(0,0,420,700,2);
// Airplane airplane new Airplane(100,200,300,620,2);
// Bee bee new Bee(200,100,200,700,2);BG bg new BG();EnemyPlane [] planes;Hero hero;int index; //用来控制敌机的数量Bullet[] bullets; //创建子弹的数组int life; //英雄机的生命值int score; //分数//定义游戏的四种状态准备运行暂停结束常量public static final int READY 0;public static final int RUNNING 1;public static final int PAUSE 2;public static final int GAMEOVER 3;//游戏的初始状态int state READY;// Airplane [] airPlanes new Airplane[10];
// Bigairplane [] bigAirPlanes new Bigairplane[10];
// Bee [] bee new Bee[10];
// Bom [] bom new Bom[4];
// Bullet [] bullets new Bullet[3];
// Hero [] hero new Hero[2];//初始化方法public void init(){bg new BG();hero new Hero();planes new EnemyPlane[0]; //默认一个敌机都没有bullets new Bullet[0]; //默认一个子弹都没有index 0; //默认从0开始life 3; //默认3条命score 0; //分数为0}public World() {init();// for(int i0;iplanes.length;i){
// if(i%20){
// planes[i]new Airplane();
// }
// else if(i%50 i%2!0){
// planes[i] new Bee();
// }
// else {
// planes[i] new Bigairplane();
// }
// }
// for(int i 0;iairPlanes.length;i){
// airPlanes[i] new Airplane();
// bigAirPlanes[i]new BigAirPlane();
// bee[i]new Bee();
// bom[i]new Bom();
// bullets[i]new Bullet();
// hero[i]new Hero();}//生成敌机的方法public void createPlane(){if(index%160){int n (int)(Math.random()*10);EnemyPlane plane;switch (n){case 9 :plane new Bee(); //10%生成小飞碟break;case 8 :case 7 :plane new Bigairplane(); //20%生成大飞机break;default:plane new Airplane(); //70%生成小飞机}//将敌机存入数组中之前要先对数组进行扩容处理planes Arrays.copyOf(planes,planes.length1);//将生存的敌机放入数组的最后一个位置planes[planes.length-1] plane;}}// for(int i 0;ibigAirPlanes.length;i){
// bigAirPlanes[i]new Bigairplane();
// }
// }//绘制图片printingOverridepublic void paint(Graphics g) { //paint挂载到当前类当前类实例化创建对象时自动调用
// super.paint(g);
// icon.paintIcon(this,g,0,y1); //绘制背景默认第一个绘制
// bg.icon.paintIcon(this,g,(int)bg.x,(int)bg.y);
// bg.move();
// airplane.icon.paintIcon(this,g,(int)airplane.x,(int)airplane.y);
// bee.icon.paintIcon(this,g,(int)bee.x,(int)bee.y);bg.painting(g);hero.painting(g);for(int i0;iplanes.length;i){planes[i].painting(g);}for(int i0;ibullets.length;i){bullets[i].painting(g);}g.setColor(new Color(255,255,255));//设置字体g.setFont(new Font(微软雅黑,Font.BOLD,20));g.drawString(生命值life,20,20);g.drawString(分数score,20,40);if(stateREADY){Images.start.paintIcon(this,g,0,0);}if(statePAUSE){Images.pause.paintIcon(this,g,0,0);}if(stateGAMEOVER){Images.gameover.paintIcon(this,g,0,0);}// for(int i 0;iairPlanes.length;i){
// airPlanes[i].painting(g);
// bigAirPlanes[i].painting(g);
// bee[i].painting(g);
// bom[i].painting(g);
// bullets[i].painting(g);
// hero[i].painting(g);
// }
// for(int i 0;ibigAirPlanes.length;i){
// bigAirPlanes[i].painting(g);
// }repaint();//刷新窗口}public static void main(String[] args) {//1.显示画框外层:JFrameJFrame jf new JFrame();//2.显示面板:JpanelWorld jp new World();//3.将面板放入画框中:jf.add(jp);//对窗口进行设置jf.setTitle(我的窗口);//设置标题jf.setSize(436,700); //设置窗口大小jf.setLocationRelativeTo(null); //居中显示jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //设置关闭窗口后自动结束程序//4.显示窗口jf.setVisible(true);jp.action();}//唤醒定时器的方法public void action(){java.util.Timer timer new Timer();MyTesk mt new MyTesk();timer.schedule(mt,1000,1000/50);this.addMouseListener(new MouseAdapter() {//监听鼠标点击Overridepublic void mouseClicked(MouseEvent e) {if(stateREADY){stateRUNNING;}if(stateGAMEOVER){stateREADY;init();}}//鼠标移除Overridepublic void mouseExited(MouseEvent e) {
// System.out.println(鼠标移出);if(stateRUNNING){statePAUSE;}}//鼠标移入Overridepublic void mouseEntered(MouseEvent e) {if(statePAUSE){stateRUNNING;}}});//匿名内部类MouseAdapter mouseAdapter new MouseAdapter() {Overridepublic void mouseMoved(MouseEvent e) {if(stateRUNNING){int x e.getX();int y e.getY();hero.move(x,y);}}};//安装监听器this.addMouseMotionListener(mouseAdapter);}public void createBullet(){if(!hero.isLiving()){ //英雄机死亡不发射子弹return;}if(index%170){Bullet[] bullets1 hero.double_fire();//俩个数组的合并Bullet[] arr Arrays.copyOf(bullets,bullets.lengthbullets1.length);System.arraycopy(bullets1,0,arr,bullets.length,bullets1.length);bullets arr;
// Bullet bullet hero.fire(); //英雄机调用fire方法生成子弹
// //将子弹放入数组中
// bullets Arrays.copyOf(bullets,bullets.length1); //子弹扩容
// bullets[bullets.length-1] bullet;}}//加分的方法public void addScore(FlyingObjesct plane){if(plane.isDead()){if(plane instanceof Enemy){Enemy enemy (Enemy) plane; //需要做一个强制转化score enemy.getScore();}if(plane instanceof Aword){Aword aword (Aword) plane;int type aword.getAword();if(type Aword.DOUBLE_FIRE){hero.getdouble_fire();}if(type Aword.LIFE){life;}}}}//检测敌机与英雄机的碰撞public void hero_hit(){if(hero.isLiving()){for(int i0;iplanes.length;i){if(!planes[i].isLiving()){continue;}if(hero.bong(planes[i])){hero.goDead();planes[i].goDead();break;}}}else if(hero.isWait()){ //僵尸状态if(life1){//重新开始hero new Hero();//清屏for(int i0;iplanes.length;i){planes[i].goDead();}life--;}else{life--;stateGAMEOVER;}}}//检测每个敌机与子弹的碰撞情况public void hit(){for(int i0;ibullets.length;i){Bullet bullet bullets[i]; //拿出每一颗子弹if(!bullet.isLiving()){continue;}for(int j0;jplanes.length;j){FlyingObjesct p planes[j];if(!p.isLiving()){continue;}if(p.bong(bullet)){ //被击中p.attack();bullet.goDead();addScore(p);}}}}//清理爆炸的飞机public void clean(){//清除飞机EnemyPlane[] living new EnemyPlane[planes.length];int index 0;for(int i0;iplanes.length;i){ //遍历敌机数组if(planes[i].isWait() || planes[i].outOfBound()){ //如果是等待状态的效果就跳过continue;}living[index] planes[i]; //将不是等待状态的敌机存在living里面}planes Arrays.copyOf(living,index);//清除子弹Bullet[] livingBullet new Bullet[bullets.length];index 0;for(int i0;ibullets.length;i){if(bullets[i].isDead() || bullets[i].outOfBound()){ //如果是已经爆炸的子弹就跳过continue;}livingBullet[index] bullets[i];}bullets Arrays.copyOf(livingBullet,index);}class MyTesk extends TimerTask {Overridepublic void run() {index;if(state RUNNING){createPlane(); //调用生成敌机的方法createBullet(); //调用生成子弹的方法hit();//调用子弹击打的效果clean(); //调用清理敌机方法hero_hit();bg.move();for(int i 0;iplanes.length;i){planes[i].move();}for(int i 0;ibullets.length;i){bullets[i].move();}}}}
}3.小飞机类Airplane.java
package plane.gzeu;import javax.swing.*;
//飞机类
public class Airplane extends EnemyPlane implements Enemy{ // 实现接口时要实现接口中的方法public Airplane(){this.images Images.airplane;w images[0].getIconWidth();h images[0].getIconHeight();x(int)(Math.random()*(420-images[0].getIconWidth()));y-2*images[0].getIconHeight();this.step(Math.random()*2.50.7);this.iconnew ImageIcon(images\\airplane1.png);
// this.icon Images.airplane[0];}public Airplane(double x, double y, double w, double h,double speed) {
// super(x, y, w, h);this.stepspeed;this.iconImages.airplane[0];}Overridepublic int getScore() {return 10;}
}4.大飞机类Bigairplane
package plane.gzeu;//大飞机类
public class Bigairplane extends EnemyPlane implements Enemy{public Bigairplane(double x, double y, double w, double h,double step) {
// super(x, y, w, h);this.stepstep;this.icon Images.bigairplane[0];}public Bigairplane(){this.images Images.bigairplane; //初始化数组w images[0].getIconWidth();h images[0].getIconHeight();x(int)(Math.random()*(420-images[0].getIconWidth()));y-2*images[0].getIconHeight();this.step(Math.random()*3.50.7);
// this.icon Images.bigairplane[0];life 4;}Overridepublic int getScore() {return 50;}
}5.英雄机类Hero.java
package plane.gzeu;import javax.swing.*;import static plane.gzeu.Images.bullet;public class Hero extends EnemyPlane{public Hero(){this.images Images.hero;x431/2-images[0].getIconWidth()/2;y510;wimages[0].getIconWidth();himages[0].getIconHeight();
// this.speed0.2;this.icon Images.hero[0];}public Hero(double x, double y, double w, double h,double step) {
// super(x, y, w, h);this.stepstep;
// this.icon new ImageIcon(images\\hero1.png); //设置了Images类直接调用类名就可以引用图片this.icon Images.hero[0];}Overridepublic void move() {}//接收鼠标的坐标public void move(int x,int y) {this.x x-images[0].getIconWidth()/2;this.y y-images[0].getIconHeight()/2;}/** 子弹的位置在英雄机上 b.x h.x-images[0].getIconWidth()/2; b.yh.y* 子弹的移动:向上b.y-b.step;* 子弹是无限的数序扩容* 射击方法当英雄机调用一次射击方法时就发射一个子弹* *///射击的方法public Bullet fire(){double x this.xw/2-4; //获取英雄机的x坐标Bullet bullet new Bullet(x,y);//将处理过后的坐标传给子弹return bullet; //将子弹返回}//获取双倍子弹的方法int doubleTime 0; // 双倍子弹的时间20次public void getdouble_fire(){doubleTime 20;}//双倍子弹方法public Bullet[] double_fire(){if(doubleTime0){double x this.xw/2-7;double x1 this.xw/2-2; //获取英雄机的x坐标Bullet bullet1 new Bullet(x,y);Bullet bullet2 new Bullet(x1,y);//将处理过后的坐标传给子弹Bullet [] bullets new Bullet[]{bullet1,bullet2};
// return new Bullet[]{bullet1,bullet2};return bullets;}else {double x this.xw/2-4; //获取英雄机的x坐标Bullet bullet1 new Bullet(x,y);return new Bullet[]{bullet1};}
//}//测试
// public static void main(String[] args) {
// Hero hero new Hero();
// hero.move(200,300);
// Bullet bullet hero.fire();
// System.out.println(bullet.x bullet.y);
// }
}6.敌机类Bee.java
package plane.gzeu;import javax.swing.*;public class Bee extends EnemyPlane implements Aword {double speed1;public Bee(){this.images Images.bee;w images[0].getIconWidth();h images[0].getIconHeight();x(int)(Math.random()*(420-images[0].getIconWidth()));y-2*images[0].getIconHeight();this.step(Math.random()*4.52.5);this.speed1(Math.random()*3.50.5);
// this.iconnew ImageIcon(images\\bee0.png);this.iconImages.bee[0];life 6;}public Bee(double x, double y, double w, double h,double step) {
// super(x, y, w, h);this.stepstep;this.icon Images.bee[0];}Overridepublic void move() {yspeed1;xstep;if(x431-images[0].getIconWidth()){step -step;}if(x0){step -step;}}Overridepublic int getAword() {return Math.random()0.5?DOUBLE_FIRE:LIFE;}
}7.背景类BG.java
package plane.gzeu;import javax.swing.*;
import java.awt.*;//背景类(子类)
//继承FlyingObjesct类得到FlyingObjesct类的方法及属性
public class BG extends FlyingObjesct {double y0;public BG(){x 0;y 0;icon Images.bg;w icon.getIconWidth();h icon.getIconHeight();y0 -h;step 2;}public BG(double x, double y, double w, double h,double step) {super(x, y, w, h); //重载调用父类的构造方法实现方法复用this.step step;this.icon Images.bg; //背景是固定的}public void painting(Graphics g){icon.paintIcon(null,g,(int)x,(int)y);icon.paintIcon(null,g,(int)x,(int)y0);}Overridepublic void move() {ystep;y0step;if(yh){y-h;}if(y0h){y0-h;}}
}8.爆炸类Bom.java
package plane.gzeu;import javax.swing.*;public class Bom extends EnemyPlane{public Bom(){x(int)(Math.random()*370);y(int)(Math.random()*370);this.step 0.2;this.icon Images.bom[0];}public Bom(double x, double y, double w, double h, double speed) {//要重写父类的构造方法。新增一个速度
// super(x, y, w, h);//重载调用父类的构造方法实现方法复用this.stepspeed;this.iconImages.bom[0];}
}
9.子弹类Bullet.java
package plane.gzeu;import javax.swing.*;//继承FlyingObjesct类得到FlyingObjesct类的方法及属性
public class Bullet extends EnemyPlane {public Bullet(double x,double y){this.icon Images.bullet;w icon.getIconWidth();h icon.getIconHeight();this.x x;this.y y;step 2;}public Bullet(){x(int)(Math.random()*370);y(int)(Math.random()*370);this.step 0.2;this.icon Images.bullet;}public Bullet(double x, double y, double w, double h, double step) {
// super(x, y, w, h); //重载调用父类的构造方法实现方法复用this.step step;this.icon Images.bullet; //背景是固定的}Overridepublic void move() {y-step;}
}10.三种敌机总类EnemyPlane.java
package plane.gzeu;
public abstract class EnemyPlane extends FlyingObjesct {Overridepublic void move() {ystep;}
}11.飞行物的总类(父类)FlyingObjesct.java
package plane.gzeu;import javax.swing.*;
import java.awt.*;//飞行物的总类(父类)
public abstract class FlyingObjesct {public static final int LIVING 1; //活着public static final int DEAD 0; //死亡public static final int WAIT -1; //等待死亡播放爆炸动画//飞机默认是活着的public int state LIVING;//飞机的生命值public int life 2;public double x,y,step,w,h;//step速度public ImageIcon icon; //图片
// Images images;public ImageIcon[] images; //数组public int index 0;public ImageIcon [] boms Images.bom; //存放爆炸效果的图片//赋初值构造方法//快速生成构建方法快捷键AltInsertpublic FlyingObjesct(double x, double y, double w, double h) {this.x x;this.y y;this.w w;this.h h;}//当飞机击打一次生命值减一当生命值为0时进入死亡状态public boolean attack(){if(life0){life--;if(life0){state DEAD;}return true;}return false;}
//让敌机快速去死public boolean goDead(){if(stateLIVING){life 0;state DEAD;return true;}else{return false;}}//判断飞机的三种状态public boolean isLiving(){return stateLIVING; //判断是否活着}public boolean isDead(){return stateDEAD; //判断是否死了}public boolean isWait(){return stateWAIT; //判断是否等待死亡}/*实现动画效果* 1.用数组存储动画效果图片* 2.初始化数组* 3.定义一个计数器作为切换图片的控制条件* 4.写一个方法实现切换图片的操作* */int i;public void nextImage(){switch (state){case LIVING:if(images null) {return;}icon images[index/300%images.length]; //0-[数组的长度-1]break;case DEAD:if(bomsnull){return;}if(i/300boms.length){state WAIT;return;}icon boms[i/300];}}public FlyingObjesct(){}//无参构造//移动public abstract void move();//绘制图形 方法public void painting(Graphics g){nextImage();icon.paintIcon(null,g,(int)x,(int)y);}//检测敌机是否碰到子弹判断条件中心距cH/2h/2public boolean bong(FlyingObjesct bullet){FlyingObjesct p this;double a Math.min(p.w,p.h)/2;double b Math.min(bullet.w,bullet.h)/2;double x1p.xp.w/2;double y1p.yp.h/2;double x2bullet.xbullet.w/2;double y2bullet.ybullet.h/2;double c Math.sqrt((x2-x1)*(x2-x1)(y2-y1)*(y2-y1));return cab;}//清除越界的敌机和子弹条件: y-h-100 || y700100public boolean outOfBound(){if(y-h-100){return true;}else if(y700100){return true;}return false;}
}12.图片类Images.java
package plane.gzeu;import javax.swing.*;public class Images {public static ImageIcon bg;public static ImageIcon bullet;public static ImageIcon start;public static ImageIcon pause;public static ImageIcon gameover;public static ImageIcon[] hero;public static ImageIcon[] bigairplane;public static ImageIcon[] bee;public static ImageIcon[] airplane;public static ImageIcon[] bom;static {airplane new ImageIcon[2];airplane[0] new ImageIcon(images/airplane0.png);airplane[1] new ImageIcon(images/airplane1.png);bigairplane new ImageIcon[2];bigairplane[0] new ImageIcon(images/bigairplane0.png);bigairplane[1] new ImageIcon(images/bigairplane1.png);bee new ImageIcon[2];bee[0] new ImageIcon(images\\bee0.png);bee[1] new ImageIcon(images\\bee1.png);bom new ImageIcon[4];bom[0] new ImageIcon(images/bom1.png);bom[1] new ImageIcon(images/bom2.png);bom[2] new ImageIcon(images/bom3.png);bom[3] new ImageIcon(images/bom4.png);bg new ImageIcon(images\\background.png);bullet new ImageIcon(images\\bullet.png);start new ImageIcon(images\\start.png);pause new ImageIcon(images\\pause.png);gameover new ImageIcon(images\\gameover.png);hero new ImageIcon[2];hero[0] new ImageIcon(images\\hero0.png);hero[1] new ImageIcon(images\\hero1.png);}//测试图片是否传过来了public static void main(String[] args) {System.out.println(start.getImageLoadStatus()); //如果输出8那图片传过来了输出其他数字有问题System.out.println(bg.getImageLoadStatus());System.out.println(bom[0].getImageLoadStatus());System.out.println(bee[1].getImageLoadStatus());System.out.println(airplane[1].getImageLoadStatus());}}13.获得奖励的接口Aword.java
package plane.gzeu;
//获得奖励的接口
public interface Aword {int DOUBLE_FIRE 1; //第一种奖励双倍火力int LIFE 2; //加生命值int getAword(); //获得奖励的方法
}14.获得分数的接口Enemy.java
package plane.gzeu;
//获得分数的接口
public interface Enemy { //加分
// int a 5;//都是静态常量
// public abstract void add(); //都是抽象方法public int getScore(); //获得分数的方法
}
运行界面
开始界面 存活界面 暂停界面 结束界面