Hello I'm after some tips regarding continous blocks of code related to the same things...
I've been thinking about some ways to handle it in a good way because the amount of code I've written seems a bit unnecessary and there must be some sort of way to make it simpler.
Let me give you an example:
I'm trying to create a game, and in it there are 5 different balls that you have to avoid. If you get hit by one of them you'll lose. So I typed this code to fix the collision:
if (!gamefinished && playerSprite.getPosition().x > obstacleSprite[0].getPosition().x - 30 &&
playerSprite.getPosition().y > obstacleSprite[0].getPosition().y - 30 &&
playerSprite.getPosition().x < obstacleSprite[0].getPosition().x + 20 &&
playerSprite.getPosition().y < obstacleSprite[0].getPosition().y + 30)
{
gamefinished = true;
musicsound.stop();
endgameText.setString(" YOU DIED\n\n\n\n\nYOU FINISHED GAME WITH: " + currentGold + " GOLD!");
}
The problem is that I have to repeat this code for all 5 balls, and later on I'm thinking of adding more balls, so that would mean an immense amount of code. This is the case for a lot of things more than collision too, so there's so much code!!
How to make it smoother and easier to read?
thank you in advance!!
edit: to clarify, what I'm looking for is to how to make it happen for all variables in the array. I could look something like:
if (!gamefinished && playerSprite.getPosition().x > obstacleSprite[0, 1, 2, 3, 4, 5].getPosition().x - 30