📄 mazefog2.java
字号:
big, we have to pick one direction over the other. How much we can round the corners is limited by <boardBorderSize> because even though the user's position appears as a circle, this circle is shifted around the display inside a square box, and the corners of that box might clip lines in the maze. */ if (Math.abs(userColOffset) > Math.abs(userRowOffset)) userRowOffset = 0; // keep larger horizontal offset else userColOffset = 0; // keep larger vertical offset } } else if (userRowOffset > 0) { /* Left is empty. There is a pixel offset trying to move down. */ if (boardTop[userRow + 1][userCol] != LineEMPTY) // can we go down? userRowOffset = 0; // cancel vertical pixel offset else if ((Math.abs(userColOffset) > boardBorderSize) || (Math.abs(userRowOffset) > boardBorderSize)) { /* Rounding a corner, select one direction over the other. */ if (Math.abs(userColOffset) > Math.abs(userRowOffset)) userRowOffset = 0; // keep larger horizontal offset else userColOffset = 0; // keep larger vertical offset } } } else if (userColOffset > 0) { /* There is a pixel offset trying to move right. There may also be a vertical offset (not yet tested). */ if (boardLeft[userRow][userCol + 1] != LineEMPTY) { /* There is a wall to the right of us. No right movement allowed. */ userColOffset = 0; // cancel horizontal pixel offset } else if (userRowOffset < 0) { /* Right is empty. There is a pixel offset trying to move up. */ if (boardTop[userRow][userCol] != LineEMPTY) // can we go up? userRowOffset = 0; // cancel vertical pixel offset else if ((Math.abs(userColOffset) > boardBorderSize) || (Math.abs(userRowOffset) > boardBorderSize)) { /* Rounding a corner, select one direction over the other. */ if (Math.abs(userColOffset) > Math.abs(userRowOffset)) userRowOffset = 0; // keep larger horizontal offset else userColOffset = 0; // keep larger vertical offset } } else if (userRowOffset > 0) { /* Right is empty. There is a pixel offset trying to move down. */ if (boardTop[userRow + 1][userCol] != LineEMPTY) // can we go down? userRowOffset = 0; // cancel vertical pixel offset else if ((Math.abs(userColOffset) > boardBorderSize) || (Math.abs(userRowOffset) > boardBorderSize)) { /* Rounding a corner, select one direction over the other. */ if (Math.abs(userColOffset) > Math.abs(userRowOffset)) userRowOffset = 0; // keep larger horizontal offset else userColOffset = 0; // keep larger vertical offset } } } /* The complex corner cases have already been done. Now check for the simple vertical-only cases. */ if (userRowOffset < 0) { /* There is a pixel offset trying to move up. */ if (boardTop[userRow][userCol] != LineEMPTY) userRowOffset = 0; // cancel vertical pixel offset } else if (userRowOffset > 0) { /* There is a pixel offset trying to move down. */ if (boardTop[userRow + 1][userCol] != LineEMPTY) userRowOffset = 0; // cancel vertical pixel offset } /* Did the user reach the exit? If so, then announce the success. If not, just draw or redraw the user's position normally. */ if (boardDistance[userRow][userCol] < 2) { gameState = GameFINISH; // no more moves allowed messageText.setText("You're there! You reached the exit! Click \"New Game\" to play again."); userColOffset = userRowOffset = 0; // cancel any pixel position offsets if (winSound != null) // if we were able to load winning sound clip winSound.play(); // play this sample sound from the Java SDK boardPaint(gr); // shows everything since state = GameFINISH flashBoardUser(gr, userRow, userCol, userRowOffset, userColOffset); } else { /* Draw the user's new board position by copying pixels from the old rectangular area to the new location. Then erase anything left behind. This avoids display flicker caused by erasing and redrawing something many times. */ userNewX = (userCol * boardGridSize) + boardBorderSize + boardLineWidth + canvasBorder + userColOffset; userNewY = (userRow * boardGridSize) + boardBorderSize + boardLineWidth + canvasBorder + userRowOffset; hz = userNewX - userOldX; // horizontal displacement vt = userNewY - userOldY; // vertical displacement if ((hz != 0) || (vt != 0)) { gr.copyArea(userOldX, userOldY, boardSymbolSize, boardSymbolSize, hz, vt); gr.setColor(BACKGROUND); // color to erase piece of old location if ((Math.abs(hz) >= boardSymbolSize) || (Math.abs(vt) >= boardSymbolSize)) { /* The old and new rectangles don't overlap, so we can just clear the old location to the background color. This will happen occasionally with really wild mouse movements, such as when the user moves the mouse up to the control buttons. */ gr.fillRect(userOldX, userOldY, boardSymbolSize, boardSymbolSize); } else { /* Old and new rectangles overlap. Clear edges in the opposite direction from the movement. */ if (hz < 0) // move left means clean right gr.fillRect(userNewX + boardSymbolSize, userOldY, -hz, boardSymbolSize); else if (hz > 0) // move right means clean left gr.fillRect(userOldX, userOldY, hz, boardSymbolSize); if (vt < 0) // move up means clean down gr.fillRect(userOldX, userNewY + boardSymbolSize, boardSymbolSize, -vt); else if (vt > 0) // move down means clean up gr.fillRect(userOldX, userOldY, boardSymbolSize, vt); } } else { /* User didn't change position, so nothing to move or redraw. */ } /* Moving to a new position may reveal some lines that were previously hidden. */ makeLinesVisible(gr, userRow, userCol, true); } gr.dispose(); // release graphics context } } // end of boardMouseMoved() method/* boardPaint() method This method is called to redraw the entire game board. It is called by our dummy Canvas class (MazeFog2Board), boardMouseMoved(), keyPressed(), and by showExitPath(). Paint() methods are called when a window is first created, gets resized, or needs to be redrawn after being overwritten. We can't assume that anything we already drew is still on the display. */ void boardPaint( Graphics gr) // graphics context for <boardCanvas> { int boardHeight; // height (in pixels) of actual game board int boardWidth; // width (in pixels) of actual game board int col; // temporary column number (index) int row; // temporary row number (index) /* Erase the canvas to have our desired background color. */ boardHeight = boardCanvas.getSize().height; // for JDK1.1 boardWidth = boardCanvas.getSize().width; // for JDK1.1 gr.setColor(BACKGROUND); // clear board to background color gr.fillRect(0, 0, boardWidth, boardHeight); /* If the game board has been created, then display visible board lines and special board squares. */ if (boardDistance != null) // is there a game board? { /* Display the visible horizontal lines. */ for (row = 0; row <= numRows; row ++) for (col = 0; col < numCols; col ++) { switch (boardTop[row][col]) { case LineEMPTY: // empty line, do nothing break; case LineHIDDEN: // hidden line if (gameState != GameACTIVE) drawBoardTopLine(gr, row, col, ColorLINE); break; case LineVISIBLE: // visible line drawBoardTopLine(gr, row, col, ColorLINE); break; default: System.out.println("error in boardPaint(): bad boardTop[" + row + "][" + col + "] = " + boardTop[row][col]); } } /* Display the visible vertical lines. */ for (row = 0; row < numRows; row ++) for (col = 0; col <= numCols; col ++) { switch (boardLeft[row][col]) { case LineEMPTY: // empty line, do nothing break; case LineHIDDEN: // hidden line if (gameState != GameACTIVE) drawBoardLeftLine(gr, row, col, ColorLINE); break; case LineVISIBLE: // visible line drawBoardLeftLine(gr, row, col, ColorLINE); break; default: System.out.println("error in boardPaint(): bad boardLeft[" + row + "][" + col + "] = " + boardLeft[row][col]); } } /* Display special board squares. */ drawBoardExit(gr, exitRow, exitCol); // mark exit on game board drawBoardUser(gr, userRow, userCol, userRowOffset, userColOffset, ColorUSER); // mark user's position } } // end of boardPaint() method/* doBiggerButton() method The user clicked the "Bigger" button for bigger board squares, or typed an equivalent keyboard mnemonic.*/ void doBiggerButton() { if (sizeIndex < (sizeList.length - 1)) sizeIndex ++; // go to a bigger size, if there is one doNewgameButton(); // and start a new game } // end of doBiggerButton() method/* doNewgameButton() method The user clicked the "New Game" button to start a new game, or typed an equivalent keyboard mnemonic.*/ void doNewgameButton() { setBoardSizes(); // set sizes of board elements makeBoard(true); // make maze, draw lines as we create boardCanvas.repaint(); // redraw the game board boardCanvas.requestFocus(); // set focus so we can listen for arrow keys gameState = GameACTIVE; // let the game begin messageText.setText(noMessage); // clear any previous message text } // end of doNewgameButton() method/* doShowmeButton() method The user clicked the "Show Me" button to see the solution, or typed an equivalent keyboard mnemonic.*/ void doShowmeButton() { gameState = GameFINISH; // not allowed to move after seeing solution showExitPath(); // animate the path to the exit boardCanvas.requestFocus(); // set focus so we can listen for arrow keys messageText.setText("Click the \"New Game\" button to play again."); } // end of doShowmeButton() method
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -