繁体   English   中英

Java for循环问题

[英]Java for-loop problem

问题是我试图使某些瓷砖被遮挡,以使玩家无法在它们上面行走。 但是,它只读取了第一个图块,它是board[0][0] ,其他所有内容均未选中。

我究竟做错了什么? :(

这是我的代码:

import java.applet.*;
import java.applet.Applet;

import java.awt.*;
import java.awt.Canvas.*;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.*;


public class gen extends Applet implements KeyListener {
    Image[] tiles;
    Image player;
    int x;
    int y;
    int px;
    int py;
    boolean left;
    boolean right;
    boolean down;
    boolean up;
    int[][] board;
    final int NUM_TILES = 5;
    public final int BLOCKED = 1;



    public void init() {
    // Load board
    board = loadBoard();





        tiles = new Image[NUM_TILES];
    for(int i = 0;i < NUM_TILES;i++) {
        tiles[i] = getImage(getClass().getResource(String.format("tile%d.png", i)));
    }
    for (int y=0;y< NUM_TILES;y++) {
        board[1][1] = BLOCKED;
        board[1][2] = BLOCKED;
        board[1][3] = BLOCKED;
        }
        player = getImage(getClass().getResource("player.png")); // our player
        addKeyListener(this);

        px = 0;

        py = 0;
    }

    public void keyPressed(KeyEvent e) {



        if (e.getKeyCode() == KeyEvent.VK_LEFT) {
            left = true;

            px = px - 32;
        }

        if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
            right = true;

            px = px + 32;
        }

        if (e.getKeyCode() == KeyEvent.VK_DOWN) {
            down = true;

            py = py + 32;
        }

        if (e.getKeyCode() == KeyEvent.VK_UP) {
            up = true;

            py = py - 32;
        }

        repaint();

    }

    public void keyReleased(KeyEvent e) {
    } // ignore

    public void keyTyped(KeyEvent e) {
    } // ignore

    public void paint(Graphics g) {
        x = 0;

        y = 0;

        // here's a sample map!

        // but we're loading from a text file!

        // so...  why is this needed?
        for (int row = 0; row < board.length; row++) {
            for (int col = 0; col < board[row].length; col++) {
                int index = board[row][col];
                g.drawImage(tiles[index], 32 * col, 32 * row, this);
                if (board[row][col] == BLOCKED) {
                System.out.println("\n"+board[row][col] + "is BLOCKED!\n");
                }else{System.out.println("\n"+board[row][col] + "is NOT Blocked!\n");}
            }
        }

        g.drawImage(player, px, py, this);
        try {
        System.out.println("ON BLOCKED TILE?: " + blocked(px,py) + "\n");
    }catch(ArrayIndexOutOfBoundsException e) {}
    } // end paint method

    public void update(Graphics g) {
        paint(g);
    }

    private int[][] loadBoard() {
        int[][] board = {
                { 0, 1, 2, 3, 4, 4, 3, 4 },
                { 0, 1, 2, 3, 4, 4, 3, 4 },
                { 2, 2, 4, 2, 2, 1, 1, 0 },
                { 0, 1, 2, 3, 4, 4, 3, 4 },
                { 0, 0, 0, 2, 3, 4, 4, 2 },
                { 2, 2, 4, 2, 2, 1, 1, 0 },
                { 0, 1, 2, 3, 4, 4, 3, 4 },
                { 0, 0, 0, 2, 3, 4, 4, 2 }
            };
    return board;
    }

    public boolean blocked(int tx, int ty) {
            return board[tx][ty] == BLOCKED;
        }
} // end whole thing

您的代码中的问题之一是在此行上您以32的倍数的px,py调用被阻止:

blocked(px, py)

但是,您将这些数字用作数组的索引,这将导致ArrayIndexOutOfBoundsException

public boolean blocked(int tx, int ty)
{
    return board[tx][ty] == BLOCKED;
}

您尝试通过忽略来“修复”它:

try
{
    System.out.println("ON BLOCKED TILE?: " + blocked(px,py) + "\n");
}
catch(ArrayIndexOutOfBoundsException e) {}

所以我怀疑它仅适用于(0,0),因为(32,32)超出范围。 您的代码中还存在其他错误,但这对您来说是一个好的开始。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM