Need a way to "flip" this char array to sprite the game to look left

So basically I need to find a good way to “flip” the char array used for the sprite to make it / look left and vice versa. Here is my array →

WARRIOR = (

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

The display procedure is as follows:

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...
    }
}​
+3
source share
3 answers

I suggest having an alternative mapping procedure to draw sprites back, rather than storing a reverse copy of the sprite.

Try changing this line:

int x = (i - 1) % 10;

:

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

This should draw sprites back.


In addition, you can take a look at the XPM format , it is very similar to what you are doing.

+2
source

, ( , , ):

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-)

0

You should view sprite images differently. For example, since you seem to love char [], you can use char [] []. If your sprites always have the same width, you might consider something like this:

//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
}
0
source

All Articles