在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → 俄罗斯方块C#源码

俄罗斯方块C#源码

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:4.62M
  • 下载次数:68
  • 浏览次数:469
  • 发布时间:2014-05-09
  • 实例类别:C#语言基础
  • 发 布 人:fghffghfgh
  • 文件格式:.rar
  • 所需积分:2
 相关标签: C# 俄罗斯方块

实例介绍

【实例简介】一个不错的C#源码,有需要的朋友可以去下载看看
【实例截图】

【核心代码】

public FrmConfig()
        {
            InitializeComponent();
        }

        private bool[,] struArr = new bool[5, 5];//声明一个bool类型的二维数组存放每个方块的信息
        //如果为0,表示这个矩形的颜色没有改变,如果为1,表示颜色改变
        private Color blockColor = Color.Red;//声明一个颜色类型的变量,设为红色,当bool值为1的时候显示红色
        private Config config = new Config();

        private void lblMode_Paint(object sender, PaintEventArgs e)//砖块上色去色,窗口变化事件
        {
            Graphics gp = e.Graphics;//创建一个本身的画板,e表示该事件本身所带的参数
            gp.Clear(Color.Black);//把画板上所有画清除后的底色是黑色
            Pen p = new Pen(Color.White);//定义一支笔,定义它的颜色是白色
            for (int i = 31; i < 156; i = 31)//画横白线 4条
                gp.DrawLine(p, 1, i, 155, i);//(pen,x1,y1,x2,y2)p是画笔类,x1,y1是起点像素坐标,x2,y2终点像素坐标
            for (int i = 31; i < 156; i = 31)//画竖白线 4条
                gp.DrawLine(p,i,1,i,155);
            //这样就把图形分割成一个5行5列的矩阵

            //填充矩阵中的方块
            SolidBrush s = new SolidBrush( blockColor );//创建红色刷子
            for (int x = 0; x < 5; x )
                for (int y = 0; y < 5; y )
                    if (struArr[x, y])//当数组为真就刷成红色
                        gp.FillRectangle(s,31*x 1,31*y 1,30,30);//把下标值转换成屏幕坐标值
        }

        private void lblMode_MouseClick(object sender, MouseEventArgs e)//砖块单击事件
        {
            if (e.Button != MouseButtons.Left)//判断是否点击了鼠标左键
                return;
            int xpos, ypos;//xpos表示数组第一个下标,ypos表示数组第二个下标,这是自定义坐标:每个方块的坐标值
            xpos = e.X / 31;//e.X表示当前鼠标点击的横坐标位置,把屏幕像素转换为数组下标值
            ypos = e.Y / 31;//同上e.X,e.Y是整形
            struArr[xpos, ypos] = !struArr[xpos, ypos];//点击一个方块,如果点击前为真则点击后为假
            bool b = struArr[xpos, ypos];//如果鼠标点击在矩形范围内则为真[0,0]-[4,4]
            Graphics gp = lblMode.CreateGraphics();//新建一个lblMode的画板
            SolidBrush s = new SolidBrush(b ? blockColor : Color.Black);//创建一个刷子,确定它的颜色,当b为真时使用红色刷子,假时使用黑色刷子
            gp.FillRectangle(s, 31 * xpos 1, 31 * ypos 1, 30, 30);//矩形刷颜色,(Brush,x,y,width,height)s表示刷子,x,y表示像素坐标值,width,height表示图形的宽度和高度
            //
            gp.Dispose();//释放画板
        }

        private void lblColor_Click(object sender, EventArgs e)
        {
            colorDialog1.ShowDialog();//打开颜色对话框
            blockColor = colorDialog1.Color;//把选择的颜色赋给变量
            lblColor.BackColor = colorDialog1.Color;//把选择的颜色赋给标签的背景颜色
            lblMode.Invalidate();//使lblMode重画,即执行它的Paint事件
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            bool isEmpty = false;//首先查找图案是否为空
            foreach (bool i in struArr)
            {
                if (i)
                {
                    isEmpty = true;
                    break;
                }
            }
            if (!isEmpty)
            {
                MessageBox.Show("图案为空,请先用鼠标点击左边窗口绘制图案!", "提示窗口",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            StringBuilder sb = new StringBuilder(25);
            foreach (bool i in struArr)
            {
                sb.Append(i ? "1" : "0");
            }
            String blockString = sb.ToString();
            //再检查是否有重复图案
            foreach(ListViewItem item in lsvBlockSet.Items)
            {
                if (item.SubItems[0].Text == blockString)
                {
                    MessageBox.Show("该图案已经存在!", "提示窗口",
                        MessageBoxButtons.OK,MessageBoxIcon.Information);
                    return;
                }   
            }
            //把新砖块图案信息添加到ListView
            ListViewItem myItem = new ListViewItem();
            myItem = lsvBlockSet.Items.Add(blockString);
            myItem.SubItems.Add(Convert.ToString(blockColor.ToArgb()));

            MessageBox.Show("添加成功", "提示信息", MessageBoxButtons.OK);
        }

        private void lsvBlockSet_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            if (e.IsSelected)//避免重复执行事件
            {
                blockColor = Color.FromArgb(int.Parse(e.Item.SubItems[1].Text));//把字符串信息转换为颜色类
                lblColor.BackColor = blockColor;
                string s = e.Item.SubItems[0].Text;//取砖块的样式信息
                for (int i = 0; i < s.Length; i )//把砖块样式从字符串转换成二维数组
                {
                    struArr[i/5,i%5]=(s[i]=='1')?true:false;
                }
                lblMode.Invalidate();
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (lsvBlockSet.SelectedItems.Count == 0)//没有项目被选中
            {
                MessageBox.Show("请在右边窗口选择一个条目进行删除!", "提示窗口",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Information);
                return;
            }
            lsvBlockSet.Items.Remove(lsvBlockSet.SelectedItems[0]);//删除被选中的项目
            btnClear.PerformClick();
            MessageBox.Show("删除成功", "提示信息", MessageBoxButtons.OK);
        }

        private void btnClear_Click(object sender, EventArgs e)//清空
        {
            for (int x = 0; x < 5; x )
                for (int y = 0; y < 5; y )
                    struArr[x, y] = false;
            lblMode.Invalidate();
        }

        private void btnUpdate_Click(object sender, EventArgs e)//修改
        {
            if (lsvBlockSet.SelectedItems.Count == 0)//没有项目被选中
            {
                MessageBox.Show("请在右边窗口选择一个条目进行修改!", "提示窗口",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Information);
                return;
            }
            bool isEmpty = false;//判断图案是否为空
            foreach (bool i in struArr)
            {
                if (i)
                {
                    isEmpty = true;
                    break;
                }
            }
            if (!isEmpty)
            {
                MessageBox.Show("图案为空,请先用鼠标点击左边窗口绘制图案再进行修改!", "提示窗口",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            StringBuilder sb = new StringBuilder(25);
            foreach (bool i in struArr)
            {
                sb.Append(i ? "1" : "0");
            }
            lsvBlockSet.SelectedItems[0].SubItems[0].Text = sb.ToString();//改变图案信息
            lsvBlockSet.SelectedItems[0].SubItems[1].Text = Convert.ToString(blockColor.ToArgb());//改变颜色信息
            MessageBox.Show("修改成功", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void txtContra_KeyDown(object sender, KeyEventArgs e)
        {
            //首先排除一些不适合的键值
            if ((e.KeyValue >= 33 && e.KeyValue <= 36) || (e.KeyValue >= 45 && e.KeyValue <= 46) ||
               (e.KeyValue >= 48 && e.KeyValue <= 57) || (e.KeyValue >= 65 && e.KeyValue <= 90) ||
               (e.KeyValue >= 96 && e.KeyValue <= 107) || (e.KeyValue >= 109 && e.KeyValue <= 111) ||
               (e.KeyValue >= 186 && e.KeyValue <= 192) ||
               (e.KeyValue >= 219 && e.KeyValue <= 212))
            {   
                //检查是否存在冲突的快捷键
                foreach (Control c in gbKeySet.Controls)
                {
                    Control TempC = c as TextBox;
                    if (TempC != null)
                    {
                        if(((int)((TextBox)TempC).Tag)==e.KeyValue)
                        {
                            ((TextBox)TempC).Text = "";
                            ((TextBox)TempC).Tag = Keys.None;
                        }
                    }
                }
                ((TextBox)sender).Text = e.KeyCode.ToString();
                ((TextBox)sender).Tag = (Keys)e.KeyValue;
            }
        }

        private void lblBackColor_Click(object sender, EventArgs e)
        {
            colorDialog1.ShowDialog();
            lblBackColor.BackColor = colorDialog1.Color;
        }

        private void FrmConfig_Load(object sender, EventArgs e)//读取保存在xml文件中的默认的信息
        {
            config.LoadFromXmlFile();//读取xml文件
            InfoArr info = config.Info;

            //读取块样式,并存储到listview
            ListViewItem myItem = new ListViewItem();
            for (int i = 0; i < info.Length; i )
            {
                myItem = lsvBlockSet.Items.Add(info[i].GetIdStr());
                myItem.SubItems.Add(info[i].GetColorStr());
            }

            //读取快捷键
            txtDown.Text = ((Keys)config.DownKey).ToString();
            txtDown.Tag = config.DownKey;
            txtDrop.Text = ((Keys)config.DropKey).ToString();
            txtDrop.Tag = config.DropKey;
            txtLeft.Text = ((Keys)config.MoveLeftKey).ToString();
            txtLeft.Tag = config.MoveLeftKey;
            txtRight.Text = ((Keys)config.MoveRightKey).ToString();
            txtRight.Tag = config.MoveRightKey;
            txtDeasil.Text = ((Keys)config.DeasilRotateKey).ToString();
            txtDeasil.Tag = config.DeasilRotateKey;
            txtContra.Text = ((Keys)config.ContraRotateKey).ToString();
            txtContra.Tag = config.ContraRotateKey;

            //读环境设置参数
            txtCoorWidth.Text = config.CoorWidth.ToString();
            txtCoorHeight.Text = config.CoorHeight.ToString();
            txtRectPix.Text = config.RectPix.ToString();
            lblBackColor.BackColor = config.BackColor;

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            InfoArr info = new InfoArr();//实例化一个infoArr类
            foreach (ListViewItem item in lsvBlockSet.Items)//从lsvBlockSet中读取砖块信息并存入info内
            {
                info.Add(item.SubItems[0].Text, item.SubItems[1].Text);
            }
            config.Info = info;//把info赋给config对象的Info属性
            config.DownKey = (Keys)txtDown.Tag;
            config.DropKey = (Keys)txtDrop.Tag;
            config.MoveLeftKey = (Keys)txtLeft.Tag;
            config.MoveRightKey = (Keys)txtRight.Tag;
            config.DeasilRotateKey = (Keys)txtDeasil.Tag;
            config.ContraRotateKey = (Keys)txtContra.Tag;
            config.CoorWidth = int.Parse(txtCoorWidth.Text);
            config.CoorHeight = int.Parse(txtCoorHeight.Text);
            config.RectPix = int.Parse(txtRectPix.Text);
            config.BackColor = lblBackColor.BackColor;

            config.SaveToXmlFile();//保存成xml文件
            MessageBox.Show("保存成功", "提示窗口", MessageBoxButtons.OK);
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void lsvBlockSet_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

标签: C# 俄罗斯方块

实例下载地址

俄罗斯方块C#源码

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警