導航:首頁 > 編程語言 > JAVABT游戲

JAVABT游戲

發布時間:2024-05-31 09:47:27

⑴ 鎺ㄨ崘鍑犱釜濂界帺鐨勬垬鐣ユ父鎴忚丣AVA鐨勶紒

濉旈槻錛堝緩鍩庯級鍨嬬瓥鐣ワ細甯濆浗鏃朵唬銆佸笣鍥芥椂浠3銆佸笣鍥芥椂浠d箣浜氭床鐜嬫湞銆佺孩鑹茶︽垝3銆佸啗欖傘佸啗欖2銆佸啗欖3鐕冪儳鐨勭兢宀涖
鍥炲悎鍨嬬瓥鐣ワ細鑻遍泟鏃朵唬銆佽繙鍙ゅ笣鍥姐佽繙鍙ゅ笣鍥2
閲嶇偣鎺ㄨ崘榪滃彜甯濆浗浜岋紝澶狟T浜嗭紝絎鍏鍏沖ぉ鍫備箣鎬掗毦錛岄毦錛岄毦錛

java小游戲編程

/**
* File: ControlPanel.java
* User:相思無償
* Date: 2004.12.3
* Describe: 俄羅斯方塊的 Java 實現
*/

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;
import java.awt.*;
import java.awt.event.*;

/**
* 控制面板類,繼承自JPanel.
* 上邊安放預顯窗口、等級、得分、控制按鈕
* 主要用來控制游戲進程。
*/
class ControlPanel extends JPanel {
private JTextField
tfLevel = new JTextField("" + ErsBlocksGame.DEFAULT_LEVEL),
tfScore = new JTextField("0");

private JButton
btPlay = new JButton("Play"),
btPause = new JButton("Pause"),
btStop = new JButton("Stop"),
btTurnLevelUp = new JButton("Turn hard"),
btTurnLevelDown = new JButton("Turn easy");

private JPanel plTip = new JPanel(new BorderLayout());
private TipPanel plTipBlock = new TipPanel();
private JPanel plInfo = new JPanel(new GridLayout(4, 1));
private JPanel plButton = new JPanel(new GridLayout(5, 1));

private Timer timer;
private ErsBlocksGame game;

private Border border = new EtchedBorder(
EtchedBorder.RAISED, Color.white, new Color(148, 145, 140));

/**
* 控制面板類的構造函數
* @param game ErsBlocksGame, ErsBoxesGame類的一個實例引用,
* 方便直接控制ErsBoxesGame類的行為。
*/
public ControlPanel(final ErsBlocksGame game) {
setLayout(new GridLayout(3, 1, 0, 4));
this.game = game;

plTip.add(new JLabel("Next block"), BorderLayout.NORTH);
plTip.add(plTipBlock);
plTip.setBorder(border);

plInfo.add(new JLabel("Level"));
plInfo.add(tfLevel);
plInfo.add(new JLabel("Score"));
plInfo.add(tfScore);
plInfo.setBorder(border);

tfLevel.setEditable(false);
tfScore.setEditable(false);

plButton.add(btPlay);
plButton.add(btPause);
plButton.add(btStop);
plButton.add(btTurnLevelUp);
plButton.add(btTurnLevelDown);
plButton.setBorder(border);

add(plTip);
add(plInfo);
add(plButton);

addKeyListener(new ControlKeyListener());

btPlay.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
game.playGame();
}
});
btPause.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
if (btPause.getText().equals(new String("Pause"))) {
game.pauseGame();
} else {
game.resumeGame();
}
}
});
btStop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
game.stopGame();
}
});
btTurnLevelUp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
try {
int level = Integer.parseInt(tfLevel.getText());
if (level < ErsBlocksGame.MAX_LEVEL)
tfLevel.setText("" + (level + 1));
} catch (NumberFormatException e) {
}
requestFocus();
}
});
btTurnLevelDown.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
try {
int level = Integer.parseInt(tfLevel.getText());
if (level > 1)
tfLevel.setText("" + (level - 1));
} catch (NumberFormatException e) {
}
requestFocus();
}
});

addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent ce) {
plTipBlock.fanning();
}
});

timer = new Timer(500, new ActionListener() {
public void actionPerformed(ActionEvent ae) {
tfScore.setText("" + game.getScore());
int scoreForLevelUpdate =
game.getScoreForLevelUpdate();
if (scoreForLevelUpdate >= ErsBlocksGame.PER_LEVEL_SCORE
&& scoreForLevelUpdate > 0)
game.levelUpdate();
}
});
timer.start();
}

/**
* 設置預顯窗口的樣式,
* @param style int,對應ErsBlock類的STYLES中的28個值
*/
public void setTipStyle(int style) {
plTipBlock.setStyle(style);
}

/**
* 取得用戶設置的游戲等級。
* @return int, 難度等級,1 - ErsBlocksGame.MAX_LEVEL
*/
public int getLevel() {
int level = 0;
try {
level = Integer.parseInt(tfLevel.getText());
} catch (NumberFormatException e) {
}
return level;
}

/**
* 讓用戶修改游戲難度等級。
* @param level 修改後的游戲難度等級
*/
public void setLevel(int level) {
if (level > 0 && level < 11) tfLevel.setText("" + level);
}

/**
* 設置"開始"按鈕的狀態。
*/
public void setPlayButtonEnable(boolean enable) {
btPlay.setEnabled(enable);
}

public void setPauseButtonLabel(boolean pause) {
btPause.setText(pause ? "Pause" : "Continue");
}

/**
* 重置控制面板
*/
public void reset() {
tfScore.setText("0");
plTipBlock.setStyle(0);
}

/**
* 重新計算TipPanel里的boxes[][]里的小框的大小
*/
public void fanning() {
plTipBlock.fanning();
}

/**
* 預顯窗口的實現細節類
*/
private class TipPanel extends JPanel {
private Color backColor = Color.darkGray, frontColor = Color.lightGray;
private ErsBox[][] boxes =
new ErsBox[ErsBlock.BOXES_ROWS][ErsBlock.BOXES_COLS];

private int style, boxWidth, boxHeight;
private boolean isTiled = false;

/**
* 預顯窗口類構造函數
*/
public TipPanel() {
for (int i = 0; i < boxes.length; i++) {
for (int j = 0; j < boxes[i].length; j++)
boxes[i][j] = new ErsBox(false);
}
}

/**
* 預顯窗口類構造函數
* @param backColor Color, 窗口的背景色
* @param frontColor Color, 窗口的前景色
*/
public TipPanel(Color backColor, Color frontColor) {
this();
this.backColor = backColor;
this.frontColor = frontColor;
}

/**
* 設置預顯窗口的方塊樣式
* @param style int,對應ErsBlock類的STYLES中的28個值
*/
public void setStyle(int style) {
this.style = style;
repaint();
}

/**
* 覆蓋JComponent類的函數,畫組件。
* @param g 圖形設備環境
*/
public void paintComponent(Graphics g) {
super.paintComponent(g);

if (!isTiled) fanning();

int key = 0x8000;
for (int i = 0; i < boxes.length; i++) {
for (int j = 0; j < boxes[i].length; j++) {
Color color = (((key & style) != 0) ? frontColor : backColor);
g.setColor(color);
g.fill3DRect(j * boxWidth, i * boxHeight,
boxWidth, boxHeight, true);
key >>= 1;
}
}
}

/**
* 根據窗口的大小,自動調整方格的尺寸
*/
public void fanning() {
boxWidth = getSize().width / ErsBlock.BOXES_COLS;
boxHeight = getSize().height / ErsBlock.BOXES_ROWS;
isTiled = true;
}
}

private class ControlKeyListener extends KeyAdapter {
public void keyPressed(KeyEvent ke) {
if (!game.isPlaying()) return;

ErsBlock block = game.getCurBlock();
switch (ke.getKeyCode()) {
case KeyEvent.VK_DOWN:
block.moveDown();
break;
case KeyEvent.VK_LEFT:
block.moveLeft();
break;
case KeyEvent.VK_RIGHT:
block.moveRight();
break;
case KeyEvent.VK_UP:
block.turnNext();
break;
default:
break;
}
}
}
}

閱讀全文

與JAVABT游戲相關的資料

熱點內容
自己購買雲主伺服器推薦 瀏覽:422
個人所得稅java 瀏覽:761
多餘的伺服器滑道還有什麼用 瀏覽:192
pdf劈開合並 瀏覽:28
不能修改的pdf 瀏覽:752
同城公眾源碼 瀏覽:489
一個伺服器2個埠怎麼映射 瀏覽:298
java字元串ascii碼 瀏覽:79
台灣雲伺服器怎麼租伺服器 瀏覽:475
旅遊手機網站源碼 瀏覽:332
android關聯表 瀏覽:946
安卓導航無聲音怎麼維修 瀏覽:333
app怎麼裝視頻 瀏覽:431
安卓系統下的軟體怎麼移到桌面 瀏覽:96
windows拷貝到linux 瀏覽:772
mdr軟體解壓和別人不一樣 瀏覽:904
單片機串列通信有什麼好處 瀏覽:340
游戲開發程序員書籍 瀏覽:860
pdf中圖片修改 瀏覽:288
匯編編譯後 瀏覽:491