我会在业余时间制作一个小游戏。这是一款您可以用枪射击敌人的游戏。 但是当我需要放置喷嘴和枪身时遇到问题。他们没有按照我的意愿放置,我也没找出原因。目的是让枪支看起来像这样:
但是我有:
有代码(var是法语(我是比利时人),但我为您介绍了一些注释),我只是将您需要的代码删除了:
Main.java:
package application;
import canons.Canon;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class Main extends Application{
private StackPane root, rootBureau;
private Scene scene;
private Stage stage;
private Text titre;
private Button solo;
private ImageView imgSolo;
private FlowPane rootJeu; // root for the gamespace
private Canvas espaceJeu; // gamespace
private GraphicsContext gc;
private Canon canonJoueur; // player's gun
@Override
public void start(Stage primaryStage) throws IOException {
getRoot();
getScene();
stage = primaryStage;
creerMenu(); // creating the menu
stage.setTitle("Mad Java Guns");
stage.setResizable(false);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
public void retourMenu() throws IOException { // deleting everything
getRoot().getChildren().clear();
getRootJeu().getChildren().clear();
start(stage);
}
public void creerMenu() throws IOException {
root.getChildren().addAll(getTitre(), getSolo(), getImgS());
}
public void creerInterface(String mode) { // creating gamespace (String mode isn't used yet)
getRoot().getChildren().add(getCanvas());
getGraphicsContext();
getRootJeu().getChildren().add(getCanonJoueur());
getRoot().getChildren().add(getRootJeu());
}
// Getters
public Button getSolo() { // creating button for solo mode
if (solo == null) {
solo = new Button("Solo");
solo.setOnMouseClicked(e -> {
root.getChildren().clear();
creerInterface("solo");
});
solo.setMinWidth(200);
solo.setFocusTraversable(false);
solo.setTranslateX(-300);
solo.setTranslateY(-300);
}
return solo;
}
public ImageView getImgS() throws FileNotFoundException { // gets the pic related to the solo mode
if(imgSolo == null) { // security
imgSolo = new ImageView(new Image(new FileInputStream("imgsMenu/image_solo.png")));
imgSolo.setTranslateY(-190);
imgSolo.setTranslateX(-300);
}
return imgSolo;
}
public Text getTitre() { // title
if(titre == null) { // security
titre = new Text("Menu Principal");
titre.setFont(Font.font("Arial", 20));
titre.setUnderline(true);
titre.setTranslateY(-380);
}
return titre;
}
public StackPane getRoot() { // main pane
if(root == null) { // security
root = new StackPane();
}
return root;
}
public Scene getScene() {
if(scene == null) { // security
scene = new Scene(root,1000,800);
}
return scene;
}
public FlowPane getRootJeu() { // root for gamespace
if(rootJeu == null) { // security
rootJeu = new FlowPane();
rootJeu.setMaxSize(990, 495);
rootJeu.setTranslateY(-149);
}
return rootJeu;
}
public Canvas getCanvas() { // gamespace
if(espaceJeu == null) { // security
espaceJeu = new Canvas(1000, 500);
espaceJeu.setTranslateY(-150);
}
return espaceJeu;
}
public GraphicsContext getGraphicsContext() {
if(gc == null) { // security
gc = espaceJeu.getGraphicsContext2D();
gc.setStroke(Color.BLACK);
gc.setFill(Color.BEIGE);
gc.setLineWidth(1);
gc.fillPolygon(new double[]{993, 7, 7, 993}, new double[]{7, 7, 498, 498}, 4); // creating borders of gamespace
gc.strokePolygon(new double[]{995, 5, 5, 995}, new double[]{5, 5, 499, 499}, 4);
}
return gc;
}
public Canon getCanonJoueur() {
if(canonJoueur == null) { // security
canonJoueur = new Canon(Color.BLACK, Color.BLACK);
}
return canonJoueur;
}
}
Canon.java:
package canons;
import javafx.scene.Parent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
public class Canon extends Parent {
private int posX = 500; // x position of the gun
private int posY = 250; // y position of the gun
private int tailleCorps = 20; // size of the body
private Rectangle buse; // nuzzle
private Circle corps; // body
public Canon(Color couleurCorps, Color couleurBuse) {
this.getChildren().addAll(getBuse(), getCorps());
this.setTranslateX(posX);
this.setTranslateY(posY);
}
public Rectangle getBuse() {
if(buse == null) { // verify if they aren't created yet. Security.
buse = new Rectangle();
buse.setWidth(70);
buse.setHeight(20);
buse.setArcWidth(15); // round extremities
buse.setArcHeight(30);
buse.setFill(Color.BLACK);
buse.setTranslateX(posX);
buse.setTranslateY(posY + tailleCorps/4);
}
return buse;
}
public Circle getCorps() {
if(corps == null) { // verify if they aren't created yet. Security.
corps = new Circle(tailleCorps);
corps.setTranslateX(posX);
corps.setTranslateY(posY);
}
return corps;
}}
如果我不太清楚,请随时问我有关问题/代码的其他问题。 谢谢。