j2me设计论文-俄罗斯方块内容摘要:

((leftpre_left)*box_size, (toppre_top)*box_size)。 (g, box_size, [colorIndex])。 } else { // just changed: (left*box_size, top*box_size)。 (g, box_size)。 // draw new position: (g, box_size, [colorIndex])。 // make prev=current until next change: previousShape=currentShape。 } } public Shape getNextShape() { return [nextShapeIndex]。 }}/*package。 import .*。 /** * To represent the container of all boxes.*/public final class Background implements Storable { public static final int EMPTY = 99。 // holds width amp。 height: private int width。 private int height。 // holds boxes, EMPTY or color_index: private int[][] boxes。 // holds the rows that can be removed. private boolean[] removingRows。 // make sure cannot instanciate from outside: public Background(int width, int height) { = width。 = height。 = new boolean[height]。 = new int[width][height]。 } public int getWidth() { return。 } public int getHeight() { return。 } /** * Clean all boxes and can start a new game. */ public void init() { for(int i=0。 iwidth。 i++) { for(int j=0。 jheight。 j++) boxes[i][j] = EMPTY。 } for(int i=0。 iheight。 i++) removingRows[i] = false。 } public byte[] getRecordData() { byte[] data = new byte[width*height]。 int offset = 0。 for(int r=0。 rheight。 r++) { for(int c=0。 cwidth。 c++) { data[offset] = (byte)boxes[c][r]。 offset++。 } } return data。 } public int setRecordData(byte[] data, int offset) { if(()width*height) return (1)。 for(int r=0。 rheight。 r++) { for(int c=0。 cwidth。 c++) { boxes[c][r] = data[offset]。 offset++。 } } return width*height。 } /** * Detect if there is a collition between two 4x4 area. * @param active The ActiveShape object. * @return True if there is a collition. */ public boolean collidesWith(ActiveShape active) { int[] data = ().getData()。 int left = ()。 int top = ()。 for(int i1=top, i2=0。 i1top+4。 i1++, i2++) { for(int j1=left, j2=0。 j1left+4。 j1++, j2++) { if(i1=0 amp。 amp。 i1height amp。 amp。 j1=0 amp。 amp。 j1width) if(data[4*i2+j2]==1 amp。 amp。 boxes[j1][i1]!=EMPTY) return true。 } } return false。 } public int[][] getBoxes() { return boxes。 } /** * Merge the ActiveShape to the Background. * @param active The ActiveShape object. */ public void merge(ActiveShape active) { int left = ()。 int top = ()。 int box。 for(int i=0。 i4。 i++) { for(int j=0。 j4。 j++) { box = ().getData()[i*4+j]。 if(box==1) { [left+j][top+i] = ()。 } } } } /** * Mark all rows that can be removed. */ public boolean[] markRemovingRows() { // mark all rows if it can be removed: for(int i=0。 iheight。 i++) { removingRows[i] = true。 for(int j=0。 jwidth。 j++) { if(boxes[j][i]==EMPTY) { removingRows[i] = false。 break。 } } } return removingRows。 } public int doRemove() { int r = height 1。 int i = height 1。 for(i=height1。 i=0。 i) { while(r=0 amp。 amp。 removingRows[r]) r。 if(r==(1)) break。 copyRow(r, i)。 r。 } for(int j=i。 j=0。 j) for(int n=0。 nwidth。 n++) boxes[n][j] = EMPTY。 return i+1。 } private void copyRow(int src, int dest) { for(int j=0。 jwidth。 j++) boxes[j][dest] = boxes[j][src]。 } private void moveDown(int removing) { for(int i=removing。 i0。 i) { // copy row[i1] to row[i]: for(int j=0。 jwidth。 j++) { boxes[j][i] = boxes[j][i1]。 } } // clean the top row: for(int j=0。 jwidth。 j++) boxes[j][0] = EMPTY。 } public void paint(Graphics g, int box_size) { (0xffffff)。 // white (0, 0, box_size*width+2, box_size*height+2)。 (0)。 (1, 1, box_size*width, box_size*height)。 (1, 1)。 // draw each boxes: for(int i=0。 iwidth。 i++) { for(int j=0。 jheight。 j++) { int color_index = boxes[i][j]。 if(color_index!=EMPTY) { ()。 (i*box_size, j*box_size, box_size, box_size)。 ([color_index])。 (i*box_size+1, j*box_size+1, box_size2, box_size2)。 } } } }}package。 public final class Colors { public static final int[] ALL_COLORS = { 0xff0000, 0xffff00, 0xff00ff, 0x00ff00, 0x00ffff, 0x0000ff, 0xffffff, 0xaa0000, 0xaaaa00, 0xaa00aa, 0x00aa00, 0x00aaaa, 0x0000aa, 0xaaaaaa }。 public static int indexOfColor(int color) { for(int i=0。 i。 i++) { if(ALL_COLORS[i]==color) return i。 } return 0。 }}package。 import .*。 import .*。 public final class MyCanvas extends Canvas implements Runnable,。
阅读剩余 0%
本站所有文章资讯、展示的图片素材等内容均为注册用户上传(部分报媒/平媒内容转载自网络合作媒体),仅供学习参考。 用户通过本站上传、发布的任何内容的知识产权归属用户或原始著作权人所有。如有侵犯您的版权,请联系我们反馈本站将在三个工作日内改正。