01/10/2018, 09:47
Di chuyển tank trong Battle City
Mình muốn làm một tank di chuyển được thông qua implement một interface IBattleClient.
Đây là interface của IBattleClient:
public class BattleCommand
{
/// <summary>
/// Action to perform
/// </summary>
public UdpCommand.Action Action { get; set; }
/// <summary>
/// Action parameter (for change of speed/rotation)
/// Speed can be -100 .. 100, you have to use values between 0 .. 200
/// (so 0 corresponds to speed -100 and the value 200 corresponds to speed +100)
/// Rotation can be 0..360, you have to use values between 0 .. 36
/// (so 0 corresponds to 0 degrees (facing to the right side), and 36 corresponds to 360 degrees)
/// </summary>
public byte ActionParameter { get; set; }
}
/// <summary>
/// Interface - must use this as a basis
/// </summary>
public interface IBattleClient
{
/// <summary>
/// The team name
/// </summary>
string TeamName { get; }
/// <summary>
/// Image filename in the Images directory
/// </summary>
string ImageFile { get; }
/// <summary>
/// This method will be called when the level is reset
/// </summary>
void GameWasReset();
/// <summary>
/// This will be called when the game quits - must close all windows/threads/handles
/// </summary>
void ShutDown();
/// <summary>
/// This will be called periodically
/// </summary>
/// <param name="items">COPY of the gameitem list</param>
/// <param name="remainingSeconds">Remaining seconds in the current map</param>
/// <param name="playerId">ID number of the current player</param>
/// <returns>The command that the AI wants to execute</returns>
BattleCommand GameTick(IEnumerable<GameItem> items, byte remainingSeconds, byte playerId);
}
Đây là Client mà mình muốn implement:
public class AutoClient : IBattleClient
{
enum StageType { Nothing, Left, Right }
StageType stage = StageType.Nothing;
static Random R = new Random();
public AutoClient()
{
}
public string ImageFile
{
get
{
return "anon.png";
}
}
public string TeamName
{
get
{
return "Zs_Auto";
}
}
public BattleCommand GameTick(IEnumerable<GameItem> items, byte remainingSeconds, byte playerId)
{
Tank ourTank = items.SingleOrDefault(x => x.OwnerId == playerId && x is Tank) as Tank;
UdpCommand.Action myAction = UdpCommand.Action.NOTHING;
byte myParameter = 0;
if (ourTank.Rotation != 0 && ourTank.Rotation != 180) // bad angle => fix rotation
{
stage = StageType.Nothing;
myAction = UdpCommand.Action.SETROTATION;
}
else
{
if (ourTank.Speed == 0) // we are not moving
{
switch (stage)
{
case StageType.Right: // we were moving right in the past, so rotate left
myAction = UdpCommand.Action.SETROTATION;
myParameter = 18;
stage = StageType.Nothing;
break;
case StageType.Left: // we were moving left in the past, so rotate right
myAction = UdpCommand.Action.SETROTATION;
myParameter = 0;
stage = StageType.Nothing;
break;
case StageType.Nothing: // we are after a turn, so move forward
myAction = UdpCommand.Action.SETSPEED;
myParameter = 150;
stage = ourTank.Rotation == 0 ? StageType.Right : StageType.Left;
break;
}
}
else
{
ourTank.Speed = 0; // No effect on the real tank
if (R.Next(0, 20) == 0)
{
if (ourTank.BulletNum > 0)
myAction = UdpCommand.Action.SHOOTBULLET;
else if (ourTank.RocketNum > 0)
myAction = UdpCommand.Action.SHOOTROCKET;
}
}
}
BattleCommand cmd = new BattleCommand() { Action = myAction, ActionParameter = myParameter };
return cmd;
}
public void GameWasReset()
{
}
public void ShutDown()
{
}
}
Mình muốn chuyển Auto sang chế độ di chuyển bằng bàn phím( Có thể lên xuống và bắn có điều khiển) , có ai đó có thể gợi ý dùm mình được không?
Bài liên quan