本文实例为大家分享了java实现飞机大战游戏的具体代码,供大家参考,具体内容如下
MyPanel类
package P; import java.awt.Font; import java.awt.Graphics; import java.awt.Point; import java.awt.Rectangle; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionListener; import java.util.ArrayList; import java.util.List; import javax.sql.RowSetInternal; import javax.swing.ImageIcon; import javax.swing.JPanel; //xx.2 //创建面板类,继承面板,实现移动监听事件 public class MyPanel extends JPanel implements MouseMotionListener { //private ImageIcon bjImage = new ImageIcon("img/bj.png"); //英雄机图片 private ImageIcon heroImage = new ImageIcon("img/头.png"); //显示logo //private ImageIcon logoImage = new ImageIcon("img/头.png"); //英雄机的宽高 private int width = heroImage.getIconWidth(); private int height = heroImage.getIconHeight(); //英雄机的坐标 private int x=180; private int y=880; //创建一个敌机的集合,用于展示多个敌机 Listenemys = new ArrayList<>(); //存储子弹集合 List bullets = new ArrayList<>(); //存储爆炸对象集合 List bombs = new ArrayList<>(); //存储英雄机爆炸集合 List heroDeads = new ArrayList<>(); private int number;//统计当前得分 private int m;//统计剩余血条量 // MyPanel jp = new MyPanel() { // { // setBackground(Color.gray);// 设置面板背景颜色 // } public MyPanel() { //在构造方法中准备10个敌机 for(int i=0;i<10;i++){ enemys.add(new Enemy()); } } @Override public void paint(Graphics g) { //用于绘制图片区域 super.paint(g); //在窗口左侧展示打飞机得分 //设置字体: 参数1:字体家族, 参数2:字体样式:加粗 参数3:字体大小 g.setFont(new Font("宋体", Font.BOLD, 30)); g.drawString("当前得分:"+number, 5, 30); g.drawString("发量(万根):"+(5-m), 5, 80); //g.drawImage(null, x, y, getBackground(), getFocusCycleRootAncestor()); //绘制英雄机图片 参数1,图片 参数2和3:坐标 g.drawImage(heroImage.getImage(), x, y, null); //g.drawImage(logoImage.getImage(), 19, 22, null); //在绘图中显示10辆敌机 for(int i=0;i GameMain.HEIGHT) { //int count1=enemys.size(); enemys.remove(en); //移除对象 enemys.add(new Enemy()); //重新再创建对象 } //L 3-4 //在敌机的循环中,再继续循环子弹; 需要判断是否有子弹和敌机重叠了, //则移除敌机对象,重新添加,移除子弹;如果有则添加爆炸对象 for(int j=0;j 6){ bombs.remove(bomb); //在循环的一定范围后,可以移除爆炸了 } } //xx.2 //每次的移动都需要停顿一下 try { Thread.sleep(6); //单位:毫秒 睡眠6毫秒 } catch (InterruptedException e) { e.printStackTrace(); } repaint(); //重新绘制图片 } } //zz-6 //英雄机与敌机的碰撞 private boolean isHit(Enemy en) { //英雄机的碰撞区域 Rectangle rect = new Rectangle(x, y, width, height); //碰撞点的位置,是在敌机的中心点 Point point = new Point(en.getX()+en.getWidth()/2, en.getY()+en.getHeight()/2); return rect.contains(point); } //L-5 private boolean isHit(Enemy en, Bullet bu) { //填充敌机的碰撞区域 Rectangle rect = new Rectangle(en.getX(), en.getY(), en.getWidth(), en.getHeight()); //将子弹的位置设置在中间 Point point = new Point(bu.getX()+bu.getWidth()/2, bu.getY()+bu.getHeight()/2); //如果位置有重叠,返回true return rect.contains(point); } }
HeroDead类
package P; import java.awt.Graphics; import javax.swing.ImageIcon; public class HeroDead { private ImageIcon heroImage = new ImageIcon("img/爆炸.gif"); private int width = heroImage.getIconWidth(); private int height = heroImage.getIconHeight(); private int x; private int y; public ImageIcon getHeroImage() { return heroImage; } public void setHeroImage(ImageIcon heroImage) { this.heroImage = heroImage; } 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 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 HeroDead(int x,int y){ this.x=x; this.y=y; } public void drawImage(Graphics g) { g.drawImage(heroImage.getImage(), x, y, null); } }
GameMain类
package P; import java.awt.Color; import javax.swing.BorderFactory; import javax.swing.JFrame; //游戏入口类 public class GameMain { static final int WIDTH = 860; //设置静态常量,作为状态值使用 static final int HEIGHT = 660; public static void main(String[] args) { JFrame jFrame = new JFrame(); //实例化顶级窗口类 jFrame.setSize(WIDTH, HEIGHT); //设置宽高像素 jFrame.setTitle("小秃头历险记"); //设置标题 jFrame.setLocationRelativeTo(null); //设置居中效果 //JFrame.EXIT_ON_CLOSE: 状态值 3 jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭窗口的同时,把程序关闭 //在窗口中,加入面板 MyPanel pl = new MyPanel(); pl.setBackground(Color.pink); pl.setBorder(BorderFactory.createLineBorder(Color.cyan, 3)); jFrame.add(pl); //添加面板组件 jFrame.addMouseMotionListener(pl); //添加鼠标移动事件 jFrame.setVisible(true); //使窗口可视化 pl.init(); //在面板中循环将组件跑起来 } }
Enemy类
package P; import java.awt.Graphics; import java.util.Random; import javax.swing.ImageIcon; //创建敌机类 public class Enemy { //创建敌机图片,宽高,坐标等属性 private ImageIcon enemyImage = new ImageIcon("img/错误.png"); private int width = enemyImage.getIconWidth(); private int height = enemyImage.getIconHeight(); private int x; private int y; public ImageIcon getEnemyImage() { return enemyImage; } public void setEnemyImage(ImageIcon enemyImage) { this.enemyImage = enemyImage; } 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 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 Enemy(){ //创建随机类,在窗口宽高范围内随机 Random random = new Random(); //随机出来的敌机位置可以显示一半图片 x = random.nextInt(GameMain.WIDTH-width/2); y = -random.nextInt(GameMain.HEIGHT-height/2); //初始敌机位置为负数 } public void drawImage(Graphics g) { //绘制敌机图片 g.drawImage(enemyImage.getImage(), x, y, null); } public void move() { y +=2; //控制y轴速度 } }
Bullet类
package P; import java.awt.Graphics; import javax.swing.ImageIcon; //创建子弹类 public class Bullet { //创建属性,子弹图片,宽高,位置 private ImageIcon bulletImage = new ImageIcon("img/aaa.gif"); private int width = bulletImage.getIconWidth(); private int height = bulletImage.getIconHeight(); private int x; private int y; public ImageIcon getBulletImage() { return bulletImage; } public void setBulletImage(ImageIcon bulletImage) { this.bulletImage = bulletImage; } 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 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 Bullet(int x,int y) { //x,y轴位置从外面传入(等于英雄级的位置) this.x = x; this.y = y; } public void drawImage(Graphics g) { g.drawImage(bulletImage.getImage(), x, y, null); } public void move() { x-=1; y -= 2; //让子弹y轴从下往上递减 } public void move1() { x+=1; y -= 2; //让子弹y轴从下往上递减 } }
爆炸类bomb类
package P; import java.awt.Graphics; import javax.swing.ImageIcon; //创建爆炸的实体了 public class Bomb { //爆炸的图片,宽高,位置属性 private ImageIcon bombImage=new ImageIcon("img/zz.png"); private int width = bombImage.getIconWidth(); private int height = bombImage.getIconHeight(); private int x; private int y; private int count; //记录多少次数后爆炸 public ImageIcon getBombImage() { return bombImage; } public void setBombImage(ImageIcon bombImage) { this.bombImage = bombImage; } 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 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 getCount() { return count; } public void setCount(int count) { this.count = count; } public Bomb(int x,int y){ this.x = x; this.y = y; } public void drawImage(Graphics g) { g.drawImage(bombImage.getImage(), x, y, null); } public void move() { count++; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。