Skip to content
Snippets Groups Projects
Commit 900acae7 authored by Jerusalem Laila's avatar Jerusalem Laila
Browse files

speed adjustments

parent daae1635
No related branches found
No related tags found
No related merge requests found
package org.example;
import javafx.scene.Scene;
import java.util.HashMap;
public class BaseController {
// HashMap<String, Scene>
// }
}
package org.example;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
public class Conf {
private static final String BUNDLE_NAME = "org.example.config";
private static final ResourceBundle RESOURCE_BUNDLE =
ResourceBundle.getBundle(BUNDLE_NAME);
private Conf() {}
public static String get(String key) {
try {
return RESOURCE_BUNDLE.getString(key);
} catch (MissingResourceException e) {
return '!' + key + '!';
}
}
}
\ No newline at end of file
package org.example;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/**
* A simple controller providing a callback method {@link #sayHello()}
*
*/
public class HelloController {
private final static Logger log = LogManager.getLogger(HelloController.class);
@FXML private TextField firstNameField;
@FXML private TextField lastNameField;
@FXML private Label helloLabel;
/**
* deriving a greeting message from firstName and lastName
*/
@FXML public void sayHello() {
final String firstName = firstNameField.getText();
final String lastName = lastNameField.getText();
final StringBuilder builder = new StringBuilder();
if (0 < firstName.trim().length()) {
builder.append(firstName);
if (0 < lastName.trim().length()) {
builder.append(' ').append(lastName);
}
} else if (0 < lastName.trim().length()) {
builder.append(lastName);
}
if (0 < builder.length()) {
final String name = builder.toString();
log.debug("Saying hello to " + name);
helloLabel.setText("Hello " + name);
} else {
log.debug("Neither first name nor last name was set, saying hello to anonymous person");
helloLabel.setText("Hello mysterious person");
}
}
}
\ No newline at end of file
......@@ -22,6 +22,11 @@ public class Snake extends Circle {
tails = new ArrayList<>();
currentDirection = Direction.UP;
}
/**
* methode will let snake
* automatically move
*/
public void step(){
for (int i = length-1; i>=0; i--){
if (i == 0){
......@@ -35,19 +40,19 @@ public class Snake extends Circle {
}
if (currentDirection == Direction.UP){
log.info("Snake moves UP");
// log.info("Snake moves UP");
setCenterY(getCenterY()- step);
}
else if (currentDirection == Direction.DOWN){
log.info("Snake moves DOWN");
// log.info("Snake moves DOWN");
setCenterY(getCenterY() + step);
}
else if (currentDirection == Direction.LEFT) {
log.info("Snake moves LEFT");
//log.info("Snake moves LEFT");
setCenterX(getCenterX() - step);
}
else if (currentDirection == Direction.RIGHT) {
log.info("Snake moves RIGHT");
//log.info("Snake moves RIGHT");
setCenterX(getCenterX() + step);
}
}
......
......@@ -41,13 +41,13 @@ public class SnakeGameController implements Initializable {
private void newFood(){
log.info("Food was created");
food = new Circle(random.nextInt(400),random.nextInt(500), radius);
food.setFill(Color.RED);
food.setFill(Color.DARKGREEN);
anchorPane.getChildren().add(food);
}
/**
* methode will create the food
* and display it randomly
* methode will create
* the snake
*/
private void newSnake(){
log.info("Snake was created");
......@@ -71,9 +71,10 @@ public class SnakeGameController implements Initializable {
if (hit() ){
log.info("Collected food");
snake.eat(food);
score.setText(" "+ snake.getLength());
score.setText("Score: "+ snake.getLength());
newFood();
log.info("Spawned new food");
}
else if (gameOver()){
log.info("Game Over");
......@@ -113,13 +114,20 @@ public class SnakeGameController implements Initializable {
newFood();
newSnake();
/**
* changes speed of snake
* after eating food
*/
Runnable r = ()->{
try {
for (; ; ) {
move();
Thread.sleep(100/((snake.getLength() / 10)));
Thread.sleep(70/((snake.getLength() / 10)));
}
}catch(InterruptedException ignored){}};
}catch(InterruptedException ignored){
log.warn("Illegal input");
}};
anchorPane.sceneProperty().addListener((observable, oldScene, newScene) -> {
......@@ -129,15 +137,19 @@ public class SnakeGameController implements Initializable {
if (code == KeyCode.UP){
snake.setCurrentDirection(Direction.UP);
log.info("User input UP");
}
else if (code == KeyCode.DOWN){
snake.setCurrentDirection(Direction.DOWN);
log.info("User input DOWN");
}
else if (code == KeyCode.LEFT) {
snake.setCurrentDirection(Direction.LEFT);
log.info("User input LEFT");
}
else if (code == KeyCode.RIGHT) {
snake.setCurrentDirection(Direction.RIGHT);
log.info("User input RIGHT");
}
......
......@@ -16,7 +16,6 @@
-fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.6) , 5, 0.0 , 0 , 1 );
}
.button:hover{
-fx-background-color: #ffffff;
}
......@@ -25,4 +24,9 @@
-fx-background-image: url("../styles/img.png");
}
#game {
-fx-font-size: 30px;
-fx-background-color: #669266;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment