gpt4 book ai didi

Java 2 播放器在 1 个键盘上使用 AWSD 和箭头

转载 作者:太空宇宙 更新时间:2023-11-04 11:06:39 24 4
gpt4 key购买 nike

我正在用java制作一个游戏,它运行得很好。但问题是,当我在 map 上生成 2 个玩家并且我正在移动 1 个玩家时,另一个玩家也会同步移动。我希望玩家 1 使用 (AWSD),玩家 2 使用箭头。

这是我现在的类代码:“KeyInput”

 public void keyPressed(KeyEvent e){
int key = e.getKeyCode();

//If the special key is pressed, set it to true.
if(key == KeyEvent.VK_W) handler.setUp(true);
if(key == KeyEvent.VK_S) handler.setDown(true);
if(key == KeyEvent.VK_A) handler.setLeft(true);
if(key == KeyEvent.VK_D) handler.setRight(true);

if(key == KeyEvent.VK_I) handler.setUp(true);
if(key == KeyEvent.VK_K) handler.setDown(true);
if(key == KeyEvent.VK_J) handler.setLeft(true);
if(key == KeyEvent.VK_L) handler.setRight(true);
}
//If the pressed key is released, set everything to false.
public void keyReleased(KeyEvent e){
int key = e.getKeyCode();

//If the special key is released, put it to false.
if(key == KeyEvent.VK_W) handler.setUp(false);
if(key == KeyEvent.VK_S) handler.setDown(false);
if(key == KeyEvent.VK_A) handler.setLeft(false);
if(key == KeyEvent.VK_D) handler.setRight(false);
if(key == KeyEvent.VK_P) handler.setDropBomb(true);

if(key == KeyEvent.VK_I) handler.setUp(false);
if(key == KeyEvent.VK_K) handler.setDown(false);
if(key == KeyEvent.VK_J) handler.setLeft(false);
if(key == KeyEvent.VK_L) handler.setRight(false);
if(key == KeyEvent.VK_M) handler.setDropBomb(true);
}

在“Player”类中,代码如下:

public Player(int x, int y, ID id, Handler handler, Game game, SpriteSheet ss) {
super(x, y, id, ss);
this.handler = handler;
this.game = game;

//Get the player image of the SpriteSheet
player_image= ss.grabImage(1,1,32,48);
}

public void tick() {
x += velX;
y += velY;

collision();

//Set the movement of the player: left,right,up,down
if(handler.isUp()) velY = -5;
else if(!handler.isDown()) velY = 0;

if(handler.isDown()) velY = 5;
else if(!handler.isUp()) velY = 0;

if(handler.isRight()) velX = 5;
else if(!handler.isLeft()) velX = 0;

if(handler.isLeft()) velX = -5;
else if(!handler.isRight()) velX = 0;

if(handler.isDropped()){
Dropbomb(x,y);
}

}

我已经思考这个问题很多年了。我尝试制作第二个处理程序,但这导致了很多重复的代码。不知何故有一个想法?

最佳答案

您需要区分哪些键影响玩家 1,哪些键影响玩家 2。像这样的事情:

    //If the special key is pressed, set it to true.
if(key == KeyEvent.VK_W) handler.setUpPlayer1(true);
if(key == KeyEvent.VK_S) handler.setDownPlayer1(true);
if(key == KeyEvent.VK_A) handler.setLeftPlayer1(true);
if(key == KeyEvent.VK_D) handler.setRightPlayer1(true);

if(key == KeyEvent.VK_I) handler.setUpPlayer2(true);
if(key == KeyEvent.VK_K) handler.setDownPlayer2(true);
if(key == KeyEvent.VK_J) handler.setLeftPlayer2(true);
if(key == KeyEvent.VK_L) handler.setRightPlayer2(true);

或者您可以将玩家作为变量传递:

...
if(key == KeyEvent.VK_W) handler.setUp(true,1);
...
if(key == KeyEvent.VK_I) handler.setUp(true,2);
...

或者,如果您可以全局访问玩家变量,如下所示:

...
if(key == KeyEvent.VK_W) handler.setUp(true, Global.PLAYER_1);
...
if(key == KeyEvent.VK_I) handler.setUp(true, Global.PLAYER_2);
...

关于Java 2 播放器在 1 个键盘上使用 AWSD 和箭头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46414771/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com