-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
62 additions
and
52 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
src/main/java/com/lcaohoanq/fxsnakegame/controllers/BoardController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.lcaohoanq.fxsnakegame.controllers; | ||
|
||
import java.awt.event.KeyAdapter; | ||
import java.awt.event.KeyEvent; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
public class BoardController extends KeyAdapter { | ||
// Snake movement directions | ||
private boolean leftDirection = false; // Flag for moving left | ||
private boolean rightDirection = true; // Flag for moving right | ||
private boolean upDirection = false; // Flag for moving up | ||
private boolean downDirection = false; // Flag for moving down | ||
|
||
@Override | ||
public void keyPressed(KeyEvent e) { | ||
|
||
int key = e.getKeyCode(); | ||
|
||
if ((key == KeyEvent.VK_LEFT) && (!rightDirection)) { | ||
leftDirection = true; | ||
upDirection = false; | ||
downDirection = false; | ||
} | ||
|
||
if ((key == KeyEvent.VK_RIGHT) && (!leftDirection)) { | ||
rightDirection = true; | ||
upDirection = false; | ||
downDirection = false; | ||
} | ||
|
||
if ((key == KeyEvent.VK_UP) && (!downDirection)) { | ||
upDirection = true; | ||
rightDirection = false; | ||
leftDirection = false; | ||
} | ||
|
||
if ((key == KeyEvent.VK_DOWN) && (!upDirection)) { | ||
downDirection = true; | ||
rightDirection = false; | ||
leftDirection = false; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters