在先前 Applet 版本基礎上,改良版遷移至 Swing JFrame 全螢幕模式,新增右鍵選單動態調整「字母數量(1~9)」與「下落速度」,並引入雙執行緒架構(遊戲迴圈 + 事件分派),修正原版碰撞檢測與統計邏輯,適合作為桌面小遊戲參考專案。
repaint() + Thread.sleep(FPS))、字母產生器持有者。// 位置不重複
int[] aa = new int[100];
for (int i=0, n=0; i < exist_letter_num; i++) {
aa[n] = ran.nextInt(9);
if (i != 0) while (check(aa, n)) aa[n] = ran.nextInt(9);
X[i] = XY[aa[n]];
let[aa[n]] = true; n++;
}
// 字元不重複
while (true) {
for (int i=0; i < exist_letter_num; i++) {
c[i] = new StringBuffer(1);
c[i].setCharAt(0, (char)(ran.nextInt(26) + 65)); // A-Z
cc[i] = c[i].toString();
}
if (checkChar(c)) break;
}
public boolean isTyped(char key) {
for (int i=0; i < exist_letter_num; i++) {
if ((char)(key - 32) == cc[i].charAt(0)) { // 小寫轉大寫比對
isTypedSum++;
myletter.reStart(i);
return true;
}
}
isWrongTypedSum++;
return false;
}
準確率 = isTypedSum / (isTypedSum + isWrongTypedSum + isOmittedSum) × 100%
| 選項 | 行為 |
|---|---|
| 開始遊戲 | 重置統計、啟動執行緒、產生字母 |
| 結束遊戲 | 停止執行緒 |
| 增加字母數量 | exist_letter_num++ (上限 9) → 重新產生 |
| 加快下落速度 | 所有 speed[i]++ |
| 減少字母數量 | exist_letter_num-- (下限 1) → 重新產生 |
| 減緩下落速度 | speed[i]-- (下限 1) |
JFrame + Toolkit.getScreenSize() 全螢幕。MyPanel,避免 static 污染。reStartX()/reStartStr(),確保位置與字元雙重不重複。createImage + offscreen Graphics),消除閃爍。javac MyPanel.java
java MyPanel
啟動後於視窗內右鍵選擇「開始遊戲」。