forked from kishanrajput23/Java-Projects-Collections
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputHandler.java
More file actions
29 lines (24 loc) · 1002 Bytes
/
InputHandler.java
File metadata and controls
29 lines (24 loc) · 1002 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package Dino_Game_java;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
// Can also use AbstractAction for more complex input mapping like in original DrawPanel
// Handles keyboard input
class InputHandler extends KeyAdapter {
private GameManager gameManager;
public InputHandler(GameManager manager) {
this.gameManager = manager;
}
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_ENTER || keyCode == KeyEvent.VK_UP || keyCode == KeyEvent.VK_SPACE && !gameManager.isGameOver()) {
// For jump action, SPACE or UP is more common than ENTER in such games
gameManager.dinoJump();
} else if (keyCode == KeyEvent.VK_SPACE) { // Use SPACE specifically for restart if game is over
if (gameManager.isGameOver()) {
gameManager.restartGame();
}
}
// Add other key bindings if needed, e.g., duck
}
}