纯java小游戏 飞机大战(上班偷着玩)

2024-03-16 07:18

本文主要是介绍纯java小游戏 飞机大战(上班偷着玩),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

相关java类

Airplane.java     敌飞机: 是飞行物,也是敌人
Award.java        奖励
Bee.java            蜜蜂 
Bullet.java          子弹类:是飞行物
Enemy.java       敌人,可以有分数
FlyingObject.java      飞行物(敌机,蜜蜂,子弹,英雄机)
Hero.java          英雄机:是飞行物
ShootGame.java   游戏启动类(main)



package com.tanrt.demo;import java.util.Random;/*** 敌飞机: 是飞行物,也是敌人*/
public class Airplane extends FlyingObject implements Enemy {private int speed = 3;  //移动步骤/** 初始化数据 */public Airplane(){this.image = ShootGame.airplane;width = image.getWidth();height = image.getHeight();y = -height;          Random rand = new Random();x = rand.nextInt(ShootGame.WIDTH - width);}/** 获取分数 */@Overridepublic int getScore() {  return 5;}/** //越界处理 */@Overridepublic 	boolean outOfBounds() {   return y>ShootGame.HEIGHT;}/** 移动 */@Overridepublic void step() {   y += speed;}}

 

package com.tanrt.demo;
/*** 奖励*/
public interface Award {int DOUBLE_FIRE = 0;  //双倍火力int LIFE = 1;   //1条命/** 获得奖励类型(上面的0或1) */int getType();
}

package com.tanrt.demo;import java.util.Random;/** 蜜蜂 */
public class Bee extends FlyingObject implements Award{private int xSpeed = 1;   //x坐标移动速度private int ySpeed = 2;   //y坐标移动速度private int awardType;    //奖励类型/** 初始化数据 */public Bee(){this.image = ShootGame.bee;width = image.getWidth();height = image.getHeight();y = -height;Random rand = new Random();x = rand.nextInt(ShootGame.WIDTH - width);awardType = rand.nextInt(2);   //初始化时给奖励}/** 获得奖励类型 */public int getType(){return awardType;}/** 越界处理 */@Overridepublic boolean outOfBounds() {return y>ShootGame.HEIGHT;}/** 移动,可斜着飞 */@Overridepublic void step() {      x += xSpeed;y += ySpeed;if(x > ShootGame.WIDTH-width){  xSpeed = -1;}if(x < 0){xSpeed = 1;}}
}

 


package com.tanrt.demo;/*** 子弹类:是飞行物*/
public class Bullet extends FlyingObject {private int speed = 3;  //移动的速度/** 初始化数据 */public Bullet(int x,int y){this.x = x;this.y = y;this.image = ShootGame.bullet;}/** 移动 */@Overridepublic void step(){   y-=speed;}/** 越界处理 */@Overridepublic boolean outOfBounds() {return y<-height;}}

 


package com.tanrt.demo;/*** 敌人,可以有分数*/
public interface Enemy {/** 敌人的分数  */int getScore();
}

 


package com.tanrt.demo;import java.awt.image.BufferedImage;/*** 飞行物(敌机,蜜蜂,子弹,英雄机)*/
public abstract class FlyingObject {protected int x;    //x坐标protected int y;    //y坐标protected int width;    //宽protected int height;   //高protected BufferedImage image;   //图片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 BufferedImage getImage() {return image;}public void setImage(BufferedImage image) {this.image = image;}/*** 检查是否出界* @return true 出界与否*/public abstract boolean outOfBounds();/*** 飞行物移动一步*/public abstract void step();/*** 检查当前飞行物体是否被子弹(x,y)击(shoot)中* @param Bullet 子弹对象* @return true表示被击中了*/public boolean shootBy(Bullet bullet){int x = bullet.x;  //子弹横坐标int y = bullet.y;  //子弹纵坐标return this.x<x && x<this.x+width && this.y<y && y<this.y+height;}}

 


package com.tanrt.demo;
import java.awt.image.BufferedImage;/*** 英雄机:是飞行物*/
public class Hero extends FlyingObject{private BufferedImage[] images = {};  //英雄机图片private int index = 0;                //英雄机图片切换索引private int doubleFire;   //双倍火力private int life;   //命/** 初始化数据 */public Hero(){life = 3;   //初始3条命doubleFire = 0;   //初始火力为0images = new BufferedImage[]{ShootGame.hero0, ShootGame.hero1}; //英雄机图片数组image = ShootGame.hero0;   //初始为hero0图片width = image.getWidth();height = image.getHeight();x = 150;y = 400;}/** 获取双倍火力 */public int isDoubleFire() {return doubleFire;}/** 设置双倍火力 */public void setDoubleFire(int doubleFire) {this.doubleFire = doubleFire;}/** 增加火力 */public void addDoubleFire(){doubleFire = 40;}/** 增命 */public void addLife(){  //增命life++;}/** 减命 */public void subtractLife(){   //减命life--;}/** 获取命 */public int getLife(){return life;}/** 当前物体移动了一下,相对距离,x,y鼠标位置  */public void moveTo(int x,int y){   this.x = x - width/2;this.y = y - height/2;}/** 越界处理 */@Overridepublic boolean outOfBounds() {return false;  }/** 发射子弹 */public Bullet[] shoot(){   int xStep = width/4;      //4半int yStep = 20;  //步if(doubleFire>0){  //双倍火力Bullet[] bullets = new Bullet[2];bullets[0] = new Bullet(x+xStep,y-yStep);  //y-yStep(子弹距飞机的位置)bullets[1] = new Bullet(x+3*xStep,y-yStep);return bullets;}else{      //单倍火力Bullet[] bullets = new Bullet[1];bullets[0] = new Bullet(x+2*xStep,y-yStep);  return bullets;}}/** 移动 */@Overridepublic void step() {if(images.length>0){image = images[index++/10%images.length];  //切换图片hero0,hero1}}/** 碰撞算法 */public boolean hit(FlyingObject other){int x1 = other.x - this.width/2;                 //x坐标最小距离int x2 = other.x + this.width/2 + other.width;   //x坐标最大距离int y1 = other.y - this.height/2;                //y坐标最小距离int y2 = other.y + this.height/2 + other.height; //y坐标最大距离int herox = this.x + this.width/2;               //英雄机x坐标中心点距离int heroy = this.y + this.height/2;              //英雄机y坐标中心点距离return herox>x1 && herox<x2 && heroy>y1 && heroy<y2;   //区间范围内为撞上了}}

 


 

package com.tanrt.demo;import java.awt.Font;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Arrays;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import java.awt.image.BufferedImage;import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;public class ShootGame extends JPanel {public static final int WIDTH = 400; // 面板宽public static final int HEIGHT = 654; // 面板高/** 游戏的当前状态: START RUNNING PAUSE GAME_OVER */private int state;private static final int START = 0;private static final int RUNNING = 1;private static final int PAUSE = 2;private static final int GAME_OVER = 3;private int score = 0; // 得分private Timer timer; // 定时器private int intervel = 1000 / 100; // 时间间隔(毫秒)public static BufferedImage background;public static BufferedImage start;public static BufferedImage airplane;public static BufferedImage bee;public static BufferedImage bullet;public static BufferedImage hero0;public static BufferedImage hero1;public static BufferedImage pause;public static BufferedImage gameover;private FlyingObject[] flyings = {}; // 敌机数组private Bullet[] bullets = {}; // 子弹数组private Hero hero = new Hero(); // 英雄机static { // 静态代码块,初始化图片资源try {background = ImageIO.read(ShootGame.class.getResource("background.png"));start = ImageIO.read(ShootGame.class.getResource("start.png"));airplane = ImageIO.read(ShootGame.class.getResource("airplane.png"));bee = ImageIO.read(ShootGame.class.getResource("bee.png"));bullet = ImageIO.read(ShootGame.class.getResource("bullet.png"));hero0 = ImageIO.read(ShootGame.class.getResource("hero0.png"));hero1 = ImageIO.read(ShootGame.class.getResource("hero1.png"));pause = ImageIO.read(ShootGame.class.getResource("pause.png"));gameover = ImageIO.read(ShootGame.class.getResource("gameover.png"));} catch (Exception e) {e.printStackTrace();}}/** 画 */@Overridepublic void paint(Graphics g) {g.drawImage(background, 0, 0, null); // 画背景图paintHero(g); // 画英雄机paintBullets(g); // 画子弹paintFlyingObjects(g); // 画飞行物paintScore(g); // 画分数paintState(g); // 画游戏状态}/** 画英雄机 */public void paintHero(Graphics g) {g.drawImage(hero.getImage(), hero.getX(), hero.getY(), null);}/** 画子弹 */public void paintBullets(Graphics g) {for (int i = 0; i < bullets.length; i++) {Bullet b = bullets[i];g.drawImage(b.getImage(), b.getX() - b.getWidth() / 2, b.getY(),null);}}/** 画飞行物 */public void paintFlyingObjects(Graphics g) {for (int i = 0; i < flyings.length; i++) {FlyingObject f = flyings[i];g.drawImage(f.getImage(), f.getX(), f.getY(), null);}}/** 画分数 */public void paintScore(Graphics g) {int x = 10; // x坐标int y = 25; // y坐标Font font = new Font(Font.SANS_SERIF, Font.BOLD, 22); // 字体g.setColor(new Color(0xFF0000));g.setFont(font); // 设置字体g.drawString("SCORE:" + score, x, y); // 画分数y=y+20; // y坐标增20g.drawString("LIFE:" + hero.getLife(), x, y); // 画命}/** 画游戏状态 */public void paintState(Graphics g) {switch (state) {case START: // 启动状态g.drawImage(start, 0, 0, null);break;case PAUSE: // 暂停状态g.drawImage(pause, 0, 0, null);break;case GAME_OVER: // 游戏终止状态g.drawImage(gameover, 0, 0, null);break;}}public static void main(String[] args) {JFrame frame = new JFrame("作死上班");ShootGame game = new ShootGame(); // 面板对象frame.add(game); // 将面板添加到JFrame中frame.setSize(WIDTH, HEIGHT); // 设置大小frame.setAlwaysOnTop(true); // 设置其总在最上frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 默认关闭操作frame.setIconImage(new ImageIcon("images/icon.jpg").getImage()); // 设置窗体的图标frame.setLocationRelativeTo(null); // 设置窗体初始位置frame.setVisible(true); // 尽快调用paintgame.action(); // 启动执行}/** 启动执行代码 */public void action() {// 鼠标监听事件MouseAdapter l = new MouseAdapter() {@Overridepublic void mouseMoved(MouseEvent e) { // 鼠标移动if (state == RUNNING) { // 运行状态下移动英雄机--随鼠标位置int x = e.getX();int y = e.getY();hero.moveTo(x, y);}}@Overridepublic void mouseEntered(MouseEvent e) { // 鼠标进入if (state == PAUSE) { // 暂停状态下运行state = RUNNING;}}@Overridepublic void mouseExited(MouseEvent e) { // 鼠标退出if (state == RUNNING) { // 游戏未结束,则设置其为暂停state = PAUSE;}}@Overridepublic void mouseClicked(MouseEvent e) { // 鼠标点击switch (state) {case START:state = RUNNING; // 启动状态下运行break;case GAME_OVER: // 游戏结束,清理现场flyings = new FlyingObject[0]; // 清空飞行物bullets = new Bullet[0]; // 清空子弹hero = new Hero(); // 重新创建英雄机score = 0; // 清空成绩state = START; // 状态设置为启动break;}}};this.addMouseListener(l); // 处理鼠标点击操作this.addMouseMotionListener(l); // 处理鼠标滑动操作timer = new Timer(); // 主流程控制timer.schedule(new TimerTask() {@Overridepublic void run() {if (state == RUNNING) { // 运行状态enterAction(); // 飞行物入场stepAction(); // 走一步shootAction(); // 英雄机射击bangAction(); // 子弹打飞行物outOfBoundsAction(); // 删除越界飞行物及子弹checkGameOverAction(); // 检查游戏结束}repaint(); // 重绘,调用paint()方法}}, intervel, intervel);}int flyEnteredIndex = 0; // 飞行物入场计数/** 飞行物入场 */public void enterAction() {flyEnteredIndex++;if (flyEnteredIndex % 40 == 0) { // 400毫秒生成一个飞行物--10*40FlyingObject obj = nextOne(); // 随机生成一个飞行物flyings = Arrays.copyOf(flyings, flyings.length + 1);flyings[flyings.length - 1] = obj;}}/** 走一步 */public void stepAction() {for (int i = 0; i < flyings.length; i++) { // 飞行物走一步FlyingObject f = flyings[i];f.step();}for (int i = 0; i < bullets.length; i++) { // 子弹走一步Bullet b = bullets[i];b.step();}hero.step(); // 英雄机走一步}/** 飞行物走一步 */public void flyingStepAction() {for (int i = 0; i < flyings.length; i++) {FlyingObject f = flyings[i];f.step();}}int shootIndex = 0; // 射击计数/** 射击 */public void shootAction() {shootIndex++;if (shootIndex % 30 == 0) { // 300毫秒发一颗Bullet[] bs = hero.shoot(); // 英雄打出子弹bullets = Arrays.copyOf(bullets, bullets.length + bs.length); // 扩容System.arraycopy(bs, 0, bullets, bullets.length - bs.length,bs.length); // 追加数组}}/** 子弹与飞行物碰撞检测 */public void bangAction() {for (int i = 0; i < bullets.length; i++) { // 遍历所有子弹Bullet b = bullets[i];bang(b); // 子弹和飞行物之间的碰撞检查}}/** 删除越界飞行物及子弹 */public void outOfBoundsAction() {int index = 0; // 索引FlyingObject[] flyingLives = new FlyingObject[flyings.length]; // 活着的飞行物for (int i = 0; i < flyings.length; i++) {FlyingObject f = flyings[i];if (!f.outOfBounds()) {flyingLives[index++] = f; // 不越界的留着}}flyings = Arrays.copyOf(flyingLives, index); // 将不越界的飞行物都留着index = 0; // 索引重置为0Bullet[] bulletLives = new Bullet[bullets.length];for (int i = 0; i < bullets.length; i++) {Bullet b = bullets[i];if (!b.outOfBounds()) {bulletLives[index++] = b;}}bullets = Arrays.copyOf(bulletLives, index); // 将不越界的子弹留着}/** 检查游戏结束 */public void checkGameOverAction() {if (isGameOver()==true) {state = GAME_OVER; // 改变状态}}/** 检查游戏是否结束 */public boolean isGameOver() {for (int i = 0; i < flyings.length; i++) {int index = -1;FlyingObject obj = flyings[i];if (hero.hit(obj)) { // 检查英雄机与飞行物是否碰撞hero.subtractLife(); // 减命hero.setDoubleFire(0); // 双倍火力解除index = i; // 记录碰上的飞行物索引}if (index != -1) {FlyingObject t = flyings[index];flyings[index] = flyings[flyings.length - 1];flyings[flyings.length - 1] = t; // 碰上的与最后一个飞行物交换flyings = Arrays.copyOf(flyings, flyings.length - 1); // 删除碰上的飞行物}}return hero.getLife() <= 0;}/** 子弹和飞行物之间的碰撞检查 */public void bang(Bullet bullet) {int index = -1; // 击中的飞行物索引for (int i = 0; i < flyings.length; i++) {FlyingObject obj = flyings[i];if (obj.shootBy(bullet)) { // 判断是否击中index = i; // 记录被击中的飞行物的索引break;}}if (index != -1) { // 有击中的飞行物FlyingObject one = flyings[index]; // 记录被击中的飞行物FlyingObject temp = flyings[index]; // 被击中的飞行物与最后一个飞行物交换flyings[index] = flyings[flyings.length - 1];flyings[flyings.length - 1] = temp;flyings = Arrays.copyOf(flyings, flyings.length - 1); // 删除最后一个飞行物(即被击中的)// 检查one的类型(敌人加分,奖励获取)if (one instanceof Enemy) { // 检查类型,是敌人,则加分Enemy e = (Enemy) one; // 强制类型转换score += e.getScore(); // 加分} else { // 若为奖励,设置奖励Award a = (Award) one;int type = a.getType(); // 获取奖励类型switch (type) {case Award.DOUBLE_FIRE:hero.addDoubleFire(); // 设置双倍火力break;case Award.LIFE:hero.addLife(); // 设置加命break;}}}}/*** 随机生成飞行物* * @return 飞行物对象*/public static FlyingObject nextOne() {Random random = new Random();int type = random.nextInt(20); // [0,20)if (type < 4) {return new Bee();} else {return new Airplane();}}}

 

上班偷着玩,自定义开局满火力哈

图片下载路径:   https://download.csdn.net/download/tanrt/10858595

 

这篇关于纯java小游戏 飞机大战(上班偷着玩)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/814708

相关文章

SpringBoot条件注解核心作用与使用场景详解

《SpringBoot条件注解核心作用与使用场景详解》SpringBoot的条件注解为开发者提供了强大的动态配置能力,理解其原理和适用场景是构建灵活、可扩展应用的关键,本文将系统梳理所有常用的条件注... 目录引言一、条件注解的核心机制二、SpringBoot内置条件注解详解1、@ConditionalOn

通过Spring层面进行事务回滚的实现

《通过Spring层面进行事务回滚的实现》本文主要介绍了通过Spring层面进行事务回滚的实现,包括声明式事务和编程式事务,具有一定的参考价值,感兴趣的可以了解一下... 目录声明式事务回滚:1. 基础注解配置2. 指定回滚异常类型3. ​不回滚特殊场景编程式事务回滚:1. ​使用 TransactionT

Spring LDAP目录服务的使用示例

《SpringLDAP目录服务的使用示例》本文主要介绍了SpringLDAP目录服务的使用示例... 目录引言一、Spring LDAP基础二、LdapTemplate详解三、LDAP对象映射四、基本LDAP操作4.1 查询操作4.2 添加操作4.3 修改操作4.4 删除操作五、认证与授权六、高级特性与最佳

Spring Shell 命令行实现交互式Shell应用开发

《SpringShell命令行实现交互式Shell应用开发》本文主要介绍了SpringShell命令行实现交互式Shell应用开发,能够帮助开发者快速构建功能丰富的命令行应用程序,具有一定的参考价... 目录引言一、Spring Shell概述二、创建命令类三、命令参数处理四、命令分组与帮助系统五、自定义S

SpringSecurity JWT基于令牌的无状态认证实现

《SpringSecurityJWT基于令牌的无状态认证实现》SpringSecurity中实现基于JWT的无状态认证是一种常见的做法,本文就来介绍一下SpringSecurityJWT基于令牌的无... 目录引言一、JWT基本原理与结构二、Spring Security JWT依赖配置三、JWT令牌生成与

Java中Date、LocalDate、LocalDateTime、LocalTime、时间戳之间的相互转换代码

《Java中Date、LocalDate、LocalDateTime、LocalTime、时间戳之间的相互转换代码》:本文主要介绍Java中日期时间转换的多种方法,包括将Date转换为LocalD... 目录一、Date转LocalDateTime二、Date转LocalDate三、LocalDateTim

如何配置Spring Boot中的Jackson序列化

《如何配置SpringBoot中的Jackson序列化》在开发基于SpringBoot的应用程序时,Jackson是默认的JSON序列化和反序列化工具,本文将详细介绍如何在SpringBoot中配置... 目录配置Spring Boot中的Jackson序列化1. 为什么需要自定义Jackson配置?2.

Java中使用Hutool进行AES加密解密的方法举例

《Java中使用Hutool进行AES加密解密的方法举例》AES是一种对称加密,所谓对称加密就是加密与解密使用的秘钥是一个,下面:本文主要介绍Java中使用Hutool进行AES加密解密的相关资料... 目录前言一、Hutool简介与引入1.1 Hutool简介1.2 引入Hutool二、AES加密解密基础

Spring Boot项目部署命令java -jar的各种参数及作用详解

《SpringBoot项目部署命令java-jar的各种参数及作用详解》:本文主要介绍SpringBoot项目部署命令java-jar的各种参数及作用的相关资料,包括设置内存大小、垃圾回收... 目录前言一、基础命令结构二、常见的 Java 命令参数1. 设置内存大小2. 配置垃圾回收器3. 配置线程栈大小

SpringBoot实现微信小程序支付功能

《SpringBoot实现微信小程序支付功能》小程序支付功能已成为众多应用的核心需求之一,本文主要介绍了SpringBoot实现微信小程序支付功能,文中通过示例代码介绍的非常详细,对大家的学习或者工作... 目录一、引言二、准备工作(一)微信支付商户平台配置(二)Spring Boot项目搭建(三)配置文件