📄 board.java
字号:
} g.setColor(next_brick.color); g.drawString(java.util.ResourceBundle.getBundle( "TetrisApplet/TetrisApplet").getString("Next_Block"), msgX, 4*BORDER); next_brick.paint(g, msgX, 5*BORDER); } /** * 绘制当前得分 */ private void drawScore(Graphics g) { g.setColor(Color.RED); g.drawString(java.util.ResourceBundle.getBundle( "TetrisApplet/TetrisApplet").getString("Score") + ": " + score, msgX, 1*BORDER); } /** * 创建下一个新的砖块形状、角度和颜色, * 角度,形状和颜色随机产生 */ private void createNewBrick() { brick.reset(next_brick.getShape(), next_brick.getAngle(), next_brick.color); brick.X = (int)MAX_COLS/2; brick.Y = 0; brick.color_index = next_brick.color_index; //随机获取颜色 int color_index = (int)Math.round(Math.random() * (MAX_COLOR_NUM-1)); //随机获取形状 int shape = (int)Math.round(Math.random() * (Brick3D.MAX_SHAPE_NUM-1)); //随机获取角度 int angle = (int)Math.round(Math.random() * 3); next_brick.reset(shape, angle, colors[color_index]); next_brick.color_index = color_index; } /** * 根据当前的brick_info数组,更新board_info数组, */ private void updateBoardInfo(boolean clear) { int[][] si = brick.getShapeInfo(); for (int i=0;i<4;i++) { for(int j=0;j<4;j++) { if (si[i][j] != 0) { board_info[brick.Y+i][brick.X+j]= (clear?BLANK_CELL:brick.color_index); } } } if (!clear) { repaint(); } } /** * 判断是否可以移动砖块 * 当砖块超过边界时,就返回false,拒绝移动 */ private boolean canMove(int newX, int newY) { //判断不能超过左边边界 if ((newX < 0) ||(newY>=MAX_ROWS)) { return false; } //判断不能和其他砖块重叠、超过底部和右边边界 int[][] si = brick.getShapeInfo(); for (int i=0;i<4;i++) { for(int j=0;j<4;j++) { if (si[i][j] != 0) { if (//判断不能超过右边边界 ((newX + j) >= MAX_COLS) || //判断不能超过底部边界 ((newY + i) >= MAX_ROWS) || //判断不能和其他砖块重叠 (board_info[newY+i][newX+j]!=BLANK_CELL) ) { return false; } } } } return true; } /** * 移动砖块,1表示向右移动,-1表示向左移动 */ private void move(int dx) { if (brick.X != -1) { updateBoardInfo(true); if (canMove(brick.X+dx, brick.Y)) { brick.X += dx; } updateBoardInfo(false); } } /** * 砖块的自由下落函数 */ private void drop() { if (brick.X == -1) { return; } //清除原内容 updateBoardInfo(true); if (canMove(brick.X, brick.Y+1)) { brick.Y+=1; } else { if (brick.Y==0) { //游戏结束 gameTerminated = true; } else { reachBottom = true; } } //更新当前内容 updateBoardInfo(false); //当到达底部时,检查是否可以消行 if (reachBottom) { checkRow(); } } /** * 检查是否可以消行,并计算分数 */ private void checkRow() { boolean full_row, full_color; //连续的行数 int full_lines = 0; //每次加分的基本分 final int base_score = 100; //本次的加分和 int score = 0; for (int i=MAX_ROWS-1;i>=0;i--) { full_row = true; full_color = true; for (int j=MAX_COLS-1;j>=0;j--) { //检查是否本行颜色相同 if ((j>0)&&(board_info[i][j] !=board_info[i][j-1])) { full_color = false; } //检查是否有空格子 if (board_info[i][j]==BLANK_CELL) { full_row = false; break; } } if (full_row) { //本行已填满 full_lines++; if (full_color) { //当颜色相同时,加分是普通的四倍 score += 4*base_score; } else { score += base_score; } //将上面的行向下移动 for (int row=i;row>0;row--) { board_info[row] = board_info[row-1]; } //清除最上面的一行 int[] top_line = new int[MAX_COLS]; for (int j=MAX_COLS-1;j>=0;j--) { top_line[j]=BLANK_CELL; } board_info[0] = top_line; //确保再次扫描本行 i++; } else { //不再是连续行了 if (full_lines>0) { score *= full_lines; } full_lines=0; if (score>0) { this.score +=score; score = 0; //根据积分修改游戏速度 //1500 - (int)(this.score*100/1000) = //1500 - (int)(this.score/10) delay = 1500 - (int)(this.score/10); } } } } /** * 旋转砖块,并更新board_info数组 */ private void rotate() { if (brick.X != -1) { updateBoardInfo(true); brick.rotate(Brick3D.ROTATION_90); if (!canMove(brick.X, brick.Y)) { //取消旋转 brick.rotate(Brick3D.ROTATION_270); } updateBoardInfo(false); } } //*****************Protected成员函数定义************************************** /** * 处理键盘事件 * 这里处理如下几个控制键: * <p> * <ol> * <li>左键 - 向左移动砖块</li> * <li>右键 - 向右移动砖块</li> * <li>上键 - 旋转砖块</li> * <li>下键 - 快速放下砖块</li> * <li>F3 - 开始游戏</li> * <li>F4 - 结束游戏</li> * </ol> */ protected void processKeyEvent(KeyEvent e) { if (e.getID() == KeyEvent.KEY_PRESSED ) { switch(e.getKeyCode()) { case KeyEvent.VK_LEFT: move(-1); break; case KeyEvent.VK_RIGHT: move(1); break; case KeyEvent.VK_DOWN: drop(); break; case KeyEvent.VK_UP: if (brick != null) { rotate(); } break; case KeyEvent.VK_F3: newGame(); break; case KeyEvent.VK_F4: endGame(); break; } } } /** * 处理鼠标事件,控制旋转和左右移动 * <p> * <ol> * <li>左键 - 向左移动</li> * <li>右键 - 向右移动</li> * <li>中键 - 旋转砖块</li> * </ol> */ protected void processMouseEvent(MouseEvent e) { super.processMouseEvent(e); if (e.getID() == MouseEvent.MOUSE_CLICKED) { grabFocus(); if (brick == null) { return; } if (e.getButton() == MouseEvent.BUTTON1) { move(-1); } else if (e.getButton() == MouseEvent.BUTTON2) { rotate(); } else if (e.getButton() == MouseEvent.BUTTON3) { move(1); } } } /** * 处理鼠标滚轮事件,当向下滚动时,控制砖块向下移动 */ protected void processMouseWheelEvent( java.awt.event.MouseWheelEvent e) { super.processMouseWheelEvent(e); if ((brick != null)&&(e.getWheelRotation()>0)) { drop(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -