Add all the things:

Rollable dice, Players, nice player movement, other stuff
This commit is contained in:
Julius de Jeu 2017-03-04 19:59:11 +01:00
parent 25f1c41e39
commit 6507dc7abd
5 changed files with 157 additions and 109 deletions

View file

@ -0,0 +1,41 @@
package nl.minkema.leendert;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import java.util.Random;
/**
* @author J0QUE
* @version 1.0
*/
public class Dice extends ImageView {
private Image[] rolls = {new Image("https://upload.wikimedia.org/wikipedia/commons/thumb/0/09/Dice-1.svg/1024px-Dice-1.svg.png"),
new Image("https://upload.wikimedia.org/wikipedia/commons/thumb/3/34/Dice-2.svg/1024px-Dice-2.svg.png"),
new Image("https://upload.wikimedia.org/wikipedia/commons/thumb/c/ca/Dice-3.svg/1024px-Dice-3.svg.png"),
new Image("https://upload.wikimedia.org/wikipedia/commons/thumb/1/16/Dice-4.svg/1024px-Dice-4.svg.png"),
new Image("https://upload.wikimedia.org/wikipedia/commons/thumb/d/dc/Dice-5.svg/1024px-Dice-5.svg.png"),
new Image("https://upload.wikimedia.org/wikipedia/commons/thumb/d/d7/Dice-6a.svg/1024px-Dice-6a.svg.png")};
private final Random r;
public Dice(int x, int y) {
this.r = new Random(System.currentTimeMillis());
this.setFitHeight(100);
this.setPreserveRatio(true);
this.setX(x);
this.setY(y);
roll();
}
public int roll() {
int roll = r.nextInt(6);
this.setImage(rolls[roll]);
return roll + 1;
}
}

View file

@ -2,120 +2,30 @@ package nl.minkema.leendert;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.text.Text;
import java.util.ArrayList;
import java.util.Collections;
/**
* @author J0QUE
* @version 1.0
*/
public class Game extends Pane {
public static int[][] locations = {{290, 163},
{331, 162},
{372, 162},
{412, 161},
{446, 160},
{466, 201},
{429, 202},
{382, 201},
{340, 201},
{313, 201},
{274, 201},
{253, 235},
{293, 242},
{339, 243},
{370, 243},
{419, 242},
{447, 241},
{484, 241},
{517, 277},
{468, 277},
{431, 278},
{402, 280},
{344, 281},
{304, 282},
{277, 282},
{228, 283},
{216, 319},
{256, 319},
{291, 320},
{338, 324},
{367, 324},
{407, 323},
{453, 320},
{486, 320},
{528, 321},
{554, 359},
{506, 362},
{478, 362},
{432, 362},
{395, 361},
{346, 359},
{302, 360},
{272, 360},
{223, 359},
{203, 359},
{179, 359},
{210, 398},
{248, 399},
{292, 400},
{333, 404},
{373, 406},
{404, 396},
{446, 399},
{484, 401},
{523, 402},
{575, 402},
{563, 430},
{520, 438},
{460, 441},
{435, 443},
{394, 442},
{361, 442},
{311, 442},
{275, 439},
{225, 438},
{194, 438},
{203, 473},
{243, 477},
{297, 482},
{335, 482},
{375, 481},
{412, 481},
{450, 481},
{486, 480},
{533, 480},
{520, 517},
{465, 520},
{426, 522},
{389, 523},
{350, 524},
{317, 524},
{276, 524},
{238, 524},
{243, 553},
{276, 555},
{332, 558},
{370, 559},
{415, 560},
{456, 559},
{484, 560},
{479, 594},
{432, 596},
{387, 596},
{347, 597},
{313, 598},
{272, 598},
{287, 635},
{332, 636},
{368, 639},
{417, 642},
{449, 643}};
public static int[][] locations = {{290, 163}, {331, 162}, {372, 162}, {412, 161}, {446, 160}, {466, 201}, {429, 202}, {382, 201}, {340, 201}, {313, 201}, {274, 201}, {253, 235}, {293, 242}, {339, 243}, {370, 243}, {419, 242}, {447, 241}, {484, 241}, {517, 277}, {468, 277}, {431, 278}, {402, 280}, {344, 281}, {304, 282}, {277, 282}, {228, 283}, {216, 319}, {256, 319}, {291, 320}, {338, 324}, {367, 324}, {407, 323}, {453, 320}, {486, 320}, {528, 321}, {554, 359}, {506, 362}, {478, 362}, {432, 362}, {395, 361}, {346, 359}, {302, 360}, {272, 360}, {223, 359}, {203, 359}, {179, 359}, {210, 398}, {248, 399}, {292, 400}, {333, 404}, {373, 406}, {404, 396}, {446, 399}, {484, 401}, {523, 402}, {575, 402}, {563, 430}, {520, 438}, {460, 441}, {435, 443}, {394, 442}, {361, 442}, {311, 442}, {275, 439}, {225, 438}, {194, 438}, {203, 473}, {243, 477}, {297, 482}, {335, 482}, {375, 481}, {412, 481}, {450, 481}, {486, 480}, {533, 480}, {520, 517}, {465, 520}, {426, 522}, {389, 523}, {350, 524}, {317, 524}, {276, 524}, {238, 524}, {243, 553}, {276, 555}, {332, 558}, {370, 559}, {415, 560}, {456, 559}, {484, 560}, {479, 594}, {432, 596}, {387, 596}, {347, 597}, {313, 598}, {272, 598}, {287, 635}, {332, 636}, {368, 639}, {417, 642}, {449, 643}};
public static ArrayList<Player> players = new ArrayList<>();
public Game() {
Collections.shuffle(players);
Image bord = new Image("/svg/Bord.svg");
ImageView view = new ImageView(bord);
@ -130,8 +40,21 @@ public class Game extends Pane {
getChildren().add(new Circle(k[0], k[1], 5, Color.RED));
getChildren().add(new Text(k[0], k[1], String.valueOf(i)));
}
System.out.print("{");
this.setOnMouseClicked(event -> System.out.printf("{%d, %d},%n", (int) event.getSceneX(), (int) event.getSceneY()));
//System.out.print("{");
//this.setOnMouseClicked(event -> System.out.printf("{%d, %d},%n", (int) event.getSceneX(), (int) event.getSceneY()));
Dice dice = new Dice(1100, 700);
this.getChildren().add(dice);
this.getChildren().add(Menu.createButton("Roll", 1100, 650, e -> {
Player p = players.get(Player.getNextActive());
int roll = dice.roll();
p.move(roll);
}));
this.setBackground(new Background(new BackgroundFill(Color.RED, null, null)));
for (Player p : players) {
this.getChildren().add(p.getImageView());
}
}
}

