1

有没有更优雅/更短/有条理的方式来编写这段代码?

for (int i = 0; i < SCREENSIZE; i++) {
        for (int j = 0; j < SCREENSIZE; j++) {
            if (map[y + i][x + j] == '@')
                g.drawImage(item, j * TILESIZE,i * TILESIZE, null);
            else if (map[y + i][x + j] == ' ')
                g.drawImage(ground, j * TILESIZE,i * TILESIZE, null);
            else if (map[y + i][x + j] == 'i')
                g.drawImage(bush, j * TILESIZE, i * TILESIZE, null);
            else if (map[y + i][x + j] == '~')
                g.drawImage(ocean, j * TILESIZE, i * TILESIZE, null);
            else if (map[y + i][x + j] == '=')
                g.drawImage(fence, j * TILESIZE, i * TILESIZE, null);
            else if (map[y + i][x + j] == '#')
                g.drawImage(grass, j * TILESIZE, i * TILESIZE, null);
            else if (map[y + i][x + j] == 'Y')
                g.drawImage(townsPerson, j * TILESIZE, i * TILESIZE, null);
            else if (map[y + i][x + j] == '/')
                g.drawImage(house01, j * TILESIZE, i * TILESIZE, null);
            else if (map[y + i][x + j] == '¯')
                g.drawImage(house02, j * TILESIZE, i * TILESIZE, null);
            else if (map[y + i][x + j] == '\\')
                g.drawImage(house03, j * TILESIZE, i * TILESIZE, null);
            else if (map[y + i][x + j] == '[')
                g.drawImage(house04, j * TILESIZE, i * TILESIZE, null);
            else if (map[y + i][x + j] == 'n')
                g.drawImage(house05, j * TILESIZE, i * TILESIZE, null);
            else if (map[y + i][x + j] == '_')
                g.drawImage(house06, j * TILESIZE, i * TILESIZE, null);
            else if (map[y + i][x + j] == ']')
                g.drawImage(house07, j * TILESIZE, i * TILESIZE, null);
            else if (map[y + i][x + j] == '`')
                g.drawImage(cground, j * TILESIZE, i * TILESIZE, null);
            else if (map[y + i][x + j] == 'O')
                g.drawImage(boulder, j * TILESIZE, i * TILESIZE, null);
            else if (map[y + i][x + j] == 'Ÿ')
                g.drawImage(alien, j * TILESIZE, i * TILESIZE, null);
            else if (map[y + i][x + j] == '.')
                g.drawImage(tree01, j * TILESIZE, i * TILESIZE, null);
            else if (map[y + i][x + j] == 'T')
                g.drawImage(tree02, j * TILESIZE, i * TILESIZE, null);
        }
    }
4

5 回答 5

5

第一个改进可能是使用 switch/case 结构,但在您的情况下,简单的 map ( Map<Char,Image>) 会更好。

更进一步,您可以使用枚举而不是字符来识别对象,这将帮助您避免拼写错误,但至少您应该使用字符常量,例如

public static final char MAP_ITEM = '@';
public static final char MAP_GROUND = ' ';

等等。

于 2011-03-08T21:26:26.517 回答
1

某处(在构造函数中?),将 a 保存Map为成员变量:

images = new HashMap<Character, Image>();
images.put('@', item);
images.put(' ', ground);

然后,您的绘图将如下所示:

for (int i = 0; i < SCREENSIZE; i++) {
    for (int j = 0; j < SCREENSIZE; j++) {
        g.drawImage(images.get(map[y+i][x+j]), j * TILESIZE, i * TILESIZE, null)
    }
}
于 2011-03-08T21:30:33.967 回答
0

由于您基于角色,因此可以使用switch语句。您可以做的另一件事是,由于您g.drawImage(SOMETHING, j * TILESIZE, i * TILESIZE, null)在每种情况下都在使用,您可以提取与切换后相同的所有内容,并分配一些变量并将其用于更改

前任:

Object graphic;
switch (map[y + i][x + j]) {
case '@': 
    graphic = item;
    break;
case '#':
    graphic = grass;
    break;
// etc....
}
g.drawImage(graphic, j * TILESIZE, i * TILESIZE, null);
于 2011-03-08T21:31:07.713 回答
0

使用开关/案例(不是地图),因为编译器将有更多空间进行优化,因为它知道您要打开的确切字符值集:

...
switch(map[y+i][x+j]) {
    case: '@': image = item; break;
    case: ' ': image = ground; break;
    ...
}
g.drawImage(image, j * TILESIZE, i * TILESIZE, null);
...
于 2011-03-08T21:32:32.523 回答
0

您可以使用带有 char 值的开关,这样可能会更清晰。此外,第二个、第三个和第四个参数总是相同的,因此可以在开关中设置一个 var 并在外部调用该方法。一点伪代码:

for() {
  for() {
     Image obj;
     switch (map[y+i][x +j]) {
       case '@':
         Obj = item;
         break;
     // other cases
       default:
       //default or error handling
   }
   g.drawImage(obj, j * TILESIZE, i * TILESIZE, null);
 }
}
于 2011-03-08T21:36:38.670 回答