C#小游戏记忆围棋源码_修正白棋bug版

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,下面是一个简单的 C# 实现五子棋小游戏的示例代码。 首先,在窗体上添加一个 PictureBox 控件,用于绘制游戏棋盘。 ```csharp public partial class Form1 : Form { private const int ROWS = 15; // 棋盘行数 private const int COLS = 15; // 棋盘列数 private const int CELL_SIZE = 30; // 每个格子的大小 private int[,] board; // 棋盘数组,0表示空格,1表示黑棋,2表示白棋 private bool isBlackTurn; // 当前轮到黑棋或白棋下 public Form1() { InitializeComponent(); board = new int[ROWS, COLS]; isBlackTurn = true; // 初始化棋盘数组 for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { board[i, j] = 0; } } // 设置PictureBox控件的大小和背景色 pictureBox1.Width = ROWS * CELL_SIZE; pictureBox1.Height = COLS * CELL_SIZE; pictureBox1.BackColor = Color.FromArgb(255, 225, 158); } private void pictureBox1_Paint(object sender, PaintEventArgs e) { // 绘制棋盘 Pen pen = new Pen(Color.Black, 2); for (int i = 0; i <= ROWS; i++) { e.Graphics.DrawLine(pen, i * CELL_SIZE, 0, i * CELL_SIZE, ROWS * CELL_SIZE); } for (int j = 0; j <= COLS; j++) { e.Graphics.DrawLine(pen, 0, j * CELL_SIZE, COLS * CELL_SIZE, j * CELL_SIZE); } // 绘制棋子 for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { if (board[i, j] == 1) { e.Graphics.FillEllipse(Brushes.Black, i * CELL_SIZE - CELL_SIZE / 2, j * CELL_SIZE - CELL_SIZE / 2, CELL_SIZE, CELL_SIZE); } else if (board[i, j] == 2) { e.Graphics.FillEllipse(Brushes.White, i * CELL_SIZE - CELL_SIZE / 2, j * CELL_SIZE - CELL_SIZE / 2, CELL_SIZE, CELL_SIZE); } } } } private void pictureBox1_MouseClick(object sender, MouseEventArgs e) { int x = e.X / CELL_SIZE; int y = e.Y / CELL_SIZE; if (x >= 0 && x < ROWS && y >= 0 && y < COLS && board[x, y] == 0) { if (isBlackTurn) { board[x, y] = 1; } else { board[x, y] = 2; } isBlackTurn = !isBlackTurn; pictureBox1.Invalidate(); // 重新绘制PictureBox控件 } } } ``` 在窗体设计器中,将 PictureBox 控件的 Paint 事件和 MouseClick 事件分别绑定到 pictureBox1_Paint 和 pictureBox1_MouseClick 方法。 这样就完成了一个简单的五子棋小游戏
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值