總網頁瀏覽量

2013年5月4日 星期六

計算機(附打地鼠)


發想:
小學時候買過一種計算機
除了計算功能之外可以玩遊戲
其中有一款是打地鼠遊戲
因為找不到有程式這款遊戲
所以就寫個計算機程式當作練習
也順便實現這遊戲


本遊戲採用swing元件來實作

整個畫面是BoredLayout
North區塊放label;Center區塊放JPanel物件
JPanel採用GridLayout


第一個檔案UI.java

/*
 * 此檔專用來版面配置
 *
 * */

package EraseNum;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class UI extends JFrame {

static UI windows = new UI();
static Container cp = windows.getContentPane();
// static JButton game = new JButton("打地鼠");
// static JLabel label= new JLabel("這是Label");
static JTextField textfield = new JTextField();
static JPanel jpanel = new JPanel();
static JButton[] buttons = new JButton[25];

UI() {

}

public static void main(String[] args) {

windows.setTitle("Erase Number");
windows.setSize(550, 500);
cp.setLayout(new BorderLayout(10, 10));
// 10是水平與垂直間距
cp.setBackground(Color.pink);

// ======TextBox=====

Font font = new Font("新細明體", Font.ITALIC + Font.BOLD, 16);
Font lb_font = new Font("新細明體", Font.ITALIC + Font.BOLD, 58);
// label.setFont(lb_font);
// cp.add(label,BorderLayout.NORTH);
// BorderLayout.North不寫的話預設放在中間且填滿整個螢幕

// 靠右對其
textfield.setHorizontalAlignment(JTextField.RIGHT);
textfield.setFont(lb_font);
textfield.setEditable(false);
cp.add(textfield, BorderLayout.NORTH);

// ======TextBox=====

// =======Panel放置GridLayout
jpanel.setLayout(new GridLayout(5, 5, 5, 5));

String[] Button_Name =
{ "score", "GAME", "Back", "Calcu", "Level",
"7", "8", "9", "/", "sqrt",
"4", "5", "6", "*", "%",
"1", "2","3", "-", "pow",
"0", ".", "=", "+", "-/+" };

ButtonActionListener listener = new ButtonActionListener();

for (int i = 0; i < 25; i++) {
buttons[i] = new JButton(Button_Name[i]);
buttons[i].setFont(font);
buttons[i].setName(Button_Name[i]);
buttons[i].setEnabled(true);
// 設定按鈕可不可以按
buttons[i].addActionListener(listener);
// 事件傾聽者向各位按鈕註冊
jpanel.add(buttons[i]);

}
cp.add(jpanel);

// =====JPanel=======
windows.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
windows.setVisible(true);

}

}


第二個檔案:Mole.java



/*
 * 此檔案是打地鼠的程式運作
 *
 * */


package EraseNum;

public class Mole implements Runnable {

private String txtNumber;
private int Number;
private boolean isContinue = true;
// 因為thread不能重新start所以自行宣告method多加一層迴圈了

private int score = 0;
private int level = 1;
private int sleep = 1700;
private int life = 3;

@Override
public void run() {

while (true) {
while (isContinue) {
txtNumber = UI.textfield.getText();
Number = (int) (Math.random() * 10);

UI.textfield.setText(txtNumber + Integer.toString(Number));

try {

Thread.sleep(sleep);
} catch (InterruptedException e) {

e.printStackTrace();
}

if (UI.textfield.getText().length() == 12) {
isContinue = false;
}

}// isContinue
}// true

}

public void terminate() {

isContinue = false;

}

public void restart() {

isContinue = true;

}

public boolean isGameover() {

return isContinue;
}

public void addscore() {

int length = UI.textfield.getText().length();

switch (length) {
case 0:
case 1:
case 2:
case 3:
score = score + 100 * level;
break;
case 4:
case 5:
case 6:
score = score + 80 * level;
break;
default:
score = score + 60 * level;
break;
}

UI.buttons[0].setText(Integer.toString(score));
}

public void levelup() {
if (score > 150000) {
level = 10;
sleep = 400;
UI.buttons[4].setText("Level:"+Integer.toString(level));

} else if (score > 70000) {
level = 9;
sleep = 500;
UI.buttons[4].setText("Level:"+Integer.toString(level));

} else if (score > 45000) {
sleep = 600;
level = 8;
UI.buttons[4].setText("Level:"+Integer.toString(level));

} else if (score > 30000) {
sleep = 700;
level = 7;
UI.buttons[4].setText("Level:"+Integer.toString(level));

} else if (score > 15000) {
sleep = 800;
level = 6;
UI.buttons[4].setText("Level:"+Integer.toString(level));

} else if (score > 10000) {
sleep = 900;
level = 5;
UI.buttons[4].setText("Level:"+Integer.toString(level));

} else if (score > 5500) {
sleep = 1000;
level = 4;
UI.buttons[4].setText("Level:"+Integer.toString(level));

} else if (score > 3000) {
sleep = 1300;
level = 3;
UI.buttons[4].setText("Level:"+Integer.toString(level));

} else if (score > 1000) {
sleep = 1500;
level = 2;
UI.buttons[4].setText("Level:"+Integer.toString(level));

} else {
sleep = 1700;
level = 1;
UI.buttons[4].setText("Level:"+Integer.toString(level));
}

}

public void  initialScore(){
score=0;
sleep=1700;
}


}


