发布时间:2026/7/20 16:10:55
MinesweeperApp.java import javax.swing.SwingUtilities;import javax.swing.UIManager;import javax.swing.plaf.metal.MetalLookAndFeel;/**程序入口设置系统外观并启动扫雷界面*/public class MinesweeperApp {public static void main(String[] args) {try {UIManager.setLookAndFeel(new MetalLookAndFeel());} catch (Exception e) {e.printStackTrace();}SwingUtilities.invokeLater(() - new MinesweeperUI().setVisible(true));}}三、项目三星际射击游戏3.1 需求分析在扫雷的回合制基础上升级为 实时动作游戏玩家控制飞船自动/手动发射子弹消灭下落的敌人支持粒子爆炸特效、暂停、难度梯度。3.2 架构设计采用 双 Timer 架构逻辑 Timer16ms更新实体位置、碰撞检测、生成敌人渲染 Timer16ms触发 repaint()与逻辑解耦实体采用 抽象基类 匿名内部类 实现多态绘制。3.3 程序界面屏幕截图 2026-07-02 230114屏幕截图 2026-07-02 230027屏幕截图 2026-07-02 2300123.4 完整代码GameEntity.javaimport java.awt.*;import java.io.Serializable;/**游戏实体基类玩家、敌人、子弹都继承此类*/public abstract class GameEntity implements Serializable {private static final long serialVersionUID 1L;protected double x, y;protected int width, height;protected double speedX, speedY;protected boolean alive true;protected Color color;protected String type;public GameEntity(double x, double y, int width, int height, double speedX, double speedY, Color color, String type) {this.x x;this.y y;this.width width;this.height height;this.speedX speedX;this.speedY speedY;this.color color;this.type type;}public void update() {x speedX;y speedY;}public Rectangle getBounds() {return new Rectangle((int)x, (int)y, width, height);}public boolean intersects(GameEntity other) {return getBounds().intersects(other.getBounds());}public abstract void draw(Graphics2D g2d);public double getX() { return x; }public double getY() { return y; }public int getWidth() { return width; }public int getHeight() { return height; }public boolean isAlive() { return alive; }public void setAlive(boolean alive) { this.alive alive; }public String getType() { return type; }public void setX(double x) { this.x x; }public void setY(double y) { this.y y; }public void setSpeedX(double speedX) { this.speedX speedX; }public void setSpeedY(double speedY) { this.speedY speedY; }}ShootingGame.javaimport java.awt.*;import java.io.Serializable;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import java.util.Random;/**射击游戏核心管理所有实体、碰撞检测、分数、难度、存档*/public class ShootingGame implements Serializable {private static final long serialVersionUID 1L;public enum Difficulty { EASY, MEDIUM, HARD }private GameEntity player;private int playerLives 3;private int maxLives 3;private List enemies;private List bullets;private List particles;private boolean running false;private boolean paused false;private boolean gameOver false;private int score 0;private int killCount 0;private int shotCount 0;private int difficulty 1;private String difficultyName “简单”;private transient long startTime;private int elapsedTime 0;private int enemySpawnTimer 0;private int enemySpawnInterval 60;private int enemySpeedBase 2;private Random random new Random();private int width 600;private int height 700;public void init(int width, int height, Difficulty diff) {this.width width;this.height height;this.difficulty diff.ordinal() 1;this.difficultyName diff.name().equals(“EASY”) ? “简单” : diff.name().equals(“MEDIUM”) ? “中等” : “困难”;switch (diff) { case EASY: enemySpawnInterval 80; enemySpeedBase 1; maxLives 5; break; case MEDIUM: enemySpawnInterval 50; enemySpeedBase 2; maxLives 3; break; case HARD: enemySpawnInterval 30; enemySpeedBase 3; maxLives 2; break; } playerLives maxLives; score 0; killCount 0; shotCount 0; elapsedTime 0; gameOver false; paused false; player new GameEntity(width / 2 - 20, height - 80, 40, 40, 0, 0, new Color(50, 130, 220), player) { Override public void draw(Graphics2D g2d) { g2d.setColor(color); int[] xs {(int)x width/2, (int)x, (int)x width}; int[] ys {(int)y, (int)y height, (int)y height}; g2d.fillPolygon(xs, ys, 3); g2d.setColor(Color.CYAN); g2d.fillOval((int)x 15, (int)y 20, 10, 10); } }; enemies new ArrayList(); bullets new ArrayList(); particles new ArrayList(); running true; startTime System.currentTimeMillis();}public void update() {if (!running || paused || gameOver) return;elapsedTime (int)((System.currentTimeMillis() - startTime) / 1000); player.update(); if (player.getX() 0) player.setX(0); if (player.getX() width - player.getWidth()) player.setX(width - player.getWidth()); enemySpawnTimer; if (enemySpawnTimer enemySpawnInterval) { enemySpawnTimer 0; spawnEnemy(); } IteratorGameEntity bit bullets.iterator(); while (bit.hasNext()) { GameEntity b bit.next(); b.update(); if (b.getY() -10) b.setAlive(false); } IteratorGameEntity eit enemies.iterator(); while (eit.hasNext()) { GameEntity e eit.next(); e.update(); if (e.getY() height) { e.setAlive(false); playerLives--; createExplosion(e.getX() e.getWidth()/2, e.getY() e.getHeight(), Color.RED); if (playerLives 0) { gameOver true; running false; } } } IteratorGameEntity pit particles.iterator(); while (pit.hasNext()) { GameEntity p pit.next(); p.update(); if (p.getY() p.getY() 50 || !p.isAlive()) pit.remove(); } for (GameEntity b : bullets) { if (!b.isAlive()) continue; for (GameEntity e : enemies) { if (!e.isAlive()) continue; if (b.intersects(e)) { b.setAlive(false); e.setAlive(false); score 10; killCount; createExplosion(e.getX() e.getWidth()/2, e.getY() e.getHeight()/2, e.color); break; } } } for (GameEntity e : enemies) { if (!e.isAlive()) continue; if (e.intersects(player)) { e.setAlive(false); playerLives--; createExplosion(player.getX() 20, player.getY(), Color.ORANGE); if (playerLives 0) { gameOver true; running false; } } } bullets.removeIf(b - !b.isAlive()); enemies.removeIf(e - !e.isAlive());}private void spawnEnemy() {int w 30 random.nextInt(20);int h 30 random.nextInt(20);int ex random.nextInt(width - w);int speed enemySpeedBase random.nextInt(2);Color[] colors {Color.RED, Color.MAGENTA, Color.ORANGE, new Color(200, 50, 50)};Color c colors[random.nextInt(colors.length)];GameEntity enemy new GameEntity(ex, -h, w, h, 0, speed, c, enemy) { Override public void draw(Graphics2D g2d) { g2d.setColor(color); g2d.fillOval((int)x, (int)y, width, height); g2d.setColor(Color.WHITE); g2d.fillOval((int)x 5, (int)y 5, width - 10, height / 3); } }; enemies.add(enemy);}public void shoot() {if (!running || paused || gameOver) return;shotCount;GameEntity bullet new GameEntity(player.getX() player.getWidth()/2 - 3,player.getY() - 10,6, 12, 0, -8, new Color(255, 220, 50), “bullet”) {Overridepublic void draw(Graphics2D g2d) {g2d.setColor(color);g2d.fillRect((int)x, (int)y, width, height);g2d.setColor(Color.WHITE);g2d.fillRect((int)x 2, (int)y, 2, 4);}};bullets.add(bullet);}public void movePlayer(int dx) {if (player ! null) {player.setX(player.getX() dx);}}public void setPlayerX(double x) {if (player ! null) player.setX(x - player.getWidth()/2);}private void createExplosion(double cx, double cy, Color c) {for (int i 0; i 8; i) {double angle Math.random() * Math.PI * 2;double speed 1 Math.random() * 3;final double sx Math.cos(angle) * speed;final double sy Math.sin(angle) * speed;final Color pc c;GameEntity p new GameEntity(cx, cy, 4, 4, sx, sy, pc, “particle”) {private int life 20;Overridepublic void update() {super.update();life–;if (life 0) setAlive(false);}Overridepublic void draw(Graphics2D g2d) {g2d.setColor(pc);g2d.fillOval((int)x, (int)y, width, height);}};particles.add§;}}public void draw(Graphics2D g2d) {g2d.setColor(new Color(20, 20, 40));g2d.fillRect(0, 0, width, height);g2d.setColor(new Color(255, 255, 255, 80)); for (int i 0; i 50; i) { int sx (i * 37 13) % width; int sy (i * 23 7) % height; g2d.fillOval(sx, (sy elapsedTime * 10) % height, 2, 2); } for (GameEntity p : particles) p.draw(g2d); for (GameEntity e : enemies) e.draw(g2d); for (GameEntity b : bullets) b.draw(g2d); if (player ! null playerLives 0) player.draw(g2d); g2d.setColor(Color.WHITE); g2d.setFont(new Font(微软雅黑, Font.BOLD, 16)); g2d.drawString(分数: score, 15, 25); g2d.drawString(时间: elapsedTime 秒, 150, 25); g2d.drawString(击杀: killCount, 280, 25); g2d.drawString(发射: shotCount, 400, 25); g2d.drawString(生命: , 15, 50); for (int i 0; i maxLives; i) { if (i playerLives) { g2d.setColor(Color.RED); g2d.fillOval(60 i * 20, 38, 12, 12); } else { g2d.setColor(Color.GRAY); g2d.drawOval(60 i * 20, 38, 12, 12); } } g2d.setColor(Color.YELLOW); g2d.drawString(难度: difficultyName, width - 100, 25); if (gameOver) { g2d.setColor(new Color(0, 0, 0, 180)); g2d.fillRect(0, 0, width, height); g2d.setColor(Color.WHITE); g2d.setFont(new Font(微软雅黑, Font.BOLD, 40)); String msg playerLives 0 ? 游戏结束 : 胜利; int msgW g2d.getFontMetrics().stringWidth(msg); g2d.drawString(msg, (width - msgW) / 2, height / 2 - 40); g2d.setFont(new Font(微软雅黑, Font.PLAIN, 20)); String info 最终得分: score 击杀: killCount 用时: elapsedTime 秒; int infoW g2d.getFontMetrics().stringWidth(info); g2d.drawString(info, (width - infoW) / 2, height / 2 10); g2d.setFont(new Font(微软雅黑, Font.PLAIN, 16)); String hint 按 R 重新开始 或 空格 发射; int hintW g2d.getFontMetrics().stringWidth(hint); g2d.drawString(hint, (width - hintW) / 2, height / 2 50); } if (paused !gameOver) { g2d.setColor(new Color(0, 0, 0, 120)); g2d.fillRect(0, 0, width, height); g2d.setColor(Color.YELLOW); g2d.setFont(new Font(微软雅黑, Font.BOLD, 36)); String msg 暂 停; int msgW g2d.getFontMetrics().stringWidth(msg); g2d.drawString(msg, (width - msgW) / 2, height / 2); g2d.setFont(new Font(微软雅黑, Font.PLAIN, 16)); g2d.drawString(按 P 继续, (width - 60) / 2, height / 2 40); }}public boolean isRunning() { return running; }public boolean isPaused() { return paused; }public boolean isGameOver() { return gameOver; }public int getScore() { return score; }public int getKillCount() { return killCount; }public int getShotCount() { return shotCount; }public int getElapsedTime() { return elapsedTime; }public int getPlayerLives() { return playerLives; }public int getDifficulty() { return difficulty; }public String getDifficultyName() { return difficultyName; }public int getWidth() { return width; }public int getHeight() { return height; }public void setPaused(boolean paused) {this.paused paused;if (!paused) {startTime System.currentTimeMillis() - elapsedTime * 1000L;}}public void setRunning(boolean running) { this.running running; }public void setGameOver(boolean gameOver) { this.gameOver gameOver; }public void setScore(int score) { this.score score; }public void setKillCount(int killCount) { this.killCount killCount; }public void setShotCount(int shotCount) { this.shotCount shotCount; }public void setElapsedTime(int elapsedTime) { this.elapsedTime elapsedTime; }public void setPlayerLives(int playerLives) { this.playerLives playerLives; }public void setDifficulty(int difficulty) { this.difficulty difficulty; }public void setDifficultyName(String difficultyName) { this.difficultyName difficultyName; }public void setStartTime(long startTime) { this.startTime startTime; }public List getEnemies() { return enemies; }public List getBullets() { return bullets; }public List getParticles() { return particles; }public GameEntity getPlayer() { return player; }public void setPlayer(GameEntity player) { this.player player; }public void setEnemies(List enemies) { this.enemies enemies; }public void setBullets(List bullets) { this.bullets bullets; }public void setParticles(List particles) { this.particles particles; }public void setMaxLives(int maxLives) { this.maxLives maxLives; }public int getMaxLives() { return maxLives; }public void setEnemySpawnInterval(int interval) { this.enemySpawnInterval interval; }public void setEnemySpeedBase(int speed) { this.enemySpeedBase speed; }public int getEnemySpawnInterval() { return enemySpawnInterval; }public int getEnemySpeedBase() { return enemySpeedBase; }}GameState.java射击import java.io.Serializable;import java.util.List;/**射击游戏存档状态*/public class GameState implements Serializable {private static final long serialVersionUID 1L;private List enemies, bullets, particles;private GameEntity player;private int playerLives, maxLives;private int score, killCount, shotCount, elapsedTime;private boolean running, paused, gameOver;private int difficulty, enemySpawnInterval, enemySpeedBase;private String difficultyName;private int width, height;public GameState(ShootingGame game) {this.enemies game.getEnemies();this.bullets game.getBullets();this.particles game.getParticles();this.player game.getPlayer();this.playerLives game.getPlayerLives();this.maxLives game.getMaxLives();this.score game.getScore();this.killCount game.getKillCount();this.shotCount game.getShotCount();this.elapsedTime game.getElapsedTime();this.running game.isRunning();this.paused game.isPaused();this.gameOver game.isGameOver();this.difficulty game.getDifficulty();this.difficultyName game.getDifficultyName();this.enemySpawnInterval game.getEnemySpawnInterval();this.enemySpeedBase game.getEnemySpeedBase();this.width game.getWidth();this.height game.getHeight();}public List getEnemies() { return enemies; }public List getBullets() { return bullets; }public List getParticles() { return particles; }public GameEntity getPlayer() { return player; }public int getPlayerLives() { return playerLives; }public int getMaxLives() { return maxLives; }public int getScore() { return score; }

相关新闻

2026/7/20 16:10:55

Shell 环境分类(不同系统默认解释器)

、Shell 基础内置命令详解 date — 查看系统日期时间 作用 输出当前系统的日期、小时、分钟、秒、星期等完整时间信息。 常用拓展用法 date # 直接输出默认格式完整时间 date %Y-%m-%d # 自定义格式:年-月-日 date %H:%M:%S #…

2026/7/20 16:10:55

大模型小白入门:收藏这份医疗大模型测试优化与应用指南

大语言模型(LLM)在医疗领域备受关注,但面临知识精准性、场景适配性等挑战。文章从LLM基本原理出发,深入探讨医疗垂直大模型的测试、优化及多模态应用,剖析核心问题并展望未来。通过实验案例揭示大模型在医学知识理解、…

2026/7/21 9:05:03

S7-1200以太网通信配置与优化实战指南

1. S7-1200开放式以太网通信基础解析 S7-1200作为西门子中小型自动化控制的核心产品,其PROFINET接口的开放式通信能力在实际工业场景中应用广泛。这个集成在CPU本体的RJ45接口,看似普通却蕴含着强大的通信潜力。我们先从硬件特性说起:这个支持…

2026/7/21 9:05:03

10个数据工程师真正每天用的算法选型指南

1. 这10个算法真能改变生活?别被标题骗了,但它们确实重塑了你每天面对的数据现实 “这10个算法能改变你的生活”——这类标题在技术类资讯平台太常见了。但这次不一样。它没说“学会就能年薪百万”,也没鼓吹“三天速成AI专家”,而…

2026/7/21 9:05:03

SpringBoot集成BPMN.js:前后端分离的轻量级流程设计器实战

如果你正在开发一个需要流程审批、任务流转或自动化业务逻辑的Java应用,比如OA系统、工单系统或CRM,那么“工作流引擎”这个词你一定不陌生。但很多开发者一听到要集成工作流引擎,第一反应往往是“复杂”、“配置繁琐”、“学习曲线陡峭”。传…

2026/7/21 9:05:03

机器学习测试不是写断言,而是构建AI质量守门人体系

1. 为什么机器学习项目最怕“没出错,但错了”——一个被低估的工程真相 你有没有遇到过这种情况:模型在测试集上F1分数高达92%,部署上线后用户投诉率却翻了三倍?或者,昨天还跑得稳稳当当的数据清洗脚本,今天…

2026/7/21 9:00:02

三步搞定QMC加密音频转换:用QMCDecoder实现音乐格式自由

1. 项目概述:从“加密”到“自由”的音频解放之路如果你是一个老牌音乐App的深度用户,或者在网上某个角落下载过一些后缀为.qmc0、.qmc3、.qmcflac的音频文件,那么你大概率遇到过这样的困扰:这些文件只能在特定的播放器里打开&…

2026/7/20 6:33:00

Unity与Python本地通信:基于Flask的跨语言数据交换实战

1. 项目概述:为什么我们需要一个本地通信服务器?在游戏开发、数字孪生、仿真训练等众多领域,Unity作为强大的实时3D内容创作平台,其核心逻辑通常由C#驱动。然而,当我们需要进行复杂的数据分析、机器学习推理、科学计算…

2026/7/21 0:08:52

华为OD机试 新系统真题 【酒店服务记录分析】

酒店服务记录分析(C++/Go/C/Js/Java/Py)题解 华为OD机试 新系统真题 华为OD上机考试 新系统真题 7月19号 100分题型 华为OD机试新系统真题目录点击查看: 华为OD机试新系统真题题库目录|机考题库 + 算法考点详解 题目内容 你是某连锁酒店的数据分析师,酒店每天都会用一串编…

2026/7/21 0:08:52

华为OD机试 新系统真题 【小明的顺风车】

小明的顺风车(C++/Go/C/Js/JAVA/Py)题解 华为OD机试新系统真题 华为OD上机考试新系统真题 7月19号 200分题型 华为OD机试新系统真题目录点击查看: 华为OD机试新系统真题题库目录|机考题库 + 算法考点详解 题目内容 小明自驾回家,为节省旅途成本,决定在网上挂出顺风车服务…

2026/7/20 19:08:28

3个高效策略:快速掌握Axure中文界面配置

3个高效策略:快速掌握Axure中文界面配置 【免费下载链接】axure-cn Chinese language file for Axure RP. Axure RP 简体中文语言包。支持 Axure 11、10、9。不定期更新。 项目地址: https://gitcode.com/gh_mirrors/ax/axure-cn 还在为Axure RP的英文界面感…