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

Posted Mathison晨默

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaFX项目游戏---完整的推箱子程序--包含各个功能相关的知识,希望对你有一定的参考价值。

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以上是关于JavaFX项目游戏---完整的推箱子程序--包含各个功能的主要内容,如果未能解决你的问题,请参考以下文章

一起来学pygame吧 游戏开发30例——推箱子 小游戏

HTML小游戏推箱子网页版(附完整源码)

C++基于easyx图形库实现推箱子游戏

项目: 推箱子游戏c/c++

c语言推箱子小游戏

项目: 推箱子图形化游戏 C++ / C