第三個檔案 ButtonActionListener.java


package EraseNum;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;

public class ButtonActionListener implements ActionListener {

private boolean isPlaying = false;
private boolean isCalculator = true;

Mole mole;
Thread thread;

@Override
public void actionPerformed(ActionEvent e) {

JButton btn = (JButton) e.getSource();
// 取得事件來源物件
String str = btn.getText();
String str2 = btn.getName();
// 如果沒有setName會回傳null喔
// 到底要用getText還是getName呢?

// 進入打地鼠遊戲
if (str.equals("GAME")) {
isCalculator = false;
isPlaying = true;

UI.buttons[4].setText("Level:"+1);
UI.buttons[0].setText("分數:0");

if (thread == null) {
//初次執行遊戲
UI.textfield.setText("");// 清空JTextfield
mole = new Mole();
thread = new Thread(mole);
thread.start();
// JTextField自動跑出數字
// start()? Thread()?
// 按下數字鍵會削掉數字

} else {
//重啟遊戲
//因為執行緒不可以再次.start()
//所以自己寫method解決掉吧
UI.textfield.setText("");
mole.initialScore();

mole.restart();
}

return;

}

// 計算機功能
if (str.equals("Calcu")) {
isCalculator = true;
isPlaying = false;

if (thread != null) {
mole.terminate();
thread = null;

}

UI.textfield.setText("0");
return;

}

if (isCalculator) {

Calculator(str);

} else {

EraseNumberGame(str);

}

}

public void Calculator(String str) {

// 按下小數點
if (str.equals(".")) {

String Jtxt = UI.textfield.getText();
if (!Jtxt.contains(".")) {
// 小數點只能出現一個
UI.textfield.setText(UI.textfield.getText() + str);
return;
}

}

// 加上負號
// if (str.equals("+/-")) {
//
// Game.textfield.setText(Game.textfield.getText() + str2);
// return;
//
// }

// 按下數字鍵
for (int i = 0; i < 10; i++) {
if (str.equals(Integer.toString(i))) {

String Jtxt = UI.textfield.getText();

if(Jtxt.length()==1 && Jtxt.charAt(0)=='0'){
UI.textfield.setText(str);
}
else{
// 沒有append method
UI.textfield.setText(UI.textfield.getText() + str);
// 做成append效果

}

return;
}
}

}

public void EraseNumberGame(String str) {
// 遊戲結束嗎
if (mole.isGameover()) {
// 按到哪個數字啊
for (int i = 0; i < 10; i++) {
if (str.equals(Integer.toString(i))) {
// JTextField的字串不可以空字串,否則會outofboundexception
if (UI.textfield.getText().length() > 0) {
// 如果textfield第一個字元跟玩家按的數字鍵一樣
if (String.valueOf(UI.textfield.getText().charAt(0))
.equals(Integer.toString(i))) { // 按下的數字鍵是textbox第一個字
// System.out.println(UI.textfield.getText().substring(1));
// 刪掉第一個數字
UI.textfield.setText(UI.textfield.getText()
.substring(1));

mole.addscore();
mole.levelup();

}
}
return;
}
}
}

}

}



沒有留言:

張貼留言