当前方块对象

#region 定义砖块int[i,j,y,x] Tricks:i为那块砖,j为状态,y为列,x为行
private int[, , ,] Tricks = {{
{
{,,,},
{,,,},
{,,,},
{,,,}
},
{
{,,,},
{,,,},
{,,,},
{,,,}
},
{
{,,,},
{,,,},
{,,,},
{,,,}
},
{
{,,,},
{,,,},
{,,,},
{,,,}
}
},
{
{
{,,,},
{,,,},
{,,,},
{,,,}
},
{
{,,,},
{,,,},
{,,,},
{,,,}
},
{
{,,,},
{,,,},
{,,,},
{,,,}
},
{
{,,,},
{,,,},
{,,,},
{,,,}
}
},
{
{
{,,,},
{,,,},
{,,,},
{,,,}
},
{
{,,,},
{,,,},
{,,,},
{,,,}
},
{
{,,,},
{,,,},
{,,,},
{,,,}
},
{
{,,,},
{,,,},
{,,,},
{,,,}
}
},
{
{
{,,,},
{,,,},
{,,,},
{,,,}
},
{
{,,,},
{,,,},
{,,,},
{,,,}
},
{
{,,,},
{,,,},
{,,,},
{,,,}
},
{
{,,,},
{,,,},
{,,,},
{,,,}
}
},
{
{
{,,,},
{,,,},
{,,,},
{,,,}
},
{
{,,,},
{,,,},
{,,,},
{,,,}
},
{
{,,,},
{,,,},
{,,,},
{,,,}
},
{
{,,,},
{,,,},
{,,,},
{,,,}
}
},
{
{
{,,,},
{,,,},
{,,,},
{,,,}
},
{
{,,,},
{,,,},
{,,,},
{,,,}
},
{
{,,,},
{,,,},
{,,,},
{,,,}
},
{
{,,,},
{,,,},
{,,,},
{,,,}
}
},
{
{
{,,,},
{,,,},
{,,,},
{,,,}
},
{
{,,,},
{,,,},
{,,,},
{,,,}
},
{
{,,,},
{,,,},
{,,,},
{,,,}
},
{
{,,,},
{,,,},
{,,,},
{,,,}
}
}}; #endregion //当前的砖块
public int[,] CurrentTrick = new int[, ]; //CurrentTrickNum当前砖块的数目, CurrentStatusNum当前状态,CurrentColor当前颜色, CurrentX当前x, CurrentY当前y
public int CurrentTrickNum, CurrentStatusNum, CurrentColor, CurrentX, CurrentY; //方块种类
private int TricksNum = ; //方块样式
private int StatusNum = ; //颜色种类
private int ColorNum = ; private Random rand = new Random(); /// <summary>
/// 随机生成方块和状态
/// </summary>
public void BeginTricks()
{
//随机生成砖码和状态码
CurrentTrickNum = rand.Next(, TricksNum);
CurrentStatusNum = rand.Next(, StatusNum);
CurrentColor = rand.Next(, ColorNum);
//分配数组
for (int y = ; y < ; y++)
{
for (int x = ; x < ; x++)
{
CurrentTrick[y, x] = Tricks[CurrentTrickNum, CurrentStatusNum, y, x];
}
}
CurrentX = ;
CurrentY = ;
} /// <summary>
/// 变化方块
/// </summary>
public void ChangeTricks()
{
if (CurrentStatusNum < )
{
CurrentStatusNum++;
}
else
{
CurrentStatusNum = ;
}
for (int y = ; y < ; y++)
{
for (int x = ; x < ; 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++;
}

Brick

操作类

#region 定义背景
private int[,] bgGraoud ={
{,,,,,,,,,,,,,},
{,,,,,,,,,,,,,},
{,,,,,,,,,,,,,},
{,,,,,,,,,,,,,},
{,,,,,,,,,,,,,},
{,,,,,,,,,,,,,},
{,,,,,,,,,,,,,},
{,,,,,,,,,,,,,},
{,,,,,,,,,,,,,},
{,,,,,,,,,,,,,},
{,,,,,,,,,,,,,},
{,,,,,,,,,,,,,},
{,,,,,,,,,,,,,},
{,,,,,,,,,,,,,},
{,,,,,,,,,,,,,},
{,,,,,,,,,,,,,},
{,,,,,,,,,,,,,},
{,,,,,,,,,,,,,},
{,,,,,,,,,,,,,},
{,,,,,,,,,,,,,}
};
#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 = ;
for (int y = ; y < ; y++)
{
for (int x = ; x < ; x++)
{
bgGraoud[y, x] = ;
}
}
} /// <summary>
/// 随机生成方块和状态
/// </summary>
public void BeginTricks()
{
brick.BeginTricks();
} /// <summary>
/// 变化方块
/// </summary>
public void ChangeTricks()
{
brick.ChangeTricks();
} /// <summary>
/// 检测是否可以向下了
/// </summary>
/// <returns></returns>
private bool CheckIsDown()
{
for (int y = ; y < ; y++)
{
for (int x = ; x < ; x++)
{
if (brick.CurrentTrick[y, x] == )
{
//超过了背景
if (y + brick.CurrentY + >= )
{
return false;
}
if (x + brick.CurrentX >= )
{
brick.CurrentX = - x;
}
if (bgGraoud[y + brick.CurrentY + , x + brick.CurrentX] >= )
{
return false;
}
}
}
}
return true;
} /// <summary>
/// 下落方块
/// </summary>
public bool DownTricks()
{
if (CheckIsDown())
{
brick.DownTricks();
}
else
{
if (brick.CurrentY == )
{
return false;
}
//下落完成,修改背景
for (int y = ; y < ; y++)
{
for (int x = ; x < ; x++)
{
if (brick.CurrentTrick[y, x] == )
{
bgGraoud[brick.CurrentY + y, brick.CurrentX + x] = brick.CurrentColor;
}
}
}
CheckSore();
BeginTricks(); }
return true;
} /// <summary>
/// 检测是否可以左移
/// </summary>
/// <returns></returns>
private bool CheckIsLeft()
{
for (int y = ; y < ; y++)
{
for (int x = ; x < ; x++)
{
if (brick.CurrentTrick[y, x] == )
{
if (x + brick.CurrentX - < )
{
return false;
}
if (bgGraoud[y + brick.CurrentY, x + brick.CurrentX - ] >= )
{
return false;
}
}
}
}
return true;
} /// <summary>
/// 左移方块
/// </summary>
public void LeftTricks()
{
if (CheckIsLeft())
{
brick.LeftTricks();
}
} /// <summary>
/// 检测是否可以右移
/// </summary>
/// <returns></returns>
private bool CheckIsRight()
{
for (int y = ; y < ; y++)
{
for (int x = ; x < ; x++)
{
if (brick.CurrentTrick[y, x] == )
{
if (x + brick.CurrentX + >= )
{
return false;
}
if (bgGraoud[y + brick.CurrentY, x + brick.CurrentX + ] >= )
{
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 = ; bgy < ; bgy++)
{
for (int bgx = ; bgx < ; bgx++)
{
if (bgGraoud[bgy, bgx] > )
{
g.FillRectangle(new SolidBrush(TrickColor[bgGraoud[bgy, bgx]]), bgx * , bgy * , , );
g.DrawRectangle(Pens.White, bgx * , bgy * , , );
}
}
}
//绘制当前的图片
for (int y = ; y < ; y++)
{
for (int x = ; x < ; x++)
{
if (brick.CurrentTrick[y, x] == )
{
g.FillRectangle(new SolidBrush(TrickColor[brick.CurrentColor]), (x + brick.CurrentX) * , (y + brick.CurrentY) * , , );
g.DrawRectangle(Pens.White, (x + brick.CurrentX) * , (y + brick.CurrentY) * , , );
}
}
} return myImage;
} private void CheckSore()
{
for (int y = ; y > -; y--)
{
bool isFull = true;
for (int x = ; x > -; x--)
{
if (bgGraoud[y, x] == )
{
isFull = false;
break;
}
}
if (isFull)
{
//增加积分
Sorce = Sorce + ;
for (int yy = y; yy > ; yy--)
{
for (int xx = ; xx < ; xx++)
{
int temp = bgGraoud[yy - , xx];
bgGraoud[yy, xx] = temp;
}
}
y++;
}
}
}

TetrisHelper

窗体代码

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 = ;
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 = ;
timer1.Start();
}
} private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (timer1.Enabled == false)
{
return;
}
if (e.KeyCode == Keys.S)
{
timer1.Stop();
timer1.Interval = ;
timer1.Start();
}
} private void Draw()
{
Image img = tetrisHelper.Draw(); Graphics gg = panel1.CreateGraphics(); gg.DrawImage(img, , );
} private void button2_Click(object sender, EventArgs e)
{
if (button2.Text == "暂停游戏")
{
button2.Text = "开始游戏";
timer1.Stop();
}
else
{
button2.Text = "暂停游戏";
timer1.Start();
}
}

Form1

页面展现

GDI学习之俄罗斯方块续的更多相关文章

  1. GDI学习之俄罗斯方块

    做个玩玩 public Form1() { InitializeComponent(); } #region 定义砖块int[i,j,y,x] Tricks:i为那块砖,j为状态,y为列,x为行 pr ...

  2. C# GDI+学习笔记1

    —前言 本文是学习C# GDI+系列的第一篇文章,简单的介绍了GDI+的一些基本绘图内容,比较粗糙.但本文主要是让大家简单的回顾一下GDI+的基本概念.本篇文章的参考代码请在此下载 . GDIPTes ...

  3. Delphi7 GDI+学习

    Delphi7自带的绘图有锯齿,所以要学习GDI+ 主要是从这个网站学习 http://www.bianceng.com/Programming/Delphi/201212/34691.htm 相关控 ...

  4. GDI+学习笔记(六)渐变画笔

    刷,顾名思义,它是一样的刷.提请设备,还记得常唱歌曲,"我是一个画家.." 好吧.跑题了. 本系列博客希望尽可能简单的描写叙述每项功能,而不希望把每一个參数都介绍的详具体细,假设须 ...

  5. GDI+学习---2.GDI+编程模式及组成类

    在使用GDI+的时候,您不必像在GDI中那样关心设备场景句柄,只需简单地创建一个Graphics对象,然后以您熟悉的面向对象的方式(如myGraphicsObject.DrawLine(paramet ...

  6. GDI+学习---1.初识GDI+

    ---恢复内容开始--- GDI+: GDI+由一组C++类实现,是对于GDI的继承,GDI+不仅优化了大部分GDI性能而且提供了更多特性.允许应用程序开发者将信息显示在显示器或者打印机上,而无需考虑 ...

  7. GDI+学习笔记

    7.1.1 GDI+概述 GDI+是微软在Windows 2000以后操作系统中提供的新的图形设备接口,其通过一套部署为托管代码的类来展现, 这套类被称为GDI+的“托管类接口”,GDI+主要提供了以 ...

  8. GDI+学习笔记(九)带插件的排序算法演示器(MFC中的GDI+实例)

    带插件的排序算法演示器 请尊重本人的工作成果,转载请留言.并说明转载地址,谢谢. 地址例如以下: http://blog.csdn.net/fukainankai/article/details/27 ...

  9. JFinal学习 & Gradle配置续 & Tomcat配置

    接上一篇对Gradle的学习,再用JFinal项目再建一个. 参考了这篇文章:https://my.oschina.net/u/1010578/blog/390094 但是其中没有代码,所以看了这篇  ...

随机推荐

  1. 分享几个cocos2dx的小游戏

    先上几个自己写的,因为最近要用cocos2dx,所以就边学边开发几个小游戏玩玩,有捕鱼,连连看,还有打地鼠!都不算完整的项目,不过拿来学习还是可以的,或者在基础上再二次开发,扩展自己! 1:捕鱼的 先 ...

  2. java和javascript双引号嵌套的问题

    双引号不能嵌套双引号,否则一对双引号就结束了. java中双引号中嵌套双引号需要转义: /** * java双引号转义符测试 * @author asus * */ public static voi ...

  3. ASP.NET后台执行JS代码

    1. 用Response.Write方法 代码如下:Response.Write("<script type='text/javascript'>alert("XXX& ...

  4. ubuntu修改hostname

    在Ubuntu系统中,快速查看主机名有多种方法:其一,打开一个GNOME终端窗口,在命令提示符中可以看到主机名,主机名通常位于“@”符号后:其二,在终端窗口中输入命令:hostname或uname – ...

  5. SqlServer一张表数据导入另一张表,收藏使用,工作中更新数据错误很有用

    sql一张表数据导入另一张表   1.如果2张表的字段一致,并且希望插入全部数据,可以用这种方法:   INSERT INTO 目标表 SELECT * FROM 来源表;   2.比如要将 arti ...

  6. 邻接矩阵c源码(构造邻接矩阵,深度优先遍历,广度优先遍历,最小生成树prim,kruskal算法)

    matrix.c #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include < ...

  7. javascript 之正则匹配HTML

    正则表达式 <(\S*?) [^>]*>.*?</\1>|<.*? /> 匹配 <html>hello</html>|<a> ...

  8. Web网站数据”实时”更新设计

    请注意这个实时打上了双引号,没有绝对的实时,只是时间的颗粒不一样罢了(1ms,1s,1m). 服务器数据有更新可以快速通知客户端.Web 基于取得模式,而服务器建立大量的和客户端连接来提供数据实时更新 ...

  9. sass开发过程中遇到的几个坑

    1.安装sass被墙的问题 安装完`ruby`后,打开`ruby cmd` 输入`gem install sass`,安装失败,有可能是镜像源的问题,也有可能是墙的问题. 因为公司内网的奇葩限制,各种 ...

  10. .NET破解之分享给新注册的朋友

    前些日子,在论坛里看了有人发过这个软件,也有大神分析过网络版,如果是重帖,请删除吧:正好11.11注册了很多新会员,给他们一个见面礼吧,抛砖引玉,我才来论坛的时候,也是看着前人教程慢慢学习的:好久没冒 ...