java 俄罗斯方块加开始,java基础俄罗斯方块加强版

该博客展示了如何使用Java编程实现一个名为Teromin的游戏,包括Teromin类及其子类T和J,实现了格子的下落、左右移动等操作,并通过Scanner实现用户输入进行交互。游戏的核心是Teromin类的细胞(Cell)数组,每个细胞包含行和列坐标。用户可以选择让格子下落、向左或向右移动,直至选择退出。
摘要由CSDN通过智能技术生成

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png

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

{

two:for(int j=0;j

{

there:for(int k=0;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

{

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[i].row--;

}

}

public void moveRight(){//实现格子右移的方法

for(int i=0;i

{

cells[i].col++;

}

}

public void moveLeft(){//实现格子右移的方法

for(int i=0;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();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值