在KeyPressed方法中,一次只能处理一个以上的密钥。我搜索了这个,但不知道如何处理我编写代码的方式。如果您能告诉我如何处理我的代码,我将不胜感激?
private boolean play = false;
private int player1Score = 0;
private int player2Score = 0;
private int player1Y = 215;
private int player2Y = 215;
private Timer timer;
private int delay = 8;
private int ballposX = 120;
private int ballposY = 350;
private int ballXdir = -2;
private int ballYdir = -3;
public GamePlay(){
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
timer = new Timer(delay, this);
timer.start();
}
public void paint(Graphics g){
g.setColor(Color.black);
g.fillRect(1, 1, 692, 592);
g.setColor(Color.yellow);
g.fillRect(0, 0, 3, 492);
g.fillRect(0, 0, 692, 3);
g.fillRect(691, 0, 3, 492);
g.fillRect(0, 468, 692, 3);
g.setColor(Color.white);
g.fillRect(15, player1Y, 10, 75);
g.fillRect(670, player2Y, 10, 75);
g.fillOval(ballposX, ballposY, 20, 20);
g.setColor(Color.white);
g.setFont(new Font("serif", Font.BOLD, 25));
g.drawString("" + player1Score, 175, 30);
g.setFont(new Font("serif", Font.BOLD, 25));
g.drawString("" + player2Score, 525, 30);
if(player1Score >= 11){
play = false;
ballXdir = 0;
ballYdir = 0;
g.setColor(Color.red);
g.setFont(new Font("serif", Font.BOLD, 30));
g.drawString("Player 1 Won", 260, 200);
g.setFont(new Font("serif", Font.BOLD, 20));
g.drawString("Press Enter to restart ", 250, 250);
}
if(player2Score >= 11){
play = false;
ballXdir = 0;
ballYdir = 0;
g.setColor(Color.red);
g.setFont(new Font("serif", Font.BOLD, 30));
g.drawString("Player 2 Won", 260, 200);
g.setFont(new Font("serif", Font.BOLD, 20));
g.drawString("Press Enter to restart ", 250, 250);
}
g.dispose();
}
@Override
public void actionPerformed(ActionEvent arg0) {
timer.start();
if(play){
if(new Rectangle(ballposX, ballposY, 20, 20).intersects(new Rectangle(15, player1Y, 10, 75))){
ballXdir = -ballXdir;
}
if(new Rectangle(ballposX, ballposY, 20, 20).intersects(new Rectangle(670, player2Y, 10, 75))){
ballXdir = -ballXdir;
}
ballposX += ballXdir;
ballposY += ballYdir;
if(ballposX < 0){
ballXdir = -ballXdir;
player2Score += 1;
}
if(ballposY < 0){
ballYdir = -ballYdir;
}
if(ballposX > 670){
ballXdir = -ballXdir;
player1Score += 1;
}
if(ballposY > 460){
ballYdir = -ballYdir;
}
}
repaint();
}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_S){
if(player1Y >= 390){
player1Y = 390;
}
else{
moveUp2();
}
}
if(e.getKeyCode() == KeyEvent.VK_W){
if(player1Y <= 10){
player1Y = 10;
}
else{
moveDown2();
}
}
if(e.getKeyCode() == KeyEvent.VK_DOWN){
if(player2Y >= 390){
player2Y = 390;
}
else{
moveUp();
}
}
if(e.getKeyCode() == KeyEvent.VK_UP){
if(player2Y <= 10){
player2Y = 10;
}
else{
moveDown();
}
}
if(e.getKeyCode() == KeyEvent.VK_ENTER){
if(!play){
play = true;
ballposX = 120;
ballposY = 350;
ballXdir = -2;
ballYdir = -3;
player1Y = 215;
player2Y = 215;
player1Score = 0;
player2Score = 0;
repaint();
}
}
}
private void moveDown() {
play = true;
player2Y -= 20;
repaint();
}
private void moveUp() {
play = true;
player2Y += 20;
repaint();
}
private void moveDown2() {
play = true;
player1Y -= 20;
repaint();
}
private void moveUp2() {
play = true;
player1Y += 20;
repaint();
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
答案 0 :(得分:0)
您可以跟踪按下的键。每当释放一个键时,您将其从按下的键列表中删除。如果按下某个键时按下的键列表不为空,则按下多个键。