您的位置:首页 > 编程语言 > Java开发

java基础俄罗斯方块加强版

2016-04-25 23:17 661 查看





<span style="font-size:18px;"><strong>package box;
import java.util.Scanner;
public class TerominGame {
public static void main(String[] args){
J j=new J(0,4);//新建一个J类对象
j.printJ();//利用J类print函数输出J类测试数据
T t=new T(0,4);//新建一个T类对象
t.printT();//利用T类print函数输出T类测试数据
Scanner sc=new Scanner(System.in);
int c=0;
do{
printTerominGame(t);//输出t对象的测试结果
System.out.println("1——下落,2——向左,3——向右,0——退出");
c=sc.nextInt();
}while(c!=0);

}
public static void printTerominGame( Teromin t){//用于输出测试结果
Cell[] cells=t.cells;//新建一个数组类型的对象,用于存储四个格子的坐标
final int TOTALROW=20;//定义一个常量表示格子总共有20行
final int TOTALCOL=10;//表示有十列
one:for(int i=0;i<TOTALROW;i++)
{
two:for(int j=0;j<TOTALCOL;j++)
{
there:for(int k=0;k<cells.length;k++)
{
if(i==cells[k].row&&j==cells[k].col)
{
System.out.print("*");
break there;//跳出there这层for循环
}
}
System.out.print("-");
}
System.out.println();
}

}
}
class Cell{//存储格子坐标的类
int row;//行
int col;//列
Cell(){

}
public Cell(int row, int col) {
this.row = row;
this.col = col;
}
public String getCellInfo(){
return row+","+col;
}
}
class Teromin{
Cell[] cells;
public Teromin(){//父类构造函数用于新建一个长度为4的数组类型对象
cells=new Cell[4];
}
public void printTeromin(){//输出测试坐标
String temp="";
for(int i=0;i<cells.length-1;i++)
{
temp=temp+"("+cells[i].getCellInfo()+")"+",";
}
temp=temp+"("+cells[cells.length-1].getCellInfo()+")";
System.out.println(temp);

}
public void drop(){//实现格子下落的方法
for(int i=0;i<cells.length;i++)
{
cells[i].row--;

}
}
public void moveRight(){//实现格子右移的方法
for(int i=0;i<cells.length;i++)
{
cells[i].col++;
}
}
public void moveLeft(){//实现格子右移的方法
for(int i=0;i<cells.length;i++)
{
cells[i].col--;
}
}
}
class T extends Teromin{
public T(int row,int col){//子类构造函数
super();//用于表示父类构造函数
cells[0]=new Cell(row,col);
cells[1]=new Cell(row,col+1);
cells[2]=new Cell(row,col+2);
cells[3]=new Cell(row+1,col+1);
}
public void printT(){
System.out.println("i am a T");
super.printTeromin();//在子类中引用父类的函数

}
}
class J extends Teromin{//与T类同理
public J(int row,int col){
super();
cells[0]=new Cell(row,col);
cells[1]=new Cell(row,col+1);
cells[2]=new Cell(row,col+2);
cells[3]=new Cell(row+1,col+2);

}
public void printJ(){
System.out.println("i am a J");
super.printTeromin();
}
}</strong></span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: