Java se入门菜鸟练手升级版小游戏俄罗斯方块
比较传统俄罗斯方块升级的地方
① 加入最高分记录;
② 加入随机位置产生方块增加游戏难度;
③ 加入开局随机产生方块增加游戏难度;
④ 加入游戏难度设置,可选择方块下降速度,根据不同下架速度匹配不同的消除每行分数也不同;
准备工作
①素材准备
②JDK8.0
③IntelliJ IDEA
升级模块代码
①在资源区新建Max文件,通过DataOutputStream和DataInputStream流进行读写加入最高分记录;
public void writeMax(){
DataOutputStream dos= null;
try {
dos = new DataOutputStream(new FileOutputStream(Thread.currentThread().getContextClassLoader().getResource("Max").getPath()));
dos.writeInt(max);
dos.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
dos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void readMax(){
DataInputStream dis = null;
try {
dis = new DataInputStream(new FileInputStream(Thread.currentThread().getContextClassLoader().getResource("Max").getPath()));
max=dis.readInt();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
dis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
②新建随机开局RandomOpen的方块类,在主类中new出来之后进行paint绘制再将遍历的每个方块嵌入wall中:
public class RandomOpen {
protected List<Cell> cells=new ArrayList<>();
public RandomOpen() {
for (int i=12;i<19;i++){
Random random = new Random();
int randomOtherTwo = random.nextInt(9);
int randomOtherThree = random.nextInt(9);
int randomOtherFour = random.nextInt(9);
int randomOtherFife = random.nextInt(9);
for (int j=0;j<9;j++){
int randomOtherOne = random.nextInt(7);
if (j!=randomOtherFour&&j!=randomOtherFife&&j!=randomOtherThree&&j!=randomOtherTwo){
switch (randomOtherOne){
case 0:
cells.add(new Cell(i,j,Tetris.L));
break;
case 1:
cells.add(new Cell(i,j,Tetris.I));
break;
case 2:
cells.add(new Cell(i,j,Tetris.J));
break;
case 3:
cells.add(new Cell(i,j,Tetris.S));
break;
case 4:
cells.add(new Cell(i,j,Tetris.Z));
break;
case 5:
cells.add(new Cell(i,j,Tetris.T));
break;
case 6:
cells.add(new Cell(i,j,Tetris.L));
break;
default:
}
}
}
}
}
}
private void paintRandomOpen(Graphics g) {
for (Cell cell:randomOpen.cells
) {
int y = cell.getRow()*CELL_SIZE;
int x = cell.getCol()*CELL_SIZE;
g.drawImage(cell.getCellImage(),x,y,null);
int row = cell.getRow();
int col = cell.getCol();
wall[row][col] =cell;
}
}
③加入开局随机产生方块,即在每个四格方块定义处使用随机数Random;
public I() {
Random random = new Random();
int r = random.nextInt(7);
while (r==0){
r=random.nextInt(7);
}
getCells()[0] = new Cell(0,r,Tetris.I);
getCells()[1] = new Cell(0,r-1,Tetris.I);
getCells()[2] = new Cell(0,r+1,Tetris.I);
getCells()[3] = new Cell(0,r+2,Tetris.I);
}
④设置游戏难度,添加按键改变方块下降速度,根据不同下架速度匹配不同的消除每行分数也不同;
//变量
int[] scores_pool = {0,1,2,5,10};
public static final int KUNNAN = 2;
public static final int PUTONG = 1;
public static final int JIANDAN = 0;
private int game_diffcuty;
String[] show_diffcuty = {"SPEED: ★ ","SPEED: ★ ★ ★","SPEED: ★ ★ ★ ★ ★"};
int [] randomDiffcuty = {1,3,6};
int [] speedDiffcuty = {6,4,2};
//绘制
private void paintDiffcuty(Graphics g) {
if(game_diffcuty == JIANDAN){
Font font =new Font(Font.SANS_SERIF,Font.BOLD,20);
g.setFont(font);
g.drawString(show_diffcuty[game_diffcuty],10,480);
}else if (game_diffcuty == PUTONG){
Font font =new Font(Font.SANS_SERIF,Font.BOLD,20);
g.setFont(font);
g.drawString(show_diffcuty[game_diffcuty],10,480);
}else if (game_diffcuty == KUNNAN){
Font font =new Font(Font.SANS_SERIF,Font.BOLD,20);
g.setFont(font);
g.drawString(show_diffcuty[game_diffcuty],10,480);
}
}
//在监听器中添加按键
case KeyEvent.VK_C:
if (game_state !=PLAYING) {
Random random = new Random();
int diff = random.nextInt(3);
game_diffcuty = diff;
}
break;
//方块下降速度设置
while (true){
if (game_state==PLAYING){
int tab = speedDiffcuty[game_diffcuty];
try {
Thread.sleep(100*tab);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (canDrop()){
currentOne.softDrop();
}else{
landTOowall();
destroyLine();
if (isGameOver()){
game_state = GAMEOVER;
if (max<totalScore){
max=totalScore;
writeMax();
}
}else{
currentOne = nextOne;
nextOne = Tetromino.randomOne();
}
}
}
repaint();
}
让我们玩一下
开局吧!!!
目前1⭐难度,按O【Open】开始!!!
退出窗口,重新进入,读取之前记录,再玩一把五星⭐⭐⭐⭐⭐刷新记录