Sign in to follow this  
Hairie

How do I reset my game?

Recommended Posts

Hairie    109
Hello,

I am making a 2d ping pong game in c#.
If the ball hits the net, the balls position and velocity should be reset to the initial values at the start of the game.
I know that the global timer therefore needs to be reset, but I don't know how to do it correctly. All my attempts yet to reset the game
have failed.

Here is the call of the ball's update method by testing class update's method:
[source lang="C#"]
public void Update(double elapsedTime)
{
ball.Update(elapsedTime,yPosition,xPosition,tablesurface,netsurface,ref c, ref r1, ref r2);
}[/source]
Here is the code of the ball update method:

[source lang="C#"]
enum ballStatus{Reset,Play};
ballStatus status = ballStatus.Play;

public void Update(double time, double height, double newXPos, List<Point> collisionsrfc,List<Point> netcollisionsrfc, ref Circle c,ref Rectangle rect1,ref Rectangle rect2)
{
if(start_timer)
{
timer+=time;
}

if((timer > 10.0) && (start_timer))
{
status = ballStatus.Reset;
}
else
{
status = ballStatus.Play;
}

if(status == ballStatus.Play)
{
Xstart = newXPos;
Ystart = height;
Tilted_Throw(time,Ystart,Xstart,collisionsrfc,netcollisionsrfc,ref c, ref rect1, ref rect2);
TotalTimePassed+=12*time;
SetPosition(GetX,GetY);
}

if(status == ballStatus.Reset)
{
SetPosition(-340,70);
Xstart=-340;
Ystart=70;
TotalTimePassed=0;
resetvelocity=true;
timer=0;
start_timer=false;
}

}

[/source]

The variable[b] timer[/b] is not the global game timer, but the timer for reseting the game after 10 seconds, if the ball collided with the net.
The global timer is the variable [b]TotalTimePassed[/b].
The values [b]-340 [/b]for[b] x[/b] and [b]70[/b] for [b]y[/b] are the initial position values of the ball at the start of the game.
If [b]resetvelocity[/b] is true, than the velocity is changed to the desired initial velocity of the ball in the ball's [b]Tilted_Throw[/b] method:

[source lang="C#"]
if(resetvelocity==false)
{
_velocity.Y = (velocity_start.Y - g.gravitation * TotalTimePassed);
_velocity.X = velocity_start.X;
}

else
{
SetStartVelocity(90+rand.Next(0,15),10+rand.Next(5,15));
_velocity.X = velocity_start.X;
_velocity.Y = velocity_start.Y;
resetvelocity=false;
}
[/source]
With [b]SetStartVelocity [/b]I set the new starting velocity([b]velocity_start[/b])(a bit different than the initial velocity, because ive added randomness).
The problem is, whenever the ball hits the net, [b]timer [/b]will first go to [b]0 [/b]and then go up to [b]10[/b], then again to [b]0[/b] and then again up to [b]10 [/b]etc.
After every 10 seconds the ball will repeat its motion near the net and at this point i notice, that the ball is only displayed for 1 frame at the [b]desired position(-340,70)[/b].

Please help me or give me solutions for this problem.
Thanks in advance!

Share this post


Link to post
Share on other sites
I didn't understand your code, but here come some observations:

1) Personally, I think "reset" should not be a status, but a method Reset() who take care of the new values;

2) From what I understand of the your explication, you want to reset the ball just after 10 seconds of the collision, right? So,first you must initialize the variable timer with value 0, then the timer starts just when the ball hit the net. When the collision is detected, you should call a method that will start the variable timer (use a boolean var to manage when update timer's value);

3) On the Update() method, you check if the timer is >= 10 seconds. If the timer reached the 10 seconds, you call Reset(). On Reset() method, you will set timer to 0, will set the variable that controls if the timer must be updated or not, to false, and will reset all the values.

I programmed few basic things using C#, so, I don't know how to work with this timers. I can help better with more informations about your game.

(Sorry for the mistakes, english isn't my native language) Edited by ptrrf

Share this post


Link to post
Share on other sites
Poigahn    598
Not certain on how to tell you how to code it, When I write a game where a restart option is required, I use a function / procedureal call to reset my intitial values.
Function Reset()
Ball.x = 100
Ball.y = 100
End Function

Then when ever I want to place the Ball back to the starting position I would make this call

IF { Ball hit the net } Then Reset()

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this