📄 bricklist.java
字号:
/* * Copyright (c) 2002, 2003 Sun Microsystems, Inc. All Rights Reserved. * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * - Redistribution of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * - Redistribution in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * Neither the name of Sun Microsystems, Inc., 'Java', 'Java'-based * names, or the names of contributors may be used to endorse or promote * products derived from this software without specific prior written * permission. * This software is provided "AS IS," without a warranty of any kind. ALL * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN * MIDROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. * You acknowledge that this software is not designed, licensed or * intended for use in the design, construction, operation or maintenance * of any nuclear facility. $Id: BrickList.java,v 1.3 2003/05/29 22:28:05 wildcard Exp $ */package com.sun.j2me.blueprints.jbricks;import javax.microedition.lcdui.Graphics;/** * This is a container class that does some of the * initialization, drawing and collision detection * for the bricks in any particular level. It also * serves as a container that manages redraws */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; YOFFSET = Screen.height / 8; } 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 n, 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; } 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; } n = y * Engine.PATTERN_WIDTH + x; brick = list[n]; xw = ball.x + ball.width; yh = ball.y + ball.height; oxw = brick.x + brick.width; oyh = brick.y + brick.height; 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; // don't do more than one collision per frame 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 + -