View file

@ -21,7 +21,7 @@ public class Main extends Application {
SvgImageLoaderFactory.install();
primaryStage.setTitle("Het spectaculaire tuinavontuur");
scene = new Scene(new Game(), 1200, 800);
scene = new Scene(new Menu(), 1200, 800);
primaryStage.setScene(scene);
primaryStage.setResizable(false);
primaryStage.show();

View file

@ -11,7 +11,6 @@ import javafx.scene.layout.StackPane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
import javafx.scene.text.TextBoundsType;
@ -27,7 +26,8 @@ import java.util.Random;
*/
public class Menu extends Group {
public static ArrayList<Paint> playerColors = new ArrayList<>();
public static ArrayList<Color> playerColors = new ArrayList<>();
private static Random random = new Random(System.currentTimeMillis());
public Menu() throws URISyntaxException, MalformedURLException {
while (playerColors.size() < 21) {
@ -61,10 +61,18 @@ public class Menu extends Group {
}));
nodes.add(createButton("3 players", 100, 150, event -> System.out.println("3 players")));
Rectangle rectangle = new Rectangle(200, 200, 200, 200);
rectangle.setOnMouseClicked(e -> {
System.out.println("Click");
Game.players.add(new Player(new Image("https://lh3.ggpht.com/OOXV4V9YyovafA10xZhq0QgWNwFwCEhMI9kWJ2FDkjMmLa7rDWJmSmnsgOtMWdDGg3A=w300", 50, 0, true, true)));
Game.players.add(new Player(new Image("https://lh3.googleusercontent.com/SQ3DumBwiaVOVXa0EKGfG5r1932Hj81-DtNVKC7IWjPlBA4lUU4k5BI0wbU1ivKmm6E=w170", 50, 0, true, true)));
Main.scene.setRoot(new Game());
});
rectangle.setFill(Color.TRANSPARENT);
nodes.add(rectangle);
getChildren().addAll(nodes);
playerColors.forEach(System.out::println);
System.out.print("[");
this.setOnMouseClicked(event -> System.out.printf("[%d, %d],%n", (int)event.getSceneX(), (int)event.getSceneY()));
}
public static StackPane createButton(String string, double x, double y, EventHandler<MouseEvent> onClick) {
@ -115,7 +123,7 @@ public class Menu extends Group {
}
public static Color getRandomColor() {
Random random = new Random(System.currentTimeMillis());
return Color.hsb(random.nextInt(360), 0.8, 0.8);
}
}

View file

@ -0,0 +1,76 @@
package nl.minkema.leendert;
import javafx.animation.TranslateTransition;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.util.Duration;
import java.util.Arrays;
import java.util.Random;
/**
* @author J0QUE
* @version 1.0
*/
public class Player {
private final Image image;
private final ImageView view;
private int plaats = 0;
private int[] coords = Game.locations[plaats];
private final int[] add;
private static int who = 0;
public Player(Image image) {
Random r = new Random(System.currentTimeMillis());
this.image = image;
this.view = new ImageView(this.image);
this.add = new int[]{-image.widthProperty().intValue() / 2, -image.heightProperty().intValue() / 2};// new int[]{r.nextInt(10) - 5, r.nextInt(10) - 5};
coords = new int[]{coords[0] + add[0], coords[1] + add[1]};
System.out.println(Arrays.toString(coords));
getImageView().setTranslateX(coords[0]);
getImageView().setTranslateY(coords[1]);
}
public ImageView getImageView() {
return view;
}
public void move(int waar) {
System.out.println(waar);
if (waar + plaats > 100) return;
TranslateTransition tt = new TranslateTransition(Duration.millis(200), getImageView());
tt.setFromX(coords[0]);
tt.setFromY(coords[1]);
tt.setToX(coords[0] = Game.locations[plaats + 1][0] + add[0]);
tt.setToY(coords[1] = Game.locations[plaats + 1][1] + add[1]);
TranslateTransition last = tt;
for (int i = plaats + 2; i < waar + plaats + 1; i++) {
TranslateTransition tl = new TranslateTransition(Duration.millis(200), getImageView());
tl.setFromX(coords[0]);
tl.setFromY(coords[1]);
tl.setToX(coords[0] = Game.locations[i][0] + add[0]);
tl.setToY(coords[1] = Game.locations[i][1] + add[1]);
last.setOnFinished(e -> tl.play());
System.out.println(Arrays.toString(coords));
last = tl;
}
tt.play();
plaats += waar;
if (plaats==100){
System.out.println("Een speler heeft gewonnen!");
}
}
public static int getNextActive() {
System.out.println(who);
who++;
if (who >= Game.players.size()) {
return who = 0;
}
return who;
}
}