Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pingpong game run faster when i open STEAM - why?

I am beginner with programming in c# and i am just doing a pingpong game. I am using threads - one for collision, one for move, one for painting.

My question is why the program run faster, when i open steam :D to me it seems like a nonsense.Witch slower i mean - ball is slower and the pads too. It looks like the processor is lazy to do the work or something like that. It is happening in real time - i open the game, it is slow, i open steam, it is faster and then i close steam and it is slow again.

My first thought was that it is because dual graphics card but making it to use nvidia card don´t help.

Another situation: i open game - slow, open skype - game is faster, skype is loaded - game is slow, closing skype - game is fast, closed skype - game is slow..

How to make the game to use processor always same?

my code i move method is

    public void move()
    {
        DelegateSetScore d = new DelegateSetScore(SetScore);

        while (!isDisposing)
        {
            pad1.Y = pad1.Y + 4 * pad1Down + 4 * pad1Up;
            if (pad1.Y < 0) { pad1.Y = 0; }
            if (pad1.Y + pad1.Height > HEIGHT) { pad1.Y = HEIGHT - pad1.Height; }

            pad2.Y = pad2.Y + 4 * pad2Down + 4 * pad2Up;
            if (pad2.Y < 0) { pad2.Y = 0; }
            if (pad2.Y + pad2.Height > HEIGHT) { pad2.Y = HEIGHT - pad2.Height; }

            ball.X = ball.X + 6 * ballXDirection;
            ball.Y = ball.Y + 2 * ballYDirection;

Here is some more code about collision with borders and score counting... and it and with waitevent.

            waitevent.WaitOne(5);

I guess it is because the automatic overclocking of the processor but i am a newbie.. :D

Here is the whole thing in one thread

    public void bigthread()
    {

        DelegateSetScore d = new DelegateSetScore(SetScore);

        while (!isDisposing)
        {
            //move

            pad1.Y = pad1.Y + 4 * pad1Down + 4 * pad1Up;
            if (pad1.Y < 0) { pad1.Y = 0; }
            if (pad1.Y + pad1.Height > HEIGHT) { pad1.Y = HEIGHT - pad1.Height; }

            pad2.Y = pad2.Y + 4 * pad2Down + 4 * pad2Up;
            if (pad2.Y < 0) { pad2.Y = 0; }
            if (pad2.Y + pad2.Height > HEIGHT) { pad2.Y = HEIGHT - pad2.Height; }

            ball.X = ball.X + 6 * ballXDirection;
            ball.Y = ball.Y + 2 * ballYDirection;

            if (ball.X < 0)
            {
                ballXDirection = 1;
                intScorePlayer2++;
                this.BeginInvoke(d, intScorePlayer2, 2);
            }

            if (ball.X + ball.Width > WIDTH)
            {
                ballXDirection = -1;
                intScorePlayer1++;
                this.BeginInvoke(d, intScorePlayer1, 1);
            }

            if (ball.Y < 0)
            {
                ballYDirection = 1;
            }

            if (ball.Y + ball.Height > HEIGHT)
            {
                ballYDirection = -1;
            }

            //collision

            if ((pad1.X + pad1.Width > ball.X) && (ball.X + ball.Width > pad1.X))
                if ((pad1.Y + pad1.Height > ball.Y) && (ball.Y + ball.Height > pad1.Y))
                {
                    ballXDirection = 1;
                    if (pad1Down == 1) { ballYDirection = 1; }
                    if (pad1Up == -1) { ballYDirection = -1; }
                }

            if ((pad2.X + pad2.Width > ball.X) && (ball.X + ball.Width > pad2.X))
                if ((pad2.Y + pad2.Height > ball.Y) && (ball.Y + ball.Height > pad2.Y))
                {
                    ballXDirection = -1;
                    if (pad2Down == 1) { ballYDirection = 1; }
                    if (pad2Up == -1) { ballYDirection = -1; }
                }

            //paint - platno is graphics from picturebox
            Platno.Clear(Color.Black);
            Platno.FillRectangle(Brushes.Orange, pad1);
            Platno.FillRectangle(Brushes.Orange, pad2);
            Platno.FillRectangle(Brushes.Green, ball);

            waitevent.WaitOne(10);
        }
    }
like image 737
Jan Luxemburk Avatar asked May 10 '13 19:05

Jan Luxemburk


2 Answers

To make sure a game/simulation feel doesn't change with CPU/GPU availability, render speed, etc, you need to use elapsed time to control things, not the loop itself. In the code sample you have, I don't see you taking into account the time elapsed since last execution. So you want your speed a function of time, e.g. 5 pixels(a distance units of sort)/second. Then in every execution, you calculate how much time has elapsed since last time the loop executed, and include that in the distance you need to move by multiplying it with your speed.

Another thing you should do is limit the number of refreshes your game performs. i.e. if a render has happened less than X milliseconds ago, then you don't render until a certain amount of time passes. You can use a target FPS and calculate the time required from that. E.g. if you want to limit your app to 40 FPS, then if your render loop executed in less than 25ms since the last render, you simply sleep until 25ms passes.

Using time as your guide rather than loop executions or fixed waits should prevent your game from rendering faster or slower regardless of what's happening on your system. Of course if you're totally out of CPU/GPU and rendering takes too long, then your FPS will drop to the point you can see the slowness but with a pong-clone game, you really shouldn't get to that point. :)

like image 27
Tombala Avatar answered Sep 28 '22 11:09

Tombala


That is almost certainly because those apps reprogram the timer interrupt to occur more frequently. By default, Sleep, the waits, and context switch checking happen in 10 or 15 millisecond intervals, depending on the Windows edition, but that can be changed by calling the timeBeginPeriod API function with an argument of 1 (one), say.

See this question for how to do that in your application.

like image 131
500 - Internal Server Error Avatar answered Sep 28 '22 12:09

500 - Internal Server Error