S'il vous plaît trouver ci-joint une partie du code source pour un jeu de serpent que je suis en train de créer:Classes pour Java jeu de serpent ne sont pas compilent
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);
}
}
}
}
Beaucoup de ceci fonctionne très bien. Cependant, où il est dit 'private Color tileColor;', je reçois 'Je reçois' Erreur de syntaxe sur token tileColor ', s'il vous plaît supprimer le jeton' mais quand je supprime cela, il provoque encore plus de rouge sur mon IDE (je suis en utilisant Eclipse).
Aussi, chaque fois MAP_SIZE et tile_size apparaissent, il dit qu'ils ne peuvent pas être résolus à une variable, en dépit du fait qu'ils sont présents dans la classe suivante:
package serpent; Au sein du même paquet, et devrait donc être facile à trouver pour le compilateur.
Il semble que 'TILE_SIZE' et' MAP_SIZE' se trouvent dans une classe différente ('GameBoard'). Essayez 'GameBoard.TILE_SIZE' et' GameBoard.MAP_SIZE'. – mostruash
Essayez de changer 'EMPTY (null),' à 'EMPTY (null);' (notez le changement de virgule en point-virgule). – OldCurmudgeon