我正在写一个蛇和梯子程序,我在使用掷骰子移动图标时遇到了很多麻烦。我想在每次掷骰子时更改网格窗格位置。相反,每当骰子滚动时,玩家的图标就会停留在该位置。整个游戏的代码还没有完全完成,但我无法弄清楚如何移动
public class SaltySnakes extends Application{
ImageView Playericon = new ImageView(new Image(getClass().getResourceAsStream("Rainbow-Circle-3.png")));
ImageView Playericon2 = new ImageView(new Image(getClass().getResourceAsStream("BERNLORD.png")));
Player player1 = new Player("Trump", 1, Playericon);
Player player2 = new Player("Bernie" , 1 , Playericon2 );
final Game RollGame = new Game();
Board cell = new Board();
public static void main(String[] args) {
// TODO Auto-generated method stub
launch(args);
}
public void start(Stage primaryStage) throws Exception {
// TODO Auto-generated method stub
primaryStage.setTitle("Salty Snakes");
primaryStage.setResizable(false);
BorderPane border = new BorderPane();
HBox hbox = new HBox();
VBox vbox = new VBox();
vbox.setPadding(new Insets(10));
vbox.setSpacing(8);
hbox.setPadding(new Insets(10));
hbox.setSpacing(8);
final String cssDefault = "-fx-border-color: black;\n"
+ "-fx-border-insets: 5;\n"
+ "-fx-border-width: 3;\n"
;
hbox.setStyle(cssDefault);
vbox.setStyle(cssDefault);
//ImageView iv = new ImageView(new Image(getClass().getResourceAsStream("snakes-and-ladders.jpg")));
hbox.getChildren().add(addGridPane());
//vbox.getChildren().add(pane);
vbox.getChildren().add(addBerniePane(1,10,2,10));
border.setCenter(vbox);
border.setRight(hbox);
primaryStage.setScene(new Scene (border));
primaryStage.getIcons().add(new Image(getClass().getResourceAsStream("Berniesnakers.png")));
primaryStage.show();
}
public GridPane addGridPane() {
final String cssDefault = "-fx-border-color: green;\n"
+ "-fx-border-insets: 5;\n"
+ "-fx-border-width: 3;\n"
+ "-fx-focus-color: transparent;"
+ "-fx-border-style: dashed;\n"
+ "-fx-focus-color: green"
;
final GridPane grid = new GridPane();
grid.getColumnConstraints().add(new ColumnConstraints(200)); // column 1 is 100 wide
grid.getColumnConstraints().add(new ColumnConstraints(181));
grid.setHgap(1);
grid.setVgap(1);
grid.setPadding(new Insets(0, 0, 0, 0));
//grid.setGridLinesVisible(true); //delete later************
grid.add(Title, 0, 0);
Text Players = new Text("Players: ");
Players.setFont(Font.font("Arial" , 18));
grid.add(Players, 0 , 35);
Text RollDice = new Text("Roll Dice: ");
RollDice.setFont(Font.font("Arial" , 35));
grid.add(RollDice, 0 , 100);
final TextField Player1name = new TextField();
grid.add(Player1name,0,40);
final TextField Player2name = new TextField();
grid.add(Player2name,0,45);
Button Player1RolltheDice = new Button("Player One Roll!");
grid.add(Player1RolltheDice, 0, 105);
Player1RolltheDice.setStyle(cssDefault);
Button Player2RolltheDice = new Button ("Player Two Roll!");
Player2RolltheDice.setStyle(cssDefault);
//grid.add(Player2RolltheDice, 0, 105);
Player1RolltheDice.setOnAction(new EventHandler<ActionEvent>(){ //add button
public void handle(ActionEvent event){
int DiceRoll = RollGame.RollDice();
player1.addtocurrentScore(DiceRoll);
int temp1=10;
int temp2=1;
int temp3=10;
int temp4=2;
for(int i = 0; i<10;i++){
for (int j = 0; j<10; j++){
if(cell.gameBoard[i][j].getCellNumber()==player1.getcurrentScore()){
temp1 = i;
temp2 = j+1;
}
}
}
addBerniePane(temp1,temp2,temp3,temp4);
ImageView DiceImage = RollGame.changeDice(DiceRoll);
grid.add(DiceImage, 0, 110);
}
});
Button Okayplayer1 = new Button("Okay");
grid.add(Okayplayer1, 1, 40);
RestartGame.setMaxWidth(180);
RestartGame.setMaxHeight(100);
RestartGame.setStyle(cssDefault);
Button Okayplayer2 = new Button("Okay");
grid.add(Okayplayer2, 1, 45);
RestartGame.setMaxWidth(180);
RestartGame.setMaxHeight(100);
RestartGame.setStyle(cssDefault);
return grid;
}
public GridPane addBerniePane(int First, int Pen, int Second, int Trator){
GridPane pane = new GridPane();
pane.setPadding(new Insets(0, 0, 0, 0));
int j=0;
int f=0;
int q=0;
int x = 0;
int z = 0;
int r = 0;
int h=0;
int k =0;
int l =0;
int p= 0;
for (int i =100; i>90; i-- ){
ImageView KEK = new ImageView(new Image(getClass().getResourceAsStream(Integer.toString(i)+".png")));
j++;
pane.add(KEK,j,0);
}
for (int i= 81; i<91; i++){
ImageView KEK = new ImageView(new Image(getClass().getResourceAsStream(Integer.toString(i)+".png")));
f++;
pane.add(KEK, f,1);
}
for (int i= 80; i>70; i--){
ImageView KEK = new ImageView(new Image(getClass().getResourceAsStream(Integer.toString(i)+".png")));
q++;
pane.add(KEK, q,2);
}
for (int i= 61; i<71; i++){
ImageView KEK = new ImageView(new Image(getClass().getResourceAsStream(Integer.toString(i)+".png")));
x++;
pane.add(KEK, x,3);
}
for (int i= 60; i>50; i--){
ImageView KEK = new ImageView(new Image(getClass().getResourceAsStream(Integer.toString(i)+".png")));
z++;
pane.add(KEK, z,4);
}
for (int i= 41; i<51; i++){
ImageView KEK = new ImageView(new Image(getClass().getResourceAsStream(Integer.toString(i)+".png")));
r++;
pane.add(KEK, r,5);
}
for (int i= 40; i>30; i--){
ImageView KEK = new ImageView(new Image(getClass().getResourceAsStream(Integer.toString(i)+".png")));
h++;
pane.add(KEK, h,6);
}
for (int i= 21; i<31; i++){
ImageView KEK = new ImageView(new Image(getClass().getResourceAsStream(Integer.toString(i)+".png")));
k++;
pane.add(KEK, k,7);
}
for (int i= 20; i>10; i--){
ImageView KEK = new ImageView(new Image(getClass().getResourceAsStream(Integer.toString(i)+".png")));
l++;
pane.add(KEK, l,8);
}
for (int i= 1; i<11; i++){
ImageView KEK = new ImageView(new Image(getClass().getResourceAsStream(Integer.toString(i)+".png")));
p++;
pane.add(KEK, p,9);
}
ImageView Bern = new ImageView(new Image(getClass().getResourceAsStream("BERNLORD.png")));
pane.add(Bern, First, Pen);
ImageView Trump = new ImageView(new Image(getClass().getResourceAsStream("Rainbow-Circle-3.png")));
pane.add(Trump, Second, Trator);
return pane;
}
}