JavaFX项目游戏—推箱子程序
文章目录
- JavaFX项目游戏---推箱子程序
- 前言
- Element基类
- Map类--关卡地图类,继承自Element
- Lists类---生成关卡地图列表
- CheckPonitAlert类---实现关卡通关后的Alert界面
- PlayGame类---实现游戏功能
- Move类---操作移动
- PlayAction---程序的主类
- Function类---实现游戏界面的各个功能
- StartScreen类---程序开始界面
- GuanqiaScreen --- 实现游戏界面
- PaihangScreen---排行榜界面---功能有bug
- PlayButton类--- 定义游戏界面的各个功能键
- SettingScreen---设置界面---功能暂未实现
- 游戏存储文件
- 游戏效果图
前言
本项目包含基本的推箱子程序,并实现了选择关卡、排行榜、设置功能。
在游戏界面实现上下关、重置、提示功能。撤销功能暂未解决。并实现了在各个界面之间切换。
博主文笔不好就直接贴上源代码。如果看不懂或者有什么想法都欢迎私聊或者评论,共同进步
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,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