java实现简易飞机大战
作者:编程夜游神 发布时间:2022-08-27 12:36:07
标签:java,飞机大战
本文实例为大家分享了java实现简易飞机大战的具体代码,供大家参考,具体内容如下
整体思路
1.创建游戏窗体,添加面板JPanel,重写JPanel中的paint方法,遍历所有飞机和 * 绘制,用定时器进行重绘,实现动画效果
2.添加敌机和发射 * 用的是多线程
3.碰撞检测采用的是矩形类Rectangle中的intersects方法
代码实现
用手机查看代码好像只显示62行
英雄战机类
package com.cml.model;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import com.cml.util.ImageUtil;
public class Hero {
private int x, y;// 坐标
private int width, height; //宽高
private Image heroImage; // 图片
private boolean isAlive = true;
private ArrayList<Bullet> bullets = new ArrayList<>();
public Hero() {
this.x = 180;
this.y = 600;
this.heroImage = ImageUtil.hero;
width = heroImage.getWidth(null);
height = heroImage.getHeight(null);
initBullets();
}
private void initBullets() {
//用线程发射 *
new Thread() {
@Override
public void run() {
while (isAlive) {
bullets.add(new Bullet(Hero.this));
try {
sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}.start();
}
public Hero(int x, int y, Image heroImage) {
super();
this.x = x;
this.y = y;
this.heroImage = heroImage;
}
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 Image getHeroImage() {
return heroImage;
}
public void setHeroImage(Image heroImage) {
this.heroImage = heroImage;
}
//绘制英雄战机
public void paint(Graphics g) {
if (!isAlive) {
heroImage = ImageUtil.hero_destory;
}
g.drawImage(heroImage, x, y, null);
for (int i = 0; i < bullets.size(); i++) {
Bullet bullet = bullets.get(i);
if (bullet.getY() < 0) {
bullets.remove(bullet);
}
bullet.paint(g);
}
}
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 void mouseDragged(MouseEvent e) {
if (isAlive) {
int x0 = e.getX();
int y0 = e.getY();
if (isInScopePanel(x0, y0)) {
if (isInScopeImageWidth(x0) && isInScopeImageheigth(y0)) {
this.x = x0 - width / 2;
this.y = y0 - height / 2;
}
} else {
if (isInScopeImageWidth(x0)) {
this.x = x0 - width / 2;
}
if (isInScopeImageheigth(y0)) {
this.y = y0 - height / 2;
}
}
}
}
private boolean isInScopePanel(int x0, int y0) {
if (x0 > 10 && x0 < 460 && y0 > 140 && y0 < 730) {
return true;
}
return false;
}
private boolean isInScopeImageheigth(int y0) {
if (y0 >= y && y0 <= y + height) {
if (y0 > 140 && y0 < 730) {
return true;
}
}
return false;
}
private boolean isInScopeImageWidth(int x0) {
if (x0 >= x && x0<= x + width) {
if (x0 > 10 && x0 < 460) {
return true;
}
}
return false;
}
public ArrayList<Bullet> getBullets() {
return bullets;
}
public void setAlive(boolean isAlive) {
this.isAlive = isAlive;
}
public boolean isAlive() {
return isAlive;
}
}
敌机类
package com.cml.model;
import java.awt.Graphics;
import java.awt.Image;
import java.util.Random;
import com.cml.util.ImageUtil;
public class Enemy {
private Random random = new Random();
private int x, y;// 坐标
private int width, height; // 宽高
private boolean isAlive = true;
private static final int SPEED = 4;
private Image enemyImage; // 图片
public Enemy() {
RandomEnemyXY();
enemyImage = ImageUtil.enemy;
width = enemyImage.getWidth(null);
height = enemyImage.getHeight(null);
}
private void RandomEnemyXY() {
x = random.nextInt(430);
y = 0;
}
public void paint(Graphics g) {
if (!isAlive) {
enemyImage = ImageUtil.bomb;
}
g.drawImage(enemyImage, x, y, null);
move();
}
public boolean isAlive() {
return isAlive;
}
public void setAlive(boolean isAlive) {
this.isAlive = isAlive;
}
private void move() {
if (isAlive) {
y += SPEED;
}
}
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;
}
}
* 类
package com.cml.model;
import java.awt.Graphics;
import java.awt.Image;
import com.cml.util.ImageUtil;
public class Bullet {
private int x, y;// 坐标
private int width, height; // 宽高
private static final int SPEED = 10; // 速度
private Image bulletImage; // 图片
public Bullet(Hero hero) {
bulletImage = ImageUtil.bullet;
width = bulletImage.getWidth(null);
height = bulletImage.getHeight(null);
this.x = hero.getX() + hero.getWidth() / 2 - width / 2;
this.y = hero.getY();
}
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 void paint(Graphics g) {
g.drawImage(bulletImage, x, y, null);
move();
}
private void move() {
y -= SPEED;
}
}
图片工具类
package com.cml.util;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
public class ImageUtil {
public static BufferedImage hero;
public static BufferedImage enemy;
public static BufferedImage bullet;
public static BufferedImage bg;
public static BufferedImage bomb;
public static BufferedImage hero_destory;
public static BufferedImage login;
static {
try {
hero = ImageIO.read(ImageUtil.class.getResource("/img/hero.png"));
enemy = ImageIO.read(ImageUtil.class.getResource("/img/enemy.png"));
bullet = ImageIO.read(ImageUtil.class.getResource("/img/bullet.png"));
bg = ImageIO.read(ImageUtil.class.getResource("/img/bg.png"));
bomb = ImageIO.read(ImageUtil.class.getResource("/img/bomb.png"));
hero_destory = ImageIO.read(ImageUtil.class.getResource("/img/hero_destory.png"));
// login = ImageIO.read(new File("img/login.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
游戏窗体类
package com.cml.frame;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import com.cml.model.Bullet;
import com.cml.model.Enemy;
import com.cml.model.Hero;
import com.cml.util.ImageUtil;
import com.sun.java.swing.plaf.windows.resources.windows;
public class GameFrame extends JFrame {
private JPanel gamePanel;
private Hero hero;
private ArrayList<Enemy> enemies = new ArrayList<Enemy>();
private ArrayList<Bullet> hero_bullet;
private Timer timer;
public GameFrame() {
// 初始化游戏窗体
initGameFrame();
// 初始化英雄战机
initHero();
// 初始化游戏面板
initGamePanel();
// 初始化定时器
initTimer();
// 初始化敌军战机
initEnemies();
}
private void initEnemies() {
new Thread() {
@Override
public void run() {
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
enemies.add(new Enemy());
}
}
}.start();
}
private void initTimer() {
timer = new Timer();
TimerTask task = new TimerTask() {
@Override
public void run() {
gamePanel.repaint();
}
};
timer.scheduleAtFixedRate(task, 0, 20);
}
private void initHero() {
hero = new Hero();
hero_bullet = hero.getBullets();
}
private void initGameFrame() {
setTitle(" * ");
setSize(480, 800);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
}
private void initGamePanel() {
gamePanel = new JPanel() {
private int score = 0;
/**
* 判断敌机与 * 相撞
* @param enemy
* @param bullet
* @return
*/
public boolean isHit(Enemy enemy, Bullet bullet) {
Rectangle r1 = new Rectangle(enemy.getX(), enemy.getY(), enemy.getWidth(), enemy.getHeight());
Rectangle r2 = new Rectangle(bullet.getX(), bullet.getY(), bullet.getWidth(), bullet.getHeight());
return r1.intersects(r2);
}
/**
* 判断英雄战机与敌机相撞
* @param enemy
* @param hero
* @return
*/
public boolean isHit(Enemy enemy, Hero hero) {
Rectangle r1 = new Rectangle(enemy.getX(), enemy.getY(), enemy.getWidth(), enemy.getHeight());
Rectangle r2 = new Rectangle(hero.getX() + hero.getWidth() / 3, hero.getY() + hero.getHeight() / 3,
hero.getWidth() / 3, hero.getHeight() / 3);
return r1.intersects(r2);
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.drawImage(ImageUtil.bg, 0, 0, 480, 800, null);
for (int i = 0; i < enemies.size(); i++) {
Enemy enemy = enemies.get(i);
for (int j = 0; j < hero_bullet.size(); j++) {
Bullet bullet = hero_bullet.get(j);
if (isHit(enemy, bullet) && enemy.isAlive()) {
enemy.setAlive(false);
hero_bullet.remove(bullet);
new Thread() {
public void run() {
score += 10;
try {
sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
enemies.remove(enemy);
};
}.start();
break;
}
}
if (isHit(enemy, hero)) {
timer.cancel();
hero.setAlive(false);
enemy.setAlive(false);
JOptionPane.showMessageDialog(this, "游戏结束,您的得分是:" + score);
System.exit(0);
}
if (enemy.getY() > 800) {
enemies.remove(enemy);
}
enemy.paint(g);
}
if (hero != null) {
hero.paint(g);
}
g.setFont(new Font("宋体", Font.BOLD, 24));
g.drawString("得分:" + score, 350, 30);
}
};
add(gamePanel);
// 设置拖拽监听事件
gamePanel.addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
if (hero != null) {
hero.mouseDragged(e);
}
}
});
}
}
启动游戏类
package com.cml.main;
import com.cml.frame.GameFrame;
public class Start {
public static void main(String[] args) {
/**
* 初始化窗体
*/
new GameFrame().setVisible(true);
}
}
运行效果图
来源:https://blog.csdn.net/weixin_48598155/article/details/106959333


猜你喜欢
- 高快省的排序算法有没有既不浪费空间又可以快一点的排序算法呢?那就是“快速排序”啦!光听这个名字是不是就觉得很高端呢。假设我们现在对“6 1
- 项目需要对接外部接口,将图片文件流发送到外部接口,下面代码就是HttpsURLConnection如何上传文件流:/** *
- 我是因为构建多渠道包的时候有这个需求,平常工作多个渠道包频繁的打包,总会忘记versioncode提高一下,从而打包出来的apk无法覆盖原先
- 获取缓存大小接口主要这里的方法已经和7.0不兼容了。import android.app.usage.UsageStats;import a
- 前言:一个游戏里的一个人物会存在多种状态,那么就需要有一个专门管理这些状态的类。不然会显得杂乱无章,不易于后面状态的增加或者减少。思路:既然
- Java Benchmark 基准测试的实例详解import java.util.Arrays; import java.util.conc
- 目录一、Gradle相比Maven的优势二、基本配置三、最佳实践四、总结一、Gradle相比Maven的优势配置简洁Maven是用pom.x
- 我就废话不多说了,还是上代码吧接口:interface OnBind {fun onBindChildViewData(holder: St
- java 设计模式之单例模式前言:在软件开发过程中常会有一些对象我们只需要一个,如:线程池(threadpool)、缓存(cac
- 面对android studio Run 一次项目要等好几分钟的痛点,不得不研究一下android studio 的单元测试。其实我的目的很
- 本文实例讲述了Android编程实现类似天气预报图文字幕垂直滚动效果的方法。分享给大家供大家参考,具体如下:在很多天气或者新闻的应用中,我们
- filter类不能注入@Autowired变量问题描述项目中的登录是用了shiro以及filter * 。输入正确的账号密码之后却不能正常登
- 一、多线程的优缺点多线程的优点:1)资源利用率更好2)程序设计在某些情况下更简单3)程序响应更快多线程的代价:1)设计更复杂虽然有一些多线程
- 涉及access_token的获取请参考《C#微信公众平台开发之access_token的获取存储与更新》一、为了实现高级群发功能,需要解决
- 实例如下:public class ConfigOperator { #region 从配置文件获取V
- 本文实例讲述了Android编程实现向桌面添加快捷方式的方法。分享给大家供大家参考,具体如下:有时候为了使用方便,需要在桌面上添加快捷方式,
- 以下四种方式:1.继承Thread类,重写run方法2.实现Runnable接口,重写run方法,实现Runnable接口的实现类的实例对象
- 一、前言关于EasyExcel,它对poi做了进一步的封装,使得整个编写流程更加的面向对象。好处嘛,我认为流程上更加清晰即易懂、可读性更好,
- java 中Spark中将对象序列化存储到hdfs摘要: Spark应用中经常会遇到这样一个需求: 需要将JAVA对象序列化并存储到HDFS
- 最近有一款2048的游戏非常火,本文将来介绍一下使用OGEngine游戏引擎开发游戏2048。OGEngine引擎是开源的,我们很容易找到,