简体   繁体   English

如何在窗格上的随机位置放置形状?

[英]How to place shape in random locations on pane?

I am working on a final project and am making a game that has a dot that will randomly move around a window, and when it is clicked, the users score will increase. 我正在做一个最终项目,并且正在制作一个带有将在窗口中随机移动的圆点的游戏,当单击它时,用户得分将增加。 I am wondering how to move the dot around randomly every half second or so. 我想知道如何每半秒左右随机移动点。 Here is all the code I have so far, thanks! 这是我到目前为止的所有代码,谢谢!

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.stage.Popup;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.text.Text;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;


public class CatchTheDot extends Application{

//create ball for game
public static Circle dot;

//create pane to run game
public static Pane window;

//create score counter
int score = 0;

//creat random
Random r = new Random((21)+1);

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

    window.getChildren().addAll(dot);

    // create scene and place on pane
    Scene s = new Scene(window, 800, 800);
    primaryStage.setTitle("Catch The Dot");
    primaryStage.setScene(s);
    primaryStage.show();

    //move dot
    Timer timer = new Timer();
    timer.schedule(new TimerTask() 
        {
        @Override
        public void run(){
        window.getChildren().remove(dot);
        int randX = 1 + (int)(Math.random()*800);
        int randY = 1 + (int)(Math.random()*800);
        window.add(dot, randX, randY);
        }
        }, 0, 5000);        


    // create listener
    ActionEvent mouseClicked(ActionEvent event)
    {
        if(event.getSource()==dot)
        {
            score = score + 10;
            if(score == 50)
            {
                popUp(primaryStage);
            }
        }
    }
}

public void popUp(final Stage primaryStage)
{
    primaryStage.setTitle("You won!");
    final Popup popup = new Popup();
    popup.setX(300);
    popup.setY(200);
    Text t = new Text("You won! Nice job!");
    Text tt = new Text("Play again?");
    Button yes = new Button("yes");
    Button no = new Button("no");
    popup.getContent().addAll(t, tt, yes, no);
    yes.setOnAction(e -> Yes());
    no.setOnAction(e -> No());
}

public void Yes()
{
    restartGame();
}
public void No()
{
    System.exit(0);
}
public void restartGame()
{
    score = 0;

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

} }

Instead of using Math.random(), I would suggest using Random . 建议不要使用Math.random(),而建议使用Random

Random random = new Random();
int ranX = random.nextInt(width-1); // random value from 0 to width
int ranY = random.nextInt(height-1);  // random value from 0 to height

The rest looks alright. 其余的看起来不错。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM