Advertisement
Guest User

Untitled

a guest
Jul 25th, 2016
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.82 KB | None | 0 0
  1. import javafx.application.Application;
  2. import javafx.stage.Stage;
  3. import javafx.scene.Scene;
  4. import javafx.scene.control.*;
  5. import javafx.scene.layout.Pane;
  6. import javafx.scene.image.Image;
  7. import javafx.scene.image.ImageView;
  8. import javafx.scene.text.*;
  9. import javafx.scene.paint.Color;
  10. import javafx.animation.*;
  11. import javafx.util.Duration;
  12.  
  13. public class CSHunt extends Application {
  14.    
  15.     private ImageView backgroundImage = new ImageView(new Image("mirage.jpg"));
  16.     private ImageView duck = new ImageView(new Image("duck.png"));
  17.     private int score = 0;
  18.     private Label scoreDisplay = new Label(""+score+"00");
  19.    
  20.     public static void main(String[] args){ launch(args);   }
  21.     private void incrementScore(){  score++;    }
  22.    
  23.     @Override public void start(Stage stage) throws Exception{
  24.         Pane root = new Pane();
  25.         Scene scene = new Scene(root, 550, 350);
  26.        
  27.         duck.setX(10);
  28.         duck.setY(175);
  29.         scoreDisplay.setFont(new Font(36));
  30.         scoreDisplay.setTextFill(Color.web("#ffffff"));
  31.         scoreDisplay.setLayoutX(477);
  32.         scoreDisplay.setLayoutY(297);
  33.        
  34.         root.getChildren().addAll(backgroundImage, duck, scoreDisplay);
  35.         stage.setScene(scene);
  36.         stage.setTitle("CS Hunt");
  37.         stage.setResizable(false);
  38.         stage.show();
  39.        
  40.         //events
  41.        
  42.         scene.setOnMouseClicked(event -> {
  43.             if (duck.contains(event.getX(), event.getY())){
  44.                 //increment score
  45.                 incrementScore();
  46.                 scoreDisplay.setText(""+score+"00");
  47.             }
  48.         });
  49.        
  50.         //animation
  51.        
  52.         KeyValue start = new KeyValue(duck.translateXProperty(), 10);
  53.         KeyValue end = new KeyValue(duck.translateXProperty(), 400);
  54.         KeyFrame startF = new KeyFrame(Duration.ZERO, start);
  55.         KeyFrame endF = new KeyFrame(Duration.seconds(10), end);
  56.         Timeline tl = new Timeline(startF, endF);
  57.         tl.setCycleCount(Timeline.INDEFINITE);
  58.         tl.setAutoReverse(true);
  59.         tl.play();
  60.     }
  61.    
  62.    
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement