Java设计模式之外观模式示例详解
327
2022-07-26
本文实例为大家分享了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;
//创建一个敌机的集合,用于展示多个敌机
List
//存储子弹集合
List
//存储爆炸对象集合
List
//存储英雄机爆炸集合
List
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 Enemy enemy = enemys.get(i); //获取敌机对象 enemy.drawImage(g); //然后分别展示 } //展示子弹集合的图片 for (int i = 0; i < bullets.size(); i++) { Bullet bullet = bullets.get(i); //取出子弹对象 bullet.drawImage(g); } //展示爆炸集合的图片 for(int i=0;i Bomb bomb = bombs.get(i); bomb.drawImage(g); } //展示英雄机的销毁图片 for(int i=0;i HeroDead heroDead = heroDeads.get(i); heroDead.drawImage(g); } } @Override public void mouseDragged(MouseEvent e) { //System.out.println("鼠标移动并拖拽触发"); //鼠标拖拽时,需要将英雄机也带着移动(只需改变x轴和y轴) x = e.getX()-width/2; //英雄机的移动,随着鼠标触发移动的 y = e.getY()-height/2; //鼠标指定到英雄机中间位置 repaint(); //重新绘制图片 } @Override public void mouseMoved(MouseEvent e) { //System.out.println("鼠标移动的触发.."); //鼠标移动时,需要将英雄机也带着移动(只需改变x轴和y轴) x = e.getX()-width/2; //英雄机的移动,随着鼠标触发移动的 y = e.getY()-height/2; //鼠标指定到英雄机中间位置 repaint(); //重新绘制图片 } //L-4. 创建子弹类Bullet,操作与敌机类类似 public void init() { //定义一个标记,循环了自定义的次数后,才去添加,这样子弹数量会变少 int flag = 0; while(true) { //循环地跑,模拟依次移动的效果 flag++; // if(flag==50) { //控制子弹数量 bullets.add(new Bullet(x+width/2, y)); //就是从英雄级的x,y轴位置发射子弹的 flag=0; //又回到0,依次计算15次 //System.out.println("子弹数量:"+ bullets.size()); } //展示子弹,让子弹飞起来 for(int i=0;i Bullet bullet = bullets.get(i); //取出对象 if(i%2!=0)//设置子弹左右发射 bullet.move(); else bullet.move1();//移动子弹位置 //如果子弹移动到y轴为0,则移除掉 if(bullet.getY()<0){ bullets.remove(bullet); //移除子弹对象 } } //xx.3. //敌机循环移动.. for(int i=0;i Enemy en = enemys.get(i); en.move(); //循环地移动每一架敌机y轴位置 //超出屏幕范围后,需要移除,并重新添加一个对象 if(en.getY()>GameMain.HEIGHT) { //int count1=enemys.size(); enemys.remove(en); //移除对象 enemys.add(new Enemy()); //重新再创建对象 } //L 3-4 //在敌机的循环中,再继续循环子弹; 需要判断是否有子弹和敌机重叠了, //则移除敌机对象,重新添加,移除子弹;如果有则添加爆炸对象 for(int j=0;j Bullet bu = bullets.get(j); if(isHit(en,bu)){ //碰撞的判断 enemys.remove(en); //移除敌机,并重新new一个 enemys.add(new Enemy()); bullets.remove(bu); //移除子弹 //添加的爆炸位置和敌机位置一致 bombs.add(new Bomb(en.getX(), en.getY())); number += 10; //爆炸后,累加得分 } } //zz-6 //判断英雄机与敌机的碰撞(英雄机的消亡) if(isHit(en)){ System.out.println("进入英雄机爆炸..."); //敌机的对象移除 enemys.remove(en); //英雄机爆炸图片位置应该与英雄机重叠 heroDeads.add(new HeroDead(x, y)); for(m=0;m<=heroDeads.size();m++) { while(heroDeads.size()==5) { return; } } //游戏结束,跳出死循环 } } //L-5 //将爆炸的所有对象,过一段时间则干掉 for(int i=0;i Bomb bomb = bombs.get(i); bomb.move(); //计算次数,统一循环多少次后,再干掉 if(bomb.getCount()>6){ bombs.remove(bomb); //在循环的一定范围后,可以移除爆炸了 } } //xx.2 //每次的移动都需要停顿一下 try { Thread.sleep(6); //单位:毫秒 睡眠6毫秒 } catch (InterruptedException e) { e.printStackTrace(); } repaint(); //重新绘制图片 } } //zz-6 //英雄机与敌机的碰撞 private boolean isHit(Enemy en) { http:// //英雄机的碰撞区域 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 getWidYqpReth() { 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++; } }
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~