加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C# 实现俄罗斯方块

发布时间:2020-12-16 01:13:37 所属栏目:百科 来源:网络整理
导读:概述 俄罗斯方块(Tetris)是一款由俄罗斯人阿列克谢·帕基特诺夫发明的休闲游戏,帕基特诺夫爱玩拼图,从拼图游戏里得到灵感,设计出了俄罗斯方块。由于上手简单、老少皆宜,从而家喻户晓,风靡世界。本文简述如何通过C#来实现俄罗斯方块,仅供学习分享使用

概述

俄罗斯方块(Tetris)是一款由俄罗斯人阿列克谢·帕基特诺夫发明的休闲游戏,帕基特诺夫爱玩拼图,从拼图游戏里得到灵感,设计出了俄罗斯方块。由于上手简单、老少皆宜,从而家喻户晓,风靡世界。本文简述如何通过C#来实现俄罗斯方块,仅供学习分享使用,如有不足之处,还请指正。

涉及知识点

  1. BackgroundWorker 在单独的线程上执行操作(主要执行比较耗时的操作)。
  2. Action .NetFramework自带的一个委托方法。
  3. TableLayoutPanel? 表示一个面板,它可以在一个由行和列组成的网格中对其内容进行动态布局,本文主要用作俄罗斯方块的容器。

方块流程图

如下图所示,描述了俄罗斯方块的设计流程图

俄罗斯方块效果图

如下图所示:主要包括状态,得分,开始按钮,停止按钮,按键盘左右箭头移动等功能

核心代码

1. 定义方块的形状

如下所示:共7中形状

 1 /// <summary>
 2     /// 俄罗斯方块的形状
 3     </summary>
 4     public enum TetrisStyle
 5     {
 6         S = 0, 7         Z = 1 8         L = 2 9         J = 310         I = 411         O = 512         T = 6
13     }

2. 定义移动的方向

如下所示:默认向下移动,同时可以左右移动

 俄罗斯方块移动方向
 TetrisDirection
 6         UP = 0,//上,表示顺时针旋转
 7         DOWN = 1,1)">下,表示向下移动
 8         LEFT = 2,1)">左,表示往左移动
 9         RIGHT = 3,1)">表示向右移动
10         DEFAULT=4 默认动作
11     }

3. 俄罗斯方块元素

