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);
}
}