JAVA大作业汇总1

JAVA大作业

代码

```
package thegreatwork;

import javafx.application.;
import javafx.scene.control.
;
import javafx.scene.;
import javafx.scene.paint.
;
import javafx.scene.shape.;
import javafx.scene.layout.
;
import javafx.stage.;
import javafx.event.
;
import javafx.scene.input.;
import javafx.scene.text.
;
import javafx.geometry.*;

import java.util.;
import java.io.
;

/Gui2048
目的:显示分数和2048游戏,
颜色伴随着分数的改变而改变/

public class Gui2048 extends Application {

private String outputBoard;
private Board board;

private static final int TILE_WIDTH = 106;
// 这是为了未来不同位数的数字提供不同的字体以便看起来美观?
private static final int TEXT_SIZE_LOW = 55;
private static final int TEXT_SIZE_MID = 45;

private static final int TEXT_SIZE_HIGH = 35;

// 不同的数字对应不同的颜色(改变的是方块的颜色的填充色)
private static final Color COLOR_EMPTY = Color.rgb(238, 228, 218, 0.35);
private static final Color COLOR_2 = Color.rgb(238, 228, 218);
private static final Color COLOR_4 = Color.rgb(237, 224, 200);
private static final Color COLOR_8 = Color.rgb(242, 177, 121);
private static final Color COLOR_16 = Color.rgb(245, 149, 99);
private static final Color COLOR_32 = Color.rgb(246, 124, 95);
private static final Color COLOR_64 = Color.rgb(246, 94, 59);
private static final Color COLOR_128 = Color.rgb(237, 207, 114);
private static final Color COLOR_256 = Color.rgb(237, 204, 97);
private static final Color COLOR_512 = Color.rgb(237, 200, 80);
private static final Color COLOR_1024 = Color.rgb(237, 197, 63);
private static final Color COLOR_2048 = Color.rgb(237, 194, 46);
private static final Color COLOR_OTHER = Color.BLACK;
private static final Color COLOR_GAME_OVER = Color.rgb(238, 228, 218, 0.5);
// 数字大小的差异可以用数字颜色的填充色来使其明显
private static final Color COLOR_VALUE_LIGHT = Color.rgb(249, 246, 242);


private static final Color COLOR_VALUE_DARK = Color.rgb(119, 110, 101);

private GridPane pane;

private int tile;
private Rectangle[][] rectangle;
private Text[][] text;
private Text txt0;
private Text txtScore;
private int[][] grid;
private StackPane pane0;
private double title;

/*start
 *初始化页面框
 */
@Override
public void start(Stage primaryStage) {
    // 界面的初始化
    processArgs(getParameters().getRaw().toArray(new String[0]));


    pane = new GridPane();
    pane.setAlignment(Pos.CENTER);//全部居中
    pane.setPadding(new Insets(11.5, 12.5, 13.5, 14.5));//设置页边距
    pane.setStyle("-fx-background-color: rgb(187, 173, 160)");
    //间距的设定
    pane.setHgap(15);
    pane.setVgap(15);

    int size = board.GRID_SIZE;
    grid = new int[size][size];
    grid = board.getGrid();//把数字粘过来

    //2048
    txt0 = new Text();
    txt0.setText("2048");
    txt0.setFont(Font.font("Times New Roman", FontWeight.BOLD, 50));
    txt0.setFill(Color.BLACK);
    //分数
    txtScore = new Text();
    txtScore.setText("score: " + this.board.getScore());
    txtScore.setFont(Font.font("Times New Roman", FontWeight.BOLD, 30));
    txtScore.setFill(Color.BLACK);

    //把“2048”和分数框放到相应的位置上
    pane.add(txt0, 0, 0,2, 1);
    pane.add(txtScore, 2, 0, size - 2, 1);
    //居中
    GridPane.setHalignment(txt0, HPos.CENTER);
    GridPane.setHalignment(txtScore, HPos.CENTER);

    //初始化网格的各种属性
    rectangle = new Rectangle[size][size];
    text = new Text[size][size];


    //把数字所在的框和数字进行初始化
    for (int row = 0; row < size; row++) {
        for (int col = 0; col < size; col++) {
            tile = grid[row][col];//把grid里面的数字取出来

            //每一个数字所在的框
            rectangle[row][col] = new Rectangle();
            rectangle[row][col].setWidth(TILE_WIDTH);
            rectangle[row][col].setHeight(TILE_WIDTH);
            rectangle[row][col].setFill(getColor(tile));
            //把画好的方格放到页面框上
            pane.add(rectangle[row][col], col, row + 1);


            //初始化数字的各种
            text[row][col] = new Text();
            if (tile == 0) {
                text[row][col].setText("");//0为空
            } else {
                text[row][col].setText(Integer.toString(tile));
            }
            int txt = getSize(tile);//根据数字大小来决定字体大小,在最下面
            text[row][col].setFont(Font.font("Times New Roman", FontWeight.BOLD, txt));//bold:粗体
            text[row][col].setFill(getTextColor(tile));
            pane.add(text[row][col], col, row+1 );
            GridPane.setHalignment(text[row][col], HPos.CENTER);


        }
    }

//有了下面这一段才能把界面显示出来,基本上少一行界面就不能显示。然而每句话是什么含义我并不是非常的清楚
pane0 = new StackPane();//堆栈面板
pane0.getChildren().add(pane);
Scene scene = new Scene(pane0);

    scene.setOnKeyPressed(new myKeyHandler());//接收山下左右的按键

    primaryStage.setTitle("Gui2048");//界面的名称
    primaryStage.setScene(scene);
    primaryStage.show();


}

/*EventHandler.java
 *目的:根据玩家输入不同的指令进行不同的操作
 *键盘输入*/
private class myKeyHandler implements EventHandler<KeyEvent> {
    public void handle(KeyEvent e) {
        if (e.getCode().equals(KeyCode.UP)) {
            if (board.canMove(Direction.UP)) {
                board.move(Direction.UP);
                System.out.println("Moving Up");
                board.addRandomTile();
            }
            if (board.isGameOver()) {
                gameOver();
            }
        } else if (e.getCode().equals(KeyCode.DOWN)) {
            if (board.canMove(Direction.DOWN)) {
                board.move(Direction.DOWN);
                System.out.println("Moving Down");
                board.addRandomTile();
            }
            if (board.isGameOver()) {
                gameOver();
            }
        } else if (e.getCode().equals(KeyCode.LEFT)) {
            if (board.canMove(Direction.LEFT)) {
                board.move(Direction.LEFT);
                System.out.println("Moving Left");
                board.addRandomTile();
            }
            if (board.isGameOver()) {
                gameOver();
            }
        } else if (e.getCode().equals(KeyCode.RIGHT)) {
            if (board.canMove(Direction.RIGHT)) {
                board.move(Direction.RIGHT);
                System.out.println("Moving Right");
                board.addRandomTile();
            }
            if (board.isGameOver()) {
                gameOver();
            }
            //按R的话页面顺时针方向旋转90度
        } else if (e.getCode().equals(KeyCode.R)) {
            board.rotate(true);
            System.out.println("Rotate 90 degrees clockwise");
            //按S的话这个界面就会被保存下来
        } else if (e.getCode().equals(KeyCode.S)) {
            System.out.println("Saving Board to " + outputBoard);
            try {
                board.saveBoard(outputBoard);
            } catch (IOException yingjun) {
                System.out.println("saveBoard throw an Exception");
            }
        }

        paint();//更新画面

    }
}


/*gameOver
 *用来表示这个游戏已经结束了
 */
private void gameOver() {
    //创建一个矩形,和之前那个游戏界面一样大小
    Rectangle rec = new Rectangle();
    rec.setFill(COLOR_GAME_OVER);
    rec.setWidth(pane.getWidth());
    rec.setHeight(pane.getHeight());

    //打印出来"gameover"
    Text over = new Text();
    over.setText("Game Over!");
    over.setFont(Font.font("Impact", FontWeight.BOLD, 50));
    over.setFill(Color.BLACK);

    //把这两个元素添加到界面上
    pane0.getChildren().add(rec);
    pane0.getChildren().add(over);

}


/*paint
 * 移动之后重新画这个页面
 */
private void paint() {
    int size = board.GRID_SIZE;
    grid = board.getGrid();

    txtScore.setText("score: " + this.board.getScore());

    //在移动以后重新打印16个数字框
    for (int row = 0; row < size; row++) {
        for (int col = 0; col < size; col++) {
            tile = grid[row][col];
            rectangle[row][col].setFill(getColor(tile));
            if (tile == 0) {
                text[row][col].setText("");
            } else {
                text[row][col].setText(Integer.toString(tile));//把数字变成字符串                }
                int txt = getSize(tile);
                text[row][col].setFont(Font.font("Times New Roman", FontWeight.BOLD, txt));
                text[row][col].setFill(getTextColor(tile));

            }
        }

    }
}


/*getColor
 *让矩形框有颜色
 *每一个数字对应一个背景颜色
 */
private Color getColor(int num) {
    if (num == 0)
        return COLOR_EMPTY;
    if (num == 2)
        return COLOR_2;
    if (num == 4)
        return COLOR_4;
    if (num == 8)
        return COLOR_8;
    if (num == 16)
        return COLOR_16;
    if (num == 32)
        return COLOR_32;
    if (num == 64)
        return COLOR_64;
    if (num == 128)
        return COLOR_128;
    if (num == 256)
        return COLOR_256;
    if (num == 512)
        return COLOR_512;
    if (num == 1024)
        return COLOR_1024;
    if (num == 2048)
        return COLOR_2048;

    return COLOR_OTHER;
}

/*getTextColor
 *每一串文字对应一个颜色
 *数字大小决定颜色
 */
private Color getTextColor(int num) {
    if (num < 8) {
        return COLOR_VALUE_DARK;
    } else {
        return COLOR_VALUE_LIGHT;
    }
}

/*getSize
 *决定一串文字的大小
 *数字大小决定字体大小
 */
private int getSize(int num) {
    if (num < 128) {
        return TEXT_SIZE_LOW;
    } else if (num < 1024) {
        return TEXT_SIZE_MID;
    } else {
        return TEXT_SIZE_HIGH;
    }
}


// 用于高级用户,命令行输入
private void processArgs(String[] args) {
    String inputBoard = null;
    int boardSize = 0;

    //参数必须成对出现
    if ((args.length % 2) != 0) {
        printUsage();
        System.exit(-1);
    }

    //加工参数
    for (int i = 0; i < args.length; i += 2) {
        if (args[i].equals("-i")) {
            //设置初始的界面,也可以载入其他的存档
            inputBoard = args[i + 1];
        } else if (args[i].equals("-o")) {
            //确定保存的时候输出文件的名字
            outputBoard = args[i + 1];
        } else if (args[i].equals("-s")) {
            //设置界面大小
            boardSize = Integer.parseInt(args[i + 1]);
        } else {   //错误的内容
            printUsage();
            System.exit(-1);
        }
    }

    //对应-o
    if (outputBoard == null)
        outputBoard = "2048.board";
    //对应-s
    if (boardSize < 2)
        boardSize = 4;

    //对应-i
    try {
        if (inputBoard != null)
            board = new Board(inputBoard, new Random());
        else
            board = new Board(boardSize, new Random());
    } catch (Exception e) {
        System.out.println(e.getClass().getName() +
                " was thrown while creating a " +
                "Board from file " + inputBoard);
        System.out.println("Either your Board(String, Random) " +
                "Constructor is broken or the file isn't " +
                "formated correctly");
        System.exit(-1);
    }
}

//帮助文档
private static void printUsage() {
    System.out.println("Gui2048");
    System.out.println("Usage:  Gui2048 [-i|o file ...]");
    System.out.println();
    System.out.println("  Command line arguments come in pairs of the " +
            "form: <command> <argument>");
    System.out.println();
    System.out.println("  -i [file]  -> Specifies a 2048 board that " +
            "should be loaded");
    System.out.println();
    System.out.println("  -o [file]  -> Specifies a file that should be " +
            "used to save the 2048 board");
    System.out.println("                If none specified then the " +
            "default \"2048.board\" file will be used");
    System.out.println("  -s [size]  -> Specifies the size of the 2048" +
            "board if an input file hasn't been");
    System.out.println("                specified.  If both -s and -i" +
            "are used, then the size of the board");
    System.out.println("                will be determined by the input" +
            " file. The default size is 4.");
}

}

转载于:https://www.cnblogs.com/tuolemi/p/6360124.html

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值