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
0
投稿
猜你喜欢
- 做消息通信,消息会不断从网络流中取得,而后台也有线程不断消费。本来我一直是使用一些线程安全标识或方法来控制,后来在网上找到一些java新特性
- 由于我经常下载一些pdf格式的电子书,有的时候一些好书下载下来没有书签,读起来感觉没有整体的感觉,所以决定自己写一个小工具,将特定格式的文本
- 一,栈1,概念在我们软件应用 ,栈这种后进先出数据结构的应用是非常普遍的。比如你用浏 览器上网时不管什么浏览器都有 个"后退&qu
- SpringBoot web项目启动后立即关闭我们在写spring boot web项目时,有时会遇到启动后立即关闭的情况,或者是无法加载某
- gateway网关与前端请求的跨域问题最近因项目需要,引入了gateway网关。可是发现将前端请求的端口指向网关后,用postman发送请求
- 一.优先队列的应用优先队列在程序开发中屡见不鲜,比如操作系统在进行进程调度时一种可行的算法是使用优先队列,当一个新的进程被fork()出来后
- 一、题目描述题目实现:运行服务器程序,等待客户端程序连接。二、解题思路首先需要在服务器中建立服务器套接字,并等待客户程序的连接。创建一个类:
- 在很多web产品中都需要实现在同一时刻,只能允许一个账号同时只能在一个浏览器当中登录。通俗点讲就是当A账号在浏览器1当中登录了,此时在浏览器
- 一,栈1,概念在我们软件应用 ,栈这种后进先出数据结构的应用是非常普遍的。比如你用浏 览器上网时不管什么浏览器都有 个"后退&qu
- 在 Java 语言中,运算符有算数运算符、关系运算符、逻辑运算符、赋值运算符、字符串连接运算符、条件运算符。算数运算符算数运算符是我们最常用
- 简介说明本文用实例来介绍@Autowired解决循环依赖的原理。@Autowired是通过 * 缓存来解决循环依赖的。 除了@Aut
- 在我们做项目的过程中,有可能会遇到跨域请求,所以需要我们自己组装支持跨域请求的JSONP数据,而在4.1版本以后的SpringMVC中,为我
- 本文介绍了浅谈Java的两种多线程实现方式,分享给大家。具有如下:一、创建多线程的两种方式Java中,有两种方式可以创建多线程:1 通过继承
- 俗话说没有规矩就没有方圆,java作为一门严谨的面向对象的高级编程语言,自然对权限整个重要的问题有严格的控制。Java中,可以通过一些Jav
- 1、什么是过滤器?在客户端到服务器的过程中,当发送请求时,如果有不符合的信息将会被filter进行拦截,如果符合则会进行放行,在服务器给客户
- 1.Quartz是什么?Quartz是一个开源的Java调度框架,可以用来实现在指定的时间或时间间隔触发任务执行的功能。它支持多种方式的作业
- JAVA 枚举单例模式及源码分析的实例详解 单例模式的实现有很多种,网上也分析了
- 最近在做项目的时候,一直用一个叫做API的东西,controller注解我会写,这个东西我也会用,但是我确实不知道这个东西是个什么,有点神奇
- 一、序言在日常一线开发过程中,总有列表转树的需求,几乎是项目的标配,比方说做多级菜单、多级目录、多级分类等,有没有一种通用且跨项目的解决方式
- 我计划在后续的一段时间内,写一系列关于java 9的文章,虽然java 9 不像Java 8或者Java 11那样的核心java版本,但是还