c#之一步一步学习制作俄罗斯方块<一>

    最近要交一个c#项目,加之以前老师讲过一些怎样制作俄罗斯方块的知识,决定做一个简易的俄罗斯方块。废话少说,现在开始进入正题。

今天主要说一哈用户设置窗体,即用户对方块的设置。先给大家上一副图片,看哈效果,(其中各个控件的具体设置就不在下面讲解了,主要是说哈代码)

主要用到的控件有:Lable, tabControl, button, listView, colorDialog.

开发过程:

      1。先添加一个tabControl控件在窗体中,再添加一个Lable控件,设置背景色为黑色,用来设置转开的样式。下面在Lable控件中画5*5的方格 ,代码如下:      

1 Graphics gh = e.Graphics;
2 gh.Clear(Color.Black);
3 Pen pen = new Pen(Color.White);
4 for (int j = 31; j < 155; j = j + 31)//画四个横线
5   {
6 gh.DrawLine(pen, 1, j, 156, j);
7 }
8 for (int i = 31; i < 155; i = i + 31)//画四个竖线
9   {
10 gh.DrawLine(pen, i, 1, i, 156);
11 }
12  

 

        2。下面实现当点击一个砖块时变色,再点击时颜色变回来,代码如下:     

1 private bool[,] Astr = new bool[5, 5];//定义一个数组,用来存放砖块的位置
2   private Color bgcolor = Color.Red;//设置单击是砖块的颜色
3   private void label1_MouseClick(object sender, MouseEventArgs e)
4 {
5 if (e.Button == MouseButtons.Right)//判断单击的是不是左键,如果是右键则不执行下面的代码
6 return;
7 int px, py;//px表示第一个数组下标,py表示第二个下标
8 px = e.X / 31;//把屏幕像数值转换为数组下标值
9 py = e.Y / 31;
10 Astr[px, py] = !Astr[px, py];//单击砖块时变颜色,再单击时颜色变回来
11 bool temp = Astr[px, py];
12 Graphics gp = label1.CreateGraphics();
13 if (temp)
14 {
15 SolidBrush sb = new SolidBrush(bgcolor);
16 gp.FillRectangle(sb, 31 * px + 1, 31 * py + 1, 30, 30);
17 }
18 else
19 {
20 SolidBrush sb = new SolidBrush(Color.Black);
21 gp.FillRectangle(sb, 31 * px + 1, 31 * py + 1, 30, 30);
22 }
23 gp.Dispose();
24 }

    

        3.现在我们希望我们能够改变砖块的颜色,添加一个colorDialog控件来实现颜色的选取。

1 private void label3_Click(object sender, EventArgs e)
2 {
3 colorDialog1.ShowDialog();//打开颜色对话框
4 this.bgcolor = colorDialog1.Color;
5 this.label3.BackColor = bgcolor;
6 label1.Invalidate();//使Lable控件重画,即调用paint事件
7 }

       

         4.现在我们希望将配置好的砖块保存,于是添加一个button和一个listView来实现保存。

1 private void button1_Click(object sender, EventArgs e)
2 {
3 bool Isempty = false;
4 foreach (bool i in Astr )
5 {
6 if (i)
7 {
8 Isempty = true;
9 break;
10 }
11 }
12 if (!Isempty)
13 {
14 MessageBox.Show("图案为空,请在上面的框中绘制图案!", "提示窗口", MessageBoxButtons.OK, MessageBoxIcon.Information);
15 }
16 StringBuilder sb = new StringBuilder();
17 foreach (bool i in Astr )
18 {
19 sb.Append(i ? 1:0);
20 }
21 string blockstring = sb.ToString();
22 foreach (ListViewItem lv in listView1.Items)
23 {
24 if (lv.SubItems[0].Text ==blockstring )
25 {
26 MessageBox.Show("该图案已经存在!","提示窗口",MessageBoxButtons .OK ,MessageBoxIcon.Information );
27 return;
28 }
29 }
30 ListViewItem lvt = new ListViewItem();
31 lvt = listView1.Items.Add(blockstring);
32 lvt.SubItems.Add(Convert.ToString(bgcolor.ToArgb()));
33 }

 

        5.保存过后可以在listview控件中选取选项在lable控件中打开,来实现修改等操作。

1 private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
2 {
3 if (e.IsSelected)//避免重复执行事件
4 {
5 bgcolor = Color.FromArgb(int.Parse(e.Item.SubItems[1].Text));//把字符串信息转换为颜色类
6 label3 .BackColor =bgcolor ;
7 string s=e.Item .SubItems[0].Text;//取砖块样式信息
8 for ( int i =0;i <s .Length ;i ++)
9 {
10 Astr [i/5,i%5]=(s [i]=='1')?true :false ;
11 }
12 label1 .Invalidate() ;
13 }
14 }

 

 

 

          差不多就这么多的代码了,有什么不懂的可以留言(部分代码参考网上的一些代码)下面附上所有的源代码:

 

