?? tetriscanvas.java
字號:
if (y < 0) {
gameState = GAME_OVER;
transmitMsg(GAME_OVER);
}
block.drawBlock(g);
block.drawNextBlock(g);
}
counter = 0;
}
else {
block.paint(g);
}
}
else if (gameState == GAME_OVER) {
g.setColor(BACKGROUND);
g.fillRect(0,0, getWidth(), getHeight());
g.setColor(255, 0, 0);
g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD,
Font.SIZE_LARGE));
g.drawString("mGame Over",
TetrisCanvas.GAMEAREA_X + 8 * TetrisCanvas.BRICK_WIDTH,
TetrisCanvas.GAMEAREA_Y + 4 * TetrisCanvas.BRICK_WIDTH,
g.BASELINE | g.HCENTER);
transmitMsg(GAME_OVER);
}
else if (gameState == GAME_WIN)
{
g.setColor(BACKGROUND);
g.fillRect(0,0, getWidth(), getHeight());
g.setColor(255, 0, 0);
g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD,
Font.SIZE_LARGE));
g.drawString("mYOU WIN",
TetrisCanvas.GAMEAREA_X + 8 * TetrisCanvas.BRICK_WIDTH,
TetrisCanvas.GAMEAREA_Y + 4 * TetrisCanvas.BRICK_WIDTH,
g.BASELINE | g.HCENTER);
}
if( gameState_Remote == GAME_OVER )
{
gameState = GAME_WIN;
}
//繪制遠端用戶的界面
if( !( Role.ROLE_SINGLE == role ) )
paint_Remote(g);
}//end paint
public void paint_Remote( Graphics g )
{
if( gameState == GAME_OVER )
{
gameState_Remote = GAME_WIN;
}
if (gameState_Remote == GAME_START_DEMO) { /* 游戲處于demo畫面狀態 */
if (!startDemoFlag_Remote) {
//游戲還沒開始,顯示demo畫面
g.setColor(BACKGROUND);
g.fillRect(0 , 0, mainWidth_Remote, mainHeight_Remote);
g.setColor(0, 255, 0);
g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD,
Font.SIZE_LARGE));
g.drawString("s俄羅斯方塊(遠端)", mainWidth_Remote / 2, mainHeight_Remote / 4 ,
g.BASELINE | g.HCENTER);
}
/* 游戲第一次啟動 */
}
else if (gameState_Remote == GAME_WAIT_REMOTE)
{
g.setColor(BACKGROUND);
g.fillRect(0, 0, mainWidth_Remote, mainHeight_Remote);
g.setColor(0, 255, 0);
g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD,
Font.SIZE_LARGE));
g.drawString("s等待(遠端)", mainWidth_Remote / 2, mainHeight_Remote / 4 ,
g.BASELINE | g.HCENTER);
}
else if (gameState_Remote == GAME_INIT) {
map_Remote.init();
map_Remote.paint(g);
block_Remote.init();
block_Remote.drawBlock(g);
block_Remote.drawNextBlock(g);
gameState_Remote = GAME_RUN;
}
else if (gameState_Remote == GAME_RUN) {
if( b_receviedMapdata )
{
map_Remote.repaintMap_Remote(g);
b_receviedMapdata = false;
}
if( b_receviedPN )
{
block_Remote.eraseBlock(g);
block_Remote.drawNextBlock(g);
b_receviedPN = false;
}
}//end if( game == GAME_RUN)
else if (gameState_Remote == GAME_OVER) {
g.setColor(BACKGROUND);
g.fillRect(0, 0,
mainWidth_Remote, mainHeight_Remote);
g.setColor(255, 0, 0);
g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD,
Font.SIZE_LARGE));
g.drawString("sGame Over",
GAMEAREA_X_REMOTE + 8 * BRICK_WIDTH_REMOTE,
GAMEAREA_Y_REMOTE + 4 * BRICK_WIDTH_REMOTE,
g.BASELINE | g.HCENTER);
}
else if( gameState_Remote == GAME_DISCONNECTED )
{
g.setColor(BACKGROUND);
g.fillRect(0, 0,
mainWidth_Remote, mainHeight_Remote);
g.setColor(255, 0, 0);
g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD,
Font.SIZE_LARGE));
g.drawString("sOTHER DISCONNECTED!",
GAMEAREA_X_REMOTE + 8 * BRICK_WIDTH_REMOTE,
GAMEAREA_Y_REMOTE + 4 * BRICK_WIDTH_REMOTE,
g.BASELINE | g.HCENTER);
}
else if( gameState_Remote == GAME_EXIT )
{
g.setColor(BACKGROUND);
g.fillRect(0, 0,
mainWidth_Remote, mainHeight_Remote);
g.setColor(255, 0, 0);
g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD,
Font.SIZE_LARGE));
g.drawString("sOTHER EXIT",
GAMEAREA_X_REMOTE + 8 * BRICK_WIDTH_REMOTE,
GAMEAREA_Y_REMOTE + 4 * BRICK_WIDTH_REMOTE,
g.BASELINE | g.HCENTER);
}
else if( gameState_Remote == GAME_WIN )
{
g.setColor(BACKGROUND);
g.fillRect(0, 0,
mainWidth_Remote, mainHeight_Remote);
g.setColor(255, 0, 0);
g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD,
Font.SIZE_LARGE));
g.drawString("sOTHER WIN",
GAMEAREA_X_REMOTE + 8 * BRICK_WIDTH_REMOTE,
GAMEAREA_Y_REMOTE + 4 * BRICK_WIDTH_REMOTE,
g.BASELINE | g.HCENTER);
}
System.gc();
}
public static void clear(Graphics g) {
// 繪制整個背景
g.setColor(0x00000000);
g.fillRect(0, offSet, mainWidth, mainHeight);
//繪制面板背景
g.setColor(BACKGROUND);
g.fillRect(TetrisCanvas.GAMEAREA_X, TetrisCanvas.GAMEAREA_Y,
16 * TetrisCanvas.BRICK_WIDTH, 16 * TetrisCanvas.BRICK_WIDTH);
}
public static void clear_Remote(Graphics g)
{
//繪制整個背景
g.setColor(0x00000000);
g.fillRect(0, 0, mainWidth_Remote, mainHeight_Remote);
//繪制面板背景
g.setColor(BACKGROUND);
g.fillRect(TetrisCanvas.GAMEAREA_X_REMOTE, GAMEAREA_Y_REMOTE,
16 * TetrisCanvas.BRICK_WIDTH_REMOTE, 16 * TetrisCanvas.BRICK_WIDTH_REMOTE);
}
public void sendPN()
{
transmitMsg( SEND_PN );
transmitMsg( block.getPattern());
transmitMsg( block.getNext());
}
public synchronized void transmitMsg(int action)
{
if( Role.ROLE_SINGLE == role )
return;
/*
* 如果遠端退出或斷開,則不必再傳送數據
*/
if( GAME_EXIT == gameState_Remote || GAME_DISCONNECTED == gameState_Remote )
return;
for( int i=0; i<btConnections.length; i++ )
{
try
{
OutputStream os = btConnections[i].getOutputStream();
os.write( action );
os.flush();
}
catch( IOException e )
{
btConnections[i].close();
if( checkIfAllClosed() )
{
return;
}
}
}
}
public void sendMapdata()
{
if( Role.ROLE_SINGLE == role )
return;
if( GAME_EXIT == gameState_Remote || GAME_DISCONNECTED == gameState_Remote )
return;
transmitMsg(SEND_MAPDATA);
for( int i=0; i<btConnections.length; i++ )
{
serialize(map,btConnections[i]);
}
}
public boolean checkIfAllClosed()
{
boolean allclosed = true;
for( int l=0; l<btConnections.length; l++ )
{
if( btConnections[l].isClosed() != true )
{
allclosed = false;
}
}
if( allclosed )
{
}
return allclosed;
}
/**
* The ReceiveThread is used to receive the remote keypresses. <br>
* For each remote device there exists an own RecieveThread.
*/
private class ReceiveThread
extends Thread
{
int index;
int receviedNum;
InputStream inputStream;
/**
* Constructor.
* @param i Index, that corresponds to the number of the BluetoothConnection.
*/
public ReceiveThread( int i )
{
// Store
index = i;
inputStream = btConnections[index].getInputStream();
}
/**
* Reads from stream until end of stream reached (disconnect).<br>
* The read character (which is the key the remote user pressed) is
* displayed to the local user.
*/
public void run()
{
// Read input stream (data from remote device)
int inp_Action;
while( true )
{
// Read (blocking)
try
{
//inp_Action = btConnections[index].getInputStream().read();
inp_Action = inputStream.read();
}
catch( IOException e )
{
btConnections[index].close();
checkIfAllClosed();
gameState_Remote = GAME_DISCONNECTED;
return;
}
if( inp_Action == -1 )
{
btConnections[index].close();
checkIfAllClosed();
bug.println("inp_action = -1, Remote user is disconnectioned!");
gameState_Remote = GAME_DISCONNECTED;
return;
}//end if
//add by wts 20060912
//這里應該分成四部分:
//一個是對傳入命令的解析(響應commandAction),
//一個是對按鍵的響應(action)
//一個是接收patter,next;
//一個是通知游戲結束狀態
switch( inp_Action )
{
case GAME_START_DEMO:
case GAME_WAIT:
case GAME_INIT:
case GAME_RUN:
case GAME_SUSPEND:
case GAME_OVER:
case GAME_EXIT:
updateRemoteGameState(inp_Action);
break;
case SEND_PN:
receivePN(inputStream, inp_Action);
break;
case Canvas.UP:
case Canvas.DOWN:
case Canvas.LEFT:
case Canvas.RIGHT:
case Canvas.FIRE:
//什么都不做
break;
case SEND_MAPDATA:
recevieMapdata();
default:
}
if( GAME_EXIT == gameState_Remote )
{
bug.println("Remote have exited!, ReceiveThread won't recevie data!");
break;
}
}// end while
}// end run
}// end ReceiveThread
//接收到的是遠端用戶的新的狀態
public synchronized void updateRemoteGameState( int _gameState )
{
//如果對方發送來GAME_WAIT狀態信息,則證明自己還沒有點擊"開始"鍵
//(僅建立連接)
if ( _gameState == GAME_WAIT )
{
gameState_Remote = GAME_WAIT_REMOTE;
return;
}else if( _gameState == GAME_INIT )
{
gameState = GAME_INIT;
gameState_Remote = GAME_INIT;
sendPN();
return;
}
gameState_Remote = _gameState;
}//end updateRemoteCommand
//判斷是否接收到新的當前方塊和下一個方塊
boolean b_receviedPN ;
public synchronized boolean receivePN( InputStream inputStream, int inp_Action)
{
if( inp_Action == this.SEND_PN )
{
try
{
pattern_Remote = inputStream.read();
next_Remote = inputStream.read();
}
catch(IOException e )
{
try {
throw new Exception("ERROR In read Remote pattern and next");
} catch (Exception e1) {
e1.printStackTrace();
}
}
block_Remote.setPN(pattern_Remote,next_Remote);
block_Remote.init();
bug.println(" receivePN.recevied PN");
b_receviedPN = true;
return true;
}
return false;
}
boolean b_receviedMapdata ;
public void recevieMapdata()
{
//能設置一個變量進行檢測最好,
bug.println("begin receviedMapdata");
deserialize(map_Remote,btConnections[0]);
b_receviedMapdata = true;
bug.println("end receviedMapdata");
}
public void serialize(Serialization serialization,
BluetoothConnection bluetoothConnection)
{
bug.println("beging serialize mapdata");
OutputStream os = bluetoothConnection.getOutputStream();
try {
data = serialization.serialize();
//數據傳輸:
//1、把數據序列化后的長度傳過去
//2、byte數組傳過去。
bluetoothConnection.writeInt(data.length);
bug.println("send mapdata length = " + data.length);
os.write(data);
os.flush();
data = null;
} catch (IOException e) {
System.out.println("Error coming from serialize");
}
}
public void deserialize(Serialization serialization,
BluetoothConnection bluetoothConnection)
{
InputStream is = bluetoothConnection.getInputStream();
try {
int length = bluetoothConnection.readInt();
bug.println("recevied data length = " + length);
data = new byte[length];
is.read(data);
serialization.deserialize(data);
data = null;
System.gc();
} catch (IOException e) {
System.out.println("Error:Coming from deserizlize");
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -