C#中国象棋网络版源代码(四)-辅助类

本文介绍了一款中国象棋游戏的开发过程和技术细节,包括图像处理、坐标转换、控制面板设计等方面的内容。提供了实用的代码示例,如棋子图像分割、棋盘坐标转换以及控制面板的时间管理和操作按钮控制。

1.Common.cs文件

公共类, 参考绿色注释

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
/********************************************************************************
* 本网站内容允许非商业用途的转载,但须保持内容的原始性并以链接的方式注明出处,
* 本公司保留内容的一切权利。
* 凡注明易学原创的源代码及文档不得进行商业用途转载!!! *

*********************************************************************************
* 易学网祝您学业有成!* by www.vjsdn.com
*********************************************************************************/

namespace www.vjsdn.com.ChineseChess.Library
{
   /// <summary>
   /// 图像处理辅助类
   /// </summary>
   public class ImageHelper
   {
      /// <summary>
      /// 获取均分图片中的某一个
      /// </summary>
      public static Image GetImageByAverageIndex(Image orignal, int count, int index)
      {
         int width = orignal.Width / count;
         return CutImage(orignal, width * (index - 1), width, orignal.Height);
      }
      
      /// <summary>
      /// 获取图片一部分
      /// </summary>
      private static Image CutImage(Image orignal, int start, int width, int height)
      {
         Bitmap partImage = new Bitmap(width, height);
         Graphics g = Graphics.FromImage(partImage);//获取画板
         Rectangle srcRect = new Rectangle(start, 0, width, height);//源位置开始
         Rectangle destRect = new Rectangle(0, 0, width, height);//目标位置
         //复制图片
         g.DrawImage(orignal, destRect, srcRect, GraphicsUnit.Pixel);
         partImage.MakeTransparent(Color.FromArgb(255, 0, 255));
         g.Dispose();
         return partImage;
      }
   }
   
   /// <summary>
   /// 棋盘坐标:作为值的集合,可用结构体
   /// </summary>
   public struct ChessPoint
   {
      private int _x;
      private int _y;
      
      /// <summary>
      /// 构造函数
      /// </summary>
      public ChessPoint(int x, int y)
      {
         this._x = x;
         this._y = y;
      }
      
      /// <summary>
      /// X轴坐标
      /// </summary>
      public int X
      {
         get { return _x; }
         set { _x = value; }
      }
      
      /// <summary>
      /// Y轴坐标
      /// </summary>
      public int Y
      {
         get { return _y; }
         set { _y = value; }
      }
   }
   
   /// <summary>
   /// 吃掉老王,异常
   /// </summary>
   public class GameLoseException : Exception
   {
      public GameLoseException(string msg) : base(msg) { }
   }
   
   /// <summary>
   /// 棋子颜色
   /// </summary>
   public enum ChessColor
   {
      Red,//红
      Black//黑
   }
   
   /// <summary>
   /// 消息提示框
   /// </summary>
   public class Msg
   {
      public static bool AskQuestion(string msg)
      {
         DialogResult r;
         r = MessageBox.Show(msg, "确认",
         MessageBoxButtons.YesNo,
         MessageBoxIcon.Question,
         MessageBoxDefaultButton.Button2);
         return (r == DialogResult.Yes);
      }
      
      public static void ShowException(Exception e)
      {
         string s = e.Message;
         Warning(s);
      }
      
      public static void Warning(string msg)
      {
         System.Windows.Forms.MessageBox.Show(msg, "警告",
         MessageBoxButtons.OK,
         MessageBoxIcon.Exclamation,
         MessageBoxDefaultButton.Button1);
      }
      
      public static void ShowError(string msg)
      {
         MessageBox.Show(msg, "警告",
         MessageBoxButtons.OK,
         MessageBoxIcon.Hand,
         MessageBoxDefaultButton.Button1);
      }
      
      public static void ShowInformation(string msg)
      {
         MessageBox.Show(msg, "信息",
         MessageBoxButtons.OK,
         MessageBoxIcon.Asterisk,
         MessageBoxDefaultButton.Button1);
      }
   }
   
   
}



2.坐标(Coordinate)

