91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

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
玛沁县| 金塔县| 岑巩县| 尉犁县| 滦南县| 奎屯市| 郑州市| 大宁县| 萨迦县| 朝阳县| 雅江县| 涞水县| 随州市| 广河县| 阜新市| 尤溪县| 雷山县| 扎鲁特旗| 开平市| 泽普县| 康平县| 霍林郭勒市| 云霄县| 乌兰察布市| 沙坪坝区| 武冈市| 五大连池市| 高密市| 海南省| 东兴市| 和林格尔县| 肃南| 山阳县| 荆门市| 凉城县| 天等县| 大同县| 镇平县| 平谷区| 蒙城县| 龙山县|