Sprite不会在基于.txt的地图中移动

时间:2017-10-20 20:22:30

标签: java

我试图让我的Zombie在我制作的地图中使用(使用按键)(使用扫描仪和文件阅读器),但它只是生育然后坐在那里。我仍然有更多的代码要做,因为程序没有完成但我不能做任何其他事情,直到僵尸移动。在此先感谢您的帮助!

PS。 EZ是一个多媒体库,旨在使新手程序员更容易快速构建包含图形和声音的Java应用程序。它在马诺阿的UH Hawaii使用。

zombie sprite sheet

barbwire

brains

40 26

WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW
M                                      M
M                                  B   M
M       B              B               M
M                                      M
M                 M                    M
M   M             M                    M
M   M             WWW                  M
M   WWWW                      B        M
M   M                                  M 
M   M                                  M
M                 B                    M
M      B                               M
M                         WWWWWW       M
M                         M            M
M            B            M            M
M                         M            M
M                         M    B       M
M                         M            M
M     WWWWWWW                          M
M           M                          M
M           M     B                    M
M      B                           B   M
M                                      M
WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW

僵尸

public class Zombie {

    EZImage zombieSheet;

    int x = 0;                // Position of Sprite
    int y = 0;
    int zombieWidth;        // Width of each sprite
    int zombieHeight;        // Height of each sprite
    int direction = 0;        // Direction character is walking in
    int walkSequence = 0;    // Walk sequence counter
    int cycleSteps;            // Number of steps before cycling to next animation step
    int counter = 0;        // Cycle counter

    Zombie(String imgFile, int startX, int startY, int width, int height, int steps) {
        x = startX;                    // position of the sprite character on the screen
        y = startY;
        zombieWidth = width;        // Width of the sprite character
        zombieHeight = height;        // Height of the sprite character
        cycleSteps = steps;            // How many pixel movement steps to move before changing the sprite graphic
        zombieSheet = EZ.addImage(imgFile, x, y);
        setImagePosition();
    }

    private void setImagePosition() {

        // Move the entire sprite sheet
        zombieSheet.translateTo(x, y);

        // Show only a portion of the sprite sheet.
        // Portion is determined by setFocus which takes 4 parameters:
        // The 1st two numbers is the top left hand corner of the focus region.
        // The 2nd two numbers is the bottom right hand corner of the focus region.
        zombieSheet.setFocus(walkSequence * zombieWidth, direction, walkSequence * zombieWidth + zombieWidth, direction + zombieHeight);
    }

    public void moveDown(int stepSize) {
        y = y + stepSize;

        direction = 0;

        if ((counter % cycleSteps) == 0) {
            walkSequence++;
            if (walkSequence > 3)
                walkSequence = 0;
        }
        counter++;
        setImagePosition();
    }

    public void moveLeft(int stepSize) {
        x = x - stepSize;
        direction = zombieHeight;

        if ((counter % cycleSteps) == 0) {
            walkSequence--;
            if (walkSequence < 0)
                walkSequence = 3;
        }
        counter++;
        setImagePosition();
    }

    public void moveRight(int stepSize) {
        x = x + stepSize;
        direction = zombieHeight * 2;

        if ((counter % cycleSteps) == 0) {
            walkSequence++;
            if (walkSequence > 3)
                walkSequence = 0;
        }
        counter++;

        setImagePosition();
    }

    public void moveUp(int stepSize) {
        y = y - stepSize;
        direction = zombieHeight * 3;

        if ((counter % cycleSteps) == 0) {
            walkSequence--;
            if (walkSequence < 0)
                walkSequence = 3;
        }
        setImagePosition();

        counter++;
    }

    // Keyboard controls for moving the character.
    public void go() {
        if (EZInteraction.isKeyDown('w')) {
            moveUp(2);
        } else if (EZInteraction.isKeyDown('a')) {
            moveLeft(2);
        } else if (EZInteraction.isKeyDown('s')) {
            moveDown(2);
        } else if (EZInteraction.isKeyDown('d')) {
            moveRight(2);
        }
    }
}

ZombieMain

import java.awt.Color;
import java.io.FileReader;
import java.util.Scanner;

public class ZombieMain {

    static EZImage[] walls = new EZImage[500];
    static EZImage[] sideWalls = new EZImage[500];
    static EZImage[] brains = new EZImage[50];
    static int wallsCount = 0;
    static int sideWallsCount = 0;
    static int brainsCount = 0;

    public static void main(String[] args) throws java.io.IOException {

        //initialize scanner
        Scanner fScanner = new Scanner(new FileReader("boundaries.txt"));

        int w = fScanner.nextInt();
        int h = fScanner.nextInt();
        String inputText = fScanner.nextLine();

        //create backdrop
        EZ.initialize(w * 33, h * 32);
        EZ.setBackgroundColor(new Color(0, 0, 0));
        Zombie me = new Zombie("zombieSheet.png", 650, 450, 48, 58, 10);

        //set reading parameters and establish results of case readings
        for (int row = 0; row < 41; row++) {

            inputText = fScanner.nextLine();

            for (int column = 0; column < inputText.length(); column++) {

                char ch = inputText.charAt(column);

                switch (ch) {
                    case 'W':
                        walls[wallsCount] = EZ.addImage("barbwire.jpg", column * 32, row * 32);
                        wallsCount++;
                        break;
                    case 'M':
                        sideWalls[wallsCount] = EZ.addImage("barb.jpg", column * 32, row * 32);
                        wallsCount++;
                        break;
                    case 'B':
                        brains[brainsCount] = EZ.addImage("brains.png", column * 32, row * 32);
                        brainsCount++;
                        break;
                    default:
                        // Do nothing
                        break;
                }

                //printed count of walls, side walls, and brains
                System.out.println("W = " + wallsCount);
                System.out.println("M = " + sideWallsCount);
                System.out.println("B = " + brainsCount);
            }
        }
        fScanner.close();

        while (true) {
            me.go();
            EZ.refreshScreen();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

好的,明白了。

使用for循环来迭代文本文件和地图。您的for循环假定您的地图文件正好有41行。它没有,因此扫描程序在到达循环外时会抛出异常。

通常,假设读取文件时固定大小是一个不好的解决方案。你应该做的是使用某种方法(通常由这些读者提供)来检查你是否还没有到达终点,并使用while循环。由于您无论如何都需要行号,因此您需要为其保留一个单独的计数器,并在每个while循环传递中递增它。

对于扫描仪,您正在寻找的方法是fScanner.hasNext(),因此您的代码将如下所示:

    int row = 0;
    //set reading parameters and establish results of case readings
    while (fScanner.hasNext()) {
         ...
         row++;
    }