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 7515749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:54:30+00:00 2026-05-30T00:54:30+00:00

Now that I’m loading my map from a 2D Array of char , the

  • 0

Now that I’m loading my map from a 2D Array of char, the game is really slow when I load more than 100 blocks (10×10 area). I have it rendering the blocks only within a certain distance of the character already. I also believe I know why it runs so slow, but I dont know how to fix it. Heres the code:

public void renderBlocks(Graphics g) {
    super.paint(g);
    Graphics2D g2d = (Graphics 2D)g;
    for(int x = 0; x < 50; x++) {
        for(int y = 0; y < 50; y++) {
            //blocks[x][y] tells it which type of block to load
            Block next = new Block(blocks[x][y], x, y); 

            if((next.getX() - player.getX()) >= (-13*32) && 
            (next.getX() - player.getX()) <= (13*32)) {

                if((next.getY() - player.getY()) >= (-6*32) && 
                (next.getY() - player.getY()) <= (6*32)) {

                     g2d.drawImage(next.getImage(), (next.getX() - player.getX()),    
                      (next.getY() - player.getY()), this);
                }
            }
         }
     }
 }

The line:

Block next = new Block(blocks[x][y], x, y);

is what is making it run really slow. Even though it’s not drawing those to the screen, it’s still setting Block next equal to new Block(blocks[x][y], x, y) for all of the elements of blocks[x][y]. The problem is I can’t just remove this line of code because it decides how far away from the character to draw blocks. I need to make it so that it only sets next equal to as many elements of blocks[x][y] that are being drawn to the screen.

I have tried putting:

if((x - player.getX()) >= (-13*32) && (x - player.getX()) <= (13*32)){
    Block next = new Block(blocks[x][y], x, y);
    ...Rest of Code here
}

around it and the same for y - playerY, but it doesn’t work, though in theory it should.

Please answer this question if you know a way to do this and make the game run as fast as it did when I was loading the Blocks from an ArrayList, which allowed me to load over 5000 blocks and it run at a good FPS rate. If you need the source code to the game to help me fix this issue, comment that you need it and I will post it.

  • 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-30T00:54:31+00:00Added an answer on May 30, 2026 at 12:54 am

    You could make the loop only over the tiles surrounding the player:

    super.paint(g);
    Graphics2D g2d = (Graphics 2D)g;
    
    // find the block with the player (might need correction)
    int playerX = (int) (player.getX() / 32);
    int playerY = (int) (player.getY() / 32);
    
    int viewDist = 6;
    
    int lowerX = Math.max(playerX - viewDist, 0);
    int upperX = Math.min(playerX + viewDist + 1, 50);
    int lowerY = Math.max(playerY - viewDist, 0);
    int upperY = Math.min(playerY + viewDist +  1, 50);
    
    for (int x = lowerX; x < upperX; x++)
    {
        for (int y = lowerY; y < upperY; y++)
        {
            Block next = new Block(blocks[x][y], x, y);
            g2d.drawImage(next.getImage(), (next.getX() - player.getX()),    
                          (next.getY() - player.getY()), this);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Now that my facebook app has got more than 1000 likes -- https://apps.facebook.com/156485447732146/ But
Now that LINQ to SQL is a little more mature, I'd like to know
Now that I am playing with NHibernate I am getting a lot more YSODs
Now that I am learning more about design time aspects of component development, I
Now that NOSQL or object only storage systems like MongoDB or memcached are really
Now that std has a real hash map in unordered_map , why (or when)
Now that most of the major browsers support full page zoom (at present, the
Now that I know C++ I want to get into desktop application that have
Now that .NET v3.5 SP1 has been released (along with VS2008 SP1), we now
Now that it's clear what a metaclass is , there is an associated concept

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.