3

所以,基本上我需要找到一种“翻转”用于精灵的字符数组的好方法,以使他/它看起来向左,反之亦然。这是我的数组->

WARRIOR = (

" " +         
 "!!!!!     " +        
 "!!oo! ^   " +
 "!!!!! ^   " +
 "##### ^   " +
 "#######   " +
 "#####     " +
 "** **     ").toCharArray();

显示例程如下所示:

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    for (int i = 0; i < WARRIOR.length; i++) {
        int x = (i - 1) % 10;
        int y = (i - 1) / 10;
        if (WARRIOR[i] == '!') {
            g.setColor(new Color(0, 0, 204));
            g.fillRect(x_pos + x * 5, y_pos + y * 5, 5, 5);
        }
        else if (WARRIOR[i] == 'o') {
            g.setColor(new Color(204, 0, 0));
            g.fillRect(x_pos + x * 5, y_pos + y * 5, 5, 5);
        }
        // other characters here...
    }
}​
4

3 回答 3

2

我建议使用备用显示例程来向后绘制精灵,而不是存储精灵的反向副本。

尝试更改此行:

int x = (i - 1) % 10;

对此:

int x = 10 - (i - 1) % 10;

这应该向后绘制精灵。


此外,您可能想查看XPM格式,它与您正在做的非常相似。

于 2012-05-09T03:18:12.467 回答
0

You should consider handling your sprites in another way. For example, since you seem to be fond of char [], you could use a char [] []. If your sprites always have the same width, you can consider something like that:

//Slices your WARRIOR into pieces
final StringBuilder builder = new StringBuilder();
short i = 0;
for (final char a:WARRIOR) {
    if (++i != 10) {    //You should be able to find it
        builder.append(a);
    } else {
        i = 0;
        builder.append('\n');   //Line sep
    }
}

//Print the right oriented WARRIOR
System.out.println(builder);

//And flip it ! yay !
final String [] slicedWarrior = builder.toString().split("\n");   //Line sep
for (final String slice : slicedWarrior) {
    final char [] slicesOfSlice;
    for (int j = (slicesOfSlice = slice.toCharArray()).length - 1; j != 0; j--) {
        System.out.print(slicesOfSlice[j]);
    }
    System.out.print('\n');   //Line sep
}
于 2012-05-09T03:10:27.013 回答
0

在不改变数据结构的情况下,这是一个解决方案(我确信它可以改进但它有效):

精简版

char[] flip = new char[warrior.length];

for(int i=0;i<warrior.length;i++){
    flip[9-i%10+ i/10*10] = warrior[i];   
}

长版

    char[] warrior= (         
             "!!!!!     " +        
             "!!oo! ^   " +
             "!!!!! ^   " +
             "##### ^   " +
             "#######   " +
             "#####     " +
             "** **     ").toCharArray();

    for(int i=0;i<warrior.length;i++){
        if(i%10==0){
            System.out.println();
        }
        System.out.print(warrior[i]);
    }

    char[] flip = new char[warrior.length];

    for(int i=0;i<warrior.length;i++){
        //9-: 0 goes to 9, 1 to 8 etc 
        //i: cycles from 0 to 9
        //i/10: provides the row (e.g. 25/10=2)
        //*10: gives me the 2d row in 1d array  

        flip[9-i%10+ i/10*10] = warrior[i]; 
    }


    for(int i=0;i<flip.length;i++){
        if(i%10==0){
            System.out.println();
        }
        System.out.print(flip[i]);
    }

这将输出

!!!!!     
!!oo! ^   
!!!!! ^   
##### ^   
#######   
#####     
** **     
     !!!!!
   ^ !oo!!
   ^ !!!!!
   ^ #####
   #######
     #####
     ** **

但我会使用不同的数据结构(例如 2D 数组)或将其视为矩阵,并使用本文中的示例

于 2012-05-09T02:35:16.123 回答