Skip to content

Instantly share code, notes, and snippets.

@rosswarren
Created March 16, 2011 22:38
Show Gist options
  • Save rosswarren/873475 to your computer and use it in GitHub Desktop.
Save rosswarren/873475 to your computer and use it in GitHub Desktop.
The Artificially Intelligent Pong Bat
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);
}
}
}
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();
}
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