当前方块对象
#region 定义砖块int[i,j,y,x] Tricks:i为那块砖,j为状态,y为列,x为行 private int[, , ,] Tricks = {{ { {0,1,0,0}, {0,1,0,0}, {0,1,0,0}, {0,1,0,0} }, { {0,0,0,0}, {0,0,0,0}, {1,1,1,1}, {0,0,0,0} }, { {0,0,1,0}, {0,0,1,0}, {0,0,1,0}, {0,0,1,0} }, { {0,0,0,0}, {1,1,1,1}, {0,0,0,0}, {0,0,0,0} } }, { { {0,0,0,0}, {0,1,1,0}, {0,1,1,0}, {0,0,0,0} }, { {0,0,0,0}, {0,1,1,0}, {0,1,1,0}, {0,0,0,0} }, { {0,0,0,0}, {0,1,1,0}, {0,1,1,0}, {0,0,0,0} }, { {0,0,0,0}, {0,1,1,0}, {0,1,1,0}, {0,0,0,0} } }, { { {0,1,0,0}, {0,1,1,0}, {0,0,1,0}, {0,0,0,0} }, { {0,0,0,0}, {0,0,1,1}, {0,1,1,0}, {0,0,0,0} }, { {0,1,0,0}, {0,1,1,0}, {0,0,1,0}, {0,0,0,0} }, { {0,0,0,0}, {0,0,1,1}, {0,1,1,0}, {0,0,0,0} } }, { { {0,0,1,0}, {0,1,1,0}, {0,1,0,0}, {0,0,0,0} }, { {0,0,0,0}, {1,1,0,0}, {0,1,1,0}, {0,0,0,0} }, { {0,0,1,0}, {0,1,1,0}, {0,1,0,0}, {0,0,0,0} }, { {0,0,0,0}, {1,1,0,0}, {0,1,1,0}, {0,0,0,0} } }, { { {0,1,1,0}, {0,0,1,0}, {0,0,1,0}, {0,0,0,0} }, { {0,0,1,0}, {1,1,1,0}, {0,0,0,0}, {0,0,0,0} }, { {0,1,0,0}, {0,1,0,0}, {0,1,1,0}, {0,0,0,0} }, { {1,1,1,0}, {1,0,0,0}, {0,0,0,0}, {0,0,0,0} } }, { { {0,1,1,0}, {0,1,0,0}, {0,1,0,0}, {0,0,0,0} }, { {1,1,1,0}, {0,0,1,0}, {0,0,0,0}, {0,0,0,0} }, { {0,0,1,0}, {0,0,1,0}, {0,1,1,0}, {0,0,0,0} }, { {1,0,0,0}, {1,1,1,0}, {0,0,0,0}, {0,0,0,0} } }, { { {0,1,0,0}, {1,1,1,0}, {0,0,0,0}, {0,0,0,0} }, { {0,1,0,0}, {1,1,0,0}, {0,1,0,0}, {0,0,0,0} }, { {0,0,0,0}, {1,1,1,0}, {0,1,0,0}, {0,0,0,0} }, { {0,1,0,0}, {0,1,1,0}, {0,1,0,0}, {0,0,0,0} } }}; #endregion //当前的砖块 public int[,] CurrentTrick = new int[4, 4]; //CurrentTrickNum当前砖块的数目, CurrentStatusNum当前状态,CurrentColor当前颜色, CurrentX当前x, CurrentY当前y public int CurrentTrickNum, CurrentStatusNum, CurrentColor, CurrentX, CurrentY; //方块种类 private int TricksNum = 4; //方块样式 private int StatusNum = 4; //颜色种类 private int ColorNum = 5; private Random rand = new Random(); /// <summary> /// 随机生成方块和状态 /// </summary> public void BeginTricks() { //随机生成砖码和状态码 CurrentTrickNum = rand.Next(0, TricksNum); CurrentStatusNum = rand.Next(0, StatusNum); CurrentColor = rand.Next(1, ColorNum); //分配数组 for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { CurrentTrick[y, x] = Tricks[CurrentTrickNum, CurrentStatusNum, y, x]; } } CurrentX = 5; CurrentY = 0; } /// <summary> /// 变化方块 /// </summary> public void ChangeTricks() { if (CurrentStatusNum < 3) { CurrentStatusNum++; } else { CurrentStatusNum = 0; } for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { CurrentTrick[y, x] = Tricks[CurrentTrickNum, CurrentStatusNum, y, x]; } } } /// <summary> /// 下落方块 /// </summary> public void DownTricks() { CurrentY++; } /// <summary> /// 左移方块 /// </summary> public void LeftTricks() { CurrentX--; } /// <summary> /// 右移方块 /// </summary> public void RightTricks() { CurrentX++; }
操作类
#region 定义背景 private int[,] bgGraoud ={ {0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0} }; #endregion //定义颜色 private Color[] TrickColor = { Color.Transparent, Color.Red, Color.Blue, Color.Orange, Color.Green }; //Sorce分数 public int Sorce; private Image myImage; private Color backColor; private Brick brick = new Brick(); public TetrisHelper(Image myImage, Color backColor) { this.myImage = myImage; this.backColor = backColor; } /// <summary> /// 重新开始 /// </summary> public void Renew() { Sorce = 0; for (int y = 0; y < 20; y++) { for (int x = 0; x < 14; x++) { bgGraoud[y, x] = 0; } } } /// <summary> /// 随机生成方块和状态 /// </summary> public void BeginTricks() { brick.BeginTricks(); } /// <summary> /// 变化方块 /// </summary> public void ChangeTricks() { brick.ChangeTricks(); } /// <summary> /// 检测是否可以向下了 /// </summary> /// <returns></returns> private bool CheckIsDown() { for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { if (brick.CurrentTrick[y, x] == 1) { //超过了背景 if (y + brick.CurrentY + 1 >= 20) { return false; } if (x + brick.CurrentX >= 14) { brick.CurrentX = 13 - x; } if (bgGraoud[y + brick.CurrentY + 1, x + brick.CurrentX] >= 1) { return false; } } } } return true; } /// <summary> /// 下落方块 /// </summary> public bool DownTricks() { if (CheckIsDown()) { brick.DownTricks(); } else { if (brick.CurrentY == 0) { return false; } //下落完成,修改背景 for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { if (brick.CurrentTrick[y, x] == 1) { bgGraoud[brick.CurrentY + y, brick.CurrentX + x] = brick.CurrentColor; } } } CheckSore(); BeginTricks(); } return true; } /// <summary> /// 检测是否可以左移 /// </summary> /// <returns></returns> private bool CheckIsLeft() { for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { if (brick.CurrentTrick[y, x] == 1) { if (x + brick.CurrentX - 1 < 0) { return false; } if (bgGraoud[y + brick.CurrentY, x + brick.CurrentX - 1] >= 1) { return false; } } } } return true; } /// <summary> /// 左移方块 /// </summary> public void LeftTricks() { if (CheckIsLeft()) { brick.LeftTricks(); } } /// <summary> /// 检测是否可以右移 /// </summary> /// <returns></returns> private bool CheckIsRight() { for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { if (brick.CurrentTrick[y, x] == 1) { if (x + brick.CurrentX + 1 >= 14) { return false; } if (bgGraoud[y + brick.CurrentY, x + brick.CurrentX + 1] >= 1) { return false; } } } } return true; } /// <summary> /// 右移方块 /// </summary> public void RightTricks() { if (CheckIsRight()) { brick.RightTricks(); } } public Image Draw() { Graphics g = Graphics.FromImage(myImage); g.Clear(backColor); for (int bgy = 0; bgy < 20; bgy++) { for (int bgx = 0; bgx < 14; bgx++) { if (bgGraoud[bgy, bgx] > 0) { g.FillRectangle(new SolidBrush(TrickColor[bgGraoud[bgy, bgx]]), bgx * 20, bgy * 20, 20, 20); g.DrawRectangle(Pens.White, bgx * 20, bgy * 20, 20, 20); } } } //绘制当前的图片 for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { if (brick.CurrentTrick[y, x] == 1) { g.FillRectangle(new SolidBrush(TrickColor[brick.CurrentColor]), (x + brick.CurrentX) * 20, (y + brick.CurrentY) * 20, 20, 20); g.DrawRectangle(Pens.White, (x + brick.CurrentX) * 20, (y + brick.CurrentY) * 20, 20, 20); } } } return myImage; } private void CheckSore() { for (int y = 19; y > -1; y--) { bool isFull = true; for (int x = 13; x > -1; x--) { if (bgGraoud[y, x] == 0) { isFull = false; break; } } if (isFull) { //增加积分 Sorce = Sorce + 100; for (int yy = y; yy > 0; yy--) { for (int xx = 0; xx < 14; xx++) { int temp = bgGraoud[yy - 1, xx]; bgGraoud[yy, xx] = temp; } } y++; } } }
窗体代码
public Form1() { InitializeComponent(); } TetrisHelper tetrisHelper; private void Form1_Load(object sender, EventArgs e) { //初始化 Image myImage = new Bitmap(panel1.Width, panel1.Height); tetrisHelper = new TetrisHelper(myImage, panel1.BackColor); loadGame(); } protected override void OnPaint(PaintEventArgs e) { Draw(); base.OnPaint(e); } private void loadGame() { timer1.Interval = 1000; tetrisHelper.Renew(); label1.Text = "分数:" + tetrisHelper.Sorce; tetrisHelper.BeginTricks(); button2.Text = "开始游戏"; this.Focus(); } private void timer1_Tick(object sender, EventArgs e) { if (tetrisHelper.DownTricks()) { Draw(); } else { timer1.Stop(); if (MessageBox.Show("哈哈,你玩玩了", "确认", MessageBoxButtons.OK, MessageBoxIcon.Information) == DialogResult.OK) { loadGame(); } } } private void Form1_KeyDown(object sender, KeyEventArgs e) { if (timer1.Enabled == false) { return; } if (e.KeyCode == Keys.W) { tetrisHelper.ChangeTricks(); Draw(); } else if (e.KeyCode == Keys.A) { tetrisHelper.LeftTricks(); Draw(); } else if (e.KeyCode == Keys.D) { tetrisHelper.RightTricks(); Draw(); } else if (e.KeyCode == Keys.S) { timer1.Stop(); timer1.Interval = 10; timer1.Start(); } } private void Form1_KeyUp(object sender, KeyEventArgs e) { if (timer1.Enabled == false) { return; } if (e.KeyCode == Keys.S) { timer1.Stop(); timer1.Interval = 1000; timer1.Start(); } } private void Draw() { Image img = tetrisHelper.Draw(); Graphics gg = panel1.CreateGraphics(); gg.DrawImage(img, 0, 0); } private void button2_Click(object sender, EventArgs e) { if (button2.Text == "暂停游戏") { button2.Text = "开始游戏"; timer1.Stop(); } else { button2.Text = "暂停游戏"; timer1.Start(); } }
页面展现