五子棋游戏-1(绘制棋盘)

首先要有一个棋谱,棋谱采用围棋棋谱19*19的棋盘,棋盘绘制在PictureBox控件上。
public int Lines = 19; //线条书
public int LineSpace = 30; //线间距
public int Margin = 30 / 2; //边距(线间距/2)
public PictureBox PicCtrl; //棋盘容器控件
然后使用GDI+绘制棋盘
Bitmap bmp = new Bitmap(PicCtrl.Width, PicCtrl.Height);
Graphics g = Graphics.FromImage(bmp);
……
Pen linePen = new Pen(Color.Black, 1);//画线笔
//Lines条横线
for(int i = 0; i < Lines; i++) {
Point start = new Point(Margin, i * LineSpace + Margin);
Point end = new Point(start.X+(Lines-1)*LineSpace, start.Y);
g.DrawLine(linePen, start, end);
}
//Lines条竖线
……
最后,设计UI界面,界面中央添加PictureBox控件,并计算好Form的大小

Code:
  1. usingSystem;
  2. usingSystem.Collections.Generic;
  3. usingSystem.Text;
  4. usingSystem.Windows.Forms;
  5. usingSystem.Drawing;
  6. namespaceFivePiece{
  7. ///
  8. ///棋盘
  9. ///
  10. classChessBoard{
  11. publicintLines=19;//线条书
  12. publicintLineSpace=30;//线间距
  13. publicintMargin=30/2;//边距(线间距/2)
  14. publicPictureBoxPicCtrl;//棋盘容器控件
  15. ///
  16. ///绘制棋盘
  17. ///
  18. publicvoidDrawBoard(){
  19. Bitmapbmp=newBitmap(PicCtrl.Width,PicCtrl.Height);
  20. Graphicsg=Graphics.FromImage(bmp);
  21. //绘制背景颜色
  22. SolidBrushbrush=newSolidBrush(Color.Gold);
  23. g.FillRectangle(brush,Margin,Margin,
  24. (Lines-1)*LineSpace,(Lines-1)*LineSpace);
  25. PenlinePen=newPen(Color.Black,1);//画线笔
  26. //Lines条横线
  27. for(inti=0;i
  28. Pointstart=newPoint(Margin,i*LineSpace+Margin);
  29. Pointend=newPoint(start.X+(Lines-1)*LineSpace,start.Y);
  30. g.DrawLine(linePen,start,end);
  31. }
  32. //Lines条竖线
  33. for(inti=0;i
  34. Pointstart=newPoint(i*LineSpace+Margin,Margin);
  35. Pointend=newPoint(start.X,start.Y+(Lines-1)*LineSpace);
  36. g.DrawLine(linePen,start,end);
  37. }
  38. PicCtrl.BackgroundImage=bmp;
  39. }
  40. }
  41. }
Code:
  1. usingSystem;
  2. usingSystem.Collections.Generic;
  3. usingSystem.ComponentModel;
  4. usingSystem.Data;
  5. usingSystem.Drawing;
  6. usingSystem.Text;
  7. usingSystem.Windows.Forms;
  8. namespaceFivePiece{
  9. publicpartialclassMainForm:Form{
  10. Gamegame=newGame();
  11. publicMainForm(){
  12. InitializeComponent();
  13. game.ChessBoard.PicCtrl=picBoard;
  14. game.ChessBoard.DrawBoard();
  15. }
  16. privatevoidpicBoard_MouseClick(objectsender,MouseEventArgse){
  17. if(e.Button==MouseButtons.Left){
  18. game.DownPiece(e.Location);
  19. if(game.GameOver){
  20. stringstrColor=game.NextColor==Color.White?"黑":"白";
  21. MessageBox.Show("恭喜你"+strColor+"棋获得胜利!");
  22. tsslblPlayer.Text="游戏结束!"+strColor+"棋获得胜利!";
  23. }else{
  24. stringstrColor=game.NextColor==Color.White?"白":"黑";
  25. tsslblPlayer.Text="玩家:"+strColor+"棋";
  26. }
  27. }elseif(e.Button==MouseButtons.Right){
  28. game.RegretPiece();
  29. stringstrColor=game.NextColor==Color.White?"白":"黑";
  30. tsslblPlayer.Text="玩家:"+strColor+"棋";
  31. }
  32. }
  33. privatevoidMainForm_Load(objectsender,EventArgse){
  34. }
  35. privatevoidtsmiStart_Click(objectsender,EventArgse){
  36. game=newGame();
  37. game.ChessBoard.PicCtrl=picBoard;
  38. game.ChessBoard.DrawBoard();
  39. tsslblPlayer.Text="玩家:白棋";
  40. }
  41. }
  42. }

视频内容请见http://www.itcast.net/course/detail/1989

<<如果您想和我交流,请点击和我成为好友>>

你可能感兴趣的:(五子棋游戏-1(绘制棋盘))