I'm developing a game called PongBreakout in C++ using a game engine I got from university.
I've got a few problems with my game that I'm struggling to fix and need some help to figure out how to do so.
The first bug is that the countdown is far to fast and I don't know how to slow it down - I've been told to use a scalar <- ball.cpp (Update())
The code for this is below, I've debugged the value for .mdframetime
and it keeps varying between 0.016 and 0.015 however it remains to countdown far to fast.
double frametime = g_theTimer.mdFrameTime; // Update frametime
if (reset == true) // If the ball needs reseting.
{
countdown -= frametime; // Reduce countdown - This needs fixing its far to fast.
if(countdown > 2)
{
pTheDrawEngine->WriteText(512,384,"3");
}
else if(countdown > 1)
{
pTheDrawEngine->WriteText(512,384,"2");
}
else if(countdown > 0)
{
pTheDrawEngine->WriteText(512,384,"1");
}
if(countdown <= 0)
{
countdown = 3;
reset = false;
position.set(512, 384);
}
}
The second bug is that the ball sometimes gets stuck at the side of the screen if the paddle is there (play it and you'll eventually notice it.) <- ball.ccp (ProcessCollisons())
void Ball::PanelCollision(Rectangle2D Panel1, Rectangle2D Panel2, Rectangle2D Panel3)
{
if (BallOutline.Intersects(Panel1))
{
velocity.XValue *= -1;
velocity.YValue = 5 * ((position.YValue - Panel1.GetCentre().YValue) / 25);
}
else if (BallOutline.Intersects(Panel2))
{
velocity.XValue *= -1;
velocity.YValue = 5 * ((position.YValue - Panel2.GetCentre().YValue) / 25);
}
else if (BallOutline.Intersects(Panel3))
{
velocity.XValue *= -1;
velocity.YValue = 5 * ((position.YValue - Panel3.GetCentre().YValue) / 25);
}
}
I'm not to sure about what's wrong with this function, as it works until the panels are at position 0 or 768 it causes the ball to stick and in some cases go of the screen but still be drawn in the same place.
Do you think it's possibly best to make a counter that will increase, after x amount of times it's increased, the ball is obviously stuck so get the position of the panel and the position of the ball, take them away to get the value of how close the ball is stuck towards the center of the panel and minus it away to unstick it?
The third bug is that the rand()
function doesn't seem to be working for getting a value between 8 and 5. <- ball.cpp
srand (time(NULL)); // Seed the random
switch(rand() % 4 + 1) // Which way should the ball shoot off?
{
case 1: velocity.set(rand()%4 + 5 ,-rand() % 5 + 1); // Rand is broken. Why? - Needs to be between 5 and 7. for X
break;
case 2: velocity.set(-rand()%4 + 5 ,rand() % 5 + 1);
break;
case 3: velocity.set(rand()%4 + 5 ,rand() % 5 + 1);
break;
case 4: velocity.set(-rand()%4 + 5 ,-rand() % 5 + 1);
break;
}
The rand()
function needs to return a value between 5 and 7 but it's returning values less than 5 in some cases and I can't figure out why. I've tried the max - min + min method and that didn't work either.