Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Unnecessary blocks of code  (Read 1731 times)

0 Members and 1 Guest are viewing this topic.

Fnak

  • Newbie
  • *
  • Posts: 22
    • View Profile
Unnecessary blocks of code
« on: April 18, 2014, 02:02:22 pm »
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:

Quote
         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
« Last Edit: April 18, 2014, 02:10:41 pm by Fnak »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Unnecessary blocks of code
« Reply #1 on: April 18, 2014, 02:07:34 pm »
The obvious approach would be to outsource the code into reusable functions. Everytime you duplicate code, consider writing a new function for it.

For rectangle collisions, you could have a look at sf::Rect::intersects().
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Fnak

  • Newbie
  • *
  • Posts: 22
    • View Profile
Re: Unnecessary blocks of code
« Reply #2 on: April 18, 2014, 03:01:26 pm »
So you mean I should have 5 different functions, one for each ball? I suppose it makes it easier to read but it's still just as much code.

It's not possible to just have this code:

Quote
         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!");
      }

work for all variables in the array instead of just (in this case, obstacleSprite[0])
« Last Edit: April 18, 2014, 03:04:00 pm by Fnak »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Unnecessary blocks of code
« Reply #3 on: April 18, 2014, 03:13:46 pm »
So you mean I should have 5 different functions, one for each ball? I suppose it makes it easier to read but it's still just as much code.
Of course not. You have one function and use it for 5 balls.

It's not possible to just have this code [...] work for all variables in the array instead of just (in this case, obstacleSprite[0])
Yes, it is. Where the codes differ, you have to use parameters. In your case, you could pass the sprite. Have you really not done this before? How to use functions and parameters is basic C++, maybe you should have a deeper look at a good book...

But read also the second comment in my last post, it would simplify a lot.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Unnecessary blocks of code
« Reply #4 on: April 18, 2014, 03:19:42 pm »
I'm thinking "loop over objects, for each object test collision". Something like:

for (const auto& elem : container) {
  if (collides(elem)) {
    gameover = true;
    ...
  }
}

Or similar.

Fnak

  • Newbie
  • *
  • Posts: 22
    • View Profile
Re: Unnecessary blocks of code
« Reply #5 on: April 18, 2014, 03:34:58 pm »
Quote
Have you really not done this before? How to use functions and parameters is basic C++, maybe you should have a deeper look at a good book..
Yes... I suppose i've been too eager to just jump into game development without learning the c++ basics.
I'll go read a book

thanks anyway