?? mainframe.java
字號:
package snake;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
/**
* <p>Title: 貪食蛇游戲</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: </p>
* @author zsb
* @version 1.0
*/
public class MainFrame extends JFrame {
private JPanel contentPane;//窗體內容網格
private JToolBar jToolBar1 = new JToolBar();//工具欄
private JButton jButton1 = new JButton();//游戲開始按鈕
private JButton jButton2 = new JButton();//游戲暫停按鈕
private JButton jButton3 = new JButton();//游戲退出按鈕
private JButton jButton4 = new JButton();//幫助按鈕
private JPanel jPanel1 = new JPanel();//游戲主體面板容器
private JPanel jPanel_PlayArea = new JPanel();//游戲區面板容器
private XYLayout xYLayout1 = new XYLayout();//容器布局管理器
private XYLayout xYLayout2 = new XYLayout();
private XYLayout xYLayout3 = new XYLayout();
private static final int UP = 1, LEFT = 2, DOWN = 3, RIGHT = 4;//貪食蛇運動方向
private static final int BEGINNER = 1, MIDDLE = 2, EXPERT = 3;//游戲級別常量
private static final int ROWS = 30;//游戲區行數
private static final int COLS = 50;//游戲區列數
private boolean isPause = false;//游戲暫停標志
private boolean isEnd;//游戲結束標志
private SnakeBody snake ;//貪食蛇
private int score = 0;//當前得分
private int level = BEGINNER;//當前游戲級別
SnakeThread thread = new SnakeThread();//游戲主線程
private GridLayout gridLayout1 = new GridLayout(ROWS, COLS, 0, 0);//游戲區布局
private JButton[][] playBlocks;//游戲區的所有方塊
JPanel jPanel2 = new JPanel();
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JLabel jLabel3 = new JLabel();
JLabel jLabel4 = new JLabel();
JLabel jLabel5 = new JLabel();
JLabel jLabel6 = new JLabel();
JLabel jLabel7 = new JLabel();
ButtonGroup buttonGroup1 = new ButtonGroup();
JRadioButton jRadioButton1 = new JRadioButton();
JRadioButton jRadioButton2 = new JRadioButton();
JRadioButton jRadioButton3 = new JRadioButton();
//Construct the frame
public MainFrame() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
//setIconImage(Toolkit.getDefaultToolkit().createImage(MainFrame.class.getResource("[Your Icon]")));
contentPane = (JPanel)this.getContentPane();
contentPane.setLayout(xYLayout2);
this.setResizable(false);
this.setSize(new Dimension(512, 414));
this.setTitle("貪食蛇");
this.addKeyListener(new MainFrame_this_keyAdapter(this));
jButton1.setFont(new java.awt.Font("DialogInput", 0, 12));
jButton1.setMaximumSize(new Dimension(79, 39));
jButton1.setMinimumSize(new Dimension(79, 39));
jButton1.setPreferredSize(new Dimension(79, 39));
jButton1.setFocusPainted(false);
jButton1.setText("開始");
jButton1.addActionListener(new MainFrame_jButton1_actionAdapter(this));
jButton1.addKeyListener(new MainFrame_this_keyAdapter(this));
jButton2.setText("暫停");
jButton2.addActionListener(new MainFrame_jButton2_actionAdapter(this));
jButton2.setPreferredSize(new Dimension(79, 39));
jButton2.setFocusPainted(false);
jButton2.setMinimumSize(new Dimension(79, 39));
jButton2.setMaximumSize(new Dimension(79, 39));
jButton2.setFont(new java.awt.Font("DialogInput", 0, 12));
jButton2.addKeyListener(new MainFrame_this_keyAdapter(this));
jButton3.setText("退出");
jButton3.addActionListener(new MainFrame_jButton3_actionAdapter(this));
jButton3.setPreferredSize(new Dimension(79, 39));
jButton3.setFocusPainted(false);
jButton3.setMinimumSize(new Dimension(79, 39));
jButton3.setMaximumSize(new Dimension(79, 39));
jButton3.setFont(new java.awt.Font("DialogInput", 0, 12));
jButton3.addKeyListener(new MainFrame_this_keyAdapter(this));
jButton4.setText("幫助");
jButton4.addActionListener(new MainFrame_jButton4_actionAdapter(this));
jButton4.setPreferredSize(new Dimension(79, 39));
jButton4.setFocusPainted(false);
jButton4.setMinimumSize(new Dimension(79, 39));
jButton4.setMaximumSize(new Dimension(79, 39));
jButton4.setFont(new java.awt.Font("DialogInput", 0, 12));
jButton4.addKeyListener(new MainFrame_this_keyAdapter(this));
jPanel1.setLayout(xYLayout1);
jPanel1.addKeyListener(new MainFrame_this_keyAdapter(this));
jPanel_PlayArea.setLayout(gridLayout1);
jPanel_PlayArea.setBorder(BorderFactory.createEtchedBorder());
jPanel_PlayArea.addKeyListener(new MainFrame_this_keyAdapter(this));
jLabel1.setFont(new java.awt.Font("DialogInput", 0, 12));
jLabel1.setText("得分:");
jPanel2.setLayout(xYLayout3);
jLabel2.setFont(new java.awt.Font("DialogInput", 0, 12));
jLabel2.setToolTipText("");
jLabel2.setText("0");
jLabel3.setText("穿身:");
jLabel3.setFont(new java.awt.Font("DialogInput", 0, 12));
jLabel4.setFont(new java.awt.Font("DialogInput", 0, 12));
jLabel4.setText("0");
jLabel5.setText("穿墻:");
jLabel5.setFont(new java.awt.Font("DialogInput", 0, 12));
jLabel5.setToolTipText("");
jLabel6.setText("0");
jLabel6.setFont(new java.awt.Font("DialogInput", 0, 12));
jLabel6.setToolTipText("");
jRadioButton1.setFont(new java.awt.Font("DialogInput", 0, 12));
jRadioButton1.setText("初級");
jRadioButton1.addActionListener(new MainFrame_jRadioButton1_actionAdapter(this));
jRadioButton1.addKeyListener(new MainFrame_this_keyAdapter(this));
jRadioButton2.setText("中級");
jRadioButton2.addActionListener(new MainFrame_jRadioButton2_actionAdapter(this));
jRadioButton2.addKeyListener(new MainFrame_this_keyAdapter(this));
jRadioButton2.setFont(new java.awt.Font("DialogInput", 0, 12));
jRadioButton3.setText("高級");
jRadioButton3.addActionListener(new MainFrame_jRadioButton3_actionAdapter(this));
jRadioButton3.setFont(new java.awt.Font("DialogInput", 0, 12));
jRadioButton3.addKeyListener(new MainFrame_this_keyAdapter(this));
buttonGroup1.add(jRadioButton1);
buttonGroup1.add(jRadioButton2);
buttonGroup1.add(jRadioButton3);
jRadioButton1.setSelected(true);
jLabel7.setFont(new java.awt.Font("DialogInput", 0, 12));
jLabel7.setText("等級:");
contentPane.add(jToolBar1, new XYConstraints(0, 0, 512, -1));
jToolBar1.add(jButton1, null);
jToolBar1.add(jButton2, null);
jToolBar1.add(jButton3, null);
jToolBar1.add(jButton4, null);
jToolBar1.addKeyListener(new MainFrame_this_keyAdapter(this));
contentPane.add(jPanel1, new XYConstraints(0, 43, 512, 371));
jPanel1.add(jPanel_PlayArea, new XYConstraints(0, 33, 524, 314));
jPanel1.add(jPanel2, new XYConstraints(0, 0, 498, 30));
jPanel2.add(jRadioButton1, new XYConstraints(331, 1, -1, -1));
jPanel2.add(jRadioButton3, new XYConstraints(443, 1, -1, -1));
jPanel2.add(jRadioButton2, new XYConstraints(390, 1, -1, -1));
jPanel2.add(jLabel6, new XYConstraints(245, 6, 30, -1));
jPanel2.add(jLabel7, new XYConstraints(287, 5, 47, -1));
jPanel2.add(jLabel5, new XYConstraints(197, 6, 47, -1));
jPanel2.add(jLabel4, new XYConstraints(166, 7, 26, -1));
jPanel2.add(jLabel3, new XYConstraints(120, 6, 38, -1));
jPanel2.add(jLabel1, new XYConstraints(23, 6, 39, -1));
jPanel2.add(jLabel2, new XYConstraints(62, 6, 54, -1));
//創建并初始化游戲區方塊
playBlocks = new JButton[ROWS][COLS];
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
playBlocks[i][j] = new JButton();
playBlocks[i][j].setBackground(Color.lightGray);
playBlocks[i][j].setVisible(false);
jPanel_PlayArea.add(playBlocks[i][j]);
}
}
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
public void start() {
//初始化游戲參數
score = 0;
isPause = false;
isEnd = false; //
jButton2.setText("暫停");
//初始化游戲區
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
playBlocks[i][j].setBackground(Color.lightGray);
playBlocks[i][j].setVisible(false);
}
}
//創建蛇身
snake = new SnakeBody();
//在游戲區內隨機放置食物
int x = (int) (Math.random() * ROWS);
int 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);
//初始化狀態提示區
jLabel2.setText(Integer.toString(score));
jLabel4.setText(Integer.toString(snake.throughbody));
jLabel2.setText(Integer.toString(snake.throughwall));
try {
//啟動游戲
thread.start();
}
catch (IllegalThreadStateException illegalThreadStateException) {}
}
class SnakeBody {
// private int snake[][] = new int [MainFrame.ROWS][MainFrame.COLS]; //0位背景,1為蛇身,2為食物,3為穿身,4為穿墻,5為墻壁
public int length = 3;
public int rows[];
public int columes[];
public int direction = RIGHT;
public int lastdirection = RIGHT;
public long speed = 300;
public int throughbody = 0;
public int throughwall = 0;
public SnakeBody() {
length = 3;
throughbody = 1;
throughwall = 1;
direction = RIGHT;
lastdirection = RIGHT;
switch(level){
case BEGINNER:speed = 300;break;
case MIDDLE:speed = 200;break;
case EXPERT:speed = 100;break;
default:speed = 200;
}
throughbody = 0;
throughwall = 0;
rows = new int[ROWS];
columes = new int[COLS];
for (int i = 0; i <= length; i++) {
rows[i] = 1;
columes[i] = length - i;
}
}
public void move() {
//去掉蛇尾
playBlocks[rows[length]][columes[length]].setVisible(false);
playBlocks[rows[length]][columes[length]].setBackground(Color.lightGray);
//顯示除蛇頭外蛇身
for (int i = 0; i < length; i++) {
playBlocks[rows[i]][columes[i]].setBackground(Color.green);
playBlocks[rows[i]][columes[i]].setVisible(true);
}
//移動除蛇頭外蛇身
for (int i = length; i > 0; i--) {
rows[i] = rows[i - 1];
columes[i] = columes[i - 1];
}
//根據蛇身運動方向,決定蛇頭位置
switch (direction) {
case UP: {
if (lastdirection == DOWN)
rows[0] += 1;
else {
rows[0] -= 1;
lastdirection = UP;
}
break;
}
case LEFT: {
if (lastdirection == RIGHT)
columes[0] += 1;
else {
columes[0] -= 1;
lastdirection = LEFT;
}
break;
}
case DOWN: {
if (lastdirection == UP)
rows[0] -= 1;
else {
rows[0] += 1;
lastdirection = DOWN;
}
break;
}
case RIGHT: {
if (lastdirection == LEFT)
columes[0] -= 1;
else {
columes[0] += 1;
lastdirection = RIGHT;
}
break;
}
}
//處理有穿墻寶物時的穿墻操作
if (throughwall != 0) {
if (rows[0] >= ROWS) {
throughwall--;
jLabel6.setText(Integer.toString(throughwall));
rows[0] = ROWS - rows[0];
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -