如何设置位置的蛇和梯子在java蛇和梯子游戏?

qgzx9mmu  于 5个月前  发布在  Java
关注(0)|答案(3)|浏览(55)

我正在用java开发一个蛇和梯子的游戏,这是可配置的,也就是说,你可以设置蛇和梯子的“头”和“尾”的位置。我试过加载蛇的图像,并重新调整它们的大小,以适应头部和尾部位置的界限,但这似乎影响了图像的质量。有人能建议一个更好的方法/算法吗?非常感谢帮助。

vm0i2vca

vm0i2vca1#

我建议把蛇的图形分成几个部分--一个头,一个尾巴和一个或多个中间部分。然后,根据蛇的长度,你可以根据需要画出中间部分。

olhwl3o2

olhwl3o22#

您使用的方法有两个可能导致质量差的"问题":
1.您正在(我假设)升级图形图像,这将导致块效应。
1.如果你改变x轴和y轴的比例(例如放大),长蛇会比短蛇更胖更宽,这不是人们期望看到的。
我想稍微修改一下fd的解决方案。如下进行:
为头部、尾部和中间的 * 单个 * 部分准备一个图形,以便您可以将任意数量的中间部分链接在一起。
当你需要画一条蛇时,计算它的长度,然后看看你需要多少中间部分才能让整条蛇等于或大于这个计算的长度。
创建一个正确大小的位图缓冲区,以容纳具有正确中间部分数量的水平(或垂直!)蛇。将背景绘制为透明,然后将蛇绘制到此位图中。它通常会比您需要的略长。
缩放和旋转位图,并将其放置在电路板上的正确位置。
你仍然会在一定程度上缩放,但是缩放因子应该足够小,不明显。例如,如果你需要5.5个中间部分来精确拟合,那么你会画6个中间部分,然后将整条蛇缩放大约5.5/6,使其完全正确的长度。这小于10%的变化,所以不应该很明显。
这解决了上述问题:
1.蛇的宽度只会有轻微的变化
1.你只做低档的,从不做高档的。
它具有以下实际好处:
1.你只需要把蛇水平(或垂直)摆放,这很容易做到
1.这应该是非常有效的。所有的真实的工作都是由图形库完成的,它必须旋转,缩放和放置构建的蛇。在大多数平台上,这些操作都是作为一个转换矩阵来实现的,它是在硬件中计算的,因此非常快。我在我写的一个Android应用程序中广泛使用这种方法,它非常快。
请注意,蛇的“摆动”越多,缩放因子与1.0的差异就越小,因此宽度的变化也就越小。

pb3s4cty

pb3s4cty3#

public class Understand {

static int turn = 0;
static int[] gameBoard = new int[100];
static Map<Integer, Integer> ladderPositionValue;
static Map<Integer, Integer> snakePositionValue;
static int userPosition = 0;
static int computerPosition = 0;

public static void populateBoard() {
    ladderPositionValue = new HashMap<>();
    snakePositionValue = new HashMap<>();
    // SUPPOSE WE HAVE 10 LADDERS AND 10 SNAKES
    for (int i = 0; i < 10; i++) {
        int ladderPositionKey = (int) (Math.random() * 85) + 10;
        if (!ladderPositionValue.keySet().contains(ladderPositionKey)) {
            ladderPositionValue.put((int) (Math.random() * 100) + 1, (int) (Math.random() * 10) + 5);
        } else {
            i--;
        }
        int snakePositionKey = (int) (Math.random() * 95) + 15;
        if (!ladderPositionValue.keySet().contains(ladderPositionKey) &&
                !snakePositionValue.keySet().contains(ladderPositionKey)) {
            snakePositionValue.put((int) (Math.random() * 100) + 1, (int) (Math.random() * 10) + 5);
        } else {
            i--;
        }
    }
}

public static int rollDice(int repeat) {
    System.out.println("ROLLING DICE...PRESS ANY KEY TO CONTINUE:");
    Scanner in = new Scanner(System.in);
    String cont = in.nextLine();
    int rolledNo = (int) (Math.random() * 6) + 1;
    if (rolledNo == 6) {
        System.out.println("NUMBER IS:" + rolledNo + ". ANOTHER CHANCE.");
        rollDice(repeat++);
    } else {
        System.out.println("NUMBER IS:" + rolledNo);
    }
    int finalCount = rolledNo + (repeat * 6);
    return finalCount;
}

public static boolean userTurn() {
    if (turn % 2 == 0) {
        turn++;
        return true;
    }
    turn++;
    return false;
}

public static void newUserPosition(int rolledNo) {
    userPosition = userPosition + rolledNo;
    System.out.println("YOUR NEW POSITION IS:" + userPosition);
    userPosition = checkSnakeOrLadder(userPosition);
}

public static void newComputerPosition(int rolledNo) {
    computerPosition = computerPosition + rolledNo;
    computerPosition = checkSnakeOrLadder(userPosition);
}

private static int checkSnakeOrLadder(int position) {
    if (snakePositionValue.keySet().contains(position)) {
        System.out.println("AAAAAAAAAAAHHHH ... BITTEN BY SNAKE");
        position = position - snakePositionValue.get(position);
    } else if (ladderPositionValue.keySet().contains(position)) {
        System.out.println("YAAAAAAAY.. GOT A LADDER");
        position = position + ladderPositionValue.get(position);
    }
    return position;
}

public static void main(String args[]) {
    System.out.println("WELCOME TO SNAKES & LADDER");
    System.out.println("***************************");
    populateBoard();
    while (userPosition != 100 && computerPosition != 100) {
        if (userTurn()) {
            System.out.println("YOUR TURN:");
            int rolledNo = rollDice(0);
            System.out.println("MOVING YOUR POSITION:");
            newUserPosition(rolledNo);
        } else {
            System.out.println("COMPUTER TURN:");
            int rolledNo = rollDice(0);
            System.out.println("MOVING COMPUTER POSITION:");
            newComputerPosition(rolledNo);
        }
    }
    if (userPosition == 100) {
        System.out.println("YOUR ARE THE WINNER!!!!");
    } else if (computerPosition == 100) {
        System.out.println("COMPUTER WON!!!!");
    }
}

字符串
}

相关问题