我寫了一些代碼,使迷宮和填充JPanel
與我的數字Wall
,Player
,Road
和Gate
。我想通過按鍵移動播放器W,A,S,D。KeyListener沒有響應
幀被繪製但球員不動:
Sprite[][] spr = fr.read();
int x1 = fr.row;
int y1 = fr.col;
fr.close();
GameFrame gf = new GameFrame(spr, x1, y1);
x1 = (x1 * 20) + 30;
y1 = (y1 * 20);
JFrame frame = new JFrame();
frame.setTitle("Lab");
frame.setSize(new Dimension(y1, x1));
frame.setPreferredSize(new Dimension(y1, x1));
frame.addKeyListener(gf);
frame.setFocusable(true);
frame.add(gf);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setResizable(false);
public void paint(Graphics g) {
int a13 = 5;
int a14 = 5;
Player pl = new Player();
super.paintComponent(g);
for (int i = 0; i < i1; i++) {
for (int i0 = 0; i0 < i2; i0++) {
Sprite currentSpr1 = sprites[i][i0];
X = currentSpr1.setXCoord(i);
Y = currentSpr1.setYCoord(i0);
H = currentSpr1.setHeight();
W = currentSpr1.setWidth();
if (currentSpr1 == pl) {
addKeyListener(this);
}
currentSpr1.YourPaint(g, X, Y, H, W);
}
}
public void keyPressed(KeyEvent e) {
int c = e.getKeyCode();
if (c == KeyEvent.VK_W) {
Y = Y - 20;
System.out.print("lol");
repaint();
}
if (c == KeyEvent.VK_S) {
Y = Y + 20;
//repaint();
}
if (c == KeyEvent.VK_D) {
X = X + 20;
//repaint();
}
if (c == KeyEvent.VK_A) {
X = X - 20;
//repaint();
}
//if (sprite instanceof Wall){
//}*/
}
兩個for循環中的'i1'和'i2'是什麼? – Blip
@Blip它的行和列,所以它貫穿整個龐大的 –
'currentSpr1 == pl'曾經計算爲'true'嗎? – Blip