在給定的上下文中,您可以使用單獨的執行緒來處理鍵綁定並更新遊戲狀態。這允許玩家使用不同的按鍵獨立控制他們的球拍,並確保兩個玩家可以同時移動他們的球拍。
以下範例示範如何使用執行緒的鍵綁定:
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; } } } }
在此範例中:
此範例可確保兩個玩家都可以使用不同的鍵獨立移動球拍,並且遊戲狀態會平滑且響應迅速地更新。
以上是線程如何改進兩人遊戲中的按鍵綁定處理?的詳細內容。更多資訊請關注PHP中文網其他相關文章!