Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 816011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:46:07+00:00 2026-05-15T01:46:07+00:00

The problem is that I am trying to make certain tiles blocked so the

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T01:46:08+00:00Added an answer on May 15, 2026 at 1:46 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to say this self.preferred_amount * object.each{|li|li.variant}.collect{|li|li.weight} The only problem is that
I'm trying to solve the 3n+1 problem and I have a for loop that
I'm trying to create a form that has an expandable widget. The problem is
I'm trying to redirect/forward a Pylons request. The problem with using redirect_to is that
I have the problem that an specific step in Ant can only be executed
I am having the problem that I cannot select a specific XML node which
i'm trying to make a small project that uses multiple forms (dialogs) for different
I'm trying to make use of precalculated values by outputting them to a header
One problem that I come across regularly and yet don't have a solution to
A problem that we need to solve regularly at my workplace is how to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.