代码
1 namespace 俄罗斯方块
2 {
3 public partial class Form1 : Form
4 {
5 public Form1()
6 {
7 InitializeComponent();
8 }
9 /// <summary>
10 /// Panint事件表示当前Lable重画的时候所发生的事情。
11 /// 比如说当控件载入、在最小化又还原、将其移到窗体外部再移回来的时候发生的事件。
12 /// </summary>
13 /// <param name="sender"></param>
14 /// <param name="e"></param>
15 private void label1_Paint(object sender, PaintEventArgs e)
16 {
17 Graphics gh = e.Graphics;
18 gh.Clear(Color.Black);
19 Pen pen = new Pen(Color.White);
20 for (int j = 31; j < 155; j = j + 31)//画四个横线
21 {
22 gh.DrawLine(pen, 1, j, 156, j);
23 }
24 for (int i = 31; i < 155; i = i + 31)//画四个竖线
25 {
26 gh.DrawLine(pen, i, 1, i, 156);
27 }
28 SolidBrush sb = new SolidBrush(bgcolor);
29 for (int x = 0; x < 5; x++)
30 {
31 for (int y = 0; y < 5; y++)
32 {
33 if (Astr[x, y])
34 {
35 gh.FillRectangle(sb, x * 31 + 1, y * 31 + 1, 30, 30);
36 }
37 }
38
39 }
40
41
42 }
43 private bool[,] Astr = new bool[5, 5];//定义一个数组,用来存放砖块的位置
44 private Color bgcolor = Color.Red;//设置单击是砖块的颜色
45 private void label1_MouseClick(object sender, MouseEventArgs e)
46 {
47 if (e.Button == MouseButtons.Right)//判断单击的是不是左键,如果是右键则不执行下面的代码
48 return;
49 int px, py;//px表示第一个数组下标,py表示第二个下标
50 px = e.X / 31;//把屏幕像数值转换为数组下标值
51 py = e.Y / 31;
52 Astr[px, py] = !Astr[px, py];//单击砖块时变颜色,再单击时颜色变回来
53 bool temp = Astr[px, py];
54 Graphics gp = label1.CreateGraphics();
55 if (temp)
56 {
57 SolidBrush sb = new SolidBrush(bgcolor);
58 gp.FillRectangle(sb, 31 * px + 1, 31 * py + 1, 30, 30);
59 }
60 else
61 {
62 SolidBrush sb = new SolidBrush(Color.Black);
63 gp.FillRectangle(sb, 31 * px + 1, 31 * py + 1, 30, 30);
64 }
65 gp.Dispose();
66 }
67
68 private void label3_Click(object sender, EventArgs e)
69 {
70 colorDialog1.ShowDialog();//打开颜色对话框
71 this.bgcolor = colorDialog1.Color;
72 this.label3.BackColor = bgcolor;
73 label1.Invalidate();//使Lable控件重画,即调用paint事件
74 }
75
76 private void button1_Click(object sender, EventArgs e)
77 {
78 bool Isempty = false;
79 foreach (bool i in Astr )
80 {
81 if (i)
82 {
83 Isempty = true;
84 break;
85 }
86 }
87 if (!Isempty)
88 {
89 MessageBox.Show("图案为空,请在上面的框中绘制图案!", "提示窗口", MessageBoxButtons.OK, MessageBoxIcon.Information);
90 }
91 StringBuilder sb = new StringBuilder();
92 foreach (bool i in Astr )
93 {
94 sb.Append(i ? 1:0);
95 }
96 string blockstring = sb.ToString();
97 foreach (ListViewItem lv in listView1.Items)
98 {
99 if (lv.SubItems[0].Text ==blockstring )
100 {
101 MessageBox.Show("该图案已经存在!","提示窗口",MessageBoxButtons .OK ,MessageBoxIcon.Information );
102 return;
103 }
104 }
105 ListViewItem lvt = new ListViewItem();
106 lvt = listView1.Items.Add(blockstring);
107 lvt.SubItems.Add(Convert.ToString(bgcolor.ToArgb()));
108 }
109
110 private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
111 {
112 if (e.IsSelected)//避免重复执行事件
113 {
114 bgcolor = Color.FromArgb(int.Parse(e.Item.SubItems[1].Text));//把字符串信息转换为颜色类
115 label3 .BackColor =bgcolor ;
116 string s=e.Item .SubItems[0].Text;//取砖块样式信息
117 for ( int i =0;i <s .Length ;i ++)
118 {
119 Astr [i/5,i%5]=(s [i]=='1')?true :false ;
120 }
121 label1 .Invalidate() ;
122 }
123 }
124
125 }
126 }
127

 

posted @ 2010-04-16 10:24  Lebron  阅读(1535)  评论(6编辑  收藏  举报