📄 shape.java
字号:
//所有游戏块类的父类,也是本程序的核心类,这个类写完后,这个游戏程序算是基本搞定
public class Shape {
int [][] table;//本地的数组
Coordinate []shape;//用来标识本地数组哪个位置为1,同样也就标识出了哪个位置为0;也就表示出了游戏块的形状
Coordinate centrePoint;//中心块,变形时围绕此块旋转,如果没有中心块,则为null
Shape(){
table = null;
shape = null;
centrePoint = null;
}
//游戏块的最大x坐标
public int xMax(){
int max = 0;
for(int i = 0; i < shape.length; i ++){
if(shape[i].x > max){
max = shape[i].x;
}
}
return max;
}
//游戏块的最大y坐标
public int yMax(){
int max = 0;
for (int i = 0; i < shape.length; i ++){
if(shape[i].y > max){
max = shape[i].y;
}
}
return max;
}
//游戏块的最小x坐标
public int xMin(){
int min = 100;
for (int i = 0; i < shape.length; i ++){
if(shape[i].x < min){
min = shape[i].x;
}
}
return min;
}
//游戏块的最小y坐标
public int yMin(){
int min = 100;
for (int i = 0; i < shape.length; i ++){
if(shape[i].y < min){
min = shape[i].y;
}
}
return min;
}
//在游戏块放到游戏数组前,要先在"下一个游戏块"里显示
//此方法对显示下一个游戏块的类的数组进行处理
public void beforeBegin(){
int x0 = 2, y0 = 2;
int m,n ;
DrawNextShape.init();
if(this.centrePoint == null){
int shapeCentreX = ( xMin() + xMax())/2;
int shapeCentreY = ( yMin() + yMax())/2;
m = x0 - shapeCentreX ;
n = y0 - shapeCentreY ;
for(int i = 0; i < shape.length; i ++){
DrawNextShape.nextShapeTable[shape[i].x + m][shape[i].y + n] = 1;
}
}
else{
m = x0 - this.centrePoint.x;
n = y0 - this.centrePoint.y;
for(int i = 0; i < shape.length; i ++){
DrawNextShape.nextShapeTable[shape[i].x + m][shape[i].y + n] = 1;
}
}
}
//将游戏块放到游戏桌对应的数组上,如果不能放,那么之后游戏就结束了
public boolean begin(){
Coordinate coor[] = new Coordinate[shape.length];
for(int q = 0; q < coor.length; q ++){
coor[q] = new Coordinate();
}
for(int i = 0, m , n; i < shape.length; i ++){
m = shape[i].x-shape[0].x;
n = shape[i].y-shape[0].y;
if(table[m][Table.getY()/2+n] == 1){
return false;
}
else{
table[m][Table.getY()/2+n] = 1;
coor[i].x = m;
coor[i].y = Table.getY()/2+n;
}
}
for(int i = 0; i < shape.length; i ++){
shape[i].x = coor[i].x;
shape[i].y = coor[i].y;
}
return true;
}
//控制游戏块的下落
public boolean down() {
if( xMax() >= Table.getX()-1){
return false;
}
else{
for(int i = 0; i < shape.length; i ++){
Table.mytable[shape[i].x][shape[i].y] = 0;
}
for(int i = 0; i < shape.length; i ++){
if( Table.mytable[shape[i].x+1][shape[i].y] == 1){
for(int j = 0; j < shape.length; j ++){
Table.mytable[shape[j].x][shape[j].y] = 1;
}
return false;
}
}
for(int i = 0; i < shape.length; i ++){
shape[i].x = shape[i].x + 1;
Table.mytable[shape[i].x][shape[i].y] = 1;
}
return true;
}
}
//控制游戏块的左移
public boolean left() {
if(yMin() <= 0){
return false;
}
else{
for(int i = 0; i < shape.length; i ++){
Table.mytable[shape[i].x][shape[i].y] = 0;
}
for(int i = 0; i < shape.length; i ++){
if(Table.mytable[shape[i].x ][shape[i].y - 1] == 1){
for(int j = 0; j < shape.length; j ++){
Table.mytable[shape[j].x][shape[j].y] = 1;
}
return false;
}
}
for(int i = 0; i < shape.length; i ++){
shape[i].y = shape[i].y - 1;
Table.mytable[shape[i].x][shape[i].y] = 1;
}
return true;
}
}
//控制游戏块的右移
public boolean right() {
if(yMax() >= Table.getY()-1){
return false;
}
else{
for(int i = 0; i < shape.length; i ++){
Table.mytable[shape[i].x][shape[i].y] = 0;
}
for(int i = 0; i < shape.length; i ++){
if(Table.mytable[shape[i].x ][shape[i].y + 1] == 1){
for(int j = 0; j < shape.length; j ++){
Table.mytable[shape[j].x][shape[j].y] = 1;
}
return false;
}
}
for(int i = 0; i < shape.length; i ++){
shape[i].y = shape[i].y + 1;
Table.mytable[shape[i].x][shape[i].y] = 1;
}
return true;
}
}
//消行,返回消行的行数
public int addPoint(){
int rows = 0;
for(int i = xMin() ,m=0; i <= xMax(); i ++){
for(int j = 0 ; j < Table.getY() ; j ++ ){
if(Table.mytable[i][j] == 1){
m = m + 1;
}
}
if(m == Table.getY()){
for(int x = i; x > 1 ; x --){
for(int y = 0; y < Table.getY(); y ++){
Table.mytable[x][y] = Table.mytable[x-1][y];
}
}
for(int y = 0; y <Table.getY(); y ++){
Table.mytable[0][y] = 0;
}
m = 0;
rows = rows + 1;
}
else {
m = 0;
}
}
return rows;
}
//控制游戏块的变形,此方法比较难写,先判断游戏块有无中心点,有的话以中心点旋转,没有的话自己判断
public boolean change(){
for(int i = 0 ; i < shape.length; i ++){
Table.mytable[shape[i].x][shape[i].y] = 0;
}
Coordinate coor[] = new Coordinate[shape.length];
for(int i = 0 ; i < coor.length; i ++){
coor[i] = new Coordinate();
}
double x0 , y0 , m , n;
if(this.centrePoint==null){
x0 = (double)(xMax() + xMin())/2;
y0 = (double)(yMax() + yMin())/2;
}
else{
x0 = this.centrePoint.x;
y0 = this.centrePoint.y;
}
for(int i = 0 ; i < coor.length; i ++){
m = shape[i].x - x0; n = shape[i].y - y0;
coor[i].x = (int) (x0 + n);
coor[i].y = (int) (y0 - m);
}
for(int i = 0 ; i < coor.length; i ++){
if(coor[i].x < 0 || coor[i].x > Table.getX()-1 || coor[i].y < 0|| coor[i].y > Table.getY() - 1){
for(int j = 0 ; j < shape.length; j ++){
Table.mytable[shape[j].x][shape[j].y] = 1;
}
return false;
}
else{
if(Table.mytable[coor[i].x][coor[i].y] == 1){
for(int j = 0 ; j < shape.length; j ++){
Table.mytable[shape[j].x][shape[j].y] = 1;
}
return false;
}
}
}
for(int i = 0 ; i < shape.length; i ++){
shape[i].x = coor[i].x;
shape[i].y = coor[i].y;
Table.mytable [shape[i].x][shape[i].y] = 1;
}
return true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -