Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javafx.application.Application;
- import javafx.stage.Stage;
- import javafx.scene.Scene;
- import javafx.scene.control.*;
- import javafx.scene.layout.Pane;
- import javafx.scene.image.Image;
- import javafx.scene.image.ImageView;
- import javafx.scene.text.*;
- import javafx.scene.paint.Color;
- import javafx.animation.*;
- import javafx.util.Duration;
- public class CSHunt extends Application {
- private ImageView backgroundImage = new ImageView(new Image("mirage.jpg"));
- private ImageView duck = new ImageView(new Image("duck.png"));
- private int score = 0;
- private Label scoreDisplay = new Label(""+score+"00");
- public static void main(String[] args){ launch(args); }
- private void incrementScore(){ score++; }
- @Override public void start(Stage stage) throws Exception{
- Pane root = new Pane();
- Scene scene = new Scene(root, 550, 350);
- duck.setX(10);
- duck.setY(175);
- scoreDisplay.setFont(new Font(36));
- scoreDisplay.setTextFill(Color.web("#ffffff"));
- scoreDisplay.setLayoutX(477);
- scoreDisplay.setLayoutY(297);
- root.getChildren().addAll(backgroundImage, duck, scoreDisplay);
- stage.setScene(scene);
- stage.setTitle("CS Hunt");
- stage.setResizable(false);
- stage.show();
- //events
- scene.setOnMouseClicked(event -> {
- if (duck.contains(event.getX(), event.getY())){
- //increment score
- incrementScore();
- scoreDisplay.setText(""+score+"00");
- }
- });
- //animation
- KeyValue start = new KeyValue(duck.translateXProperty(), 10);
- KeyValue end = new KeyValue(duck.translateXProperty(), 400);
- KeyFrame startF = new KeyFrame(Duration.ZERO, start);
- KeyFrame endF = new KeyFrame(Duration.seconds(10), end);
- Timeline tl = new Timeline(startF, endF);
- tl.setCycleCount(Timeline.INDEFINITE);
- tl.setAutoReverse(true);
- tl.play();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement