Created
March 16, 2011 22:38
-
-
Save rosswarren/873475 to your computer and use it in GitHub Desktop.
The Artificially Intelligent Pong Bat
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Pong | |
{ | |
using System; | |
using Microsoft.Xna.Framework; | |
using Microsoft.Xna.Framework.Content; | |
public class AIBat : Bat | |
{ | |
public AIBat(ContentManager content, Vector2 screenSize, bool side) : base(content, screenSize, side) | |
{ | |
} | |
public override void UpdatePosition(Ball ball) | |
{ | |
if (ball.GetDirection() > 1.5 * Math.PI || ball.GetDirection() < 0.5 * Math.PI) | |
{ | |
if (ball.GetPosition().Y - 5 > GetPosition().Y + GetSize().Height / 2) | |
{ | |
MoveDown(); | |
} | |
else if (ball.GetPosition().Y == GetPosition().Y + GetSize().Height / 2) | |
{ | |
} | |
else if (ball.GetPosition().Y + 5 < GetPosition().Y + GetSize().Height / 2) | |
{ | |
MoveUp(); | |
} | |
} | |
base.UpdatePosition(ball); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (rightBat.GetType() != typeof(Pong.AIBat)) | |
{ | |
if (input.LeftDown) leftBat.MoveDown(); | |
else if ((input.LeftUp)) leftBat.MoveUp(); | |
if (input.RightDown) rightBat.MoveDown(); | |
else if (input.RightUp) rightBat.MoveUp(); | |
} | |
else if (rightBat.GetType() == typeof(Pong.AIBat)) | |
{ | |
if (input.LeftDown) leftBat.MoveDown(); | |
else if ((input.LeftUp)) leftBat.MoveUp(); | |
if (input.RightDown) leftBat.MoveDown(); | |
else if (input.RightUp) leftBat.MoveUp(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void SetUpSingle() | |
{ | |
rightBat = new AIBat(Content, new Vector2(screenWidth, screenHeight), false); | |
leftBat = new Bat(Content, new Vector2(screenWidth, screenHeight), true); | |
} | |
private void SetUpMulti() | |
{ | |
rightBat = new Bat(Content, new Vector2(screenWidth, screenHeight), false); | |
leftBat = new Bat(Content, new Vector2(screenWidth, screenHeight), true); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment