对于我们的Tetris项目,我们在400x800 JPanel中嵌套了一个10x20的网格。创建的Tetromino类具有一个private int[] coordinate
,该类可以在生成碎片时设置坐标
public void spawnTetromino() {
...
int[] spawnCoordinate = new int[] {Y_VALUE, 5};
fallingTetromino.setCoordinate(spawnCoordinate);
....
if (!gameOver) {
projectTetromino(spawnCoordinate)
projectTetromino()
在此处突出显示:
public void projectTetromino(int[] coordinate) {
// projects the shape of the tetromino onto the board based on its coordinate
// only projects the non-0 (the filled) indices of the shape
int[][] shape = fallingTetromino.getShape();
for (int y = 0; y < 4; y++) {
for (int x = 0; x < 4; x++) {
if (shape[y][x] != 0) {
grid[coordinate[0]+y][coordinate[1]+x] = shape[y][x];
}
}
}
}
创建了一个测试网格,它将输出如下内容:
|0 0 0 0 0 0 0 0 0 0|
|0 0 0 0 0 0 0 0 0 0|
|0 0 0 0 0 0 0 0 0 0|
|0 0 0 0 0 0 0 0 0 0|
|0 0 0 0 0 0 0 0 0 0|
|0 0 0 0 0 0 0 0 0 0|
|0 0 0 0 0 0 0 0 0 0|
|0 0 0 0 1 1 1 0 0 0|
|0 0 0 0 0 1 0 0 0 0|
|0 0 0 0 0 0 0 0 0 0|
|0 0 0 0 0 0 0 0 0 0|
|0 0 0 0 0 0 0 0 0 0|
|0 0 0 0 0 0 0 0 0 0|
|0 0 0 0 0 0 0 0 0 0|
|0 0 0 0 0 0 0 0 0 0|
|0 0 0 0 0 0 0 0 0 0|
|0 0 0 0 0 0 0 0 0 0|
|0 0 0 0 0 0 0 0 0 0|
|0 0 0 0 0 0 0 0 0 0|
|0 0 0 0 0 0 0 0 0 0|
我的工作是弄清楚如何在此网格中实现图形。这是我目前拥有的代码
public void paintComponent(Graphics g) {
super.paintComponent(g);
tile = new Rectangle(tileX,tileY,tileWidth, tileHeight);
int[] tileCoordinate = {tileX, tileY};
Graphics2D g2 = (Graphics2D) g;
g2.setBackground(Color.BLACK);
//filling out the tiles
for (int x = 0; x <= 10; x++) {
tile.y = 0;
for (int y = 0; y <=20; y++) {
//check if the tetronimo shape is 0
tile.y = tile.y + 40;
int[] shape = fallingTetromino.getCoordinate();
//somehow check if tetromino is inside one of the tile????
g2.fill(tile);
}
tile.x = tile.x + 40;
}
}
我的最终目标是遍历此图形网格并填写只有Tetromino占据的图块(即非0的图块)。我将如何处理?
答案 0 :(得分:0)
如果要将其实现为图形,则需要在主要游戏方法中进行常规游戏循环。因为您尝试移动对象,所以必须将它们重新初始化(或更新其位置)。
我的猜测是安装Java编辑器并查看几个可用的对象。该程序将为您提供图形界面和实时代码更新,您只需在常规IDE中复制它们即可。我的建议是绘制多边形或使用一个小物体(1像素x 1像素),然后为其着色。
Java编辑器:Download here