aboutsummaryrefslogtreecommitdiff
path: root/ShiftOS.WinForms/Applications/Pong.cs
diff options
context:
space:
mode:
authorMichael <[email protected]>2017-06-15 17:45:57 -0400
committerMichael <[email protected]>2017-06-15 17:45:57 -0400
commit6a28bbbb634cb0e17d8b07cec35acb18f7613d34 (patch)
treead6c78ffd395b07a889cd65783f3278cabb73ad4 /ShiftOS.WinForms/Applications/Pong.cs
parente6d58c235c4d772cd5245d89cdc7dde6207165b8 (diff)
downloadshiftos_thereturn-6a28bbbb634cb0e17d8b07cec35acb18f7613d34.tar.gz
shiftos_thereturn-6a28bbbb634cb0e17d8b07cec35acb18f7613d34.tar.bz2
shiftos_thereturn-6a28bbbb634cb0e17d8b07cec35acb18f7613d34.zip
Ball prediction AI
Diffstat (limited to 'ShiftOS.WinForms/Applications/Pong.cs')
-rw-r--r--ShiftOS.WinForms/Applications/Pong.cs46
1 files changed, 46 insertions, 0 deletions
diff --git a/ShiftOS.WinForms/Applications/Pong.cs b/ShiftOS.WinForms/Applications/Pong.cs
index a88d69f..1221729 100644
--- a/ShiftOS.WinForms/Applications/Pong.cs
+++ b/ShiftOS.WinForms/Applications/Pong.cs
@@ -33,6 +33,9 @@ namespace ShiftOS.WinForms.Applications
private double ballX = 0.0f;
private double ballY = 0.0f;
+ private double aiBallX = 0.0f;
+ private double aiBallY = 0.0f;
+
private double speedFactor = 0.025;
@@ -44,6 +47,8 @@ namespace ShiftOS.WinForms.Applications
private double playerY = 0.0;
private double opponentY = 0.0;
+ bool doAi = true;
+
public void UpdateBall()
{
double ballXLocal = linear(ballX, -1.0, 1.0, 0, pnlcanvas.Width);
@@ -52,6 +57,13 @@ namespace ShiftOS.WinForms.Applications
ballXLocal -= ((double)paddleWidth / 2);
ballYLocal -= ((double)paddleWidth / 2);
+ double aiBallXLocal = linear(aiBallX, -1.0, 1.0, 0, pnlcanvas.Width);
+ double aiBallYLocal = linear(aiBallY, -1.0, 1.0, 0, pnlcanvas.Height);
+
+ aiBallXLocal -= ((double)paddleWidth / 2);
+ aiBallYLocal -= ((double)paddleWidth / 2);
+
+
double playerYLocal = linear(playerY, -1.0, 1.0, 0, pnlcanvas.Height);
double opponentYLocal = linear(opponentY, -1.0, 1.0, 0, pnlcanvas.Height);
@@ -60,6 +72,9 @@ namespace ShiftOS.WinForms.Applications
Rectangle ballRect = new Rectangle((int)ballXLocal, (int)ballYLocal, paddleWidth, paddleWidth);
+ Rectangle aiBallRect = new Rectangle((int)aiBallXLocal, (int)aiBallYLocal, paddleWidth, paddleWidth);
+
+
Rectangle playerRect = new Rectangle((int)paddleWidth, (int)(playerYLocal - (int)(paddleHeight / 2)), (int)paddleWidth, (int)paddleHeight);
Rectangle opponentRect = new Rectangle((int)(pnlcanvas.Width - (paddleWidth * 2)), (int)(opponentYLocal - (int)(paddleHeight / 2)), (int)paddleWidth, (int)paddleHeight);
@@ -90,6 +105,16 @@ namespace ShiftOS.WinForms.Applications
}
+ //Enemy paddle - AI:
+ if (aiBallRect.IntersectsWith(opponentRect))
+ {
+ //check if the ball x is greater than the player paddle's middle coordinate
+ if (aiBallRect.Right <= opponentRect.Right - (opponentRect.Width / 2))
+ {
+ doAi = false;
+ }
+
+ }
//Player paddle:
@@ -104,6 +129,10 @@ namespace ShiftOS.WinForms.Applications
//set y velocity based on where the ball hit the paddle
yVel = linear((ballRect.Top + (ballRect.Height / 2)), playerRect.Top, playerRect.Bottom, -1, 1);
+ //reset the ai location
+ aiBallX = ballX;
+ aiBallY = ballY;
+ doAi = true;
}
}
@@ -115,6 +144,23 @@ namespace ShiftOS.WinForms.Applications
ballX += xVel * speedFactor;
ballY += yVel * speedFactor;
+ aiBallX += xVel * (speedFactor * 2);
+ aiBallY += yVel * (speedFactor * 2);
+
+ if (doAi == true)
+ {
+ if (opponentY != aiBallY)
+ {
+ if (opponentY < aiBallY)
+ {
+ opponentY += speedFactor;
+ }
+ else
+ {
+ opponentY -= speedFactor;
+ }
+ }
+ }
}
private void pnlcanvas_Paint(object sender, PaintEventArgs e)