按键已激活,但按键未释放



在谷歌上搜索答案,一无所获。

我开始用Java制作2D游戏。

目前在游戏中,应该有两个坦克由两个用户在同一个键盘上控制。

它们在屏幕上的移动方式如下:

按下左右键(或A和D键),应设置角度并旋转精灵,按下向上箭头键(或W键),只要按下向上键,应沿角度方向移动精灵。

我现在在程序中主要有两个wierd错误:

  1. 按下向上箭头键开始移动其中一个坦克的精灵,但释放向上键不会阻止其移动
  2. 用左右箭头旋转一个水箱,会旋转整个屏幕

我以为我会一次解决一个问题(第一个),但如果你知道如何解决另一个问题,请说。

以下是代码中最相关的部分(尽量缩短):

Board类,具有大部分游戏逻辑:

import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Board extends JPanel implements KeyListener, Runnable {
Tank tank1,tank2;
boolean[] keysPressed1,keysPressed2;
public Board(){
setFocusable(true);
setBackground(Color.BLACK);
addKeyListener(this);
tank1 = new Tank("red");
tank2 = new Tank("blue");
Thread gameloop = new Thread(this);
gameloop.start();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.rotate( Math.toRadians(tank1.getAngle()), tank1.getCenter().getX(), tank1.getCenter().getY() );
g2d.drawImage(tank1.getImage(), (int) tank1.getX(), (int) tank1.getY(), this);
g2d.rotate( Math.toRadians(tank2.getAngle()), tank2.getCenter().getX(), tank2.getCenter().getY() );
g2d.drawImage(tank2.getImage(), (int) tank2.getX(), (int) tank2.getY(), this);
}
// simplified version of the game-loop:
public void run(){
while( game_is_running ) {
keysPressed1 = tank1.getKeys();
keysPressed2 = tank2.getKeys();
if(keysPressed1[0]==true)tank1.setAngle(tank1.getAngle()+3);
if(keysPressed1[1]==true)tank1.setAngle(tank1.getAngle()-3);
if(keysPressed1[2]==true){
tank1.setDX(2 * Math.sin(Math.toRadians(tank1.getAngle())));
tank1.setDY(2 * Math.cos(Math.toRadians(tank1.getAngle())));
}
tank1.move();
if(keysPressed2[0]==true)tank2.setAngle(tank2.getAngle()+3);
if(keysPressed2[1]==true)tank2.setAngle(tank2.getAngle()-3);
if(keysPressed2[2]==true){
tank2.setDX(2 * Math.sin(Math.toRadians(tank2.getAngle())));
tank2.setDY(2 * Math.cos(Math.toRadians(tank2.getAngle())));
}
tank2.move();
next_game_tick += SKIP_TICKS;
loops++;
}
repaint();
}
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if(key == KeyEvent.VK_UP || key == KeyEvent.VK_LEFT || key == KeyEvent.VK_RIGHT){ tank1.keyPressed(e); }
if(key == KeyEvent.VK_W || key == KeyEvent.VK_A || key == KeyEvent.VK_D){ tank2.keyPressed(e); }
}
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
if(key == KeyEvent.VK_UP || key == KeyEvent.VK_LEFT || key == KeyEvent.VK_RIGHT){ tank1.keyReleased(e); }
if(key == KeyEvent.VK_W || key == KeyEvent.VK_A || key == KeyEvent.VK_D){ tank2.keyReleased(e); }
}
public void keyTyped(KeyEvent arg0) {}
}

Tank类:

import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Tank extends Entity {
public Tank(String type){
this.type = type;
if(type=="red"){
image = new ImageIcon(this.getClass().getResource("sprites/redtank1.PNG")).getImage();
x = 200 - image.getWidth(null);
y = 400;
}
if(type=="blue"){
image = new ImageIcon(this.getClass().getResource("sprites/bluetank1.PNG")).getImage();
x = 850;
y = 400;
}
width = image.getWidth(null);
height = image.getHeight(null);
center = new Point( ((int) getX()) + (width/2), ((int) getY() + (height/2)) );
angle = 0;
dx = 0;
dy = 0;
keysPressed = new boolean[3];
}
public void keyPressed(KeyEvent e){
int key = e.getKeyCode();
if(key == KeyEvent.VK_LEFT){
keysPressed[0] = true;
}
if(key == KeyEvent.VK_RIGHT){
keysPressed[1] = true;
}
if(key == KeyEvent.VK_UP){
keysPressed[2] = true;
}
}
public void keyReleased(KeyEvent e){
int key = e.getKeyCode();
if(key == KeyEvent.VK_LEFT){
keysPressed[0] = false;
}
if(key == KeyEvent.VK_RIGHT){
keysPressed[1] = false;
}
if(key == KeyEvent.VK_UP){
keysPressed[2] = false;
}
}
}

我不知道如何解决这个问题,非常感谢你的帮助。感谢

对于移动问题当您检查按键时,您可以设置DX和DY。它们控制移动方向和距离。在tank.move()时执行此操作。您永远不会将DX和DY重置为零,因此油箱永远不会停止。您需要添加对keyreleased的检查,并将DX和DY设置为零。

像这样:

if(keysPressed1[2]==false){
tank1.setDX(0);
tank1.setDY(0);
}
tank1.move();
if(keysPressed2[2]==false){
tank2.setDX(0);
tank2.setDY(0);
}
tank2.move();

对于旋转问题:而不是调用

g2d.rotate()

您需要调用带有旋转变换的drawImage。

请参阅,drawImage

g2d.rotate()旋转整个图形上下文。

以下是绘制旋转图像的示例:

public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
AffineTransform tx = new AffineTransform();
tx.setToRotation(Math.toRadians(tank1.getAngle());
tx.translate(tank1.getCenter().getX(),  tank1.getCenter().getY());
g2d.drawImage(tank1.getImage(), tx, this);
AffineTransform tx2 = new AffineTransform();
tx2.setToRotation(Math.toRadians(tank2.getAngle());
tx2.translate(tank2.getCenter().getX(),  tank2.getCenter().getY());
g2d.drawImage(tank2.getImage(), tx2, this);
}

相关内容

  • 没有找到相关文章

最新更新