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

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

阅读更多
这里先给出shape类的所有代码
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Drawing;
using System.Text;

namespace Fang
...{
    
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)
        
...{
            
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 void Roll()
        
...{
           
            
int Sx = pb[2].Location.X - pb[0].Location.X;
            
int Sy = pb[2].Location.Y - pb[0].Location.Y;
            
//根据计算
            if         (Sx >= 0 && Sy < 0) Rollx(-1,-1);
            
else if (Sx < 0 && Sy <= 0) Rollx(-1,1); 
            
else if (Sx <= 0 && Sy > 0) Rollx(1,1);
            
else if (Sx > 0 && Sy >= 0) Rollx(1,-1);
        }


        
//ROllX()由下面的4个方法综合而成

        
/**/////四个象限的旋转方法
        //private void Roll1()//在第一象限转第二象限
        
// {
        
//     for (int i = 1; i < 4; i++)
        
//     {
        
//         int x = System.Math.Abs(pb[0].Location.X - pb[i].Location.X);
        
//         int y = System.Math.Abs(pb[0].Location.Y - pb[i].Location.Y);
        
//         pb[i].Location = new Point(pb[0].Location.X - y, pb[0].Location.Y - x);
        
//     }
        
//     //如果坐标越界就进行反旋转
        
//     if (pb[0].Location.X > 200 || pb[1].Location.X > 200 || pb[2].Location.X > 200 || pb[3].Location.X > 200) Roll4();
        
//     if (pb[0].Location.X < 0 || pb[1].Location.X < 0 || pb[2].Location.X < 0 || pb[3].Location.X < 0) Roll4();
        
// }
        
// private void Roll2()//在2转3
        
// {
        
//     for (int i = 1; i < 4; i++)
        
//     {
        
//         int x = System.Math.Abs(pb[0].Location.X - pb[i].Location.X);
        
//         int y = System.Math.Abs(pb[0].Location.Y - pb[i].Location.Y);
        
//         pb[i].Location = new Point(pb[0].Location.X - y, pb[0].Location.Y + x);
        
//     }
        
//     //如果坐标越界就进行反旋转
        
//     if (pb[0].Location.X < 0 || pb[1].Location.X < 0 || pb[2].Location.X < 0 || pb[3].Location.X < 0) Roll1();
        
//     if (pb[0].Location.X > 200 || pb[1].Location.X > 200 || pb[2].Location.X > 200 || pb[3].Location.X > 200) Roll1();
        
// }
        
// private void Roll3()//在3转4
        
// {
        
//     for (int i = 1; i < 4; i++)
        
//     {
        
//         int x = System.Math.Abs(pb[0].Location.X - pb[i].Location.X);
        
//         int y = System.Math.Abs(pb[0].Location.Y - pb[i].Location.Y);
        
//         pb[i].Location = new Point(pb[0].Location.X + y, pb[0].Location.Y + x);
        
//     }
        
//     //如果坐标越界就进行反旋转
        
//     if (pb[0].Location.X < 0 || pb[1].Location.X < 0 || pb[2].Location.X < 0 || pb[3].Location.X < 0) Roll2();
        
//     if (pb[0].Location.X > 200 || pb[1].Location.X > 200 || pb[2].Location.X > 200 || pb[3].Location.X > 200) Roll2();
        
// }
        
// private void Roll4()//在4转1
        
// {
        
//     for (int i = 1; i < 4; i++)
        
//     {
        
//         int x = System.Math.Abs(pb[0].Location.X - pb[i].Location.X);
        
//         int y = System.Math.Abs(pb[0].Location.Y - pb[i].Location.Y);
        
//         pb[i].Location = new Point(pb[0].Location.X + y, pb[0].Location.Y - x);
        
//     }
        
//     //如果坐标越界就进行反旋转
        
//     if (pb[0].Location.X > 200 || pb[1].Location.X > 200 || pb[2].Location.X > 200 || pb[3].Location.X > 200) Roll3();
        
//     if (pb[0].Location.X < 0 || pb[1].Location.X < 0 || pb[2].Location.X < 0 || pb[3].Location.X < 0) Roll3();
        
// }

        
//检查是否到底 到底则进行转换
        private void Rollx(int a,int b)
        
...{
            
bool flag=true;
            PictureBox[] tmpPB
=new PictureBox[4];
            
for(int i=0;i<4;i++)
            
...{
                tmpPB[i]
=new PictureBox();
                
int x = System.Math.Abs(pb[0].Location.X - pb[i].Location.X);
                
int y = System.Math.Abs(pb[0].Location.Y - pb[i].Location.Y);
                tmpPB[i].Location 
= new Point(pb[0].Location.X +a* y, pb[0].Location.Y +b*x);
                
if (!checkPes(tmpPB[i].Location)) flag = false;
            }

            
if (flag)
            
...{
                
for (int i = 0; i < 4; i++)
                
...{
                    
int x = System.Math.Abs(pb[0].Location.X - pb[i].Location.X);
                    
int y = System.Math.Abs(pb[0].Location.Y - pb[i].Location.Y);
                    pb[i].Location 
= new Point(pb[0].Location.X + a * y, pb[0].Location.Y + b * x);
                }

            }

        }


        
public bool check(Form f)
        
...{
            
for(int i=0;i<4;i++)
            
...{
                
if (pb[i].Location.Y > 500)
                
...{
                    
//任一一个图像到底则整个图像进行转换
                    changToOld(f);
                    
return true;
                }

                
//判断该图像所对应的位置的下方是否有已经触底的图形
                int x 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics