2016-11-16 62 views
0

我是初學者,嘗試用Java FX創建一個簡單的迷你高爾夫遊戲。 我使用線建小型高爾夫球場。我從滑塊獲得關於擊球強度的用戶輸入,並且可以使用鍵盤將擊球變成8個不同的方向。Java FX迷你高爾夫球遊戲 - 如何減慢移動球

如果我點擊按鈕H,球開始向右移動。如果滑塊值最小,它會緩慢移動,如果滑塊的最大值移動速度非常快。 但是我不能讓動作慢下來,直到停止。

任何想法如何做到這一點?我的代碼到目前爲止在下面。

謝謝您提前!

package sample; 

import javafx.animation.AnimationTimer; 
import javafx.application.Application; 
import javafx.scene.Scene; 
import javafx.scene.control.Slider; 
import javafx.scene.input.KeyCode; 
import javafx.scene.layout.Pane; 
import javafx.scene.paint.Color; 
import javafx.scene.shape.Circle; 
import javafx.scene.shape.Line; 
import javafx.stage.Stage; 

public class Main extends Application { 

    double directionx = 0; 
    double directiony = 0; 
    Line line1,line2,line3,line4,line5,line6,line7,line8,line9; 

    @Override 
    public void start(Stage primaryStage) throws Exception{ 

     primaryStage.setTitle("Minigolf"); 
     Pane pane = new Pane(); 
     Scene scene = new Scene(pane, 900, 600); 
     pane.setStyle("-fx-background-color: green"); 
     primaryStage.setScene(scene); 

     Slider slider = new Slider(); 
     slider.setMin(1); 
     slider.setMax(10); 
     slider.setValue(1); 
     slider.setShowTickLabels(true); 
     slider.setShowTickMarks(true); 
     slider.setLayoutX(10); 
     slider.setLayoutY(10); 

     Circle ball = new Circle(10); 
     Circle hole = new Circle(15); 

     hole.setCenterX(75); 
     hole.setCenterY(430); 
     hole.setFill(Color.YELLOW); 

     ball.setCenterX(100); 
     ball.setCenterY(300); 
     // creating golfcourse with lines 
     line1 = new Line(50,200,500,200); 
     line2 = new Line(50,350,650,350); 
     line3 = new Line(50,200,50,350); 
     line4 = new Line(500,200,500,50); 
     line5 = new Line(650,350,650,200); 
     line6 = new Line(500,50,800,50); 
     line7 = new Line(800,50,800,500); 
     line8 = new Line(800,500,50,500); 
     line9 = new Line(50,500,50,350); 

     pane.getChildren().addAll(slider,ball,hole,line1,line2,line3,line4,line5,line6,line7,line8,line9); 


     new AnimationTimer() { 
      @Override 
      public void handle(long now) { 

        double ballY = ball.getCenterY(); 
        double newballY = ballY + directiony; 
        // if ball touches vertical walls 
        if (ball.getBoundsInParent().intersects(line5.getBoundsInParent()) 
          || ball.getBoundsInParent().intersects(line3.getBoundsInParent()) 
          || ball.getBoundsInParent().intersects(line4.getBoundsInParent()) 
          || ball.getBoundsInParent().intersects(line7.getBoundsInParent()) 
          || ball.getBoundsInParent().intersects(line9.getBoundsInParent())) { 
         // changes direction 
         directionx = directionx * -1; 
        } 
        ball.setCenterX(ball.getCenterX() + directionx); 
        ball.setCenterY(newballY); 
        // if ball touches horizontal walls 
        if (ball.getBoundsInParent().intersects(line1.getBoundsInParent()) 
          || ball.getBoundsInParent().intersects(line2.getBoundsInParent()) 
          || ball.getBoundsInParent().intersects(line6.getBoundsInParent()) 
          || ball.getBoundsInParent().intersects(line8.getBoundsInParent())) { 
         directiony = directiony * -1; 
        } 
        // if ball touches hole, then game is over 
        if (ball.getBoundsInParent().intersects(hole.getBoundsInParent())) { 
         System.out.println("Game over"); 
         pane.getChildren().removeAll(ball); 
        } 




      } 

     }.start(); 

     // I get the slider value and making array where are 10 different strengts 
     scene.setOnKeyPressed(event -> { 
      double b = Math.round(slider.getValue()); 
      b--; 
      int c = (int) b; 
      double [] strengts = new double[10]; 
      double k = 0.1; 
      for (int i = 0; i < strengts.length; i++) { 
       strengts[i] = k; 
       k = k + 0.5; 
      } 
      // 8 different directions to move the ball 
      if (event.getCode().equals(KeyCode.H)) { 
       directionx = directionx + strengts[c]; 
      } 
      else if(event.getCode().equals(KeyCode.F)) { 
       directionx = directionx - strengts[c]; 
      } 
      else if(event.getCode().equals(KeyCode.V)) { 
       directiony=directiony + strengts[c]; 
      } 
      else if(event.getCode().equals(KeyCode.T)) { 
       directiony= directiony - strengts[c]; 
      } 
      else if(event.getCode().equals(KeyCode.B)) 
      { 
       directionx = directionx + strengts[c]; 
       directiony = directiony + strengts[c]; 
      } 
      else if(event.getCode().equals(KeyCode.Y)) 
      { 
       directionx = directionx + strengts[c]; 
       directiony = directiony - strengts[c]; 
      } 
      else if(event.getCode().equals(KeyCode.R)) 
      { 
       directionx = directionx - strengts[c]; 
       directiony = directiony - strengts[c]; 
      } 
      else if(event.getCode().equals(KeyCode.C)) 
      { 
       directionx = directionx - strengts[c]; 
       directiony = directiony + strengts[c]; 
      } 
     }); 
     primaryStage.show(); 
    } 


