🎲 趣味 Java · 命令行小游戏合集

95 阅读1分钟

微信图片_20251014151033_10_20.jpg

① 数字炸弹 💣

系统随机 1-100,你用最短次数炸掉它!

import java.util.*;
public class NumberBomb {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int bomb = new Random().nextInt(100) + 1, guess = 0, cnt = 0;
        System.out.println("💣 数字炸弹 1-100 已安置!");
        while (guess != bomb) {
            System.out.print("👉 输入数字:");
            guess = sc.nextInt(); cnt++;
            System.out.println(guess < bomb ? "📈 小了" : guess > bomb ? "📉 大了" : "💥 炸弹引爆!");
        }
        System.out.printf("🎉 你用了 %d 次,%s\n", cnt, cnt <= 7 ? "大神" : "继续加油");
    }
}

② 石头剪刀布 ✊✋✌️

三局两胜,赢够 2 把即胜利!

import java.util.*;
public class RPS {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String[] rps = {"✊", "✋", "✌️"};
        int win = 0;
        for (int i = 1; i <= 3; i++) {
            System.out.print("第" + i + "局 0=石头 1=剪刀 2=布:");
            int me = sc.nextInt(), bot = new Random().nextInt(3);
            System.out.println("你:" + rps[me] + " vs 电脑:" + rps[bot]);
            if (me == bot) System.out.println("🤝 平局");
            else if ((me - bot + 3) % 3 == 1) System.out.println("😎 你赢");
            else System.out.println("🤖 电脑赢");
            win += (me != bot && (me - bot + 3) % 3 == 1) ? 1 : 0;
        }
        System.out.println(win >= 2 ? "🎊 你获得最终胜利!" : "💔 下次再来");
    }
}

③ 打字竞速 🏃‍♂️

屏幕掉落单词,你敲对即得分!

import java.util.*;
import java.io.*;

public class TypingRace {
    private static final String[] WORDS = {"java", "scala", "python", "code", "speed"};
    public static void main(String[] args) throws Exception {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        Random rand = new Random();
        int score = 0, total = 10;
        long start = System.currentTimeMillis();
        for (int i = 1; i <= total; i++) {
            String word = WORDS[rand.nextInt(WORDS.length)];
            System.out.printf("第%2d个:%s\n", i, word);
            String input = in.readLine();
            if (input.equals(word)) { score++; System.out.println("✅ 正确"); }
            else System.out.println("❌ 错误");
        }
        long time = (System.currentTimeMillis() - start) / 1000;
        System.out.printf("🎯 得分:%d/%d  用时:%d 秒  速度:%.1f 词/分\n",
                score, total, time, total * 60.0 / time);
    }
}

④ 命令行贪吃蛇 🐍(极简版)

方向键控制,撞墙或咬自己 GameOver!

代码较长(~150 行),这里给核心逻辑

核心思路:

  • Scanner 读取 W A S D 控制方向
  • char[][] board 渲染地图
  • Timer 或多线程刷新屏幕
  • 吃到 * 食物长度 +1,撞 # 墙或自身 → GameOver

🏁 一键编译运行

javac GameName.java
java GameName