ADD file via upload

main
nxist2202005026 1 year ago
parent 96e389fe8d
commit dfb3563d8a

@ -0,0 +1,305 @@
package model;
import java.util.TimerTask;
import java.util.Timer;
import util.Constant;
import view.GameCanvas;
import view.MyFrame;
/**
* 4 * 4 Box
*/
public class Block {
private GameCanvas gc;//画布
private Box[][] boxs = new Box[4][4];
// public static int[][] yinying=new int[4][4];
public int y, x;
public int style;
public int newH = 0;
public int newL = 0;
private static int st = 7;//默认属于初级
public boolean isAlive = false;//判断方块是否存活
public boolean pausing = false;//暂停为假
/**
*
*/
public static void set_addl(int a) {
Block.st = a;
}
/**
*
*/
public static int get_addl() {
return st;
}
/**
*
*/
public Block() {
}
/**
*
*
* @param style
* STYLES28
* @param y
* canvas
* @param x
* canvas
* @param level
*
* @param canvas
*
*/
public Block(int style, int y, int x, GameCanvas gc) {
this.style = style;
this.y = y;
this.x = x;
this.gc = gc;
// this.high=high;
int key = 0x8000;// 匹配器
// 获得一种方块
for (int i = 0; i < boxs.length; i++) {
for (int j = 0; j < boxs[i].length; j++) {
boolean isColor = ((style & key) != 0);
boxs[i][j] = new Box(isColor);
key >>= 1;
}
}
display();// 调用显示函数
}
/**
*
*/
public void earse() {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (boxs[i][j].isColorBox()) {
Box box = gc.getBox(i + y, j + x);
if (box == null)
continue;
box.setColor(false);
}
}
}
}
/**
*
*/
public void display() {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (boxs[i][j].isColorBox()) {
Box box = gc.getBox(y + i, x + j);// 获得在游戏界面的位置
if (box == null)
continue;
else
box.setColor(true);
}
}
}
}
/**
*
*/
public void moveLeft() {
newL = x - 1;
newH = y;
if (isMoveAble(newH, newL) && !pausing) {
earse();
x--;
display();
gc.repaint();
}
}
/**
*
*/
public void moveRight() {
newL = x + 1;
newH = y;
if (isMoveAble(newH, newL) && !pausing) {
earse();
x++;
display();
gc.repaint();
}
}
/**
*
*/
public void moveDown() {
newL = x;
newH = y + 1;
if (pausing == true)
return;
if (isMoveAble(newH, newL)) {
earse();
y++;
display();
gc.repaint();
} else {
isAlive = false;
// mytask.cancel();
}// 取消定时器任务
}
/**
*
*/
public void quickDown() {
newL = x;
newH = y + 1;
while (isMoveAble(newH, newL)) {
earse();
y++;
display();
gc.repaint();
newL = x;
newH = y + 1;
}
isAlive = false;
}
/* *//**
*
*//*
public void yy() {
int yy = y;
newL = x;
newH = yy + 1;
while (isMoveAble(newH, newL)) {
earse();
yy++;
display();
newL = x;
newH = yy + 1;
}
earse();
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (boxs[i][j].isColorBox() == true)
yinying[i][j]=yy+j;
}
}
}*/
/**
*
*/
public void moveUp() {
earse();
if (pausing == true)
return;
if (isChangeAble()) {
// 获取下一形态
int key = 0x8000;
for (int a = 0; a < 4; a++) {
for (int b = 0; b < 4; b++) {
boolean isColor = ((this.style & key) != 0);
boxs[a][b].setColor(isColor);
key >>= 1;
}
}
display();
gc.repaint();
}
}
/**
*
*
*/
public boolean isMoveAble(int newH, int newL) {
earse();
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (boxs[i][j].isColorBox()) {
Box box = gc.getBox(newH + i, newL + j);
if (box == null || (box.isColorBox())) {
display();// 显示原先的位置
return false;
}
}
}
}
display();
return true;
}
/**
*
*/
private boolean isChangeAble() {
int key = 0x8000;
int newStyle = 0;
earse();
for (int i = 0; i < st; i++) {
for (int j = 0; j < 4; j++) {
if (Constant.STYLES[i][j] == this.style) {
newStyle = Constant.STYLES[i][(j + 1) % 4];// 取得下一个状态
break;
}
// display();
}
}
// 判断此状态在游戏界面上显示是否有障碍
for (int a = 0; a < boxs.length; a++) {
for (int b = 0; b < boxs[a].length; b++) {
if ((newStyle & key) != 0) {
Box box = gc.getBox(y + a, x + b);
if (box == null || box.isColorBox()) {
// display();
return false;
}
}
key >>= 1;
}
}
this.style = newStyle;
return true;
}
/**
*
*/
public void pause() {
// TODO Auto-generated method stub
if (MyFrame.playing == true) {
pausing = true;
// isMoveAble(newH, newL);
gc.pau = true;
gc.repaint();
}
}
/**
*
*/
public void jixu() {
if (MyFrame.playing == true) {
pausing = false;
gc.pau = false;
gc.repaint();
;
}
}
}
Loading…
Cancel
Save