• entries
55
55
• views
24772

# Fix You

198 views

Breakout has been improved.[smile] After feedback from MrCpaw (many thanks), I have added a couple of things to the game. First, you can now delete letters in the password screen with the backspace key. And second, I have made it so that when the ball goes out it will restart 'stuck' to the player and will be released with the spacebar. These were a bit hacked in (I'm not sure if its a good idea to keep on deleting and recreating an object just to keep the ball stuck to the player) but they work. [smile]

With regard to the Shmup, I am still planning the game but its all scribbled on paper. I will post what I have sorted out so far later tonight after a bit more work on it.

-AEdmonds

You should never delete an object and re create it for something like the ball. I'm assuming you're using new when the ball is made and delete when it's out of play, then recreating it as new? Not sure if that is what you meant from your post.

You should have a bool like this instead:

bool Active; or bool Visible;

Once the ball comes out of play it's no longer active, which means you need to add in a check to collision to make sure it cannot delete blocks or do anything when it's not active. Then reset the X and Y values and make it Active. Under your draw phase just put and if statement that only draws your ball if Active == true.

Just encase you're using new and delete, this is better suited for resources you never plan on using again or for awhile. For example, if you had an intro screen at the start of your game with your logo or something, after it's done playing and you've hit the main menu you don't need it anymore. So deleting that make senses. If you have a shooter and a enemy ship graphic will no longer be used for a major part of the game or even until the next level or two, you can delete it and re allocated when needed, however it doesn't make sense to use new and delete on a lazer for example every time it hits an object or goes off screen, and you need to re-shoot it.

I'm not fully sure if this is what you're doing or not, could you post your code for your ball for which you're creating a new one every time?

(Link fixed) Yes that is what I'm doing [rolleyes]. Here's the code:
// Update the ball's position (if space has been pressed)
if (spacePressed == true)
{
ball->UpdatePosition();
}
// Otherwise make sure keep deleting an creating balls that follow the player
else
{
// Delete the old ball
delete ball;

// Create a new ball
ball = new Ball( (player->GetX() + (PLAYER_WIDTH / 2)), SCREEN_HEIGHT - (3 * PLAYER_HEIGHT) );
}



I start with spacePressed = true in the constructor for this GameState and then when the player misses the ball:
// If the ball goes out the bottom of the screen
if (ball->GetY() > SCREEN_HEIGHT)
{
// Lose a life
player->ChangeLives(-1);
lives_lost++;

// Delete the old ball
delete ball;

// Create a new ball
ball = new Ball( (player->GetX() + (PLAYER_WIDTH / 2)), SCREEN_HEIGHT - (3 * PLAYER_HEIGHT) );

// Stop the ball moving (space not been pressed)
spacePressed = false;
}



I didn't think this was a very good way to do it at the time...

Edit: Also, why is it better to have a bool Active/Visible instead of deleting and recreating? Is it just safer in some way doing that? This is down to my lack of proper computer science education I think.

Try to picture this in your mind. You're playing a game of basketball, 1 on 1. Would you recreate the ball in real life every time you put in in a net or it went out of bounds? No, you would reuse that ball. What you're doing is adding more steps than required to do the same thing as making that ball not active.

Here is an example I made for you. I have a class called Ball, and I only use one Red_Ball from it for two loops. Make a new win32 console for C++ and paste this in a run it. You should always reuse as much as you can instead of recreating all over the place. This may not be the best example, but it's just showing me reusing the ball after it goes out of bounds, which is at location 100 for x.

#include <iostream>

class Ball
{
private:

// Position
int X;

// Speed
int Speed;

public:

// Constructor
Ball::Ball()
{
X = 0;

Speed = 1;
}

// Set X
void Ball::Set_X(int X_Temp)
{
X = X_Temp;
}

// Get X
int Ball::Get_X()
{
return X;
}

// Show X
void Ball::Show_X()
{
std::cout << "\nX = " << X;
}

// Move X Right
void Ball::Move_X_Right()
{
X += Speed;

// Check if X goes out of the area
if (X >= 100)
{
std::cout << "\nX is out of bounds!\n\nX = " << X;

// Reset X to 0
Set_X(0);

std::cout << "\nPutting X at 0!  X = " << X;
}
}
};

int main()
{
// Create The Ball
Ball Red_Ball;

// Game Loop
bool Game_Active = true;
int Loop_Amount = 0;

while (Game_Active == true)
{
// Show X Value
Red_Ball.Show_X();

// Move X
Red_Ball.Move_X_Right();

// If X is Reset to 0, exit!
if (Red_Ball.Get_X() == 0)
{
Loop_Amount ++;

if (Loop_Amount != 2)
{
std::cout << "\n\nPress Any Key To Loop Again (It will only happen once more!)";
std::cin.get();
}

if (Loop_Amount == 2)
{
Game_Active = false;
}
}
}

// Wait For Keyboard Input To Exit
std::cout << "\n\nPress Any Key To Quit";

std::cin.get();

return 0;
}



I hope this helps you understand what I mean. You should have a function called Reset, or something like that. So when the ball goes out of the play area you can reset the X and Y, plus other settings as if it was a fresh new object.

Thanks a lot. That was very helpful. So its just more efficient for the computer to use the same ball instead of creating new balls? Especially if I had many objects (e.g. bullets) where I was doing the same thing?

PS Just as an amusing aside: at football (soccer) matches the ball boys around the pitch always throw out a new ball instead of running after the one just kicked out. More efficient for humans I guess [lol]. But I do see what you mean. (I think)

Technically speaking since you're using new and delete you wont loose anything from doing it, however it's just a bad practice to getting into allocating memory over and over when you don't need to. It's no different than using pointers, never over use something and never use something unless you need too. For such simple games it's really pointless. I was working on another game a while back which I had to allocate memory very often and delete because I had to use a ton of resources, plus I didn't want to have a slow down on older machines. I was extremely picky on what was loaded into memory and what wasn't. However that was mainly graphic files, not classes.

In general, if you can reuse that code you should. In some cases you wont, for example I'm working on a game now which I have a ton of classes made that are all enemies, which was done for a good reason because I needed a ton of enemies on the screen at once which use different settings from that class. However in your case, you're dealing with one ball here and you should reuse it instead of deleting and using new.

In the end it all comes down to what works for you, even if it isn't considered right by me or others, some might consider it the right way. I personally stay away from new and delete unless I must use it, period.

Ok. Thanks a lot for the insight.

## Create an account

Register a new account