中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

java實現簡單的俄羅斯方塊

小云
127
2023-08-10 14:06:54
欄目: 編程語言

以下是使用Java實現的簡單俄羅斯方塊游戲的示例代碼:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Tetris extends JFrame {
private final int BOARD_WIDTH = 10;
private final int BOARD_HEIGHT = 22;
private Timer timer;
private boolean isFallingFinished = false;
private boolean isStarted = false;
private boolean isPaused = false;
private int numLinesRemoved = 0;
private int curX = 0;
private int curY = 0;
private JLabel statusBar;
private Shape curPiece;
private Shape.Tetrominoes[] board;
public Tetris() {
initUI();
}
private void initUI() {
statusBar = new JLabel(" 0");
add(statusBar, BorderLayout.SOUTH);
board = new Shape.Tetrominoes[BOARD_WIDTH * BOARD_HEIGHT];
addKeyListener(new TAdapter());
setFocusable(true);
setFocusTraversalKeysEnabled(false);
timer = new Timer(400, new GameCycle());
timer.start();
clearBoard();
}
private int squareWidth() {
return (int) getContentPane().getSize().getWidth() / BOARD_WIDTH;
}
private int squareHeight() {
return (int) getContentPane().getSize().getHeight() / BOARD_HEIGHT;
}
private Shape.Tetrominoes shapeAt(int x, int y) {
return board[(y * BOARD_WIDTH) + x];
}
private void start() {
if (isPaused) {
return;
}
isStarted = true;
isFallingFinished = false;
numLinesRemoved = 0;
clearBoard();
newPiece();
timer.start();
}
private void pause() {
if (!isStarted) {
return;
}
isPaused = !isPaused;
if (isPaused) {
timer.stop();
statusBar.setText(" paused");
} else {
timer.start();
statusBar.setText(String.valueOf(numLinesRemoved));
}
repaint();
}
private void doDrawing(Graphics g) {
Dimension size = getSize();
int boardTop = (int) size.getHeight() - BOARD_HEIGHT * squareHeight();
for (int i = 0; i < BOARD_HEIGHT; ++i) {
for (int j = 0; j < BOARD_WIDTH; ++j) {
Shape.Tetrominoes shape = shapeAt(j, BOARD_HEIGHT - i - 1);
if (shape != Shape.Tetrominoes.NoShape) {
drawSquare(g, j * squareWidth(), boardTop + i * squareHeight(), shape);
}
}
}
if (curPiece.getShape() != Shape.Tetrominoes.NoShape) {
for (int i = 0; i < 4; ++i) {
int x = curX + curPiece.x(i);
int y = curY - curPiece.y(i);
drawSquare(g, x * squareWidth(), boardTop + (BOARD_HEIGHT - y - 1) * squareHeight(), curPiece.getShape());
}
}
}
private void dropDown() {
int newY = curY;
while (newY > 0) {
if (!tryMove(curPiece, curX, newY - 1)) {
break;
}
--newY;
}
pieceDropped();
}
private void oneLineDown() {
if (!tryMove(curPiece, curX, curY - 1)) {
pieceDropped();
}
}
private void removeFullLines() {
int numFullLines = 0;
for (int i = BOARD_HEIGHT - 1; i >= 0; --i) {
boolean lineIsFull = true;
for (int j = 0; j < BOARD_WIDTH; ++j) {
if (shapeAt(j, i) == Shape.Tetrominoes.NoShape) {
lineIsFull = false;
break;
}
}
if (lineIsFull) {
++numFullLines;
for (int k = i; k < BOARD_HEIGHT - 1; ++k) {
for (int j = 0; j < BOARD_WIDTH; ++j) {
board[(k * BOARD_WIDTH) + j] = shapeAt(j, k + 1);
}
}
}
}
if (numFullLines > 0) {
numLinesRemoved += numFullLines;
statusBar.setText(String.valueOf(numLinesRemoved));
isFallingFinished = true;
curPiece.setShape(Shape.Tetrominoes.NoShape);
repaint();
}
}
private void newPiece() {
curPiece = new Shape();

0
剑河县| 鞍山市| 肇州县| 汉沽区| 施甸县| 和静县| 巴林左旗| 红河县| 桃园市| 都安| 宽甸| 湾仔区| 合川市| 济源市| 界首市| 阜宁县| 册亨县| 海阳市| 武乡县| 光山县| 沾益县| 安达市| 凤翔县| 海淀区| 朝阳县| 沾化县| 汉川市| 水富县| 湄潭县| 江孜县| 库尔勒市| 且末县| 桐乡市| 濉溪县| 定远县| 确山县| 霞浦县| 泰州市| 玛沁县| 梧州市| 盐津县|