在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
软设之后想着休息几天再继续其他课程的学习,于是花了点时间写了一个俄罗斯方块的小游戏(通过网上大神的版本)。俄罗斯方块的游戏主要有两个类组成。一个是方块类,用于创建不同形状的俄罗斯方块,并且提供在界面画方块的方法。还有一个就是游戏类,提供了键盘响应事件以及游戏的判定。 以下是代码: 1 方块类 2 public class Blocks 3 { 4 private const int blockWidth = 21; 5 private const int blockHeight = 21; 6 private int _width;//方块的宽,表示有多少个单元组成 7 private int _height;//方块的高 8 private int _left;//方块左边的位置 9 private int _top;//右边的位置 10 public int[,] shape;//用于存储方块,0表示无,1表示有 11 public int Top 12 { 13 get { return _top; } 14 set { _top = value; } 15 } 16 17 public int Left 18 { 19 get { return _left; } 20 set { _left = value; } 21 } 22 public int Height 23 { 24 get { return _height; } 25 set { _height = value; } 26 } 27 public int Width 28 { 29 get { return _width; } 30 set { _width = value; } 31 } 32 public Blocks() 33 { 34 getBlocks(); 35 } 36 /// <summary> 37 /// 产生一种图案的俄罗斯方块 38 /// </summary> 39 private void getBlocks() 40 { 41 Random r = new Random(); 42 int randomId= r.Next(1,9); 43 //int randomId = 1; 44 switch (randomId) 45 { 46 case 1://表示横条 47 Width = 4; 48 Height = 1; 49 Left = 3; 50 Top = 0; 51 shape = new int[Width, Height]; 52 shape[0, 0] = 1; 53 shape[1, 0] = 1; 54 shape[2, 0] = 1; 55 shape[3, 0] = 1; 56 break; 57 case 2://表示方块 58 Width = 2; 59 Height = 2; 60 Left = 4; 61 Top = 0; 62 shape = new int[Width, Height]; 63 shape[0, 0] = 1; 64 shape[0, 1] = 1; 65 shape[1, 0] = 1; 66 shape[1, 1] = 1; 67 break; 68 case 3://表示L 69 Width = 2; 70 Height = 3; 71 Left = 4; 72 Top = 0; 73 shape = new int[Width, Height]; 74 shape[0, 0] = 1; 75 shape[1, 2] = 1; 76 shape[0, 1] = 1; 77 shape[0, 2] = 1; 78 break; 79 case 4://表示L的反向 80 Width = 2; 81 Height = 3; 82 Left = 4; 83 Top = 0; 84 shape = new int[Width, Height]; 85 shape[0, 2] = 1; 86 shape[1, 0] = 1; 87 shape[1, 1] = 1; 88 shape[1, 2] = 1; 89 break; 90 case 5://表示z 91 Width = 2; 92 Height = 3; 93 Left = 4; 94 Top = 0; 95 shape = new int[Width, Height]; 96 shape[0, 1] = 1; 97 shape[1, 0] = 1; 98 shape[1, 1] = 1; 99 shape[0, 2] = 1; 100 break; 101 case 6://表示z的反向 102 Width = 2; 103 Height = 3; 104 Left = 4; 105 Top = 0; 106 shape = new int[Width, Height]; 107 shape[0, 0] = 1; 108 shape[0, 1] = 1; 109 shape[1, 1] = 1; 110 shape[1, 2] = 1; 111 break; 112 case 7://表示T 113 Width = 3; 114 Height = 2; 115 Left = 4; 116 Top = 0; 117 shape = new int[Width, Height]; 118 shape[0, 1] = 1; 119 shape[1, 0] = 1; 120 shape[2, 1] = 1; 121 shape[1, 1] = 1; 122 break; 123 case 8: 124 Width = 1; 125 Height = 1; 126 Left = 4; 127 Top = 0; 128 shape=new int[Width,Height]; 129 shape[0, 0] = 1; 130 break; 131 } 132 } 133 134 /// <summary> 135 /// 画单元方块 136 /// </summary> 137 /// <param name="g"></param> 138 public void DrawBlock(Graphics g) 139 { 140 Image backImg = Image.FromFile("image/block0.gif"); 141 for (int i = 0; i < Width; i++) 142 { 143 for (int j = 0; j < Height; j++) 144 { 145 if (shape[i, j] == 1) 146 { 147 Rectangle rectangle = new Rectangle((Left+i)*blockWidth,(Top+j)*blockHeight,blockWidth,blockHeight); 148 g.DrawImage(backImg,rectangle); 149 } 150 } 151 } 152 } 153 154 } 1 游戏类 2 public class Game 3 { 4 public const int gameWidth = 10; 5 public const int gameHeight = 20; 6 public const int blockWidth = 21; 7 public const int blockHeight = 21; 8 private Blocks currentBlock; 9 private Blocks nextBlock; 10 private int[,] gameMainPile=new int[gameWidth,gameHeight]; 11 public int score=0; 12 public bool over=false; 13 private AxWindowsMediaPlayer musicPlayer; 14 public Game(AxWindowsMediaPlayer musicPlayer) 15 { 16 ClearPile(); 17 CreateNewBlock(); 18 this.musicPlayer = musicPlayer; 19 //musicPlayer.settings.setMode("loop", true); 20 } 21 22 /// <summary> 23 /// 初始化游戏面板 24 /// </summary> 25 private void ClearPile() 26 { 27 for (int i = 0; i < gameWidth; i++) 28 { 29 for (int j = 0; j < gameHeight; j++) 30 { 31 gameMainPile[i, j] = 0; 32 } 33 } 34 } 35 36 /// <summary> 37 /// 创建一个俄罗斯方块 38 /// </summary> 39 private void CreateNewBlock() 40 { 41 if (nextBlock != null) 42 { 43 currentBlock = nextBlock; 44 } 45 else 46 { 47 currentBlock = new Blocks(); 48 } 49 nextBlock = new Blocks(); 50 } 51 52 /// <summary> 53 /// 画游戏主界面 54 /// </summary> 55 /// <param name="g"></param> 56 public void DrawPile(Graphics g) 57 { 58 Image backImg = Image.FromFile("image/block1.gif"); 59 for (int i = 0; i < gameWidth; i++) 60 { 61 for (int j = 0; j < gameHeight; j++) 62 { 63 if (gameMainPile[i, j] == 1) 64 { 65 Rectangle rectangle = new Rectangle(i*blockWidth,j*blockHeight,blockWidth,blockHeight); 66 g.DrawImage(backImg,rectangle); 67 } 68 } 69 } 70 } 71 72 /// <summary> 73 /// 画出当前方块所在位置 74 /// </summary> 75 /// <param name="g"></param> 76 public void DrawCurrentBlock(Graphics g) 77 { 78 if (currentBlock != null) 79 { 80 currentBlock.DrawBlock(g); 81 } 82 } 83 84 /// <summary> 85 /// 画出下一个俄罗斯方块 86 /// </summary> 87 /// <param name="g"></param> 88 public void DrawNextBlock(Graphics g) 89 { 90 if (nextBlock != null) 91 { 92 int tempLeft = nextBlock.Left; 93 int tempTop = nextBlock.Top; 94 nextBlock.Left = (6 - nextBlock.Width) / 2; 95 nextBlock.Top = (6 - nextBlock.Height) / 2; 96 nextBlock.DrawBlock(g); 97 nextBlock.Left = tempLeft; 98 nextBlock.Top = tempTop; 99 } 100 } 101 102 /// <summary> 103 /// 固定当前俄罗斯方块 104 /// </summary> 105 private void FixedCurrentBlock() 106 { 107 int fx, fy; 108 for (int i = 0; i < currentBlock.Width; i++) 109 { 110 for (int j = 0; j < currentBlock.Height; j++) 111 { 112 fx = currentBlock.Left + i; 113 fy = currentBlock.Top + j; 114 if (currentBlock.shape[i, j] == 1) 115 { 116 gameMainPile[fx, fy] = 1; 117 } 118 } 119 } 120 int n = CheckFullLines(); 121 if (n > 0) 122 { 123 musicPlayer.URL = "sounds/line.mp3"; 124 } 125 else 126 { 127 musicPlayer.URL = "sounds/hit.mp3"; 128 } 129 musicPlayer.Ctlcontrols.play(); 130 if (CheckOver()) 131 { 132 over = true; 133 } 134 } 135 136 /// <summary> 137 /// 判断俄罗斯方块是否到达底边 138 /// </summary> 139 /// <returns></returns> 140 public bool DownCheck() 141 { 142 bool hit = false; 143 currentBlock.Top++; 144 if ((currentBlock.Top + currentBlock.Height) > gameHeight) 145 { 146 //已经触碰到了底边 147 hit = true; 148 } 149 else 150 { 151 //检测是否触碰到已经存在的行 152 int fx, fy; 153 for (int i = 0; i < currentBlock.Width; i++) 154 { 155 for (int j = 0; j < currentBlock.Height; j++) 156 { 157 fx = currentBlock.Left + i; 158 fy = currentBlock.Top + j; 159 if ((currentBlock.shape[i, j] == 1) && (gameMainPile[fx, fy] == 1)) 160 { 161 hit = true; 162 } 163 } 164 } 165 } 166 167 if (hit) 168 { 169 currentBlock.Top--; 170 FixedCurrentBlock(); 171 CreateNewBlock(); 172 } 173 return hit; 174 } 175 176 /// <summary> 177 /// 键盘按键后响应移动俄罗斯方块 178 /// </summary> 179 /// <param name="direction"></param> 180 public void MoveBlocks(string direction) 181 { 182 bool canMove = true; 183 int fx, fy; 184 if (direction == "left") 185 { 186 //向左运动 187 if (currentBlock.Left > 0) 188 { 189 for (int i = 0; i < currentBlock.Width; i++) 190 { 191 for (int j = 0; j < currentBlock.Height; j++) 192 { 193 fx = currentBlock.Left + i; 194 fy = currentBlock.Top + j; 195 if ((currentBlock.shape[i, j] == 1) && (gameMainPile[(fx - 1), fy] == 1)) 全部评论
专题导读
上一篇:C#——窗体和控件随着分辨率的变化自适应大小发布时间:2022-07-13下一篇:AxWindowsMediaPlayer创建、添加播放列表(C#)---媒体播放器发布时间:2022-07-13热门推荐
热门话题
阅读排行榜
|
请发表评论