2013-03-05 68 views
2

附上一些源代码为贪吃蛇的游戏,我想创建:类的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; 
} 

在同一个包中,因此编译器应该很容易找到。

+0

看起来'TILE_SIZE'和'MAP_SIZE'属于不同的类('GameBoard')。尝试'GameBoard.TILE_SIZE'和'GameBoard.MAP_SIZE'。 – mostruash 2013-03-05 21:29:46

+0

尝试将'EMPTY(null)'改为'EMPTY(null);'(注意从逗号变为分号)。 – OldCurmudgeon 2013-03-05 23:20:19

回答

7

你需要一个分号这里:

SNAKE(Color.GREEN), 
FRUIT(Color.RED), 
EMPTY(null); <-- 

这是必需的enums含有不是简单的常量定义更多。从docs

当有字段和方法时,枚举常量列表必须以分号结尾。


MAP_SIZETILE_SIZE,因为他们在不同的类存在不能得到解决,GameBoard。这里有2种选择:

  • 使用合格的名称,即GameBoard.MAP_SIZE & GameBoard.TILE_SIZE

  • 作为enums可以实现接口:请GameBoard接口实施的界面。这些变量将成为成员变量。
+0

谢谢,但我仍然得到MAP_SIZE和TILE_SIZE不能解析为变量。我该怎么分类呢? – 2013-03-05 21:28:30

+0

你需要把他们的类名放在他们面前,像这样: GameBoard.MAP_SIZE – 2013-03-05 21:30:12

+0

好吧我刚刚试过'公共枚举TileType实现GameBoard {'。我现在已经使GameBoard成为一个界面,它说'类型GameBoard不能是TileType的超级界面;一个超级接口必须是一个接口'。这有什么办法吗?或者我误解你的建议? – 2013-03-05 22:07:54