我正在编写一个游戏,并且有2个玩家同时玩游戏。如果两个玩家同时按下一个键并且玩家释放了他的钥匙,则另一个玩家没有移动。
import java.awt.*;
import javax.swing.*;
import java.awt.*;
import javax.swing.*;
import java.applet.*;
import java.awt.event.*;
import java.util.ArrayList;
public class MultiKey extends Applet implements KeyListener {
int x = 240, y = 240;
int x2 = 240, y2 = 240;
public ArrayList<Integer> keysDown;
public void init() {
this.addKeyListener(this);
keysDown = new ArrayList<Integer>();
setFocusable(true);
}
public void paint(Graphics g) {
setSize(500, 500);
g.setColor(Color.GREEN);
g.fillRect(x, y, 50, 50);
g.setColor(Color.BLUE);
g.fillRect(x2 - 50, y2 - 50, 50, 50);
g.drawString(keysDown + "", 100, 100);
}
public void keyPressed(KeyEvent e) {
if (!keysDown.contains(e.getKeyCode())) {
keysDown.add(new Integer(e.getKeyCode()));
}
if (keysDown.contains(new Integer(KeyEvent.VK_UP))) {
y = y - 5;
}
if (keysDown.contains(new Integer(KeyEvent.VK_DOWN))) {
y = y + 5;
}
if (keysDown.contains(new Integer(KeyEvent.VK_LEFT))) {
x = x - 5;
}
if (keysDown.contains(new Integer(KeyEvent.VK_RIGHT))) {
x = x + 5;
}
if (keysDown.contains(new Integer(KeyEvent.VK_W))) {
y2 = y2 - 5;
}
if (keysDown.contains(new Integer(KeyEvent.VK_S))) {
y2 = y2 + 5;
}
if (keysDown.contains(new Integer(KeyEvent.VK_A))) {
x2 = x2 - 5;
}
if (keysDown.contains(new Integer(KeyEvent.VK_D))) {
x2 = x2 + 5;
}
repaint();
}
public void keyReleased(KeyEvent e) {
keysDown.remove(new Integer(e.getKeyCode()));
}
public void keyTyped(KeyEvent e) {
}
}