亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

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
申扎县| 顺昌县| 扎兰屯市| 凌海市| 广德县| 工布江达县| 广平县| 永春县| 武胜县| 休宁县| 句容市| 滦南县| 石柱| 盐津县| 嫩江县| 晋宁县| 巢湖市| 民丰县| 霍城县| 南阳市| 罗城| 青川县| 锡林浩特市| 望奎县| 南乐县| 麻江县| 文成县| 金寨县| 绥芬河市| 探索| 沅陵县| 石景山区| 怀来县| 罗源县| 永善县| 宜君县| 扎囊县| 定兴县| 怀柔区| 无极县| 郓城县|