package entities; import java.awt.Color; import java.awt.Graphics; import util.Global; public class Block { /** * 变形(旋转) */ public static final int ROTATE = 5; /** * 上移 */ public static final int UP = 1; /** * 下落 */ public static final int DOWN = 2; /** * 左移 */ public static final int LEFT = 3; /** * 右移 */ public static final int RIGHT = 4; private int[][][] body = null; private Color color; private int rotate; private int px,py; /** * 左上角的位置 */ private int left; private int top; /**下落速度*/ private int speed; /** * 图形的真实高度 */ private int height; /** * 图形的真实宽度 */ private int width; /**当图形为下一个的时候的X和Y坐标,单位:像素*/ private int staticX,staticY; public Block(int[][][] body, int rotate, Color color) { this.body =body; this.rotate =rotate; this.color = color; init(); } public void init() { //求图形高度和宽度 setBlockW_H(); left = Global.RIGHTCONTAINER_WIDTH/2-2; top = 0 - height; speed = Global.DOWN_SPEED; staticX = Global.LEFTCONTAINER_X+(Global.LEFTCONTAINER_WIDTH-width)/2*Global.CELL_WIDTH; staticY = Global.LEFTCONTAINER_Y+(Global.LEFTCONTAINER_HEIGHT-height)/2*Global.CELL_WIDTH; } /**旋转图形*/ public void rotBlock() { rotate = (rotate+1)%body.length; //旋转后重新得到宽度和高度 setBlockW_H(); } /**求得图形的宽度和高度,单位:格*/ public void setBlockW_H() { height =0; width =0; int temp =0; for(int i=0;i<body[rotate].length;i++) { for(int j=0;j<body[rotate][i].length;j++) { //如果 i 和 j是图像中的格子 if(body[rotate][i][j]==1) { height = i+1; temp = j+1; width = width>temp?width:temp; } // if(body[rotate][i][j]==1) // { // // height = height+1; // break;//跳出内循环 // } } } } public void moveUp() { top--; } /** * 向下移动 */ public void moveDown() { top++; } /** * 向左移动 */ public void moveLeft() { left--; } /** * 向右移动 */ public void moveRight() { left++; } /** * 画小砖块 * * @param px * x坐标 * @param py * y坐标 * @param color * 颜色 */ public void drawCell(Graphics g,int px, int py, Color color) { //画小方块 g.setColor(color); g.fillRect(px,py,Global.CELL_WIDTH,Global.CELL_HEIGHT); // 画白边 g.setColor(Global.COLOR_WHITE); g.drawRect(px, py, Global.CELL_WIDTH-1, Global.CELL_HEIGHT-1); } /**画下坠的方块*/ public void drawBlock(Graphics g) { for(int i=0;i<body[rotate].length;i++) { for(int j =0;j<body[rotate][i].length;j++) { if(body[rotate][i][j]==1) { //i是行数,j是列数 drawCell(g,Global.RIGHTCONTAINER_X+(left+j)*Global.CELL_WIDTH,Global.RIGHTCONTAINER_Y+(top+i)*Global.CELL_HEIGHT,color); } } } } /**如果方块是下一个方块时调用此方法 * (画静止的方块) * */ public void drawStaticBlock(Graphics g) { for(int i=0;i<body[rotate].length;i++) { for(int j =0;j<body[rotate][i].length;j++) { if(body[rotate][i][j]==1) { //i是行数,j是列数 drawCell(g,staticX+j*Global.CELL_WIDTH,staticY+i*Global.CELL_HEIGHT,color); } } } } public int getLeft() { return left; } public void setLeft(int left) { this.left = left; } public int getTop() { return top; } public void setTop(int top) { this.top = top; } public int getHeight() { return height; } public void setHeight(int height) { this.height = height; } public int getWidth() { return width; } public void setWidth(int width) { this.width = width; } public int[][][] getBody() { return body; } public void setBody(int[][][] body) { this.body = body; } public int getRotate() { return rotate; } public void setRotate(int rotate) { this.rotate = rotate; } }

lhm789 LV6
2024年9月23日
luoyanglin LV2
2024年9月14日
微信网友_7062902643691520 LV1
2024年7月2日
caojiaxuan LV1
2024年6月20日
鬼屋报道 LV3
2024年6月4日
yx666666 LV1
2024年1月5日
ClydeSon LV5
2023年12月27日
1351408674 LV1
2023年12月27日
1112WHQ LV7
2023年11月3日
ggl163163eel LV2
2023年10月18日

lhm789 LV6
2024年9月23日
luoyanglin LV2
2024年9月14日
54ty1212
2024年8月6日
暂无贡献等级
微信网友_7062902643691520 LV1
2024年7月2日
feelinfer LV1
2024年6月27日
qqqww11 LV2
2024年6月26日
azzzz1 LV2
2024年6月26日
hainabian1977 LV2
2024年6月24日
caojiaxuan LV1
2024年6月20日
鬼屋报道 LV3
2024年6月1日