📄 bricklist.java
字号:
package pinball;import javax.microedition.lcdui.Graphics;/** * * <p>Title: BrickList</p> * * <p>Description: 砖块的集合类</p> * */public class BrickList { public static final int NORTH = 0; public static final int SOUTH = 1; public static final int WEST = 2; public static final int EAST = 3; public final static int XOFFSET; public final static int YOFFSET; static { int w = Engine.PATTERN_WIDTH * Brick.WIDTH; //方块的宽度 int g = (Engine.PATTERN_WIDTH - 1) * Brick.GAP; //方块的间隔 XOFFSET = (Screen.width - (w + g)) / 2 - 1; //开始绘制方块的点的x坐标 YOFFSET = Screen.height / 8; //开始绘制方块的点的y坐标 } private Brick[] list; //砖块列表 private int columns; //列数 private int[] recent_collision; Sprite inter; private int[] step = { 0, -1, 0, 1, -1, 0, 1, 0 }; private int[] all_step = { 0, 0, 0, -1, 1, 0, 0, 1, 0, 1, -1, 0, -1, 0, 0, -1, 0, -1 }; private ThreeDColor[] rainbow = { ThreeDColor.purple, ThreeDColor.purple, ThreeDColor.blue, ThreeDColor.blue, ThreeDColor.red, ThreeDColor.red, ThreeDColor.orange, ThreeDColor.orange, }; public BrickList(int[] type_list, int pattern_width, int level) { int x = XOFFSET; int y = YOFFSET - (Brick.HEIGHT + Brick.GAP); int n = -1; list = new Brick[type_list.length]; //创建方块数组 recent_collision = new int[list.length]; inter = new Sprite(); for (int i = 0; i < list.length; i++) { if (i % pattern_width == 0) { //开始新的行 //确定当前行的列数 columns = (x - XOFFSET) / (Brick.WIDTH + Brick.GAP); x = XOFFSET; y += Brick.HEIGHT + Brick.GAP; n++; } list[i] = new Brick(this, x, y, i, type_list[i]); //创建方块 if (level == 0 && list[i].getType() == Brick.STANDARD) { list[i].setColor(rainbow[n]); } x += Brick.WIDTH + Brick.GAP; } } public Brick getBrickAt(int n) { if (n < 0 || n >= list.length) { return null; } return list[n]; } public void moveBrick(int from, int to) { int to_x = list[to].x; int to_y = list[to].y; list[to] = new Brick(list[from]); list[to].moveTo(to_x, to_y); list[to].setPos(to); list[from].erase(Screen.GRAPHICS); list[from].clear(); list[to].paintShadow(Screen.GRAPHICS); list[to].paint(Screen.GRAPHICS); } /** * 碰撞检测 */ public int checkForCollision(Ball ball) { Brick brick; int xw, yh, oxw, oyh; int x, y, score, dir; int width = Engine.PATTERN_WIDTH; int height = list.length / Engine.PATTERN_WIDTH; boolean intersects; score = dir = 0; //得到小球所对应的方块的列位置 x = (ball.getCenterX() - XOFFSET) / (Brick.WIDTH + Brick.GAP); if (x < 0 || x >= width) { //不在方块范围内 return 0; } //得到小球所对应的方块的行位置 y = (ball.getCenterY() - YOFFSET) / (Brick.HEIGHT + Brick.GAP); if (y < 0 || y >= height) { //不在方块范围内 return 0; } /*********************************** (-1, -1) ( 0, -1) ( 1, -1) (-1, 0) ( 0, 0) ( 1, 0) (-1, 1) ( 0, 1) ( 1, 1) **********************************/ //以九宫格的方式检测小球与方块是否发生了碰撞 for (int i = 0; i < all_step.length / 2; i++) { x += all_step[2 * i + 0]; // y += all_step[2 * i + 1]; if (x < 0 || x >= width || y < 0 || y >= height) { continue; } int n = y * Engine.PATTERN_WIDTH + x; brick = list[n]; xw = ball.x + ball.width; //小球的最左边x坐标 yh = ball.y + ball.height; //小球的最下边y坐标 oxw = brick.x + brick.width; //方块的最左边x坐标 oyh = brick.y + brick.height; //方块的最下边y坐标 intersects = (ball.x >= brick.x && ball.x < oxw && ball.y >= brick.y && ball.y < oyh) || (xw >= brick.x && xw < oxw && ball.y >= brick.y && ball.y < oyh) || (ball.x >= brick.x && ball.x < oxw && yh >= brick.y && yh < oyh) || (xw >= brick.x && xw < oxw && yh >= brick.y && yh < oyh); if (intersects) { if (recent_collision[n] == 0) { ball.bounce(brick); } if (ball.getCenterX() < brick.x) dir = EAST; if (ball.getCenterX() > brick.x + Brick.WIDTH) dir = WEST; if (ball.getCenterY() < brick.y) dir = SOUTH; if (ball.getCenterY() > brick.y + Brick.HEIGHT) dir = NORTH; if (brick.getType() != Brick.SLIDE) brick.erase(Screen.GRAPHICS); score += brick.hit(dir); recent_collision[n] = 2; break; //每次仅仅测试与一个方块的碰撞 } else if (recent_collision[n] > 0) { recent_collision[n]--; } } return score; } public boolean isClean() { for (int i = 0; i < list.length; i++) { if (list[i].getType() == Brick.STANDARD) { return false; } } return true; } public Brick getNeighbor(Brick brick, int direction) { int i; if (brick == null) { return null; } for (i = 0; i < list.length; i++) { if (list[i] == brick) { break; } } if (i == list.length) { return null; } int x = i % columns; int y = i / columns; int dx = step[2 * direction]; int dy = step[2 * direction + 1]; x += dx; y += dy; if (x < 0 || x >= columns || y < 0 || y >= list.length / columns) { return null; } return list[y * columns + x]; } public void paintShadow(Graphics g) { for (int i = 0; i < list.length; i++) { list[i].paintShadow(g); } } public void paint(Graphics g) { for (int i = 0; i < list.length; i++) { list[i].paint(g); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -