JavaFX项目游戏---完整的推箱子程序--包含各个功能

这是一个使用JavaFX开发的推箱子游戏项目,包括关卡选择、排行榜和设置功能。游戏界面支持上下关切换、重置和提示,但撤销功能尚未实现。项目源代码详细,涉及Element基类、Map类、Lists类、CheckPointAlert类等多个关键组件。
摘要由CSDN通过智能技术生成

JavaFX项目游戏—推箱子程序



前言

本项目包含基本的推箱子程序,并实现了选择关卡、排行榜、设置功能。
在游戏界面实现上下关、重置、提示功能。撤销功能暂未解决。并实现了在各个界面之间切换。
博主文笔不好就直接贴上源代码。如果看不懂或者有什么想法都欢迎私聊或者评论,共同进步


Element基类

package Game;
/*
 * 图片基础类
 * 定义:
 * 1.图片的x、y坐标
 * 2.图片宽高
 * 3.图片类型
 * */

import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;

public class Element extends Pane {
   
    public static int cell = 30;
    static int imageCell = 50;
    private int x = 0;
    private int y = 0;
    private Image image;
    private ImageView imageView;

    public Element() {
   
    }

    public int getCell() {
   
        return cell;
    }

    public void setCell(int cell) {
   
        Element.cell = cell;
    }

    public int getX() {
   
        return x;
    }

    public void setX(int x) {
   
        this.x = x;
    }

    public int getY() {
   
        return y;
    }

    public void setY(int y) {
   
        this.y = y;
    }

    public Image getImage() {
   
        return image;
    }

    public void setImage(Image image) {
   
        this.image = image;
    }

    public void setImageView() {
   
        this.imageView = new ImageView(image);
        this.imageView.setFitWidth(cell);
        this.imageView.setFitHeight(cell);
        this.imageView.setY(y);
        this.imageView.setX(x);
    }

    public ImageView getImageView() {
   
        return imageView;
    }
}

Map类–关卡地图类,继承自Element

package Game;
/*
 * 关卡地图类
 * 绘制关卡
 * 作为主Pane
 * */


import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

public class Map extends Element {
   
    static int width = 380;
    private Element element = new Element();
    public static int cell  = 30;
    private int height = 320;
    private int[][] list;

    public static void setCell() {
   
        if(PlayGame.listNumber < 5){
   
            cell = 30;
        }else{
   
            cell = 20;
        }
        Element.cell = cell;
    }
    public static int getMapCell(){
   
        return cell;
    }

    public void setList(int[][] list) {
   
        this.list = list;
    }

    public ImageView getImagePlayer(int[] playerList) {
   
        return drawImagePlayer(playerList[0], playerList[1]);
    }

    public ImageView[] getImageBox(int boxNumber, int[][] boxList) {
   
        ImageView[] imageBox = new ImageView[boxNumber];
        for (int i = 0; i < boxNumber; i++) {
   
            imageBox[i] = drawImageBox(boxList[i][0], boxList[i][1]);
        }
        return imageBox;
    }


    public void drawMap() {
   
        Image image;
        for (int i = 0; i < list.length; i++) {
   
            for (int j = 0; j < list[0].length; j++) {
   
                if (list[i][j] == 1) {
   
                    image = new Image("/image/qiang.jpg");
                    drawElement(i, j, image);
                } else if (list[i][j] == 2) {
   
                    image = new Image("/image/beijing.jpg");
                    drawElement(i, j, image);
                } else if (list[i][j] < 0) {
   
                    image = new Image("/image/beijing.jpg");
                    drawElement(i, j, image);
                } else if (list[i][j] == 4) {
   
                    image = new Image("/image/mubiaotu.jpg");
                    drawElement(i, j, image);
                } else if (list[i][j] == 5) {
   
                    image = new Image("/image/beijing.jpg");
                    drawElement(i, j, image);
                }
            }
        }
    }

    public void drawElement(int i, int j, Image image) {
   
        element.setX(cell * j);
        element.setY(cell * i);
        element.setImage(image);
        element.setImageView();
        getChildren().add(element.getImageView());
    }

    public ImageView drawImagePlayer(int i, int j) {
   
        element.setX(j * cell);
        element.setY(i * cell);
        element.setImage(new Image("/image/dongman.png"));
        element.setImageView();
        getChildren().add(element.getImageView());
        return element.getImageView();
    }

    public ImageView drawImageBox(int i, int j) {
   
        element.setX(j * cell);
        element.setY(i * cell);
        element.setImage(new Image("/image/xiangzi.jpg"));
        element.setImageView();
        System.out.println(cell);
        getChildren().add(element.getImageView());
        return element.getImageView();
    }

}

Lists类—生成关卡地图列表

package Game;
/*
* 关卡列表
* 用以输出关卡的列表表示
* */

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Lists {
   
    public int[][][] list;
    private int listNumber = 0;

    private int boxNumber = 0;
    private int[][] boxIndexList ;
    private int[] playerList = new int[2];
    private int[] boxList;
    private int[][] mubiaoList;

    public int[][][] getAllList(){
   
        return list;
    }

    public void initList(int[][] list){
   
        boxIndexList = new int[boxNumber][2];
        boxList = new int[boxNumber];
        mubiaoList = new int[boxNumber][2];
        int num = 0;
        int mubiaonumber = 0;
        for(int i=0;i<list.length;i++){
   
            for(int j=0;j<list[0].length;j++){
   
                if(list[i][j] < 0){
   
                    boxIndexList[num][0] = i;
                    boxIndexList[num][1] = j;
                    boxList[num] = list[i][j];
                    num++;
                }else if(list[i][j] == 5){
   
                    playerList[0] = i;
                    playerList[1] = j;
                }else if(list[i][j] == 4){
   
                    mubiaoList[mubiaonumber][0] = i;
                    mubiaoList[mubiaonumber][1] = j;
                    mubiaonumber++;
                }

            }
        }
    }

    public void setBoxNumber(int[][] list){
   
        boxNumber = 0;
        for(int[] row:list){
   
            for(int column : row){
   
                if(column < 0){
   
                    boxNumber++;
                }
            }
        }
    }
    public int getBoxNumber(){
   
        return boxNumber;
    }
    public int[] getPlayerList(){
   
        return playerList;
    }
    public int[][] getBoxIndexList(){
   
        return boxIndexList;
    }
    public int[] getBoxList(){
   
        return boxList;
    }
    public int[][] getMubiaoList(){
   
        return mubiaoList;
    }
    public int[][] getList(){
   
        return list[listNumber];
    }

    public boolean isSetBox(int i,int j){
   
        for(int[] row : mubiaoList){
   
            if(row[0] == i && row[1] == j){
   
                return true;
            }
        }
        return false;
    }
    public void setListNumber(int listNumber){
   
        this.listNumber = listNumber;
    }
    public int getListNumber(){
   
        return listNumber;
    }
    public int[][] setrowList(String str){
   
        int[][] strlists;
        int row = 0;
        int column = 0;
        for(int i=0;i<str.length();i++){
   
            if(str.charAt(i) != '\n'){
   
                column++;
            }else{
   
                row++;
            }
        }
        row++;
        column = column / row ;
        strlists = new int[row][column];
        int box = -1;
        for(int i=0;i<row;i++){
   
            for(int j=0;j<column;j++){
   
                char charat = str.charAt((column + 1) * i + j);
                if(charat == '_'){
   
                    strlists[i][j] = 0;
                } else if(charat == '#'){
   
                    strlists[i][j] = 1;
                }else if(charat == '-'){
   
                    strlists[i][j] = 2;
                }else if(charat == '.'){
   
                    strlists[i][j] = 4;
                }else if(charat == '@'){
   
                    strlists[i][j] = 5;
                }else if(charat == '$'){
   
                    strlists[i][j] = box;
                    box--;
                }
            }
        }
        return strlists;
    }
    public void setList(){
   
        int[][][] listqianwu = {
   
                {
   
                        {
   0, 0, 1, 1, 1, 1, 0, 0, 0},
                        {
   1, 1, 1, 2, 2, 1, 1, 1, 1},
                        {
   1, 2, 2, 2, 2, 2, -1, 2, 1},
                        {
   1, 2, 1, 2, 2, 1, -2, 2, 1},
                        {
   1, 2, 4, 2, 4, 1, 5, 2, 1},
                        {
   1, 1, 1, 1, 1, 1, 1, 1, 1}
                },
                {
   
                        {
   0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0},
                        {
   1, 1, 1, 1, 2, 2, 2, 1, 0, 0, 0, 0},
                        {
   1, 4, 4, 1, -1, -2, 2, 1, 0, 0, 0, 0},
                        {
   1, 2, 4, 4, -3, 2, 2, 1, 1, 1, 1, 1},
                        {
   1, 2, 5, 1, 2, 1, 2, 1, 2, 2, 2, 1},
                        {
   1, 2, 2, -4, 2, 2, 2, 2, 2, -5, 2, 1},
                        {
   1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 4, 1},
                        {
   1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1}
                },
                {
   
                        {
   0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0},
                        {
   0, 0, 1, 1, 2, 1, 0, 1, 1, 1, 1},
                        {
   0, 1, 1, 2, 2, 1, 1, 1, 2, 2, 1},
                        {
   1, 1, 2, -1, 2, 2, 2, 2, 2, 2, 1},
                        {
   1, 2, 2, 2, 5, -2, 2, 1, 2, 2, 1},
                        {
   1, 1, 1, 2, -3, 1, 1, 1, 2, 2, 1},
                        {
   0, 0, 1, 2, 2, 1, 4, 4, 2, 2, 1},
                        {
   0, 1, 1, 2, 1, 1, 4, 1, 2, 1, 1},
                        {
   0, 1, 2, 2, 2, 2, 2, 2, 1, 1, 0},
                        {
   0, 1, 2, 2, 2, 2, 2, 1, 1, 0, 0},
                        {
   0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0}
                },
                {
   
                        {
   0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1},
                        {
   0, 0, 0, 1, 2, 2, 2, 2, 2, 2, 1},
                        {
   1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1},
                        {
   1, 2, -1, 2, -2, 2, 4, 4, 1, 2, 1},
                        {
   1, 2, 1, 2, 2, 4, 1, 2, 1, 2, 1},
                        {
   1, 2, 1, 2, 1, 1, 1, 2, 1, 2, 1},
                        {
   1, 2, 1, 4, 1, 2, -3, 2, 2, 2, 1},
                        {
   1, 2, 1, 4, -5, 5, 
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
javafx -fx-background-image是JavaFX中用于设置背景图片的样式属性。通过设置该属性,可以将指定的图片作为背景图像应用于JavaFX场景或节点。具体的使用方式是在CSS样式中使用类似以下语法进行设置: -fx-background-image: url("image.jpg"); 其中,url("image.jpg")表示要使用的图片的路径和文件名。注意,该路径可以是相对路径或绝对路径。 此外,还可以通过其他样式属性来调整背景图片的显示方式,例如设置背景图片的位置、尺寸、重复方式等。可以使用类似以下语法进行设置: -fx-background-position: center; -fx-background-size: cover; -fx-background-repeat: no-repeat; 以上是设置背景图片的一些基本用法,具体的样式属性和用法可以参考JavaFX的相关文档和API。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [JavaFX官方教程(一)之JavaFX概述](https://blog.csdn.net/moakun/article/details/83045838)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [JavaFX学习笔记](https://blog.csdn.net/lsj1997718117/article/details/122781016)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mathison晨默

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值