请找到我正在尝试创建的蛇游戏的一些源代码:
package Snake;
import java.awt.*;
import Snake.GameBoard.*;
public enum TileType {
SNAKE(Color.GREEN),
FRUIT(Color.RED),
EMPTY(null),
private Color tileColor;
private TileType(Color color) {
this.tileColor = color;
}
// @ return
public Color getColor() {
return tileColor;
}
private TileType[] tiles;
public void GameBoard() {
tiles = new TileType[MAP_SIZE * MAP_SIZE];
resetBoard();
}
// Reset all of the tiles to EMPTY.
public void resetBoard() {
for(int i = 0; i < tiles.length; i++) {
tiles[i] = TileType.EMPTY;
}
}
// @ param x The x coordinate of the tile.
// @ param y The y coordinate of the tile.
// @ return The type of tile.
public TileType getTile(int x, int y) {
return tiles[y * MAP_SIZE + x];
}
/**
* Draws the game board.
* @param g The graphics object to draw to.
*/
public void draw(Graphics2D g) {
//Set the color of the tile to the snake color.
g.setColor(TileType.SNAKE.getColor());
//Loop through all of the tiles.
for(int i = 0; i < MAP_SIZE * MAP_SIZE; i++) {
//Calculate the x and y coordinates of the tile.
int x = i % MAP_SIZE;
int y = i / MAP_SIZE;
//If the tile is empty, so there is no need to render it.
if(tiles[i].equals(TileType.EMPTY)) {
continue;
}
//If the tile is fruit, we set the color to red before rendering it.
if(tiles[i].equals(TileType.FRUIT)) {
g.setColor(TileType.FRUIT.getColor());
g.fillOval(x * TILE_SIZE + 4, y * TILE_SIZE + 4, TILE_SIZE - 8, TILE_SIZE - 8);
g.setColor(TileType.SNAKE.getColor());
} else {
g.fillRect(x * TILE_SIZE + 1, y * TILE_SIZE + 1, TILE_SIZE - 2, TILE_SIZE - 2);
}
}
}
}
很多这样的工作正常。但是,在它显示“私有颜色 tileColor;”的地方,我收到“我收到 '令牌 tileColor 的语法错误',请删除令牌”,但是当我删除它时,它会在我的 IDE 上导致更多的红色(我使用 Eclipse)。
此外,每当 MAP_SIZE 和 TILE_SIZE 出现时,它表示它们无法解析为变量,尽管它们存在于以下类中:
包蛇;
public class GameBoard {
public static final int TILE_SIZE = 25;
public static final int MAP_SIZE = 20;
}
在同一个包中,因此编译器应该很容易找到。