如下所示,每一种形状都由四个方块组成,根据不同形状设置不同的位置

  1   2      俄罗斯方块元素
  3       4     class TetrisElement
  5   6           7          构造函数
  8           9         <param name="style"></param>
 10         public TetrisElement(TetrisStyle style) {
 11             this.style = style;
 12         }
 13 
 14          15          16          17         <param name="style">形状</param>
 18         <param name="content">内容 19         <param name="location">位置 20          TetrisElement(TetrisStyle style,Point[] content,Point location)
 21         {
 22              23             this.content = content;
 24             this.location = location;
 25  26 
 27          28          元素字母类型
 29          30         public TetrisStyle style { get; set; }
 31 
 32          33          内容
 34          35         public Point[] content {  36 
 37          38          元素位置
 39          40         public Point location {  41 
 44          45          位置改变
 46          47         <param name="x"></param>
 48         <param name="y"></param>
 49         void move(int x,1)">int y)
 50  51             this.location = new Point(x,y);
 52  53 
 54          Point[] getContent(TetrisStyle style)
 55  56             内容由四个点组成,顺序:先上后下,先左后右
 57             Point[] content = new Point[];
 58             switch (style)
 59             {
 60                 case TetrisStyle.I:
 61                     I形状
 62                     content[0] = new Point();
 63                     content[1] =  64                     content[2] =  65                     content[3] =  66                     break;
 67                  TetrisStyle.J:
 68                     J形状
 69                     content[ 70                     content[ 71                     content[ 72                     content[ 73                      74                  TetrisStyle.L:
 75                     L形状
 76                     content[ 77                     content[ 78                     content[ 79                     content[ 80                      81                  TetrisStyle.O:
 82                     O形状
 83                     content[ 84                     content[ 85                     content[ 86                     content[ 87                      88                  TetrisStyle.S:
 89                     S形状
 90                     content[ 91                     content[ 92                     content[ 93                     content[ 94                      95                  TetrisStyle.T:
 96                     T形状
 97                     content[ 98                     content[ 99                     content[100                     content[101                     102                  TetrisStyle.Z:
103                     Z形状
104                     content[105                     content[106                     content[107                     content[108                     109                 default:
110                     默认I
111                     content[112                     content[113                     content[114                     content[115                     116             }
117             return118 119     }

4. 容器类

如下所示:容器类主要是移动方块元素,并更新页面上的值

 俄罗斯方块容器
 TetrisContainer
  6         private int[,] tetris = new int[10,1)">20];定义二维数组,表示坐标信息,默认值为0
  7 
  8         public Action<Point,Point[],TetrisDirection> onPartialChanged;局部变更事件
  9 
public Action<元素全变更事件,即有整行被清除事件
 11 
 12         public Action onCompleted; 结束事件
 14         int scorce =  15 
 状态发生改变
<param name="element"></param>
 20         <param name="direction"></param>
 21         <returns></returns>
 22          TetrisElement change(TetrisElement element,TetrisDirection direction)
 23  24             TetrisElement tmp=null 25             判断不同的方向
 26              (direction) {
 27                  TetrisDirection.DEFAULT:
 28                     如果可以向下移动
 29                     if (checkDefault(element))
 30                     {
 31                         向下移动一个元素
 32                         element.move(element.location.X,element.location.Y +  33                         tmp = element;
 34                     }
 35                     else {
 36                         如果不可以向下移动,则更新容器
 37                         updateTetris(element);
 38                         tmp =  39  40                    
 41                      42                  TetrisDirection.DOWN:
 43                      44                  TetrisDirection.UP:
 45                      46                  TetrisDirection.LEFT:
 47                      (checkLeft(element)){
 48                         判断是否可以向左移动
 49                          50                         element.move(element.location.X- 51                         tmp = 53                      54                  TetrisDirection.RIGHT:
 55                      (checkRight(element))
 56  57                         判断是否可以右左移动
 58                          59                         element.move(element.location.X+ 60                         tmp = 61  62                      63  64 
 65             局部变更
 66             if (onPartialChanged != )
 67  68                 Point location = element.location;
 69                 Point[] content =  70                 element.content.CopyTo(content,1)"> 71 
 72                 for (int i = 0; i < content.Length; i++ 73                 {
 74                     content[i].X = location.X + content[i].X;
 75                     content[i].Y = location.Y + content[i].Y;
 76                 }
 77                 onPartialChanged(location,content,direction);
 78  79 
 80             判断游戏是否结束
 81             if (onCompleted != ) {
 82                  (checkComplete()) {
 83                     onCompleted();
 84  85  86 
 87             全部变更
 88             if (onFullChanged !=  89  90                 判断是是否有权为1的行,如果有则消掉
 91                 int[] rows = checkAllTetris();
 92                 if (rows.Length> 93  94                     updateAllTetris(rows);消掉行
 95                     onFullChanged(tetris);
 96  97  98 
 99              tmp;
100 101 
102         103          更新tetris
104         105         106         void updateTetris(TetrisElement element)
107 108             Point location =109             Point[] content = element.content;
110             int minX = element.getMinX(element.style);
111             int maxX = element.getMaxX(element.style);
112             int minY = element.getMinY(element.style);
113             int maxY = element.getMaxY(element.style);
114             foreach (Point p in content)
115 116                 if (location.Y + p.Y < 20 && location.Y + p.Y >= 0 && location.X + p.X >= 0 && location.X + p.X < 10117 118                     this.tetris[location.X + p.X,location.Y + p.Y] = 119 120 121 122 
123         124          检查全部列
125         126         [] checkAllTetris()
127 128             List<int> lst = new List<int>();
129             20行
130             int y = 0; y < 20; y++131 132                 int col = 133                 10列
134                 int x = 0; x < 10; x++135 136                     if (tetris[x,y] == 137 138                         139 140                     else
141 142                         col += 143 144 145                 if (col == 146 147                     col = 148                     lst.Add(y);
149 150 151              lst.ToArray();
152 153 
154         155          更新
156         157         void updateAllTetris([] rows) {
158             foreach (int row  rows) {
159                 当前行清掉
160                 161                     tetris[x,row] = 162 163                 row行之上的往下移动一行
164                 int y = row-1; y >=0; y--165                     166                         167                             tetris[x,y + 1] = 168                             tetris[x,y] = 169                         }
170 171 172 173 174 
175         176          判断游戏是否结束
177         178         179         bool checkComplete() {
180             bool isComplete = false181             0; i < 10; i++182                 if (tetris[i,1)">0] == 183                     isComplete = true184                     185 186 187              isComplete;
188 189 
190         191          更新得分
192         193         <param name="s"></param>
194         void updateScore( s) {
195             this.scorce = this.scorce + s;
196 197 
198         199          重置信息
200         201          Reset() {
202             this.tetris = 20203             this.scorce = 204 205     }

5. 随机生成方块元素和起始位置

 2          静态函数,生成Tetris元素对象
 3          4          5         static TetrisElement generate()
 6  7             Random r = new Random( 8             随机生成形状
 9             int tstyle = getRandom();
10             tstyle = tstyle % 711             TetrisStyle style = TetrisStyle.I;
12             style = (TetrisStyle)Enum.Parse(typeof(TetrisStyle),tstyle.ToString());
13             随机生成起始坐标
14             int x =15             x = x % 16             17             根据形状生成位置信息
18             TetrisElement element =  TetrisElement(style);
19             20             Point[] content = element.getContent(style);
21             获取最小坐标和最大坐标,防止越界
22              element.getMinX(style);
23              element.getMinY(style);
24              element.getMaxX(style);
25              element.getMaxY(style);
26             修正起始坐标
27             x = (x <= minX) ? minX : x;
28             x = (x >= maxX) ? maxX : x;
29             y = minY;
30             Point location = 31             element.location =32             element.content =33             34         }

备注

源码下载链接

闲下来的时候,放一段柔情音乐,翻阅几页好书,然后睡个懒觉,快哉。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读