所以,基本上我现在正在尝试为我的游戏原型的角色精灵使用 char 数组,但我找不到一种工作方法来读取正确“行”中的每个元素以打印出角色(试图找到一个使用逐行填充矩形来绘制精灵的方法)。同样,我尝试了许多方法,例如if (i % 5 == 0) y_temp += 5;
“缩进”以在新行上填充精灵的矩形,但没有一种方法有效。
建议/帮助任何人?
代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class test extends JFrame {
private int x_pos, y_pos;
private JFrame frame;
private draw dr;
private char[] WARRIOR;
private Container con;
public test() {
x_pos = y_pos = 200;
frame = new JFrame("StixRPG");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1000, 500);
frame.setResizable(false);
frame.setVisible(true);
con = frame.getContentPane();
con.setBackground(Color.black);
dr = new draw();
dr.setBackground(Color.black);
con.add(dr);
WARRIOR = (
" " +
"!!!!!" +
"!!ooo" +
"!!!!!" +
"#####" +
"#####" +
"#####" +
"** **").toCharArray();
}
public static void main(String[] args) {
test tst = new test();
}
class draw extends JPanel {
public draw() {
}
public void paintComponent(Graphics g) {
super.paintComponents(g);
int y_temp = y_pos;
for (int i = 0; i < WARRIOR.length; i++) {
if (WARRIOR[i] == '!') {
g.setColor(new Color(0, 0, 204));
g.fillRect(x_pos+i*5, y_temp, 5, 5);
}
else if (WARRIOR[i] == 'o') {
g.setColor(new Color(204, 0, 0));
g.fillRect(x_pos+i*5, y_temp, 5, 5);
}
else if (WARRIOR[i] == '#') {
g.setColor(new Color(0, 0, 102));
g.fillRect(x_pos+i*5, y_temp, 5, 5);
}
else if (WARRIOR[i] == '*') {
g.setColor(Color.black);
g.fillRect(x_pos+i*5, y_temp, 5, 5);
}
}
}
}
}