附上一些源代碼爲貪吃蛇的遊戲,我想創建:類的Java蛇遊戲沒有編制
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;
}
在同一個包中,因此編譯器應該很容易找到。
看起來'TILE_SIZE'和'MAP_SIZE'屬於不同的類('GameBoard')。嘗試'GameBoard.TILE_SIZE'和'GameBoard.MAP_SIZE'。 – mostruash 2013-03-05 21:29:46
嘗試將'EMPTY(null)'改爲'EMPTY(null);'(注意從逗號變爲分號)。 – OldCurmudgeon 2013-03-05 23:20:19