很简单:
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Samll_Game
{
public partial class frmGame : Form
{
private bool[,] strArr = new bool[4, 4];
private Color blockColor = Color.Red;
public frmGame()
{
InitializeComponent();
}
private void frmGame_Load(object sender, EventArgs e)
{
}
private void label1_Paint(object sender, PaintEventArgs e)
{
Graphics gp = e.Graphics;
gp.Clear(Color.Black);
Pen p = new Pen(Color.White);
for (int i = 0; i <= 240; i = i + 60)
{
gp.DrawLine(p,0,i,240,i);
}
for (int i = 0; i <= 240; i = i + 60)
{
gp.DrawLine(p, i, 0, i, 240);
}
////定义笔刷
//SolidBrush s = new SolidBrush(blockColor);
//for (int i = 0; i < 4; i++)
//{
// for (int j = 0; j < 4; j++)
// {
// if (strArr[i, j])
// {
// gp.FillRectangle(s, 60 *i, 60 * j, 60, 60);
// }
// }
//}
}
private void label1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left)
{
return;
}
int xPox = e.X / 60;
int yPox = e.Y / 60;
strArr[xPox, yPox] = !strArr[xPox, yPox];
if (xPox > 0) strArr[xPox - 1, yPox] = !strArr[xPox - 1, yPox];
if (xPox < 3) strArr[xPox + 1, yPox] = !strArr[xPox + 1, yPox];
if (yPox > 0) strArr[xPox, yPox - 1] = !strArr[xPox, yPox - 1];
if (yPox < 3) strArr[xPox, yPox + 1] = !strArr[xPox, yPox + 1];
bool b = strArr[xPox, yPox];
bool b1 = strArr[xPox - 1 < 0 ? 0 : xPox - 1, yPox];
bool b2 = strArr[xPox + 1 > 3 ? 3 : xPox + 1, yPox];
bool b3 = strArr[xPox, yPox - 1 < 0 ? 0 : yPox - 1];
bool b4 = strArr[xPox, yPox + 1 > 3 ? 3 : yPox + 1];
Graphics gp = label1.CreateGraphics();
SolidBrush s = new SolidBrush(b ? blockColor : Color.Black);
SolidBrush s1 = new SolidBrush(b1 ? blockColor : Color.Black);
SolidBrush s2 = new SolidBrush(b2 ? blockColor : Color.Black);
SolidBrush s3 = new SolidBrush(b3 ? blockColor : Color.Black);
SolidBrush s4 = new SolidBrush(b4 ? blockColor : Color.Black);
gp.FillRectangle(s, xPox * 60 + 1, yPox * 60 + 1, 59, 59);
gp.FillRectangle(s1, (xPox - 1) * 60 + 1, yPox * 60 + 1, 59, 59);
gp.FillRectangle(s2, (xPox + 1) * 60 + 1, yPox * 60 + 1, 59, 59);
gp.FillRectangle(s3, xPox * 60 + 1, (yPox - 1) * 60 + 1, 59, 59);
gp.FillRectangle(s4, xPox * 60 + 1, (yPox + 1) * 60 + 1, 59, 59);
gp.Dispose();
}
}
}