Jump to content

  • Log In with Google      Sign In   
  • Create Account

How do I reset my game?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 Hairie   Members   -  Reputation: 109

Like
0Likes
Like

Posted 03 December 2012 - 06:20 PM

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 timer 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 TotalTimePassed.
The values -340 for x and 70 for y are the initial position values of the ball at the start of the game.
If resetvelocity is true, than the velocity is changed to the desired initial velocity of the ball in the ball's Tilted_Throw 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 SetStartVelocity I set the new starting velocity(velocity_start)(a bit different than the initial velocity, because ive added randomness).
The problem is, whenever the ball hits the net, timer will first go to 0 and then go up to 10, then again to 0 and then again up to 10 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 desired position(-340,70).

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

Sponsor:

#2 ptrrf   Members   -  Reputation: 395

Like
0Likes
Like

Posted 03 December 2012 - 08:56 PM

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, 03 December 2012 - 08:58 PM.


#3 Aurioch   Crossbones+   -  Reputation: 1304

Like
0Likes
Like

Posted 03 December 2012 - 09:03 PM

First, a question for you. Do you use "bulk" C# or some library / wrapper (if yes, which)?

#4 Hairie   Members   -  Reputation: 109

Like
0Likes
Like

Posted 04 December 2012 - 01:57 PM

I use "bulk" C# code. I get the elapsed time by calling QueryPerformancetimer regularly...the only wrapper i have is tao for opengl.

#5 Poigahn   Crossbones+   -  Reputation: 520

Like
0Likes
Like

Posted 08 December 2012 - 05:58 AM

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()

Your Brain contains the Best Program Ever Written : Manage Your Data Wisely !!





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS