Java for循环问题

发布于 2024-09-03 02:14:01 字数 3296 浏览 4 评论 0原文

问题是我试图阻止某些图块,以便玩家无法在它们上行走。但是,它只读取第一个图块,即 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

The problem is that I am trying to make certain tiles blocked so the player cannot walk on them. However, it's only reading the FIRST tile which is board[0][0] and everything else is not checked....

What am I doing wrong? :(

Here's my code:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陪我终i 2024-09-10 02:14:01

代码中的问题之一是在这一行中,您使用 px, py 调用阻塞,它们是 32 的倍数:

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)超出范围。您的代码中还存在其他错误,但这对您来说应该是一个好的开始。

One of the problems in your code is on this line you are calling blocked with px, py which are multiples of 32:

blocked(px, py)

But you use these number as indexes into your array which would cause an ArrayIndexOutOfBoundsException:

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

Which you've tried to "fix" by ignoring it:

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

So I suspect that it only works for (0,0) because (32,32) is out of bounds. There are also other errors in your code, but this should be a good start for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文