MinesweeperApp.java

发布时间:2026/9/10 1:45:06

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/9/10 5:38:38

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

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

2026/9/6 2:40:25

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

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

2026/9/11 1:14:51

OpenClaw与Google Chat集成:智能对话在养殖监控中的应用

1. OpenClaw与Google Chat集成概述 OpenClaw作为一款新兴的智能对话平台,其与Google Chat的集成方案正在技术社区引发广泛讨论。这个方案本质上是通过OpenClaw的API网关功能,将智能对话能力无缝嵌入到Google Workspace的日常协作场景中。我最近在实际部署…

2026/9/11 1:14:51

光机电软一体化协同控制技术在激光加工中的应用

1. 激光加工技术现状与挑战激光加工技术作为现代制造业的核心工艺之一,已经从早期的单一功能应用发展到如今的复合型精密加工阶段。在金属切割、焊接、打标、表面处理等领域,激光技术凭借其非接触、高精度、高效率的特点,已经成为不可替代的加…

2026/9/11 1:14:51

鸿蒙PC版真机环境搭建与卡片应用开发实战

1. 项目概述:鸿蒙PC版真机运行环境搭建去年华为开发者大会上首次亮相的HarmonyOS PC版,终于在6.0版本迎来了开发者模式的重大更新。作为一个长期关注鸿蒙生态的开发者,我第一时间在ThinkPad X1 Carbon上完成了真机环境部署,并成功…

2026/9/11 1:09:51

新媒体运营转型指南:从零基础到实战进阶

1. 转行新媒体运营的底层逻辑 刚接触新媒体运营时,很多人会陷入一个误区——认为只要学会发微博、写公众号就是运营。实际上,现代新媒体运营是一个系统工程,需要同时具备内容创作、用户洞察、数据分析、活动策划等多维能力。我从传统行业转行…

2026/9/10 16:39:38

超人会飞不算本事:系统稳定依赖清晰规则与边界设计

开头先不绕弯子。“#斯坦李吐槽dc 所以超人是无缘无故会飞的嘛哈哈哈哈哈哈哈锤哥真是技术人才啊!#雷神 #复联”这类调侃式短标题,第一波冲击力在于它把两个宇宙的角色塞进同一个吐槽箱里,但细想一下就能发现,它真正碰到的根本不是…

2026/9/10 11:16:38

超人VS蜘蛛侠:拆解超级IP的影响力与传播方法论

把“蜘蛛侠 vs 超人”放在 CSDN 上聊,可能很多人第一反应是走错片场了。但如果把这两个角色看成“两个持续运营了 80 多年的文化产品”,你会发现,这场比较本质上是两个不同 IP 策略的长期结果对比:超人赢在定义了整个超级英雄题材…

2026/9/9 16:31:09

基于CNN的调制信号识别:MATLAB实现时频图分类实战

简介:本资源是一套面向通信工程与信号处理方向学习者、研究者的深度学习实践方案,聚焦调制信号自动检测与识别这一典型无线通信任务,解决传统方法依赖人工特征、低信噪比下性能下降等痛点。压缩包共12个文件(10.73MB)&…

2026/9/10 12:32:02

USB Type-C PCB布局分区设计:电源、高速信号与PD协议全攻略

做硬件这行,Type-C接口算是典型的“看着简单,做起来全坑”的东西。光引脚就24个,高低速信号、电源、控制线全部塞在一个小小的连接器里,如果PCB布局不做规划,打样回来基本就是“插上没反应”、“高速掉线”、“静电一打…

2026/9/10 15:19:50

系统编程学习原型如何补齐稳定性边界

系统编程学习原型如何补齐稳定性边界预算有限时&#xff0c;我先优化明显多余的复制&#xff0c;而不是猜测性地换容器。用借用传递只读数据通常就能减少分配&#xff1a; fn parse(line: &str) -> Result<Item, Error> { /* ... */ }用基准确认热点确实在分配&am…

2026/9/10 15:49:53

雨花区哪家财务公司代理记账比较好?

在雨花区&#xff0c;企业处理财税事务常常面临诸多挑战&#xff0c;选择一家靠谱的财务公司至关重要。湖南巨勤财务管理咨询有限公司就是本地正规实体财税服务机构&#xff0c;深耕本地工商财税行业多年&#xff0c;熟悉当地工商局、税务局最新政策与申报流程。主营公司注册、…

还想了解更多?直接咨询顾问

免费诊断 + 免费方案 + 透明报价。

全国咨询热线400-8866-253
免费获取方案
咨询二维码