`
zmo_xu
  • 浏览: 62447 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
最近访客 更多访客>>
社区版块
存档分类
最新评论

逐步讲解用C#实现俄罗斯方块之核心代码[上]

阅读更多

从分析到实现讲解俄罗斯方块的做法
俄罗斯方块在.NET下的制作,我写的只是一些拙见,望高手不要板砖,也请高手不惜赐教!
首先我们要分析俄罗斯方块的原理
1.由移动的方块和不能动的方块组成
2.一行排满消除
3.能产生多种方块
---
首先我们来分析方块
前面说了方块由两部分组成一种是移动下落的一种是固定的,我们逐个来分解,移动的方块一般由4个方块组成[我们这里只讨论保准版]我们假设吧这个4个下落的方块看成一个封装的类,那么他就有4个图形成员,我们不妨假设是4个黑色的pictureBox组建[大小10*10],再在类里面添加一个初始化方法用于通过picturebox.Location来控制没干方块的位置,那么我们先生成一个初步的类shape,其中pb是下落的图形,而oldpb是已经固定的图形,为什么这样定义呢 

    public class sharp
    {
        
//用于记录分数
        
//用于记录已经触底的图像
        protected PictureBox[] oldpb=new PictureBox[1000];
        
//用于控制移动中的图像
        public PictureBox[] pb = new PictureBox[4];
        
//移动图像的初始化位置
        private int initX = 100;
        
private int initY = 10;

        
//构造函数[参数用于制定控件添加到什么窗口]
        public sharp(Form f)
        {
            newObj(f);
        }

        
//用于给定时器调用 向下移动
        public void moveDown(Form f)
        {
        }

        
//用于旋转图形
        public void Roll()
        {
        }

       
        
private void Rollx(int a,int b)
        {
        }
        
//检查是否到底 到底则进行转换
        public bool check(Form f)
        {
        }

        
//此方法用于将图形转换为触底图形
        private void changToOld(Form f)
        {
        }

        
public void sharp_0()
        {
            pb[
0].Location = new Point(initX, initY);
            pb[
1].Location = new Point(initX, initY - 10);
            pb[
2].Location = new Point(initX + 10, initY - 10);
            pb[
3].Location = new Point(initX + 10, initY );
        }
        
public void sharp_1()
        {
            pb[
0].Location = new Point(initX, initY);
            pb[
1].Location = new Point(initX, initY - 10);
            pb[
2].Location = new Point(initX, initY - 20);
            pb[
3].Location = new Point(initX, initY - 30);
        }
        
public void sharp_2()
        {
            pb[
0].Location = new Point(initX, initY);
            pb[
1].Location = new Point(initX, initY - 10);
            pb[
2].Location = new Point(initX-10, initY - 10);
            pb[
3].Location = new Point(initX-20, initY - 10);
        }
        
public void sharp_3()
        {
            pb[
0].Location = new Point(initX, initY);
            pb[
1].Location = new Point(initX, initY - 10);
            pb[
2].Location = new Point(initX + 10, initY - 10);
            pb[
3].Location = new Point(initX + 20, initY - 10);
        }
        
public void sharp_4()
        {
            pb[
0].Location = new Point(initX, initY);
            pb[
1].Location = new Point(initX+10, initY - 10);
            pb[
2].Location = new Point(initX + 10, initY );
            pb[
3].Location = new Point(initX + 20, initY);
        }
        
public void sharp_5()
        {
            pb[
0].Location = new Point(initX, initY);
            pb[
2].Location = new Point(initX + 10, initY);
            pb[
1].Location = new Point(initX+10, initY + 10);
            pb[
3].Location = new Point(initX +20, initY + 10);
        }
        
public void sharp_6()
        {
            pb[
0].Location = new Point(initX, initY);
            pb[
1].Location = new Point(initX+10, initY );
            pb[
2].Location = new Point(initX + 10, initY - 10);
            pb[
3].Location = new Point(initX + 20, initY - 10);
        }

        
//生成新图形样式
        public void makeNew()
        {
        }

        
//用于新实例化移动图形组
        public void newObj(Form f)
        {
        }

        
//检查一行是否排满
        public void finish()
        {
        }

        
//消除制定行并将上面的都向下移动
        public void ClareLine(int y)
        {
        }

        
// 向左移动的方法
        public void movLeft()
        {

        }
        
//向右移动的方法
        public void movRigth()
        {
            
        }

        
//用于检查方块单元的位置是否合法
        private bool checkPes(Point p)
        {
           
        }

        
public void flash(int y)
        {
           
        }
        
//------------------------------------
    }

我们在上面定义了一些我们游戏时操作所对应的成员方法,这些操作都是相应我们游戏操作的,那么游戏中还有什么是我们所看不到的方法呢..对,你说的很对,就是方块什么时候到底下什么时候消除行--我们现在要考虑令一组方块了,就是那些不能动的,对是不能动的我们就从不能动来解决...我们创建一个pictureBox数组其元素个数为面板最多能显示的方块的个数我这里用的是20*50(每行20个50行,我作的大了点,呵呵,如果诸位要自己实践的话不妨采用15*40,一行排20个玩起来比较慢也比较累,好在我只是写写核心代码不用考虑游戏的平衡性什么的呵呵)好了言归正传,为什么要这样作呢 其实很简单当移动图形到底的时候我们就把他的4个pb[指pictureBox,下同]复制到oldPb数组,这样便实现了图形到底固定的效果,你问怎么判断图形移动到底..这个,呵呵,其实,额 难道你还没想到.我的图形在下移的时候要每移动一次都要检查oldpb所对应的位置的下方是否有图形(即是否==null),这里我给出完整的下落检查..

 public void moveDown(Form f)
        {
            
for (int i = 0; i < 4; i++)
            {
                
//向下移动
                pb[i].Location = new Point(pb[i].Location.X, pb[i].Location.Y + 10);
            }
            
//调用check来判断是否到底 
            if (check(f))
            {
                
//到底着构建新图像
                makeNew();
            }
        }
 public bool check(Form f)
        
...{
            
for(int i=0;i<4;i++)
            
...{
                
if (pb[i].Location.Y > 500)
                
...{
                    
//任一一个图像到底则整个图像进行转换
                    changToOld(f);
                    
return true;
                }

                
//判断该图像所对应的位置的下方是否有已经触底的图形
                int x = pb[i].Location.X;
                
int y = pb[i].Location.Y;
                
if ((y - 20* 20 + x - 10 > 0 && oldpb[(y - 20/ 10 * 20 + (x - 10/ 10 + 20!= null)
                
...{
                    
//任一一个图像到底则整个图像进行转换
                    changToOld(f);
                    
return true;
                }

            }

            
return false;
        }
   private void changToOld(Form f)
        
...{
            
for (int i = 0; i < 4; i++)
            
...{
                
int x = pb[i].Location.X;
                
int y = pb[i].Location.Y;
                
if (pb[0].Location.Y > 10 && pb[1].Location.Y > 10 && pb[2].Location.Y > 10 && pb[3].Location.Y > 10)
                
...{
                    
//将图形"copy"到触底图形组
                    oldpb[(y - 20/ 10 * 20 + (x - 10/ 10= pb[i];
                }

            }

            
//检查finish-用于检查一排是否已经排满
            finish();
            
//生成新移动图形组
            newObj(f);
        }
 public void newObj(Form f)
        
...{
            
for (int i = 0; i < 4; i++)
            
...{
                
//重新实例化模型
                
//实例化
                this.pb[i] = new System.Windows.Forms.PictureBox();
                
//设置大小
                this.pb[i].Size = new System.Drawing.Size(1010);
                
//设置图片
                this.pb[i].Image = global::Fang.Properties.Resources.fang;
                
//添加到窗口
                f.Controls.Add(pb[i]);
            }

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics