java俄罗斯方块排行榜_俄罗斯方块JAVA

java俄罗斯方块排行榜_俄罗斯方块JAVApublicclassTetrisextendsJPanel{privateBaseTetrominocurrentTeromino=BaseTetromino.randomTetromino();privateBaseTetrominonextTeromino=BaseTetromino.randomTetromino();privatestaticfinalintC…

public class Tetris extendsJPanel {private BaseTetromino currentTeromino =BaseTetromino.randomTetromino();private BaseTetromino nextTeromino =BaseTetromino.randomTetromino();private static final int CELL_SIZE = 26;private static final int X_SIZE = 10;private static final int Y_SIZE = 20;//刷新时间

private static final int INTERVAL = 1000;private static int mark = 0;private Cell[][] wall = newCell[X_SIZE][Y_SIZE];public staticBufferedImage bufferedImage0;public staticBufferedImage bufferedImage1;public staticBufferedImage bufferedImage2;public staticBufferedImage bufferedImage3;public staticBufferedImage bufferedImage4;public staticBufferedImage bufferedImage5;public staticBufferedImage bufferedImage6;public staticBufferedImage background;public staticBufferedImage gameOver;static{try{

bufferedImage0= ImageIO.read(Tetris.class.getResource(“img/stone0.png”));

bufferedImage1= ImageIO.read(Tetris.class.getResource(“img/stone1.png”));

bufferedImage2= ImageIO.read(Tetris.class.getResource(“img/stone2.png”));

bufferedImage3= ImageIO.read(Tetris.class.getResource(“img/stone3.png”));

bufferedImage4= ImageIO.read(Tetris.class.getResource(“img/stone4.png”));

bufferedImage5= ImageIO.read(Tetris.class.getResource(“img/stone5.png”));

bufferedImage6= ImageIO.read(Tetris.class.getResource(“img/stone6.png”));

background= ImageIO.read(Tetris.class.getResource(“img/background1.png”));

gameOver= ImageIO.read(Tetris.class.getResource(“img/background1.png”));

}catch(Exception e) {

e.printStackTrace();

}

}

@Overridepublic voidpaint(Graphics g) {

g.drawImage(background,0, 0, null);

g.translate(15, 15);

paintWall(g);

paintCurrentOne(g);

paintNextOne(g);

}//渲染下一个图形

private voidpaintNextOne(Graphics g) {

Cell[] cells=nextTeromino.getCells();for(Cell cell : cells) {int x =cell.getColumn();int y =cell.getRow();

g.drawImage(cell.getBufferedImage(), x* CELL_SIZE + 260, y * CELL_SIZE + 26, null);

}

}//渲染当前图形

private voidpaintCurrentOne(Graphics g) {

Cell[] cells=currentTeromino.getCells();for(Cell cell : cells) {

g.drawImage(cell.getBufferedImage(), cell.getColumn()* CELL_SIZE, cell.getRow() * CELL_SIZE, null);

}

}//渲染墙壁

private voidpaintWall(Graphics g) {for (int i = 0; i < X_SIZE; i++) {for (int j = 0; j < Y_SIZE; j++) {if (wall[i][j] == null) g.drawRect(i, j, CELL_SIZE, CELL_SIZE);else g.drawImage(wall[i][j].getBufferedImage(), i * CELL_SIZE, j * CELL_SIZE, null);

}

}

}//渲染得分

private voidpaintMark(Graphics g) {

g.drawString(String.valueOf(mark),300, 30);

}public voidstart() {

KeyListener listener= newKeyAdapter() {

@Overridepublic voidkeyPressed(KeyEvent e) {int code =e.getKeyCode();switch(code) {caseKeyEvent.VK_DOWN:

moveDown();break;caseKeyEvent.VK_LEFT:

moveLeft();break;caseKeyEvent.VK_RIGHT:

moveRight();break;caseKeyEvent.VK_UP:

spin();break;default:break;

}

repaint();

}

};this.addKeyListener(listener);this.requestFocus();while (true) {try{

Thread.sleep(INTERVAL);

}catch(InterruptedException interruptedException) {

interruptedException.printStackTrace();

}if(canDrop()) currentTeromino.moveDown();else{

landToWall();

currentTeromino=nextTeromino;

nextTeromino=BaseTetromino.randomTetromino();

}

repaint();

}

}/*** @Author Niuxy

* @Date 2020/11/29 7:26 下午

* @Description 画布下沉并计分

* 递归的进行行下沉处理

* 每处理完一行,下一次递归由该行继续

* 不再重复遍历该行以下的行*/

private final voidelimination() {int index = Y_SIZE – 1;while (index > -1) {if (!isFull(index)) {

index–;continue;

}//空行剪枝

if (isEmpty(index)) break;

sink(index);//计分

this.mark++;

}

}//画布下沉

private final void sink(introwIndex) {if (rowIndex == 0) {for (int i = 0; i < X_SIZE; i++) {

wall[i][0] = null;

}return;

}for (int i = 0; i < X_SIZE; i++) {

wall[i][rowIndex]= wall[i][rowIndex – 1];

}

sink(rowIndex- 1);

}//判断 index 行是否已满

private boolean isFull(intindex) {if (index < 0 || index >= Y_SIZE) throw new RuntimeException(“index is out of the wall!”);for (int i = 0; i < X_SIZE; i++) {if (wall[i][index] == null) return false;

}return true;

}//判断空行

private final boolean isEmpty(intindex) {if (index < 0 || index >= Y_SIZE) throw new RuntimeException(“index is out of the wall!”);for (int i = 0; i < X_SIZE; i++) {if (wall[i][index] != null) return false;

}return true;

}//旋转图形

public voidspin() {

currentTeromino.spin(wall);

}//键盘监听事件-图形右移

public voidmoveRight() {

currentTeromino.moveRight();if (outOfBounds() ||coincide()) currentTeromino.moveLeft();

}//键盘监听事件-图形左移

public voidmoveLeft() {

currentTeromino.moveLeft();if (outOfBounds() ||coincide()) currentTeromino.moveRight();

}//键盘监听事件-图形下移

public voidmoveDown() {if(canDrop()) currentTeromino.moveDown();else{

landToWall();

currentTeromino=nextTeromino;

nextTeromino=BaseTetromino.randomTetromino();

}

}//图形坐标是否重复

private booleancoincide() {

Cell[] cells=currentTeromino.getCells();for(Cell cell : cells) {if (wall[cell.getColumn()][cell.getRow()] != null) return true;

}return false;

}//图形是否超出画布

private booleanoutOfBounds() {

Cell[] cells=currentTeromino.getCells();for(Cell cell : cells) {if (cell.getColumn() < 0 || cell.getColumn() >= X_SIZE || cell.getRow() >= Y_SIZE – 1) return true;

}return false;

}//图形是否可以下移

public booleancanDrop() {

Cell[] cells=currentTeromino.getCells();for(Cell c : cells) {if (c.getRow() == Y_SIZE – 1 || wall[c.getColumn()][c.getRow() + 1] != null) return false;

}return true;

}//图形位置确定,载入画布

private voidlandToWall() {

Cell[] cells=currentTeromino.getCells();for (Cell c : cells) wall[c.getColumn()][c.getRow()] =c;

elimination();

}public static voidmain(String[] args) {

JFrame jFrame= new JFrame(“斯巴达!”);

Tetris tetris= newTetris();

jFrame.add(tetris);

jFrame.setVisible(true);

jFrame.setSize(535, 595);

jFrame.setLocationRelativeTo(null);

jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

tetris.start();

}

}

今天的文章java俄罗斯方块排行榜_俄罗斯方块JAVA分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/25788.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注