Im gegebenen Kontext können Sie separate Threads verwenden, um Tastenkombinationen zu verwalten und den Spielstatus zu aktualisieren. Dadurch können Spieler ihre Paddles unabhängig voneinander mit verschiedenen Tasten steuern und sicherstellen, dass beide Spieler ihre Paddles gleichzeitig bewegen können.
Hier ist ein Beispiel, das zeigt, wie Tastenkombinationen mit Threads verwendet werden:
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; } } } }
In diesem Beispiel:
Dieses Beispiel stellt sicher, dass beide Spieler ihre Paddles unabhängig voneinander mit unterschiedlichen Tasten bewegen können und der Spielstatus reibungslos und reaktionsschnell aktualisiert wird.
Das obige ist der detaillierte Inhalt vonWie können Threads die Handhabung von Tastenkombinationen in einem Zwei-Spieler-Spiel verbessern?. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!