我正在尝试创建一个七段计分板

时间:2019-04-26 14:01:02

标签: java javafx

我正在尝试创建一个七段式显示器,以保存我的剪刀石头布游戏的得分。但是,当我将矩形移动到一侧时,它将移动另一时钟或将左侧的第一个时钟恢复为左侧。现在,两个时钟都位于屏幕的远端,我最好将它们放在中间。这是我第一次使用JavaFX,在制作游戏时一直在学习。另外,如果有什么办法,我可以浓缩一些代码,我很愿意提出建议。谢谢!

主类:

package sample;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.Group;
import javafx.geometry.*;
import javafx.scene.text.TextAlignment;
import javafx.scene.control.*;
import javafx.stage.Stage;
import javafx.scene.layout.*;
import javafx.scene.shape.Rectangle;
import javafx.scene.paint.*;
import javafx.scene.text.Font;
import java.util.Random;

public class Main extends Application {

    Stage window;
    Label label1 = new Label();
    Scene scene1, scene2;

    //ScoreBoard Rectangles
    Group root = new Group();

    public static Shapes shape = new Shapes();
    public static Rectangle r, r1, r2, r3, r4, r5, r6;
    public static final int ROCK = 0;
    public static final int PAPER = 1;
    public static final int SCISSORS = 2;
    public static int userChoice;
    public static int compChoice;
    public static int userCounter = 0;
    public static int compCounter = 0;


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

    @Override
    public void start(Stage primaryStage) throws Exception {
        window = primaryStage;
        window.setResizable(false);

        //Layouts
        VBox layout = new VBox(20);
        HBox hbox = new HBox(10);

        //Background color
        BackgroundFill backFill = new BackgroundFill(Color.BLACK,       CornerRadii.EMPTY, Insets.EMPTY);
        Background background = new Background(backFill);
        layout.setBackground(background);

        //Start Screen Label
        Label label = new Label("Rock Paper Scissors");
        label.setFont(Font.font(20));
        label.setTextFill(Color.WHITE);

        //Start Button
        Button myButton = new Button("Start");
        myButton.setOnAction(e -> window.setScene(scene2));

        //Exit Button
        Button exit = new Button("Exit");
        exit.setOnAction(e -> System.exit(0));

        //Add Children
        hbox.getChildren().addAll(myButton, exit);
        hbox.setAlignment(Pos.CENTER);
        layout.getChildren().addAll(label, hbox);
        layout.setAlignment(Pos.CENTER);

        //Add to scene1
        scene1 = new Scene(layout, 300, 300, Color.BLACK);

        //Layout 2
        VBox layout1 = new VBox(10);
        label1.setText(" ");
        label1.setFont(Font.font(18)); //Setting font size
        label.setTextFill(Color.WHITE);
        layout1.setAlignment(Pos.CENTER);
        VBox vbox = new VBox(10);
        HBox layout2 = new HBox(10);
        HBox lay2 = new HBox(10);

        //ScoreBoard Created
        root = shape.displayRect();

        //Rock Button
        Button rock = new Button("Rock");
        rock.setOnAction(e -> {
            userChoice = ROCK;
            compChoice = compChoice();
            label1 = winner(label1);
            shape.clearRect(root);
            shape.fillRect(root, userCounter, compCounter);
        });

        //Paper Button
        Button paper = new Button("Paper");
        paper.setOnAction(e -> {
            userChoice = PAPER;
            compChoice = compChoice();
            label1 = winner(label1);
            shape.clearRect(root);
            shape.fillRect(root, userCounter, compCounter);
        });

        //Scissor Button
        Button scissors = new Button("Scissors");
        scissors.setOnAction(e -> {
            userChoice = SCISSORS;
            compChoice = compChoice();
            label1 = winner(label1);
            shape.clearRect(root);
            shape.fillRect(root, userCounter, compCounter);
        });

        //Quit Button
        Button quit = new Button("Return");
        quit.setOnAction(e -> window.setScene(scene1));

        //Add Children
        lay2.getChildren().add(label1);
        lay2.setAlignment(Pos.CENTER);
        layout2.getChildren().addAll(rock, paper, scissors, quit);
        layout2.setAlignment(Pos.CENTER);
        vbox.getChildren().addAll(root, lay2, layout2);
        vbox.setBackground(background);

        //Add to scene2
        scene2 = new Scene(vbox, 300, 300);

        window.setTitle("Rock Paper Scissors");
        window.setScene(scene1);
        window.show();
    }

    public static int compChoice() {
        Random rand = new Random();

        int randNum = rand.nextInt(3);

        return randNum;
    }

    public static Label winner(Label label1) {    
        label1.setTextFill(Color.WHITE);
        label1.setTextAlignment(TextAlignment.CENTER);

        if (userChoice == ROCK && compChoice == SCISSORS) {
            label1.setText("You WIN\nRock beats Scissors");
            userCounter++;
        }

        if (userChoice == ROCK && compChoice == PAPER) {
            label1.setText("You LOSE\nPaper beats Rock");
            compCounter++;
        }

        if (userChoice == PAPER && compChoice == ROCK) {
            label1.setText("You WIN\nPaper beats Rock");
            userCounter++;
        }

        if (userChoice == PAPER && compChoice == SCISSORS) {
            label1.setText("You LOSE\nScissors beats Paper");
            compCounter++;
        }

        if (userChoice == SCISSORS && compChoice == ROCK) {
            label1.setText("You LOSE\nRock beats Scissors");
            compCounter++;
        }

        if (userChoice == SCISSORS && compChoice == PAPER) {
            label1.setText("You WIN\nScissors beats Paper");
            userCounter++;
        }

        if (userChoice == compChoice)
            label1.setText("TIE\n    ");

        return label1;
    }    
}

形状类别:

package sample;

import javafx.scene.Group;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;

public class Shapes {    
    Rectangle r,r1,r2,r3,r4,r5,r6;
    Rectangle rc,rc1,rc2,rc3,rc4,rc5,rc6;

    public Group displayRect() {    
        //User ScoreBoard
        r = new Rectangle(10,10,10,40); //10 , 50
        r.setArcWidth(20);
        r.setArcHeight(20);

        r1 = new Rectangle(20,0,40,10); //20 , 40
        r1.setArcWidth(20);
        r1.setArcHeight(20);

        r2 = new Rectangle(60,10,10,40); //60 , 50
        r2.setArcWidth(20);
        r2.setArcHeight(20);

        r3 = new Rectangle(20,50,40,10); //20 , 90
        r3.setArcWidth(20);
        r3.setArcHeight(20);

        r4 = new Rectangle(10,60,10,40); // 10 , 100
        r4.setArcWidth(20);
        r4.setArcHeight(20);

        r5 = new Rectangle(60,60,10,40); //60 , 100
        r5.setArcWidth(20);
        r5.setArcHeight(20);

        r6 = new Rectangle(20,100,40,10); //20 , 140
        r6.setArcWidth(20);
        r6.setArcHeight(20);

        //Computer ScoreBoard
        rc = new Rectangle(250, 10, 10,40);
        rc.setArcWidth(20);
        rc.setArcHeight(20);

        rc1 = new Rectangle(260,0,40,10);
        rc1.setArcWidth(20);
        rc1.setArcHeight(20);

        rc2 = new Rectangle(300,10,10,40);
        rc2.setArcWidth(20);
        rc2.setArcHeight(20);

        rc3 = new Rectangle(260,50,40,10);
        rc3.setArcWidth(20);
        rc3.setArcHeight(20);

        rc4 = new Rectangle(250,60,10,40);
        rc4.setArcWidth(20);
        rc4.setArcHeight(20);

        rc5 = new Rectangle(300,60,10,40);
        rc5.setArcWidth(20);
        rc5.setArcHeight(20);

        rc6 = new Rectangle(260,100,40,10);
        rc6.setArcWidth(20);
        rc6.setArcHeight(20);

        Group root = new Group(r,r1,r2,r3,r4,r5,r6,rc,rc1,rc2,rc3,rc4,rc5,rc6);

        return root;
    }

    public void clearRect(Group root) {    
        r.setFill(Color.BLACK);
        r1.setFill(Color.BLACK);
        r2.setFill(Color.BLACK);
        r3.setFill(Color.BLACK);
        r4.setFill(Color.BLACK);
        r5.setFill(Color.BLACK);
        r6.setFill(Color.BLACK);

        rc.setFill(Color.BLACK);
        rc1.setFill(Color.BLACK);
        rc2.setFill(Color.BLACK);
        rc3.setFill(Color.BLACK);
        rc4.setFill(Color.BLACK);
        rc5.setFill(Color.BLACK);
        rc6.setFill(Color.BLACK);
    }

    public void fillRect(Group root, int userCounter, int compCounter) {    
        if (userCounter == 1) {
            r2.setFill(Color.RED);
            r5.setFill(Color.RED);
        }

        if (userCounter == 2) {
            r1.setFill(Color.RED);
            r2.setFill(Color.RED);
            r3.setFill(Color.RED);
            r4.setFill(Color.RED);
            r6.setFill(Color.RED);
        }

        if (userCounter == 3) {
            r1.setFill(Color.RED);
            r2.setFill(Color.RED);
            r3.setFill(Color.RED);
            r5.setFill(Color.RED);
            r6.setFill(Color.RED);
        }

        if (userCounter == 4) {
            r.setFill(Color.RED);
            r2.setFill(Color.RED);
            r3.setFill(Color.RED);
            r5.setFill(Color.RED);
        }

        if (userCounter == 5) {
            r.setFill(Color.RED);
            r1.setFill(Color.RED);
            r3.setFill(Color.RED);
            r5.setFill(Color.RED);
            r6.setFill(Color.RED);
        }

        if (userCounter == 6) {
            r.setFill(Color.RED);
            r1.setFill(Color.RED);
            r3.setFill(Color.RED);
            r4.setFill(Color.RED);
            r5.setFill(Color.RED);
            r6.setFill(Color.RED);
        }

        if (userCounter == 7) {
            r1.setFill(Color.RED);
            r2.setFill(Color.RED);
            r5.setFill(Color.RED);
        }

        if (userCounter == 8) {
            r.setFill(Color.RED);
            r1.setFill(Color.RED);
            r2.setFill(Color.RED);
            r3.setFill(Color.RED);
            r4.setFill(Color.RED);
            r5.setFill(Color.RED);
            r6.setFill(Color.RED);
        }

        if (userCounter == 9) {
            r.setFill(Color.RED);
            r1.setFill(Color.RED);
            r2.setFill(Color.RED);
            r3.setFill(Color.RED);
            r5.setFill(Color.RED);
            r6.setFill(Color.RED);
        }

        switch (compCounter) {
            case 1:
                rc2.setFill(Color.RED);
                rc5.setFill(Color.RED);
                break;
            case 2:
                rc1.setFill(Color.RED);
                rc2.setFill(Color.RED);
                rc3.setFill(Color.RED);
                rc4.setFill(Color.RED);
                rc6.setFill(Color.RED);
                break;
            case 3:
                rc1.setFill(Color.RED);
                rc2.setFill(Color.RED);
                rc3.setFill(Color.RED);
                rc5.setFill(Color.RED);
                rc6.setFill(Color.RED);
                break;
            case 4:
                rc.setFill(Color.RED);
                rc2.setFill(Color.RED);
                rc3.setFill(Color.RED);
                rc5.setFill(Color.RED);
                break;
            case 5:
                rc.setFill(Color.RED);
                rc1.setFill(Color.RED);
                rc3.setFill(Color.RED);
                rc5.setFill(Color.RED);
                rc6.setFill(Color.RED);
                break;
            case 6:
                rc.setFill(Color.RED);
                rc1.setFill(Color.RED);
                rc3.setFill(Color.RED);
                rc4.setFill(Color.RED);
                rc5.setFill(Color.RED);
                rc6.setFill(Color.RED);
                break;
            case 7:
                rc1.setFill(Color.RED);
                rc2.setFill(Color.RED);
                rc5.setFill(Color.RED);
                break;
            case 8:
                rc.setFill(Color.RED);
                rc1.setFill(Color.RED);
                rc2.setFill(Color.RED);
                rc3.setFill(Color.RED);
                rc4.setFill(Color.RED);
                rc5.setFill(Color.RED);
                rc6.setFill(Color.RED);
                break;
            case 9:
                rc.setFill(Color.RED);
                rc1.setFill(Color.RED);
                rc2.setFill(Color.RED);
                rc3.setFill(Color.RED);
                rc5.setFill(Color.RED);
                rc6.setFill(Color.RED);
                break;
            default: break;
        }    
    }
}

