주어진 컨텍스트에서 별도의 스레드를 사용하여 키 바인딩을 처리하고 게임 상태를 업데이트할 수 있습니다. 이를 통해 플레이어는 서로 다른 키를 사용하여 독립적으로 패들을 제어할 수 있으며 두 플레이어가 동시에 패들을 움직일 수 있습니다.
다음은 스레드와 함께 키 바인딩을 사용하는 방법을 보여주는 예입니다.
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.util.HashSet; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.atomic.AtomicBoolean; import javax.swing.JFrame; import javax.swing.SwingUtilities; public class Main { private static final int WIDTH = 600; private static final int HEIGHT = 500; public static void main(String[] args) { // Create a new game instance Game game = new Game(WIDTH, HEIGHT); // Create a thread pool to handle key bindings ExecutorService keyBindingPool = Executors.newCachedThreadPool(); // Create key bindings for the two players KeyBindings player1Bindings = new KeyBindings(game, "W", "S"); KeyBindings player2Bindings = new KeyBindings(game, "UP", "DOWN"); // Install the key bindings using SwingUtilities to ensure they work on the EDT SwingUtilities.invokeLater(() -> { game.getComponent().getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_W, 0, false), "player1Up"); game.getComponent().getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_S, 0, false), "player1Down"); game.getComponent().getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, false), "player2Up"); game.getComponent().getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0, false), "player2Down"); game.getComponent().getActionMap().put("player1Up", new ActionListener() { @Override public void actionPerformed(ActionEvent e) { player1Bindings.up = true; } }); game.getComponent().getActionMap().put("player1Down", new ActionListener() { @Override public void actionPerformed(ActionEvent e) { player1Bindings.down = true; } }); game.getComponent().getActionMap().put("player2Up", new ActionListener() { @Override public void actionPerformed(ActionEvent e) { player2Bindings.up = true; } }); game.getComponent().getActionMap().put("player2Down", new ActionListener() { @Override public void actionPerformed(ActionEvent e) { player2Bindings.down = true; } }); }); // Add the key bindings to the thread pool keyBindingPool.submit(player1Bindings); keyBindingPool.submit(player2Bindings); // Start the game loop game.start(); } private static class Game { private final int width; private final int height; private final Paddle paddle1; private final Paddle paddle2; private final Thread gameLoopThread; private final AtomicBoolean running; public Game(int width, int height) { this.width = width; this.height = height; this.paddle1 = new Paddle(0, 100, 10, 100); this.paddle2 = new Paddle(width - 10, 200, 10, 100); this.running = new AtomicBoolean(false); this.gameLoopThread = new Thread(this::gameLoop); } public void start() { running.set(true); gameLoopThread.start(); } private void gameLoop() { while (running.get()) { // Update the game state based on the key bindings if (player1Bindings.up) { paddle1.moveUp(); } else if (player1Bindings.down) { paddle1.moveDown(); } if (player2Bindings.up) { paddle2.moveUp(); } else if (player2Bindings.down) { paddle2.moveDown(); } // Render the game on the screen paint(); } } public void paint() { // Clear the screen // Draw the paddles // Draw the ball // Update the display } public JComponent getComponent() { return null; } } private static class KeyBindings implements Runnable { private final Game game; private final String upKey; private final String downKey; private final HashSet<String> keysPressed; public boolean up; public boolean down; private AtomicBoolean running; public KeyBindings(Game game, String upKey, String downKey) { this.game = game; this.upKey = upKey; this.downKey = downKey; this.keysPressed = new HashSet<>(); this.running = new AtomicBoolean(false); } public void start() { running.set(true); } @Override public void run() { while (running.get()) { // Check for key presses if (keysPressed.contains(upKey)) { up = true; } else { up = false; } if (keysPressed.contains(downKey)) { down = true; } else { down = false; } // Sleep for a bit try { Thread.sleep(10); } catch (InterruptedException e) {} } } } private static class Paddle { private int x; private int y; private int width; private int height; public Paddle(int x, int y, int width, int height) { this.x = x; this.y = y; this.width = width; this.height = height; } public void moveUp() { if (y > 0) { y -= 5; } } public void moveDown() { if (y < game.getHeight() - height) { y += 5; } } } }
이 예에서:
이 예에서는 두 플레이어가 서로 다른 키를 사용하여 독립적으로 패들을 움직일 수 있으며 게임 상태가 원활하고 반응적으로 업데이트됩니다.
위 내용은 스레드는 2인 게임에서 키 바인딩 처리를 어떻게 향상시킬 수 있습니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!