    public static void main(String[] args) { 
     launch(args); 
    } 
} 
+0

嘗試等待(200)減慢球速度 –

回答

0

只需使用物理學的摩擦。假定每一幀的速度(v)保持不變。

能量球(質量m):基於距離的摩擦

E = 0.5 * m * v² 

工作(s)旅行和摩擦不斷μ

ΔE = s * m * μ = Δt * v * m * μ 

新能源是E - ΔE,你可以用第一個公式計算新的速度v'

0.5 * m * v'² = 0.5 * m * v² - s * m * μ 
      v'² = v² - s * μ * 2 
      v' = sqrt(v² - s * μ * 2) 

上面假設地面是平坦的。

這可以讓你降低速度是這樣的:

@Override 
public void start(Stage primaryStage) { 
    Point2D direction = new Point2D(1, 1).normalize(); 
    Circle c = new Circle(10, 10, 10); 

    Timeline tl = new Timeline(); 
    tl.getKeyFrames().setAll(new KeyFrame(Duration.seconds(1/60d), new EventHandler() { 

     double speed = 3; 
     double friction = 0.02; 

     @Override 
     public void handle(Event event) { 
      double distance = speed; 

      Point2D movement = direction.multiply(distance); 
      c.setCenterX(c.getCenterX() + movement.getX()); 
      c.setCenterY(c.getCenterY() + movement.getY()); 

      double energyBefore = speed * speed; 
      double energyLost = distance * friction; 

      if (energyLost > energyBefore) { 
       tl.stop(); 
       speed = 0; 
       System.out.println("stopped"); 
      } else { 
       speed = Math.sqrt(energyBefore - energyLost); 
      } 

     } 
    })); 

    tl.setCycleCount(Animation.INDEFINITE); 

    Pane root = new Pane(); 
    root.getChildren().add(c); 

    Scene scene = new Scene(root, 400, 400); 
    scene.setOnMouseClicked(evt -> tl.play()); 

    primaryStage.setScene(scene); 
    primaryStage.show(); 
} 
0

感謝您的答覆,但作爲一個初學者我自己簡單的解決方案,現在發現。

我只加4 if語句如下:

if (directionx>0) { 
       directionx = directionx - 0.01; 
      } 
if(directionx<0){ 
       directionx = directionx + 0.01; 
      } 
if(directiony>0) { 
       directiony = directiony - 0.01; 
      } 
if(directiony<0) { 
       directiony = directiony + 0.01; 
      } 

也許在現實的物理不知何故錯誤的,但它看起來對我來說不太真實。