一个重要的类,由棋盘像素转换为棋子坐标

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace www.vjsdn.com.ChineseChess.Library
{
   /// <summary>
   /// 坐标系转换类
   /// </summary>
   public class CoordinateHelper
   {
      private static int _cellWidth = 48;
      
      /// <summary>
      /// 根据棋盘坐标计算物理坐标
      /// </summary>
      public static Point CalculatePyshicalPoint(ChessPoint p)
      {
         return new Point(_cellWidth * p.X + 31 - 43 / 2, _cellWidth * p.Y + 31 - 43 / 2);
      }
      
      /// <summary>
      /// 转换物理坐标到棋盘坐标
      /// </summary>
      public static ChessPoint TranslatePointToChessPoint(Point p)
      {
         int x = p.X - (31 - 43 / 2);
         int y = p.Y - (31 - 43 / 2);
         return new ChessPoint(x / 49, y / 49);
      }
      
      /// <summary>
      ///反向坐标
      /// </summary>
      public static ChessPoint Reverse(ChessPoint point)
      {
         int x = Math.Abs(point.X - 8);
         int y = Math.Abs(point.Y - 9);
         return new ChessPoint(x, y);
      }
   }
}



3.控制面板(Controll Panel)

分离主界面的部分代码

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace www.vjsdn.com.ChineseChess.Library
{
   /// <summary>
   /// 控制面板,
   /// </summary>
   public class GameControlPanel
   {
      private DateTime NullDate = new DateTime(1900, 1, 1, 0, 0, 0);
      private PictureBox _redHistory;
      private PictureBox _blackHistory;
      private PictureBox _pbBegin;
      private PictureBox _pbRegret;
      private PictureBox _pbLost;
      
      private DateTime _RedGlobalTimer; //局时
      private DateTime _RedStepTimer; //步时
      
      private DateTime _BlackGlobalTimer; //局时
      private DateTime _BlackStepTimer; //步时
      
      private Timer _RedTimer;
      private Timer _BlackTimer;
      
      private SetImage _SetImage;
      private SetTimer _SetTimer;
      
      //启动计时器
      public void StartTimer(ChessColor playerColor)
      {
         if (ChessColor.Red == playerColor)
         {
            _RedStepTimer = NullDate;
            (_SetTimer.Target as Form).Invoke(_SetTimer, _RedTimer, true);
         }
         else
         {
            _BlackStepTimer = NullDate;
            (_SetTimer.Target as Form).Invoke(_SetTimer, _BlackTimer, true);
         }
      }
      
      //停用计时器
      public void StopTimer(ChessColor playerColor)
      {
         if (ChessColor.Red == playerColor)
         {
            (_SetTimer.Target as Form).Invoke(_SetTimer, _RedTimer, false);
            
            _RedGlobalTimer.AddTicks(_RedStepTimer.Ticks);
            _RedStepTimer = NullDate;
         }
         else
         {
            (_SetTimer.Target as Form).Invoke(_SetTimer, _BlackTimer, false);
            
            _BlackGlobalTimer.AddTicks(_BlackStepTimer.Ticks);
            _BlackStepTimer = NullDate;
         }
      }
      
      //构造器
      public GameControlPanel(PictureBox redHistory, PictureBox blackHistory,
      PictureBox begin, PictureBox regret, PictureBox lost, Timer red, Timer black, SetImage setImage, SetTimer setTimer)
      {
         _SetImage = setImage;
         _SetTimer = setTimer;
         
         _redHistory = redHistory;
         _blackHistory = blackHistory;
         _pbBegin = begin;
         _pbRegret = regret;
         _pbLost = lost;
         
         _RedTimer = red;
         _BlackTimer = black;
         _RedTimer.Tick += new EventHandler(RedTimer_Tick);
         _BlackTimer.Tick += new EventHandler(BlackTimer_Tick);
         
         _RedGlobalTimer = NullDate;
         _RedStepTimer = NullDate;
         _BlackGlobalTimer = NullDate;
         _BlackStepTimer = NullDate;
      }
      
      //红方计时器
      private void RedTimer_Tick(object sender, EventArgs e)
      {
         _RedStepTimer = _RedStepTimer.AddSeconds(1);
         _RedGlobalTimer = _RedGlobalTimer.AddSeconds(1);
         this.DrawTimerString(ChessColor.Red);
      }
      
      //黑方计时器
      private void BlackTimer_Tick(object sender, EventArgs e)
      {
         _BlackStepTimer = _BlackStepTimer.AddSeconds(1);
         _BlackGlobalTimer = _BlackGlobalTimer.AddSeconds(1);
         this.DrawTimerString(ChessColor.Black);
      }
      
      // 绘制显示在左边玩家信息里的棋子
      public void ShowHistory(Chess chess)
      {
         //用于显示当前玩家走的什么棋子
         //判断当前棋子是什么颜色
         if (chess.Color == ChessColor.Red)
         {
            Image img = DrawHistoryChess(_redHistory.Image, chess);
            _redHistory.Invoke(_SetImage, _redHistory, img);
         }
         else
         {
            Image img = DrawHistoryChess(_blackHistory.Image, chess);
            _blackHistory.Invoke(_SetImage, _blackHistory, img);
         }
      }
      
      /// <summary>
      /// 绘制棋子
      /// </summary>
      public static Image DrawHistoryChess(Image source, Chess chess)
      {
         Bitmap map = new Bitmap(source);
         using (Graphics hisc = Graphics.FromImage(map))
         {
            hisc.DrawImage(chess.ChessImage, 36, 20);
         }
         return map;
      }
      
      //显示计时时间
      public void DrawTimerString(ChessColor color)
      {
         Graphics g;
         Font f;
         PictureBox ori;
         DateTime global;
         DateTime step;
         
         if (ChessColor.Red == color)
         {
            ori = _redHistory;
            global = _RedGlobalTimer;
            step = _RedStepTimer;
         }
         else
         {
            ori = _blackHistory;
            global = _BlackGlobalTimer;
            step = _BlackStepTimer;
         }
         
         Bitmap bmp = ChineseChess.Res.Properties.Resources.timer;
         g = Graphics.FromImage(bmp);
         f = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
         g.DrawString(GetTimerStr(global), f, new SolidBrush(Color.Red), 12, 6);
         g.DrawString(GetTimerStr(step), f, new SolidBrush(Color.Red), 12, 30);
         g.Dispose();
         
         Rectangle rr = new Rectangle(new Point(43, 82), bmp.Size);
         g = Graphics.FromImage(ori.Image);
         g.DrawImage(bmp, rr);
         g.Dispose();
         
         ori.Invalidate();
      }
      
      //返回 mm ss格式
      public string GetTimerStr(DateTime time)
      {
         string m = GetPad(time.Minute.ToString());
         string s = GetPad(time.Second.ToString());
         return string.Format("{0} {1}", m, s);
      }
      
      //左边补0
      private string GetPad(string s)
      {
         s = s.PadLeft(2, char.Parse("0"));
         return s.Substring(s.Length - 2, 2);
      }
      
      private ControlButtonHandle _SetButtonStart;
      private ControlButtonHandle _SetButtonLost;
      private ControlButtonHandle _SetButtonRegret;
      
      public ControlButtonHandle HSetButtonStart { get { return _SetButtonStart; } set { _SetButtonStart = value; } }
      public ControlButtonHandle HSetButtonLost { get { return _SetButtonLost; } set { _SetButtonLost = value; } }
      public ControlButtonHandle HSetButtonRegret { get { return _SetButtonRegret; } set { _SetButtonRegret = value; } }
      
      public void SetButtonStart(bool enable)
      {
         _pbBegin.Invoke(_SetButtonStart, enable);
      }
      
      public void SetButtonLost(bool enable)
      {
         _pbLost.Invoke(_SetButtonLost, enable);
      }
      
      public void SetButtonRegret(bool enable)
      {
         _pbRegret.Invoke(_SetButtonRegret, enable);
      }
      
      /// <summary>
      /// 重设控制面板
      /// </summary>
      public void Reset()
      {
         //停掉计时器
         this.StopTimer(ChessColor.Black);
         this.StopTimer(ChessColor.Red);
         
         //重画图片
         _redHistory.Invoke(_SetImage, _redHistory, ChineseChess.Res.Properties.Resources.infopane);
         _blackHistory.Invoke(_SetImage, _blackHistory, ChineseChess.Res.Properties.Resources.infopane);
      }
   }
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值