gpt4 book ai didi

java - 玩家移动的 ActionListener 或 Action

转载 作者:行者123 更新时间:2023-11-30 02:08:30 25 4
gpt4 key购买 nike

我只是想编写一个小足球游戏,在完成所有绘画并初始化球员和球之后,我现在正尝试移动球员。我观看了一些如何在游戏中对运动进行编程的教程,但它们都使用 KeyListeners。正如我在网上写的那样,KeyListener 不是一个好方法。 (还有使用KeyListeners的情况吗?)。然而,我正在尝试实现一个平稳且面向对象的正确运动。我找不到以“正确”方式教授这一点的在线资源。我指定“正确”的方式是因为我从顶级 Java 程序员那里读到了关于 SO 的建议。我正在努力解决什么用于移动以及在哪里实现它?我考虑过使用 Actions 并在 Player 的内部类中实现它?这是正确的方法吗?

到目前为止,这是我的代码:

速度球:

package SpeedballMinimal;

import javax.swing.*;
import java.awt.*;

public class Speedball{

public static final int AREA_WIDTH = 1400;
public static final int AREA_HEIGHT = 700;

public Speedball() {
}

private void start() {
JFrame mainFrame = new JFrame("Speedball");
SpeedballPanel panel = new SpeedballPanel();
mainFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
mainFrame.setLayout(new BorderLayout());
mainFrame.setSize(AREA_WIDTH, AREA_HEIGHT);
mainFrame.add(panel, BorderLayout.CENTER);
mainFrame.setLocationRelativeTo(null);
mainFrame.setResizable(false);
mainFrame.setVisible(true);
}

public static void main(String[] args) {
Speedball speedball = new Speedball();
speedball.start();
}
}

速度球面板:

package SpeedballMinimal;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class SpeedballPanel extends JPanel implements ActionListener {

private Timer timer;
private Renderer renderer;
private Player player1;
private Player player2;

public SpeedballPanel() {
initPanel();
initUserInteractions();
initTimer();

this.renderer = new Renderer();
this.player1 = new Player(300, 300);
this.player2 = new Player(500, 500);
}

private void initPanel() {
this.setSize(Speedball.AREA_WIDTH, Speedball.AREA_HEIGHT);
}


@Override
public void paintComponent(Graphics g) {
renderer.drawBackground(g, getWidth(), getHeight());
renderer.drawPlayer(g, player1.getX(), player1.getY(), player1.getWidth(), player1.getHeight());
renderer.drawPlayer(g, player2.getX(), player2.getY(), player2.getWidth(), player2.getHeight());
}

/*
* Playermovement
*/
private void playerMoveUp(Player player) {
player.setY(player.getY() - 10);
System.out.println("Player Y: " + player.getY());
}

private void playerMoveDown(Player player) {
player.setY(player.getY() + 10);
System.out.println("Player Y: " + player.getY());
}

private void playerMoveLeft(Player player) {
player.setX(player.getX() - 10);
System.out.println("Player X: " + player.getX());
}

private void playerMoveRigth(Player player) {
player.setX(player.getX() + 10);
System.out.println("Player X: " + player.getX());
}

/*
* Actions for playermovement
*/
Action player1MoveUp = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
playerMoveUp(player1);
}
};

Action player1MoveDown = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
playerMoveDown(player1);
}
};

Action player1MoveLeft = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
playerMoveLeft(player1);
}
};

Action player1MoveRight = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
playerMoveRigth(player1);
}
};

Action player2MoveUp = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
playerMoveUp(player2);
}
};

Action player2MoveDown = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
playerMoveDown(player2);
}
};

Action player2MoveLeft = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
playerMoveLeft(player2);
}
};

Action player2MoveRight = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
playerMoveRigth(player2);
}
};

/*
* Define keys for user interaction
*/
private void initUserInteractions() {
//define InputMaps
this.getInputMap().put(KeyStroke.getKeyStroke("W"), "player1MoveUp");
this.getInputMap().put(KeyStroke.getKeyStroke("S"), "player1MoveDown");
this.getInputMap().put(KeyStroke.getKeyStroke("A"), "player1MoveLeft");
this.getInputMap().put(KeyStroke.getKeyStroke("D"), "player1MoveRight");

this.getInputMap().put(KeyStroke.getKeyStroke("UP"), "player2MoveUp");
this.getInputMap().put(KeyStroke.getKeyStroke("DOWN"), "player2MoveDown");
this.getInputMap().put(KeyStroke.getKeyStroke("LEFT"), "player2MoveLeft");
this.getInputMap().put(KeyStroke.getKeyStroke("RIGHT"), "player2MoveRight");

//define ActionMaps
this.getActionMap().put("player1MoveUp", player1MoveUp);
this.getActionMap().put("player1MoveDown", player1MoveDown);
this.getActionMap().put("player1MoveLeft", player1MoveLeft);
this.getActionMap().put("player1MoveRight", player1MoveRight);

this.getActionMap().put("player2MoveUp", player2MoveUp);
this.getActionMap().put("player2MoveDown", player2MoveDown);
this.getActionMap().put("player2MoveLeft", player2MoveLeft);
this.getActionMap().put("player2MoveRight", player2MoveRight);
}

private void initTimer() {
timer = new Timer(1, this);
timer.start();
}

@Override
public void actionPerformed(ActionEvent e) {
repaint();
}
}

渲染器:

package SpeedballMinimal;

import java.awt.*;

public class Renderer {

public Renderer() {
}

public void drawBackground(Graphics g, int areaWidth, int areaHeight) {
g.setColor(new Color(106, 237, 49));
g.fillRect(0,0, areaWidth, areaHeight);
}

public void drawPlayer(Graphics g, int playerX, int playerY, int playerWidth, int playerHeight) {
g.setColor(Color.BLACK);
g.fillOval(playerX, playerY, playerWidth, playerHeight);
}
}

玩家:

package SpeedballMinimal;

import java.util.Random;

public class Player {

private int x;
private int y;
private int width;
private int height;
private int velocity = 2;

private Random rand = new Random();

public Player() {
this.x = 300;
this.y = 300;
this.width = 50;
this.height = 50;
}

public int getX() {
return x;
}

public void setX(int x) {
this.x = x;
}

public int getY() {
return y;
}

public void setY(int x) {
this.y = y;
}

public int getWidth() {
return width;
}

public void setWidth(int width) {
this.width = width;
}

public int getHeight() {
return height;
}

public void setHeight(int height) {
this.height = height;
}
}

编辑:

我将代码编辑为一个最小的示例。此代码在我的 JPanel 上生成一个椭圆形。我尝试按照建议使用 KeyBindings 为该椭圆形创建运动。我使用了这个资源:

我现在想要解决以下问题:

  • 我无法同时移动两个玩家,而且我在网上找不到任何解决此问题的方法。当玩家1正在移动并且我按下玩家2的向上键时,玩家1停止移动。现在我想知道如何让两个玩家同时移动

编辑2:

这是我基于 c0der 的答案的新代码

SpeedballPanel(类已更改):

package SpeedballMinimal;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class SpeedballPanel extends JPanel {

private Renderer renderer;
private Player player1;
private Player player2;

public SpeedballPanel() {
initPanel();
initUserInteractions();

this.renderer = new Renderer();
this.player1 = new Player(Team.ONE);
this.player2 = new Player(Team.TWO);
}

private void initPanel() {
this.setSize(Speedball.AREA_WIDTH, Speedball.AREA_HEIGHT);
}


@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
renderer.drawBackground(g, getWidth(), getHeight());
renderer.drawField(g, getWidth(), getHeight());
renderer.drawGoals(g, getWidth(), getHeight());
renderer.drawPlayer(g, player1.getX(), player1.getY(), player1.getWidth(), player1.getHeight());
renderer.drawPlayer(g, player2.getX(), player2.getY(), player2.getWidth(), player2.getHeight());
}

/*
* Playermovement
*/
private void movePlayer(Player player, MovementDirection direction) {
if (direction == MovementDirection.UP) {
player.setY(player.getY() - 10);
}
if (direction == MovementDirection.DOWN) {
player.setY(player.getY() + 10);
}
if (direction == MovementDirection.LEFT) {
player.setX(player.getX() - 10);
}
if (direction == MovementDirection.RIGHT) {
player.setX(player.getX() + 10);
}
repaint();
}

/*
* Actions for playermovement
*/
Action player1MoveUp = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
movePlayer(player1, MovementDirection.UP);
}
};

Action player1MoveDown = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
movePlayer(player1, MovementDirection.DOWN);
}
};

Action player1MoveLeft = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
movePlayer(player1, MovementDirection.LEFT);
}
};

Action player1MoveRight = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
movePlayer(player1, MovementDirection.RIGHT);
}
};

Action player2MoveUp = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
movePlayer(player2, MovementDirection.UP);
}
};

Action player2MoveDown = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
movePlayer(player2, MovementDirection.DOWN);
}
};

Action player2MoveLeft = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
movePlayer(player2, MovementDirection.LEFT);
}
};

Action player2MoveRight = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
movePlayer(player2, MovementDirection.RIGHT);
}
};

/*
* Define keys for user interaction
*/
private void initUserInteractions() {
//define InputMaps
this.getInputMap().put(KeyStroke.getKeyStroke("W"), "player1MoveUp");
this.getInputMap().put(KeyStroke.getKeyStroke("S"), "player1MoveDown");
this.getInputMap().put(KeyStroke.getKeyStroke("A"), "player1MoveLeft");
this.getInputMap().put(KeyStroke.getKeyStroke("D"), "player1MoveRight");

this.getInputMap().put(KeyStroke.getKeyStroke("UP"), "player2MoveUp");
this.getInputMap().put(KeyStroke.getKeyStroke("DOWN"), "player2MoveDown");
this.getInputMap().put(KeyStroke.getKeyStroke("LEFT"), "player2MoveLeft");
this.getInputMap().put(KeyStroke.getKeyStroke("RIGHT"), "player2MoveRight");

//define ActionMaps
this.getActionMap().put("player1MoveUp", player1MoveUp);
this.getActionMap().put("player1MoveDown", player1MoveDown);
this.getActionMap().put("player1MoveLeft", player1MoveLeft);
this.getActionMap().put("player1MoveRight", player1MoveRight);

this.getActionMap().put("player2MoveUp", player2MoveUp);
this.getActionMap().put("player2MoveDown", player2MoveDown);
this.getActionMap().put("player2MoveLeft", player2MoveLeft);
this.getActionMap().put("player2MoveRight", player2MoveRight);
}
}

MovementDirection(已创建枚举):

package SpeedballMinimal;

public enum MovementDirection {
UP, DOWN, LEFT, RIGHT
}

注意:剩余的类没有改变

实际上我不想随机或自动移动player2,我希望它通过箭头键移动,如我对InputMaps的定义所示。但我仍然遇到同样的问题,当我尝试使用 WASD 键移动玩家 1 并尝试同时使用向上、向下、向左、向右箭头键移动玩家 2 时,第一个移动的玩家停止移动,而另一个玩家停止移动开始移动,但他们无法同时移动。抱歉,如果我在第一篇文章中不清楚玩家的移动。

最佳答案

从代码中可以假设您希望计时器移动另一个球。
按照以下代码应用更改来移动玩家 1(关键操作是移动玩家 2,尽管您希望它移动玩家 1。我没有检查原因)。

private void initTimer() {
timer = new Timer(500, this);
timer.start();
}

@Override
public void actionPerformed(ActionEvent e) {
//move player 1
randomMovePlayer(player1);
repaint();
}

//randomly move player
private void randomMovePlayer(Player player) {

Random rand = new Random(); //refactor it to a field
float direction = rand.nextFloat();
if(direction < .25) {
playerMoveLeft(player);
}else if(direction < .5) {
playerMoveUp(player);
}else if(direction < .75) {
playerMoveDown(player);
}else {
playerMoveRigth(player1);
}
//todo improve logic to get the right movement within bounds
}

此外,在四种移动方法中,您都需要在移动后重新绘制:

private void playerMoveUp(Player player) {
player.setY(player.getY() - 10);
System.out.println("Player Y: " + player.getY());
repaint(); //repaint after each move
}

旁注:所有四个 Action 都可以通过一种方法处理:

enum Direction{LEFT, RIGHT, UP, DOWN};

private void movePalyer(Player player, Direction dir) {
//todo move logic
}

回应编辑2:
尝试在按下键时开始连续运动(通过启动计时器),并在释放同一键时停止它。

//do on w key press
this.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_W,0, false), "player1MoveUp");
//do on w key release
this.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_W,0, true), "stop");

Action player1MoveUp = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
move(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent arg0) {
movePlayer(player1, MovementDirection.UP);
}
});
}
};

//note that you can't use the same timer for both
//players if you don't want a button release stop both
private void move(Action play) {
if((timer !=null) && timer.isRunning()) { //timer is a field
return;
}
timer = new Timer(100, play);
timer.setInitialDelay(0);
timer.start();
}

Action stop = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
timer.stop();
}
};

关于java - 玩家移动的 ActionListener 或 Action,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50756741/

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