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

溫馨提示×

java做俄羅斯方塊

小云
92
2023-08-24 16:15:18
欄目: 編程語言

要使用Java編寫俄羅斯方塊游戲,可以使用Java圖形庫(如AWT或JavaFX)來創建游戲界面和圖形。下面是一個簡單的示例代碼以幫助你入門:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Tetris extends JPanel implements ActionListener, KeyListener {
private static final int WIDTH = 10;
private static final int HEIGHT = 20;
private static final int SQUARE_SIZE = 30;
private static final int DELAY = 300;
private Timer timer;
private boolean isFallingFinished = false;
private boolean isStarted = false;
private boolean isPaused = false;
private int score = 0;
private int currentX = 0;
private int currentY = 0;
private List<int[][]> tetrominoes = new ArrayList<>();
private int[][] board = new int[WIDTH][HEIGHT];
public Tetris() {
setPreferredSize(new Dimension(WIDTH * SQUARE_SIZE, HEIGHT * SQUARE_SIZE));
setBackground(Color.BLACK);
setFocusable(true);
addKeyListener(this);
initializeTetrominoes();
}
private void initializeTetrominoes() {
tetrominoes.add(new int[][] { { 1, 1, 1, 1 } }); // I shape
tetrominoes.add(new int[][] { { 1, 1, 0 }, { 0, 1, 1 } }); // Z shape
tetrominoes.add(new int[][] { { 0, 1, 1 }, { 1, 1, 0 } }); // S shape
tetrominoes.add(new int[][] { { 1, 1, 1 }, { 0, 0, 1 } }); // J shape
tetrominoes.add(new int[][] { { 1, 1, 1 }, { 1, 0, 0 } }); // L shape
tetrominoes.add(new int[][] { { 0, 1, 0 }, { 1, 1, 1 } }); // T shape
tetrominoes.add(new int[][] { { 1, 1 }, { 1, 1 } }); // O shape
}
private void start() {
isStarted = true;
isPaused = false;
score = 0;
clearBoard();
newPiece();
timer = new Timer(DELAY, this);
timer.start();
}
private void pause() {
if (!isStarted) {
return;
}
isPaused = !isPaused;
if (isPaused) {
timer.stop();
} else {
timer.start();
}
repaint();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
drawBoard(g);
drawPiece(g);
}
private void drawBoard(Graphics g) {
for (int i = 0; i < WIDTH; i++) {
for (int j = 0; j < HEIGHT; j++) {
if (board[i][j] != 0) {
g.setColor(Color.CYAN);
g.fillRect(i * SQUARE_SIZE, j * SQUARE_SIZE, SQUARE_SIZE, SQUARE_SIZE);
}
}
}
}
private void drawPiece(Graphics g) {
g.setColor(Color.YELLOW);
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (tetrominoes.get(0)[i][j] != 0) {
g.fillRect((currentX + i) * SQUARE_SIZE, (currentY + j) * SQUARE_SIZE, SQUARE_SIZE, SQUARE_SIZE);
}
}
}
}
private void newPiece() {
int randomIndex = (int) (Math.random() * tetrominoes.size());
currentX = WIDTH / 2;
currentY = 0;
if (!isMoveValid(tetrominoes.get(randomIndex), currentX, currentY)) {
isStarted = false;
timer.stop();
}
}
private boolean isMoveValid(int[][] shape, int newX, int newY) {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
int x =

0
当涂县| 盐池县| 涞源县| 抚顺县| 贵州省| 定州市| 泾源县| 汕头市| 定襄县| 乐业县| 台安县| 沁阳市| 富平县| 牙克石市| 深圳市| 昆明市| 云梦县| 长顺县| 佛学| 赤峰市| 龙南县| 宁化县| 英德市| 云安县| 垦利县| 吴江市| 海淀区| 靖远县| 西盟| 湛江市| 铁岭县| 鄯善县| 渑池县| 五河县| 泊头市| 赤水市| 普陀区| 叶城县| 蓝山县| 吉木乃县| 衡阳市|