我一直试图制作像agar.io这样的游戏,但是我已经陷入了无法弄清楚如何将方法点中的所有点添加到界面的地步,有人可以帮我找出如何编码吗?
这是主要块
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
public class Main extends Application {
Random r = new Random();
Pane playfield;
static int score = 30;
List<User> allUsers = new ArrayList<>();
List<Dots> allDots = new ArrayList<>();
Movement mouse = new Movement(0,0,0);
@Override
public void start(Stage primaryStage) {
// create containers
BorderPane root = new BorderPane();
StackPane layerPane = new StackPane();
// playfield for our Users
playfield = new Pane();
layerPane.getChildren().addAll(playfield);
root.setCenter(layerPane);
Scene scene = new Scene(root,
Settings.SCENE_WIDTH, Settings.SCENE_HEIGHT);
primaryStage.setScene(scene);
primaryStage.show();
/**if(r.nextInt(10) == 5){
addDot();
}**/
// add 1 User
addUser();
if(r.nextInt(10)==5)
{
addDots();
}
// capture mouse position
scene.addEventFilter(MouseEvent.ANY, e -> {
mouse.set(e.getX(), e.getY(), 0);
});
// process all Users
AnimationTimer loop = new AnimationTimer() {
@Override
public void handle(long now) {
// move
allUsers.forEach((User) -> User.step(mouse));
// check border
allUsers.forEach(User::checkBoundaries);
// update in fx scene
allUsers.forEach(User::display);
}
};
loop.start();
}
/**
* Add single User to list of Users and to the playfield
*/
private void addUser() {
User User = new User();
allUsers.add(User);
playfield.getChildren().add(User);
}
private void addDots(){
int randX = r.nextInt(800);
int randY = r.nextInt(600);
Dots d = new Dots(randX, randY);
allDots.add(d);
playfield.getChildren().add(d);
}
/**private void addDot() {
Circle a = new Circle();
int randX = r.nextInt(800);
int randY = r.nextInt(600);
Dot d = new Dot(randX,randY);
synchronized(dots){
dots.add(d);
}
playfield.getChildren().add(d);
}**/
public static void main(String[] args) {
launch(args);
}
}
设置
public class Settings {
public static double SCENE_WIDTH = 800;
public static double SCENE_HEIGHT = 600;
}
运动
public class Movement {
public double x;
public double y;
public double z;
public Movement(double x, double y, double z) {
super();
this.x = x;
this.y = y;
this.z = z;
}
public void normalize() {
double m = mag();
if (m != 0 && m != 1) {
div(m);
}
}
public void div(double value) {
x /= value;
y /= value;
z /= value;
}
public void mult(double value) {
x *= value;
y *= value;
z *= value;
}
public void add(Movement v) {
x += v.x;
y += v.y;
z += v.z;
}
public void sub(Movement v) {
x -= v.x;
y -= v.y;
z -= v.z;
}
public void limit(float max) {
if (mag() > max) {
normalize();
mult(max);
}
}
public double mag() {
return Math.sqrt(x * x + y * y + z * z);
}
public static Movement sub(Movement v1, Movement v2) {
return sub(v1, v2, null);
}
public static Movement sub(Movement v1, Movement v2, Movement target) {
if (target == null) {
target = new Movement(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
} else {
target.set(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
}
return target;
}
public void set(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
}
点
import java.util.Random;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
public class Dots extends Pane{
public int x;
public int y;
private static Random random = new Random();
int size = 10;
Circle circle;
public Dots(int x, int y) {
this.x=x;
this.y=y;
circle = new Circle(this.x, this.y, size);
circle.setCenterX(size);
circle.setCenterY(size);
circle.setStroke(Color.WHITE);
circle.setFill(Color.CYAN);
getChildren().add(circle);
}
}
用户
import java.util.Random;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
public class Dots extends Pane{
public int x;
public int y;
private static Random random = new Random();
int size = 10;
Circle circle;
public Dots(int x, int y) {
this.x=x;
this.y=y;
circle = new Circle(this.x, this.y, size);
circle.setCenterX(size);
circle.setCenterY(size);
circle.setStroke(Color.WHITE);
circle.setFill(Color.CYAN);
getChildren().add(circle);
}
}