1 个答案:

答案 0 :(得分:1)

据我了解,您只是希望记分板居中而不是靠近侧边。我已经解决了这个问题,但是在浏览完您的代码之后,我最终添加了一些调整,以便以后不再“自己动手”(请记住,这是快速修复,还有很多我会改变的)。我看到的最明显的问题是重复的代码和实例化/返回的变量,这些变量根本没有使用,因此我修复了大部分问题。我会确保所有变量均以有意义的方式命名,以便更容易理解(即,label,label1需要一些更具描述性的内容)。
主要:

package sample;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.Group;
import javafx.geometry.*;
import javafx.scene.text.TextAlignment;
import javafx.scene.control.*;
import javafx.stage.Stage;
import javafx.scene.layout.*;
import javafx.scene.paint.*;
import javafx.scene.text.Font;
import java.util.Random;

public class test extends Application {

    Stage window;
    Label label1 = new Label();
    Scene scene1, scene2;

    //ScoreBoard Rectangles
    Group userScoreRoot, computerScoreRoot;

    public static ScoreBoard userScoreBoard = new ScoreBoard();
    public static ScoreBoard computerScoreBoard = new ScoreBoard();
    public static final int ROCK = 0;
    public static final int PAPER = 1;
    public static final int SCISSORS = 2;
    public static int userChoice;
    public static int compChoice;
    public static int userCounter = 0;
    public static int compCounter = 0;


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

    @Override
    public void start(Stage primaryStage) throws Exception {
        window = primaryStage;
        window.setResizable(false);


        //Layouts
        VBox layout = new VBox(20);
        HBox hbox = new HBox(10);

        //Background color
        BackgroundFill backFill = new BackgroundFill(Color.BLACK, CornerRadii.EMPTY, Insets.EMPTY);
        Background background = new Background(backFill);
        layout.setBackground(background);

        //Start Screen Label
        Label label = new Label("Rock Paper Scissors");
        label.setFont(Font.font(20));
        label.setTextFill(Color.WHITE);

        //Start Button
        Button myButton = new Button("Start");
        myButton.setOnAction(e -> window.setScene(scene2));

        //Exit Button
        Button exit = new Button("Exit");
        exit.setOnAction(e -> System.exit(0));

        //Add Children
        hbox.getChildren().addAll(myButton, exit);
        hbox.setAlignment(Pos.CENTER);
        layout.getChildren().addAll(label, hbox);
        layout.setAlignment(Pos.CENTER);

        //Add to scene1
        scene1 = new Scene(layout, 300, 300, Color.BLACK);

        //Layout 2
        VBox layout1 = new VBox(10);
        label1.setText(" \n ");
        label1.setFont(Font.font(18)); //Setting font size
        label1.setTextFill(Color.WHITE);
        label1.setTextAlignment(TextAlignment.CENTER);
        label.setTextFill(Color.WHITE);
        layout1.setAlignment(Pos.CENTER);
        VBox vbox = new VBox(10);
        HBox layout2 = new HBox(10);
        HBox lay2 = new HBox(10);


        //ScoreBoard Created
        userScoreRoot = userScoreBoard.getRoot();
        computerScoreRoot = computerScoreBoard.getRoot();

        //Rock Button
        Button rock = new Button("Rock");
        rock.setOnAction(e -> {
            userChoice = ROCK;
            compChoice = compChoice();
            winner(label1);
        });
        //Paper Button
        Button paper = new Button("Paper");
        paper.setOnAction(e -> {
            userChoice = PAPER;
            compChoice = compChoice();
            winner(label1);
        });
        //Scissor Button
        Button scissors = new Button("Scissors");
        scissors.setOnAction(e -> {
            userChoice = SCISSORS;
            compChoice = compChoice();
            winner(label1);
        });

        //Quit Button
        Button quit = new Button("Return");
        quit.setOnAction(e -> window.setScene(scene1));

        Region region1 = new Region();
        Region region2 = new Region();
        HBox.setHgrow(region1, Priority.ALWAYS);
        HBox.setHgrow(region2, Priority.ALWAYS);
        HBox scoreBox = new HBox(40);
        scoreBox.getChildren().addAll(region1, userScoreRoot, computerScoreRoot, region2);

        //Add Children
        lay2.getChildren().add(label1);
        lay2.setAlignment(Pos.CENTER);
        layout2.getChildren().addAll(rock, paper, scissors, quit);
        layout2.setAlignment(Pos.CENTER);
        vbox.getChildren().addAll(scoreBox, lay2, layout2);
        vbox.setBackground(background);

        //Add to scene2
        scene2 = new Scene(vbox, 300, 300);

        window.setTitle("Rock Paper Scissors");
        window.setScene(scene1);
        window.show();
    }

    public static int compChoice() {
        Random rand = new Random();

        int randNum = rand.nextInt(3);

        return randNum;
    }

    public static void winner(Label label1) {
        /*String[] result = new String[] {"TIE\n ", "You WIN\n ", "You LOSE\n "}; 
        String[] choice = new String[] {"Rock", "Paper", "Scissors"};
        int index = Math.abs(((userChoice - compChoice) % 3));
        String answer = result[index];
        if(index == 1) {
            userCounter++;
            answer += choice[userChoice] + " beats " + choice[compChoice];
        }
        else if(index == 2) {
            compCounter++;
            answer += choice[compChoice] + " beats " + choice[userChoice];
        }
        label1.setText(answer);*/
        if (userChoice == ROCK && compChoice == SCISSORS) {
            label1.setText("You WIN\nRock beats Scissors");
            userCounter++;
        }

        if (userChoice == ROCK && compChoice == PAPER) {
            label1.setText("You LOSE\nPaper beats Rock");
            compCounter++;
        }

        if (userChoice == PAPER && compChoice == ROCK) {
            label1.setText("You WIN\nPaper beats Rock");
            userCounter++;
        }

        if (userChoice == PAPER && compChoice == SCISSORS) {
            label1.setText("You LOSE\nScissors beats Paper");
            compCounter++;
        }

        if (userChoice == SCISSORS && compChoice == ROCK) {
            label1.setText("You LOSE\nRock beats Scissors");
            compCounter++;
        }

        if (userChoice == SCISSORS && compChoice == PAPER) {
            label1.setText("You WIN\nScissors beats Paper");
            userCounter++;
        }

        if (userChoice == compChoice)
            label1.setText("TIE\n    ");

        userScoreBoard.setScore(userCounter);
        computerScoreBoard.setScore(compCounter);
    }

}

ScoreBoard类:

package sample;

import javafx.scene.Group;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;

public class ScoreBoard {

    Rectangle r,r1,r2,r3,r4,r5,r6;
    Rectangle[] display;
    Group root;

    public ScoreBoard() {
        r = new Rectangle(10,10,10,40); //10 , 50
        r.setArcWidth(20);
        r.setArcHeight(20);

        r1 = new Rectangle(20,0,40,10); //20 , 40
        r1.setArcWidth(20);
        r1.setArcHeight(20);

        r2 = new Rectangle(60,10,10,40); //60 , 50
        r2.setArcWidth(20);
        r2.setArcHeight(20);

        r3 = new Rectangle(20,50,40,10); //20 , 90
        r3.setArcWidth(20);
        r3.setArcHeight(20);

        r4 = new Rectangle(10,60,10,40); // 10 , 100
        r4.setArcWidth(20);
        r4.setArcHeight(20);

        r5 = new Rectangle(60,60,10,40); //60 , 100
        r5.setArcWidth(20);
        r5.setArcHeight(20);

        r6 = new Rectangle(20,100,40,10); //20 , 140
        r6.setArcWidth(20);
        r6.setArcHeight(20);

        display = new Rectangle[]{r,r1,r2,r3,r4,r5,r6};
        root = new Group(display);
    }

    public void clearRect() {
        for(Rectangle r : display) r.setFill(Color.BLACK);
    }

    public void setScore(int score) {
        clearRect();
        switch (score%9) {
            case 0:
                 r.setFill(Color.RED);
                 r1.setFill(Color.RED);
                 r2.setFill(Color.RED);
                 r4.setFill(Color.RED);
                 r5.setFill(Color.RED);
                 r6.setFill(Color.RED);
                 break;
            case 1:
                r2.setFill(Color.RED);
                r5.setFill(Color.RED);
                break;
            case 2:
                r1.setFill(Color.RED);
                r2.setFill(Color.RED);
                r3.setFill(Color.RED);
                r4.setFill(Color.RED);
                r6.setFill(Color.RED);
                break;
            case 3:
                r1.setFill(Color.RED);
                r2.setFill(Color.RED);
                r3.setFill(Color.RED);
                r5.setFill(Color.RED);
                r6.setFill(Color.RED);
                break;
            case 4:
                r.setFill(Color.RED);
                r2.setFill(Color.RED);
                r3.setFill(Color.RED);
                r5.setFill(Color.RED);
                break;
            case 5:
                r.setFill(Color.RED);
                r1.setFill(Color.RED);
                r3.setFill(Color.RED);
                r5.setFill(Color.RED);
                r6.setFill(Color.RED);
                break;
            case 6:
                r.setFill(Color.RED);
                r1.setFill(Color.RED);
                r3.setFill(Color.RED);
                r4.setFill(Color.RED);
                r5.setFill(Color.RED);
                r6.setFill(Color.RED);
                break;
            case 7:
                r1.setFill(Color.RED);
                r2.setFill(Color.RED);
                r5.setFill(Color.RED);
                break;
            case 8:
                r.setFill(Color.RED);
                r1.setFill(Color.RED);
                r2.setFill(Color.RED);
                r3.setFill(Color.RED);
                r4.setFill(Color.RED);
                r5.setFill(Color.RED);
                r6.setFill(Color.RED);
                break;
            case 9:
                r.setFill(Color.RED);
                r1.setFill(Color.RED);
                r2.setFill(Color.RED);
                r3.setFill(Color.RED);
                r5.setFill(Color.RED);
                r6.setFill(Color.RED);
                break;
            default: break;
        }

    }

    public Group getRoot() {
        return this.root;
    }
}

一旦您更加熟悉JavaFX,如果您不介意将大量与GUI相关的代码转换为xml,请查看fxml和SceneBuilder。

如果您想要更聪明的东西,我留下了可选的注释代码,用于计算获胜者。它代替了您的IF语句,但可读性较差。这两个数组可以是static final,并且可以替换Rock,Paper和Scissors整数。