C#实践开发_Winform 系列九:五子棋游戏

五子棋游戏


文章目录

  • 五子棋游戏
  • 前言
  • 一、结果呈现
    • 1. 界面设计
    • 2. 运行结果呈现
  • 二、源码
    • 1.Form.cs
    • 2.Form.Designer.cs
  • 三、总结


前言

C#实践开发_Winform 系列第九篇篇:五子棋游戏,进一步熟悉pictureBox控件使用。

一、结果呈现

1. 界面设计

窗体界面设计:两个label标签,两个文本框textBox,两个Button按钮,一个pictureBox。

C#实践开发_Winform 系列九:五子棋游戏_第1张图片

2. 运行结果呈现

C#实践开发_Winform 系列九:五子棋游戏_第2张图片

二、源码

1.Form.cs

代码如下(示例):

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace test5
{
   
    public partial class Form3 : Form
    {
   
        private enum Chess {
    none = 0, Black, White };
        private Chess[,] Box = new Chess[15, 15];
        private Chess mplayer = Chess.Black;    //假设持黑子 
        private int r;

        public Form3()
        {
   
            InitializeComponent();
        }

        //绘制棋盘
        private void DrawBoard()
        {
   
            int i;
            Graphics g = this.pictureBox1.CreateGraphics();
            Pen myPen = new Pen(Color.Gray); //棋盘颜色
            myPen.Width = 1;
            r = pictureBox1.Width / 30;
            pictureBox1.Height = pictureBox1.Width;
            for (i = 0; i <= 14; i++)   //竖线
            {
   
                if (i == 0 || i == 14)
                    myPen.Width = 2;
                else
                    myPen.Width = 1;
                g.DrawLine(myPen, r + i * 2 * r, r, r + i * 2 * r, r * 2 * 15 - r - 1);
            }
            for (i = 0; i <= 14; i++)  //横线
            {
   
                if (i == 0 || i == 14)
                    myPen.Width = 2;
                else
                    myPen.Width = 1;
                g.DrawLine(myPen, r, r + i * 2 * r, r * 2 * 15 - r - 1, r + i * 2 * r);
            }
            SolidBrush myBrush = new SolidBrush(Color.Black);
            //绘制4个天星
            g.FillEllipse(myBrush, r + 3 * r * 2 - 4, r + 3 * r * 2 - 4, 8, 8);
            g.FillEllipse(myBrush, r + 3 * r * 2 - 4, r + 11 * r * 2 - 4, 8, 8);
            g.FillEllipse(myBrush, r + 11 * r * 2 - 4, r + 11 * r * 2 - 4, 8, 8);
            g.FillEllipse(myBrush, r + 11 * r * 2 - 4, r + 3 * r * 2 - 4, 8, 8);
        }

        //绘制棋子
        private void Draw(Graphics g, Point p2, Chess mplayer)
        {
   
            SolidBrush myBrush;
            if (mplayer == Chess.Black)
                myBrush = new SolidBrush(Color.Black);
            else
                myBrush = new SolidBrush(Color.White);
            g.FillEllipse(myBrush, p2.X * 2 * r, p2.Y * 2 * r, 2 * r, 2 * r)

你可能感兴趣的:(C#实践开发,c#,winform)