Collision Detection in a 2D shooter game I have the following code that works just fine for collision detection. The only problem is its size and its performance. I'm trying to do it and many other things 60 times per second, which doesn't work out very well. Is there any way to condense it as it's much the same stuff over and over again?

for(int x=0; x&lt;40; x++) {
     for(int y=0; y&lt;28; y++) {
        if(tiles[x][y]==0) {
            if(keyManager.up) {
                Rectangle player = new Rectangle(you.getX(), you.getY()-1, 10, 10), tile = new Rectangle(x*25, y*25, 25, 25);
                if(player.intersects(tile)) {
                    return;
                }
                if(x==39 &amp;&amp; y==27) {
                    you.setY(you.getY()-1);
                }
            } if(keyManager.down) {
                Rectangle player = new Rectangle(you.getX(), you.getY()+1, 10, 10), tile = new Rectangle(x*25, y*25, 25, 25);
                if(player.intersects(tile)) {
                    return;
                }
                if(x==39 &amp;&amp; y==27) {
                    you.setY(you.getY()+1);
                }
            } if(keyManager.left) {
                Rectangle player = new Rectangle(you.getX()-1, you.getY(), 10, 10), tile = new Rectangle(x*25, y*25, 25, 25);
                if(player.intersects(tile)) {
                    return;
                }
                if(x==39 &amp;&amp; y==27) {
                    you.setX(you.getX()-1);
                }
            } if(keyManager.right) {
                Rectangle player = new Rectangle(you.getX()+1, you.getY(), 10, 10), tile = new Rectangle(x*25, y*25, 25, 25);
                if(player.intersects(tile)) {
                    return;
                }
                if(x==39 &amp;&amp; y==27) {
                    you.setX(you.getX()+1);
                }
            }
        }
    }
}
You are creating a new Player and Tile every time this code executes. That is likely your performance hit. Try moving the existing player rather than creating a new one in a new location.

Does performance slow down further the longer the game runs? This is because your objects are piling up.

For further reading I recommend looking at AABB Collision Detection and Quad Trees.