Question
this code show a game in javaFx make some changes : 1- add an image for car inside Rectangle red 2- add an image for
this code show a game in javaFx make some changes :
1- add an image for car inside Rectangle red
2- add an image for human inside Rectangle green
3- set background for the game color black
import javafx.animation.AnimationTimer; import javafx.animation.FadeTransition; import javafx.application.Application; import javafx.scene.Node; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.scene.layout.HBox; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; import javafx.scene.shape.Rectangle; import javafx.scene.text.Font; import javafx.scene.text.Text; import javafx.stage.Stage; import javafx.util.Duration;
import java.util.ArrayList; import java.util.List; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.paint.ImagePattern;
public class croos_the_street extends Application {
private AnimationTimer timer;
private Pane root;
private List
private Parent createContent() { root = new Pane(); root.setPrefSize(800, 600);
human = initFrog();
root.getChildren().add(human);
timer = new AnimationTimer() { public void handle(long now) { onUpdate(); } }; timer.start();
return root; }
private Node initFrog() { Rectangle rect = new Rectangle(38, 38, Color.GREEN); rect.setTranslateY(600 - 39);
return rect; }
private Node spawnCar() { Rectangle rect = new Rectangle(40, 40, Color.RED); rect.setTranslateY((int)(Math.random() * 14) * 40);
root.getChildren().add(rect); return rect; }
private void onUpdate() { for (Node car : cars) car.setTranslateX(car.getTranslateX() + Math.random() * 10);
if (Math.random() < 0.075) { cars.add(spawnCar()); }
checkState(); }
private void checkState() { for (Node car : cars) { if (car.getBoundsInParent().intersects(human.getBoundsInParent())) { human.setTranslateX(0); human.setTranslateY(600 - 39); return; } }
if (human.getTranslateY() <= 0) { timer.stop(); String win = "YOU WIN";
HBox hBox = new HBox(); hBox.setTranslateX(300); hBox.setTranslateY(250); root.getChildren().add(hBox);
for (int i = 0; i < win.toCharArray().length; i++) { char letter = win.charAt(i);
Text text = new Text(String.valueOf(letter)); text.setFont(Font.font(48)); text.setOpacity(0);
hBox.getChildren().add(text);
FadeTransition ft = new FadeTransition(Duration.seconds(0.66), text); ft.setToValue(1); ft.setDelay(Duration.seconds(i * 0.15)); ft.play(); } } }
@Override public void start(Stage stage) throws Exception { stage.setScene(new Scene(createContent()));
stage.getScene().setOnKeyPressed(event -> { switch (event.getCode()) { case W: human.setTranslateY(human.getTranslateY() - 40); break; case S: human.setTranslateY(human.getTranslateY() + 40); break; case A: human.setTranslateX(human.getTranslateX() - 40); break; case D: human.setTranslateX(human.getTranslateX() + 40); break; default: break; } });
stage.show(); }
public static void main(String[] args) { launch(args); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started