?? mainframe.java
字號:
}
else if (rows[0] < 0) {
throughwall--;
jLabel6.setText(Integer.toString(throughwall));
rows[0] = (rows[0] + ROWS) % ROWS;
}
else if (columes[0] >= COLS) {
throughwall--;
jLabel6.setText(Integer.toString(throughwall));
columes[0] = COLS - columes[0];
}
else if (columes[0] < 0) {
throughwall--;
jLabel6.setText(Integer.toString(throughwall));
columes[0] = (columes[0] + COLS) % COLS;
}
}
//當沒有穿墻寶物,并且蛇頭碰到墻時,游戲結束
if (rows[0] >= ROWS || rows[0] < 0 || columes[0] >= COLS ||
columes[0] < 0 && throughwall == 0) {
isEnd = true;
}
//蛇頭碰到蛇身時的處理操作
if (playBlocks[rows[0]][columes[0]].getBackground().equals(Color.green)) {
if (throughbody != 0) {
throughbody--;
jLabel4.setText(Integer.toString(throughbody));
}
else {
isEnd = true;
}
}
//吃食物時的操作
if (playBlocks[rows[0]][columes[0]].getBackground().equals(Color.yellow)) {
score += 100;
jLabel2.setText(Integer.toString(score));
if (score % 2000 == 0 && speed > 100) {
JOptionPane.showMessageDialog(jPanel1,
"真是了不起,準備進入下一關\nREADY? GO ! ! !");
speed -= 100;
}
}
//獲得穿墻寶物時的操作
if (playBlocks[rows[0]][columes[0]].getBackground().equals(Color.blue)) {
score += 100;
throughbody++;
jLabel2.setText(Integer.toString(score));
jLabel4.setText(Integer.toString(throughbody));
if (score % 2000 == 0 && speed > 100) {
JOptionPane.showMessageDialog(jPanel1,
"真是了不起,準備進入下一關\nREADY? GO ! ! !");
speed -= 100;
}
}
//獲得穿身寶物時的操作
if (playBlocks[rows[0]][columes[0]].getBackground().equals(Color.red)) {
score += 100;
throughwall++;
jLabel2.setText(Integer.toString(score));
jLabel6.setText(Integer.toString(throughwall));
if (score % 2000 == 0 && speed > 100) {
JOptionPane.showMessageDialog(jPanel1,
"真是了不起,準備進入下一關\nREADY? GO ! ! !");
speed -= 100;
}
}
//蛇頭吃完食物后,蛇身加長,并隨機顯示下一個食物或寶物
if (playBlocks[rows[0]][columes[0]].getBackground().equals(Color.yellow)
||playBlocks[rows[0]][columes[0]].getBackground().equals(Color.blue)
||playBlocks[rows[0]][columes[0]].getBackground().equals(Color.red)) {
length++;
int x, y;
int random = (int) (Math.random() * 10);
if (random < 7) {
x = (int) (Math.random() * ROWS);
y = (int) (Math.random() * COLS);
while (playBlocks[x][y].isVisible()) {
x = (int) (Math.random() * ROWS);
y = (int) (Math.random() * COLS);
}
playBlocks[x][y].setBackground(Color.yellow);
playBlocks[x][y].setVisible(true);
}
if (random >=7 && random < 9) {
x = (int) (Math.random() * ROWS);
y = (int) (Math.random() * COLS);
while (playBlocks[x][y].isVisible()) {
x = (int) (Math.random() * ROWS);
y = (int) (Math.random() * COLS);
}
playBlocks[x][y].setBackground(Color.blue);
playBlocks[x][y].setVisible(true);
}
if (random >= 9) {
x = (int) (Math.random() * ROWS);
y = (int) (Math.random() * COLS);
while (playBlocks[x][y].isVisible()) {
x = (int) (Math.random() * ROWS);
y = (int) (Math.random() * COLS);
}
playBlocks[x][y].setBackground(Color.red);
playBlocks[x][y].setVisible(true);
}
}
//顯示蛇頭
playBlocks[rows[0]][columes[0]].setBackground(Color.green);
playBlocks[rows[0]][columes[0]].setVisible(true);
}
}
class SnakeThread extends Thread {
public void run() {
while (true) {
try {
//停頓
Thread.sleep(snake.speed);
//當游戲處于正常運行狀態,則移動蛇身
if (!isEnd && !isPause) {
snake.move();
}
}
catch (Exception ex){}
}
}
}
void jButton1_actionPerformed(ActionEvent e) {
start();
}
void this_keyPressed(KeyEvent e) {
//判斷游戲狀態
if (!isEnd && !isPause) {
//根據用戶按鍵,設置蛇運動方向
if (e.getKeyCode() == KeyEvent.VK_UP) {
snake.direction = UP;
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
snake.direction = DOWN;
}
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
snake.direction = LEFT;
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
snake.direction = RIGHT;
}
}
}
class MainFrame_jButton1_actionAdapter
implements java.awt.event.ActionListener {
MainFrame adaptee;
MainFrame_jButton1_actionAdapter(MainFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
class MainFrame_this_keyAdapter
extends java.awt.event.KeyAdapter {
MainFrame adaptee;
MainFrame_this_keyAdapter(MainFrame adaptee) {
this.adaptee = adaptee;
}
public void keyPressed(KeyEvent e) {
adaptee.this_keyPressed(e);
}
}
void jButton3_actionPerformed(ActionEvent e) {
System.exit(0);
}
void jButton2_actionPerformed(ActionEvent e) {
if (isPause == true )
{jButton2.setText("暫停");}
if (isPause == false)
{jButton2.setText("繼續");}
isPause = !isPause;
}
void jRadioButton1_actionPerformed(ActionEvent e) {
level = BEGINNER;
}
void jRadioButton2_actionPerformed(ActionEvent e) {
level = MIDDLE;
}
void jRadioButton3_actionPerformed(ActionEvent e) {
level = EXPERT;
}
void jButton4_actionPerformed(ActionEvent e) {
Help_Dialog dlg = new Help_Dialog();
Dimension dlgSize = dlg.getPreferredSize();
Dimension frmSize = getSize();
Point loc = getLocation();
dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y);
dlg.setModal(true);
dlg.pack();
dlg.show();
}
}
class MainFrame_jButton3_actionAdapter implements java.awt.event.ActionListener {
MainFrame adaptee;
MainFrame_jButton3_actionAdapter(MainFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton3_actionPerformed(e);
}
}
class MainFrame_jButton2_actionAdapter implements java.awt.event.ActionListener {
MainFrame adaptee;
MainFrame_jButton2_actionAdapter(MainFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton2_actionPerformed(e);
}
}
class MainFrame_jRadioButton1_actionAdapter implements java.awt.event.ActionListener {
MainFrame adaptee;
MainFrame_jRadioButton1_actionAdapter(MainFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jRadioButton1_actionPerformed(e);
}
}
class MainFrame_jRadioButton2_actionAdapter implements java.awt.event.ActionListener {
MainFrame adaptee;
MainFrame_jRadioButton2_actionAdapter(MainFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jRadioButton2_actionPerformed(e);
}
}
class MainFrame_jRadioButton3_actionAdapter implements java.awt.event.ActionListener {
MainFrame adaptee;
MainFrame_jRadioButton3_actionAdapter(MainFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jRadioButton3_actionPerformed(e);
}
}
class MainFrame_jButton4_actionAdapter implements java.awt.event.ActionListener {
MainFrame adaptee;
MainFrame_jButton4_actionAdapter(MainFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton4_actionPerformed(e);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -