当然可以!以下是一个简单的Java推箱子游戏代码示例:

import java.util.Scanner;

public class PushBoxGame {
    // 游戏地图
    private static final int[][] MAP = {
            {1,1,1,1,1,1,1,1,1,1},
            {1,0,0,0,1,0,0,0,0,1},
            {1,0,0,0,1,0,0,0,0,1},
            {1,0,0,0,0,0,0,0,0,1},
            {1,0,0,0,0,0,0,0,0,1},
            {1,0,0,0,0,0,0,0,0,1},
            {1,0,0,0,0,0,0,0,0,1},
            {1,0,0,0,0,0,0,0,0,1},
            {1,0,0,0,0,0,0,0,0,1},
            {1,1,1,1,1,1,1,1,1,1}
    };

    // 玩家当前位置
    private static int playerX = 1;
    private static int playerY = 1;

    // 推箱子的目标位置
    private static int boxTargetX = 8;
    private static int boxTargetY = 3;

    // 箱子当前位置
    private static int boxX = 3;
    private static int boxY = 6;

    public static void main(String[] args) {
        showMap();
        Scanner input = new Scanner(System.in);
        while (true) {
            System.out.print("请输入操作(WASD):");
            String operation = input.nextLine();
            switch (operation.toUpperCase()) {
                case "W":
                    movePlayer(0, -1);
                    break;
                case "A":
                    movePlayer(-1, 0);
                    break;
                case "S":
                    movePlayer(0, 1);
                    break;
                case "D":
                    movePlayer(1, 0);
                    break;
                default:
                    System.out.println("无效操作,请重新输入!");
                    break;
            }
            showMap();
            if (isWin()) {
                System.out.println("你赢了!");
                break;
            }
        }
    }

    /**
     * 移动玩家
     * @param offsetX X轴偏移量
     * @param offsetY Y轴偏移量
     */
    private static void movePlayer(int offsetX, int offsetY) {
        int newX = playerX + offsetX;
        int newY = playerY + offsetY;
        if (MAP[newY][newX] == 0) {
            playerX = newX;
            playerY = newY;
            if (newX == boxX && newY == boxY) {
                int newBoxX = boxX + offsetX;
                int newBoxY = boxY + offsetY;
                if (MAP[newBoxY][newBoxX] == 0) {
                    boxX = newBoxX;
                    boxY = newBoxY;
                }
            }
        }
    }

    /**
     * 判断是否胜利
     * @return boolean
     */
    private static boolean isWin() {
        return boxX == boxTargetX && boxY == boxTargetY;
    }

    /**
     * 显示地图
     */
    private static void showMap() {
        System.out.println("┌───────────────┐");
        for (int y = 0; y < MAP.length; y++) {
            System.out.print("│");
            for (int x = 0; x < MAP[y].length; x++) {
                if (playerX == x && playerY == y) {
                    System.out.print("P");
                } else if (boxX == x && boxY == y) {
                    System.out.print("B");
                } else if (boxTargetX == x && boxTargetY == y) {
                    System.out.print("T");
                } else if (MAP[y][x] == 1) {
                    System.out.print("■");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println("│");
        }
        System.out.println("└───────────────┘");
    }
}

该代码实现了一个简单的推箱子游戏,游戏地图和玩家、箱子的位置都是在代码中定义的,玩家通过输入WASD控制移动,移动过程中会判断是否有箱子需要推动,如果推动成功就会更新箱子的位置。最后判断箱子是否到达目标位置,如果到达就胜利结束游戏。

能用java写一个推箱子吗

原文地址: https://www.cveoy.top/t/topic/FDw 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录