Simple JavaFX Game
Simple JavaFX Game
My weekend project ,it just basically to demonstrate javafx collision detect and an advantage of the abstract class Transition in this case TranslateTransition extends Transition.
when a collision happens the text increments by one,the TranslateTransition the rate,fromY and duration parameters are set at random.
Demo SnapShot
The Rectangle's buildMethod
public Rectangle buildCar(){
Rectangle rect=new Rectangle();
rect.setFill(colors.get(r.nextInt(colors.size())));
rect.setWidth(30);
rect.setHeight(30);
rect.setLayoutX(-30);
t=new TranslateTransition(Duration.seconds(r.nextInt(30+5)),rect);
t.setFromX(r.nextInt(305+10));
t.setFromY(0);
t.setToY(580);
t.setCycleCount(r.nextInt(30+5));
t.setRate(r.nextInt(5+1));
//we want to remove the rectangle from the root when transition is finished
t.setOnFinished((ActionEvent event) -> {
root.getChildren().remove(rect);
});
t.play();
return rect;
}
On keyPressed Method
private void move(Scene scene,Circle circle){
imageX=circle.getLayoutX();
scene.setOnKeyPressed((KeyEvent event) -> {
KeyCode key=event.getCode();
if(key==KeyCode.D){
if(imageX >=0 && imageX<=305){
imageX+=30;
circle.setLayoutX(imageX);
}
}
if(key==KeyCode.A){
if(imageX>=30 && imageX<=330){
imageX-=30;
circle.setLayoutX(imageX);
}
}
});
}
Check for Collision
private void checkCollision(ObservableList<Shape> block,Circle rect) {
block.stream().forEach((car) -> {
Shape intersect = Shape.intersect(car, rect);
if (intersect.getBoundsInLocal().getWidth() != -1) {
if (root.getChildren().remove(car)) {
Platform.runLater(() -> {
hit_count.setText("Hit Count: " +touches);
});
touches++;
}
}
});
}
The sourceCode
public class GameFX extends Application {
Group root;
Scene scene;
double imageX;
ObservableList<Color> colors=FXCollections.observableArrayList(Color.ALICEBLUE,Color.WHITE,Color.BISQUE,Color.GREY,Color.DARKGOLDENROD
,Color.PINK,Color.GREEN,Color.YELLOW,Color.PURPLE);
ObservableList<Shape> cars=FXCollections.observableArrayList();
Random r;
Circle me;
TranslateTransition t;
ImageView bg_image;
int touches=0;
Text hit_count;
@Override
public void start(Stage primaryStage) throws UnirestException, IOException {
r=new Random();
root=new Group();
bg_image=new ImageView("file:road.png");
hit_count=new Text("Hit Count:"+touches);
hit_count.setFont(Fonts.robotoBold(21));
hit_count.setUnderline(true);
hit_count.setFill(Color.RED);
hit_count.setLayoutX(130);
hit_count.setLayoutY(15);
me=new Circle(30);
me.setLayoutX(30);
me.setLayoutY(bg_image.getImage().getHeight()-me.getRadius());
me.setFill(Color.WHITE);
Rectangle car=buildCar();
Rectangle car1=buildCar();
Rectangle car2=buildCar();
Rectangle car3=buildCar();
Rectangle car4=buildCar();
Rectangle car5=buildCar();
Rectangle car6=buildCar();
Rectangle car7=buildCar();
cars.addAll(car,car1,car2,car3,car4,car5,car6,car7);
root.getChildren().addAll(bg_image,car,car1,car2,car3,car4,car5,car6,car7,me,hit_count);
AnimationTimer tm=new AnimationTimer() {
@Override
public void handle(long now) {
checkCollision(cars,me);
}
};
tm.start();
scene=new Scene(root,350,550);
move(scene,me);
primaryStage.setTitle("GameFX");
primaryStage.setScene(scene);
primaryStage.show();
}
public Rectangle buildCar(){
Rectangle rect=new Rectangle();
rect.setFill(colors.get(r.nextInt(colors.size())));
rect.setWidth(30);
rect.setHeight(30);
rect.setLayoutX(-30);
t=new TranslateTransition(Duration.seconds(r.nextInt(30+5)),rect);
t.setFromX(r.nextInt(305+10));
t.setFromY(0);
t.setToY(580);
t.setCycleCount(r.nextInt(30+5));
t.setRate(r.nextInt(5+1));
t.setOnFinished((ActionEvent event) -> {
root.getChildren().remove(rect);
});
t.play();
return rect;
}
private void move(Scene scene,Circle circle){
imageX=circle.getLayoutX();
scene.setOnKeyPressed((KeyEvent event) -> {
KeyCode key=event.getCode();
if(key==KeyCode.D){
if(imageX >=0 && imageX<=305){
imageX+=30;
circle.setLayoutX(imageX);
}
}
if(key==KeyCode.A){
if(imageX>=30 && imageX<=330){
imageX-=30;
circle.setLayoutX(imageX);
}
}
});
}
private void checkCollision(ObservableList<Shape> block,Circle rect) {
block.stream().forEach((car) -> {
Shape intersect = Shape.intersect(car, rect);
if (intersect.getBoundsInLocal().getWidth() != -1) {
if (root.getChildren().remove(car)) {
Platform.runLater(() -> {
hit_count.setText("Hit Count: " +touches);
});
touches++;
}
}
});
}
}
Results
Comments
Post a Comment