用C#做个小游戏!

是个手机的"点灯"游戏!规则是这样的:有9个方格!3个列,共3行!每个方格代表一个灯,开始的时候所有的灯都是灭的!当你每点击一个灯时,被点的这个灯和它上、下、左、右的灯都... 是个手机的"点灯"游戏!
规则是这样的: 有9个方格!3个列,共3行!
每个方格代表一个灯,开始的时候所有的灯都是灭的!

当你每点击一个灯时,被点的这个灯和它上、下、左、右的灯都会切换开/关
游戏的结果是点亮所有的灯的!

我不会做!
请各位高手多多指点!
提供的信息越多越好!
谢谢!
展开
 我来答
owen27
2009-03-03 · TA获得超过900个赞
知道小有建树答主
回答量:980
采纳率:100%
帮助的人:654万
展开全部
做法很多,这里提供其中一种思路:

建class存放格子坐标,参数x,y还有个boolean状态,总共这三个参数。

建9个该类的实例,分别赋值,boolean状态就是灯的亮和灭,初始化的时候都赋值灭的状态。

事件处理:点击某个格子,对应的object以及它所具有的x值正负1(y相等),y值正负1(x相等)的object,这几个object的boolean状态全部反向, 这样遇到点击边角格子的情况也可以正确处理。

还有个更简单的办法,因为这个程序需要处理的对象不多,直接建立个长度为9的数组,存放boolean变量表示灯的亮和灭,建立9个click时间,每个事件需要反向哪些数组元素的boolean值直接手动设置,然后最后定义一个UpdateStatus()之类的方法,更新9个格子的状态,就OK了。

我简单搞了一个,工作正常。

namespace TurnOnLightGame
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < 9; i++)
{
box[i] = false;
}
}

private void button1_Click(object sender, EventArgs e)
{
box[0] = !box[0];
box[1] = !box[1];
box[3] = !box[3];
UpdateBox();
}

private void button2_Click(object sender, EventArgs e)
{
box[0] = !box[0];
box[1] = !box[1];
box[2] = !box[2];
box[4] = !box[4];
UpdateBox();
}

private void button3_Click(object sender, EventArgs e)
{

box[1] = !box[1];
box[2] = !box[2];
box[5] = !box[5];
UpdateBox();
}

private void button4_Click(object sender, EventArgs e)
{
box[0] = !box[0];
box[3] = !box[3];
box[4] = !box[4];
box[6] = !box[6];
UpdateBox();
}

private void button5_Click(object sender, EventArgs e)
{
box[1] = !box[1];
box[3] = !box[3];
box[4] = !box[4];
box[5] = !box[5];
box[7] = !box[7];
UpdateBox();
}

private void button6_Click(object sender, EventArgs e)
{
box[5] = !box[5];
box[8] = !box[8];
box[2] = !box[2];
box[4] = !box[4];
UpdateBox();
}

private void button7_Click(object sender, EventArgs e)
{
box[3] = !box[3];
box[6] = !box[6];
box[7] = !box[7];

UpdateBox();
}

private void button8_Click(object sender, EventArgs e)
{
box[4] = !box[4];
box[6] = !box[6];
box[7] = !box[7];
box[8] = !box[8];
UpdateBox();
}

private void button9_Click(object sender, EventArgs e)
{
box[5] = !box[5];
box[7] = !box[7];
box[8] = !box[8];

UpdateBox();
}

public void UpdateBox()
{

if (box[0] == true) button1.Text = "@"; else button1.Text = "";
if (box[1] == true) button2.Text = "@"; else button2.Text = "";
if (box[2] == true) button3.Text = "@"; else button3.Text = "";
if (box[3] == true) button4.Text = "@"; else button4.Text = "";
if (box[4] == true) button5.Text = "@"; else button5.Text = "";
if (box[5] == true) button6.Text = "@"; else button6.Text = "";
if (box[6] == true) button7.Text = "@"; else button7.Text = "";
if (box[7] == true) button8.Text = "@"; else button8.Text = "";
if (box[8] == true) button9.Text = "@"; else button9.Text = "";

}
}
}
sq_zhuyi
2009-03-02 · TA获得超过680个赞
知道小有建树答主
回答量:1127
采纳率:0%
帮助的人:995万
展开全部
准备两张图片:亮灯、暗灯
在图片/按钮的click事件中重新赋值相关图片(当前图片和上下左右的图片)
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式