- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- namespace zhipai
- {
- public partial class Form1 : Form
- {
- MyButton[,] mybtns = new MyButton[8, 8];
- public Form1()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- this.Controls.Add(panel1);
- }
- private void button1_Click(object sender, EventArgs e)
- {
- for (int i = 0; i < 8; i++)
- {
- for (int j = 0; j < 8; j++)
- {
- MyButton btn = new MyButton();
- btn.BackColor = Color.Red;
- btn.Left = i * panel1.Width / 8;
- btn.Top = j * panel1.Height / 8;
- btn.Width = panel1.Width / 8;
- btn.Height = panel1.Height / 8;
- btn.x = i;
- btn.y = j;
- mybtns[i, j] = btn;
- mybtns[i,j].Click+=new EventHandler(changecolor1);
- this.panel1.Controls.Add(mybtns[i, j]); //往容器里面添加按钮
- }
- }
- this.Refresh();
- }
- private void panel1_Paint(object sender, PaintEventArgs e)
- {
- }
- private void changecolor1(object sender, EventArgs e)
- {
- MyButton chbtn =(MyButton) sender;
- chbtn.BackColor = Color.Yellow;
- int a_i = chbtn.x;
- int a_j = chbtn.y;
- changecolor2(a_i-1,a_j); //左边
- changecolor2(a_i+1,a_j); //右边
- changecolor2(a_i,a_j-1); //上面
- changecolor2(a_i,a_j+1); //下面
- }
- private void changecolor2(int i,int j)
- {
- if (i>= 0 && i < 8 && j>= 0 && j < 8)
- {
- if ((Color)mybtns[i, j].BackColor == Color.Yellow)
- {
- mybtns[i, j].BackColor = Color.Red;
- }
- else
- {
- mybtns[i, j].BackColor = Color.Yellow;
- }
- }
- }
- class MyButton : Button
- {
- public int x;
- public int y;
- }
- }
- }
一个翻纸牌的游戏
最新推荐文章于 2024-09-11 14:54:26 发布