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.
Thanks for reading
-AEdmonds
// 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) );
}
// 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;
}
#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;
}
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?