Untitled
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.Random; // 主類:包含初始場景邏輯 public class LoveTest { public static void main(String[] args) { // 建立主框架 JFrame frame = new JFrame("你愛我嗎?"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(400, 200); frame.setLayout(null); // 使用絕對佈局 // 建立標籤 JLabel label = new JLabel("你愛我嗎?"); label.setFont(new Font("微軟正黑體", Font.PLAIN, 14)); label.setBounds(150, 20, 200, 30); frame.add(label); // 「愛」按鈕 JButton yesButton = new JButton("愛"); yesButton.setFont(new Font("微軟正黑體", Font.PLAIN, 12)); yesButton.setBounds(70, 100, 100, 30); yesButton.addActionListener(e -> label.setText("我就知道你一定愛我~")); frame.add(yesButton); // 「不愛」按鈕 JButton noButton = new JButton("不愛"); noButton.setFont(new Font("微軟正黑體", Font.PLAIN, 12)); noButton.setBounds(230, 100, 100, 30); frame.add(noButton); // 當滑鼠移到「不愛」按鈕時移動按鈕 noButton.addMouseListener(new MouseAdapter() { Random random = new Random(); @Override public void mouseEntered(MouseEvent e) { int newX = random.nextInt(300); // 隨機生成新位置 int newY = random.nextInt(120); // 確保按鈕不超出範圍 noButton.setBounds(newX, newY, 100, 30); } }); // 當按下「不愛」按鈕,跳轉到 LoveChaseGame noButton.addActionListener(e -> { frame.dispose(); // 關閉當前框架 new LoveChaseGame(); // 啟動 LoveChaseGame }); // 顯示框架 frame.setVisible(true); } } public class LoveChaseGame extends JPanel { private int loveX = 150, loveY = 150; // 「愛」的位置 private int noLoveX = 50, noLoveY = 50; // 「不愛」的位置 private int itemX = -1, itemY = -1; // 道具的位置 private int loveSpeed = 0; // 「愛」的速度 private int noLoveSpeed = 0; // 「不愛」的速度 private int life = 3; // 生命值 private int timeLeft = 10; // 每關遊戲時間 private int level = 1; // 關卡 private boolean isInvincible = false; // 無敵狀態 private boolean isCrazyMode = false; // 瘋狂模式標誌 private boolean backgroundToggle = false; // 背景閃爍控制 private Timer gameTimer; // 倒計時 Timer private Timer chaseTimer; // 「不愛」追逐 Timer private Timer bloodTimer; // 血跡更新 Timer private Random random = new Random(); // 隨機數生成器 private String[] messages = { // 病嬌語錄 "你真的愛我嗎?", "為什麼要逃?", "別離開我……", "你只能屬於我!" }; private String currentMessage = ""; // 當前顯示的語錄 private boolean showMessage = false; // 是否顯示語錄 private int[][] bloodStains; // 血跡位置數組 private Color loveColor = Color.RED; // 「愛」的顏色 private JFrame frame; public LoveChaseGame() { // 初始化血跡數組 bloodStains = new int[10][2]; // 10 個血跡,每個包含 x 和 y 坐標 resetBloodStains(); // 顯示遊戲規則 showGameRules(); // 設置主遊戲視窗 frame = new JFrame("愛與不愛的追逐"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(600, 400); frame.add(this); frame.setVisible(true); // 顯示第一關故事 showLevelStory(); // 鍵盤事件監聽器 frame.addKeyListener(new KeyAdapter() { private int prevX = loveX, prevY = loveY; // 上一次「愛」的位置 @Override public void keyPressed(KeyEvent e) { int key = e.getKeyCode(); if (key == KeyEvent.VK_UP) loveY -= 10; if (key == KeyEvent.VK_DOWN) loveY += 10; if (key == KeyEvent.VK_LEFT) loveX -= 10; if (key == KeyEvent.VK_RIGHT) loveX += 10; // 防止「愛」移出邊界 loveX = Math.max(0, Math.min(loveX, getWidth() - 50)); loveY = Math.max(0, Math.min(loveY, getHeight() - 50)); // 計算「愛」的速度 loveSpeed = (int) Math.sqrt(Math.pow(loveX - prevX, 2) + Math.pow(loveY - prevY, 2)); prevX = loveX; prevY = loveY; // 撿取道具 if (itemX != -1 && Math.abs(loveX - itemX) < 30 && Math.abs(loveY - itemY) < 30) { if (life < 3) life++; // 增加生命值,但最多不超過 3 itemX = itemY = -1; // 移除道具 } repaint(); } }); // 倒計時計時器 gameTimer = new Timer(1000, e -> { if (timeLeft > 0) { timeLeft--; if (timeLeft % 5 == 0) spawnItem(); // 每隔 5 秒生成道具 repaint(); } else { gameTimer.stop(); chaseTimer.stop(); if (level < 3) { level++; showLevelStory(); // 每關開始前顯示故事 timeLeft = 10; JOptionPane.showMessageDialog(frame, "第 " + level + " 關開始!"); if (level == 3) startCrazyMode(); startNextLevel(); } else { JOptionPane.showMessageDialog(frame, "遊戲結束!你成功通過所有關卡!剩餘生命:" + life); frame.dispose(); new MainMenu(); // 返回主選單 } } }); gameTimer.start(); // 「不愛」追逐計時器 chaseTimer = new Timer(100, e -> { // 計算「不愛」的速度 double speedFactor = Math.min(1.0 + (level - 1) * 0.2, 0.95); // 不超過「愛」的速度 int step = Math.max(1, (int) (loveSpeed * speedFactor)); if (loveX > noLoveX) noLoveX += step; if (loveX < noLoveX) noLoveX -= step; if (loveY > noLoveY) noLoveY += step; if (loveY < noLoveY) noLoveY -= step; // 碰撞檢測 if (!isInvincible && Math.abs(loveX - noLoveX) < 30 && Math.abs(loveY - noLoveY) < 30) { life--; isInvincible = true; showMessage = true; // 顯示語錄 currentMessage = messages[random.nextInt(messages.length)]; new Timer(2000, evt -> showMessage = false).start(); // 2 秒後隱藏語錄 new Timer(1000, evt -> isInvincible = false).start(); // 無敵持續 1 秒 } // 判斷生命值是否歸 0 if (life <= 0) { gameOver(); return; } repaint(); }); chaseTimer.start(); } private void showGameRules() { String rules = """ 遊戲規則: 1. 控制「愛」(紅色字體) 避開「不愛」。 2. 撿取「我愛你」道具可以恢復生命值。 3. 共有三關,每關「不愛」速度增加。 4. 第三關將進入瘋狂模式,請小心! 操作方式: 使用上下左右方向鍵移動。 """; JOptionPane.showMessageDialog(null, rules, "遊戲規則", JOptionPane.INFORMATION_MESSAGE); } private void showLevelStory() { String[] stories = { "第一關:\n你和「不愛」開始了命運的追逐。\n能否躲避並活下去?", "第二關:\n「不愛」越追越快,你開始感到壓迫……", "第三關:\n「愛」漸漸變成黑暗,悲傷籠罩了你的內心……" }; JOptionPane.showMessageDialog(null, stories[level - 1], "故事背景", JOptionPane.INFORMATION_MESSAGE); } private void startCrazyMode() { isCrazyMode = true; // 血跡更新計時器 bloodTimer = new Timer(1000, e -> { resetBloodStains(); repaint(); }); bloodTimer.start(); // 背景閃爍計時器 new Timer(500, e -> { backgroundToggle = !backgroundToggle; repaint(); }).start(); } private void resetBloodStains() { for (int i = 0; i < bloodStains.length; i++) { bloodStains[i][0] = random.nextInt(600); // 隨機生成 x 坐標 bloodStains[i][1] = random.nextInt(400); // 隨機生成 y 坐標 } } private void startNextLevel() { noLoveX = 50; noLoveY = 50; chaseTimer.restart(); gameTimer.restart(); } private void spawnItem() { itemX = random.nextInt(getWidth() - 30); itemY = random.nextInt(getHeight() - 30); } private void gameOver() { chaseTimer.stop(); gameTimer.stop(); if (bloodTimer != null) bloodTimer.stop(); // 顯示結束畫面 JOptionPane.showMessageDialog(frame, "你逃不掉的,乖乖屬於我吧!", "遊戲結束", JOptionPane.ERROR_MESSAGE); // 返回主選單 frame.dispose(); new MainMenu(); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); // 背景 if (isCrazyMode) { g.setColor(backgroundToggle ? Color.BLACK : Color.PINK); } else { g.setColor(Color.PINK); } g.fillRect(0, 0, getWidth(), getHeight()); // 繪製「愛」 g.setColor(loveColor); g.setFont(new Font("微軟正黑體", Font.BOLD, 30)); g.drawString("愛", loveX, loveY); // 繪製「不愛」 g.setColor(isCrazyMode ? new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)) : Color.BLUE); g.drawString("不愛", noLoveX, noLoveY); // 繪製補血道具 if (itemX != -1 && itemY != -1) { g.setColor(Color.GREEN); g.setFont(new Font("微軟正黑體", Font.BOLD, 20)); g.drawString("我愛你", itemX, itemY); } // 顯示病嬌語錄 if (showMessage) { g.setColor(Color.RED); g.setFont(new Font("微軟正黑體", Font.BOLD, 25)); g.drawString(currentMessage, getWidth() / 2 - 150, getHeight() / 2); } // 繪製生命值 g.setColor(Color.BLACK); g.drawString("生命: ", 10, 40); g.setColor(Color.RED); for (int i = 0; i < life; i++) { g.drawString("❤", 80 + i * 30, 40); } // 繪製倒計時和關卡 g.setColor(Color.BLACK); g.drawString("時間: " + timeLeft, 500, 40); g.drawString("關卡: " + level, 500, 70); } public static void main(String[] args) { new LoveChaseGame(); } } // 主選單類別 class MainMenu { public MainMenu() { JFrame frame = new JFrame("愛與不愛的追逐遊戲"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(400, 300); frame.setLayout(null); JLabel title = new JLabel("愛與不愛的追逐遊戲"); title.setFont(new Font("微軟正黑體", Font.BOLD, 18)); title.setBounds(100, 50, 200, 30); frame.add(title); JButton startButton = new JButton("開始遊戲"); startButton.setBounds(100, 100, 200, 30); startButton.addActionListener(e -> { frame.dispose(); new LoveChaseGame(); }); frame.add(startButton); frame.setVisible(true); } }
Leave a Comment