?? chessman.cs
字號:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Data;
using System.Text;
using System.Windows.Forms;
namespace GameWorld.ChineseChess
{
public abstract partial class ChessMan : UserControl
{
public ChessMan()
{
InitializeComponent();
}
public const int RED = 2;
public const int BLACK = 1;
public const int GREY = 0;
public const int GENERAL = 7;
public const int GUARD = 6;
public const int STAFF = 5;
public const int HORSE = 4;
public const int CHARIOT = 3;
public const int CANNON = 2;
public const int SOLDIER = 1;
public const int BLANK = 0;
public const int REDSIDE = 0;
public const int BLACKSIDE = 1;
private const int gridSize = 40;
private const int ChessSize = 32;
public int color;
public int kind;
public int number;
public bool beAttack;
public int x;
public int y;
public Graphics Graphics;
//public int top;
//public int left;
public ChessMan[,] situation
{
get
{
if (this.Parent != null)
{
ChineseChessControl parent = this.Parent as ChineseChessControl;
if (parent != null)
return parent.situation;
}
return null;
}
}
private Request Request = Request.CreateInstance();
public ChessMan(int color, int kind, int number, int boardside)
{
InitializeComponent();
this.DrawChessMan(color, kind, number, boardside);
}
public ChessMan(int color, int kind, int x, int y, int boardside)
{
InitializeComponent();
this.DrawChessMan(color, kind, x, y, boardside);
}
public void DrawChessMan(int color, int kind,int x, int y,int boardside)
{
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.BackColor = Color.Transparent;
this.x = x;
this.y = y;
this.Width = ChessSize;
this.Height = ChessSize;
this.color = color;
this.kind = kind;
this.beAttack = false;
this.fileName = @"ChineseChess\Image\";
switch (this.color)
{
case RED:
{
this.fileName += "red";
break;
}
case BLACK:
{
this.fileName += "black";
break;
}
}
switch (this.kind)
{
case GENERAL:
{
this.fileName += "general";
break;
}
case GUARD:
{
this.fileName += "guard";
break;
}
case STAFF:
{
this.fileName += "staff";
break;
}
case HORSE:
{
this.fileName += "horse";
break;
}
case CHARIOT:
{
this.fileName += "chariot";
break;
}
case CANNON:
{
this.fileName += "cannon";
break;
}
case SOLDIER:
{
this.fileName += "soldier";
break;
}
case BLANK:
{
this.fileName += "blank";
break;
}
}
this.fileName += ".ico";
this.Left = Math.Abs(8 * boardside - this.x) * gridSize - ChessSize / 2 + gridSize / 2;//gridSize/2是偏移
this.Top = Math.Abs(9 * boardside - this.y) * gridSize - ChessSize / 2 + gridSize / 2;
}
public void DrawChessMan(int color, int kind, int number, int boardside)
{
//GraphicsPath graphicsPath = new GraphicsPath();
//graphicsPath.AddEllipse(0, 0, ChessSize, ChessSize);
//this.Region = new Region(graphicsPath);
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.BackColor = Color.Transparent;
this.Width = ChessSize;
this.Height = ChessSize;
this.color = color;
this.kind = kind;
this.number = number;
this.beAttack = false;
this.fileName = @"ChineseChess\Image\";
switch (this.color)
{
case RED:
{
this.fileName += "red";
break;
}
case BLACK:
{
this.fileName += "black";
break;
}
}
switch (this.kind)
{
case GENERAL:
{
this.fileName += "general";
this.x = 4;
this.y = 9 * (this.color - 1);
break;
}
case GUARD:
{
this.fileName += "guard";
this.x = 3 + 2 * this.number;
this.y = 9 * (this.color - 1);
break;
}
case STAFF:
{
this.fileName += "staff";
this.x = 2 + 4 * this.number;
this.y = 9 * (this.color - 1);
break;
}
case HORSE:
{
this.fileName += "horse";
this.x = 1 + 6 * this.number;
this.y = 9 * (this.color - 1);
break;
}
case CHARIOT:
{
this.fileName += "chariot";
this.x = 8 * this.number;
this.y = 9 * (this.color - 1);
break;
}
case CANNON:
{
this.fileName += "cannon";
this.x = 1 + 6 * this.number;
this.y = 2 + 5 * (this.color - 1);
break;
}
case SOLDIER:
{
this.fileName += "soldier";
this.x = 2 * this.number;
this.y = 3 * this.color;
break;
}
case BLANK:
{
this.fileName += "blank";
break;
}
}
this.fileName += ".ico";
//int chessMan = document.createElement("<div class='chessMan'>");
//int imgsrc = document.createElement("<img src='"+this.fileName+"'>");
//chessMan.style.top = Math.Abs(9*boardside-this.y) * gridSize - 16;
//chessMan.style.left = Math.Abs(8*boardside-this.x) * gridSize - 16;
//chessMan.appendChild(imgsrc);
//this.top = Math.Abs(9 * boardside - this.y) * gridSize - ChessSize / 2;
//this.left = Math.Abs(8 * boardside - this.x) * gridSize - ChessSize / 2;
this.Left = Math.Abs(8 * boardside - this.x) * gridSize - ChessSize / 2 + gridSize / 2;//gridSize/2是偏移
this.Top = Math.Abs(9 * boardside - this.y) * gridSize - ChessSize / 2 + gridSize / 2;
}
//public void Draw(Graphics g)
//{
// Icon ico = new Icon(this.fileName);
// g.DrawIcon(ico, this.left, this.top);
//}
private void ChessMan_MouseDown(object sender, MouseEventArgs e)
{
check();
}
private void ChessMan_Paint(object sender, PaintEventArgs e)
{
//Matrix transformMatrix = new Matrix();
//transformMatrix.Scale(1.5F, 1.5F);
//e.Graphics.MultiplyTransform(transformMatrix);
Icon ico = new Icon(this.fileName);
e.Graphics.DrawIcon(ico, new Rectangle(0, 0, ChessSize, ChessSize));
}
private void check()
{
ChineseChessControl control = this.Parent as ChineseChessControl;
if (control.end == true)
{
MessageBox.Show("對局已經結束, 請重新開局!");
return;
}
if (((control.BoardSide == REDSIDE) && (this.color == BLACK)) || ((control.BoardSide == BLACKSIDE) && (this.color == RED)))
{
if (this.beAttack == false)
{
MessageBox.Show("不能移動對方棋子!");
return;
}
}
if (control.selection == null)
{
if (this.color != control.side)
{
MessageBox.Show("不該你走棋!");
return;
}
}
else
{
if (this.color != control.selection.color)
{
if ((this.color != GREY)
&& (this.beAttack == false))
{
MessageBox.Show("不該你走棋!");
return;
}
}
}
if ((this.color == GREY)||(this.beAttack == true))
{
//坐標變換,本客戶端采用左上角為原點,Web 服務采用左下角作原點
int x1 = control.selection.x;
int y1 = 9 - control.selection.y;
int x2 = this.x;
int y2 = 9 - this.y;
control.ToPos = this;
this.Request.Move(x1, y1, x2, y2);
return;
}
control.clearBoard();
control.selection = this;
this.BackColor = Color.Blue;
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 10; j++)
{
if (this.canGo(i, j))
{
control.addBlank(i, j);
}
}
}
}
public abstract bool canGo(int i, int j);
private void ChessMan_Load(object sender, EventArgs e)
{
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -