最近在看C#,写了一个很水的方块游戏练手。
代码:
namespace game
{
class Square
{
public Square()
{
state = 0;
positionY = 0;
positionX = 0;
}
public Square(int InitShapeCnt, int InitState)
{
state = InitState;
positionY = 0;
positionX = 0;
InitShape(InitShapeCnt);
}
public void InitShape(int ShapeCnt)
{
if (ShapeCnt > 6 || ShapeCnt < 0)
return;
else
{
switch (ShapeCnt)
{
case (0):
Shape = LeftLShape;
break;
case (1):
Shape = RightLShape;
break;
case (2):
Shape = RightZShape;
break;
case (3):
Shape = LeftZShape;
break;
case (4):
Shape = lShape;
break;
case (5):
Shape = TuShape;
break;
case (6):
Shape = TianShape;
break;
default:
break;
}
}
}
//方向 外形 颜色
public int[, ,,] Shape = new int[4, 4, 4, 2];
private int state; //方向
public int State
{
get
{
return (state);
}
set
{
state = value;
}
}
public void DrawSquare(ref Graphics dc)
{
for (int Cnt1 = 0; Cnt1<4; Cnt1++)
{
for(int Cnt2=0;Cnt2<4;Cnt2++)
{
//Shape[State][Cnt1][Cnt2][1] = 0;
if (Shape[state, Cnt1, Cnt2, 0] == 1)
{
SolidBrush Brush;
switch(Shape[state, Cnt1, Cnt2, 1])
{
case (1):
Brush = new SolidBrush(Color.Red);
break;
case (2):
Brush = new SolidBrush(Color.Blue);
break;
case (3):
Brush = new SolidBrush(Color.Yellow);
break;
case (4):
Brush = new SolidBrush(Color.Green);
break;
case (5):
Brush = new SolidBrush(Color.Tan);
break;
case (6):
Brush = new SolidBrush(Color.Honeydew);
break;
case (7):
Brush = new SolidBrush(Color.ForestGreen);
break;
default:
Brush = new SolidBrush(Color.Red);
break;
}
dc.FillRectangle(Brush, new Rectangle((positionX*16+16*Cnt2), (positionY*16+16*Cnt1), 16, 16));
}
}
}
}
private int positionX;
public int PositionX
{
get
{
return (positionX);
}
set
{
positionX = value;
}
}
private int positionY;
public int PositionY
{
get
{
return (positionY);
}
set
{
positionY = value;
}
}
public void Switch(GameCtrl game)
{
if (CoverEdge(game))
state = (state + 1) % 4;
}
public void AddX(GameCtrl game)
{
//判断右侧
if(RightEdge(game))
positionX++;
}
public bool AddY(GameCtrl game)
{
if (this.Land(game) == false)
{
positionY++;
return (true);
}
else
return(false);
}
public void SubX(GameCtrl game)
{
//判断右侧
if (LeftEdge(game))
positionX--;
}
public void SubY()
{
}
private bool Land(GameCtrl game)
{
for (int i = 3; i >= 0; i--)
{
int rowNum = 21 - this.PositionY - i;
for (int j = 0; j < 4; j++)
{
int colNum = this.PositionX + j + 3;
if (this.Shape[this.State, i, j, 0] == 1)
{
if (game.GameBox[rowNum][colNum, 0] == 1)
{
game.AddBox(this);
return (true);
}
}
}
}
return (false);
}
private bool RightEdge(GameCtrl game)
{
//判断右侧
for (int i = 3; i >= 0; i--)
{
int rowNum = this.PositionX + i + 4;
for (int j = 0; j < 4; j++)
{
int colNum = 22-this.PositionY-j;
if (this.Shape[this.State, j, i, 0] == 1)
{
if (game.GameBox[colNum][rowNum, 0] == 1)
{
return (false);
}
}
}
}
return (true);
}
private bool LeftEdge(GameCtrl game)
{
//判断左侧
for (int i = 0; i < 4; i++)
{
int rowNum = this.PositionX + i +2;
for (int j = 0; j < 4; j++)
{
int colNum = 22 - this.PositionY - j;
if (this.Shape[this.State, j, i, 0] == 1)
{
if (game.GameBox[colNum][rowNum, 0] == 1)
{
return (false);
}
}
}
}
return (true);
}
private bool CoverEdge(GameCtrl game)
{
//判断变行是否有覆盖
int preState = (this.State + 1) % 4;
for (int i = 0; i < 4; i++)
{
int rowNum = this.PositionX + i + 3;
for (int j = 0; j < 4; j++)
{
int colNum = 22 - this.PositionY - j;
if (this.Shape[preState, j, i, 0] == 1)
{
if (game.GameBox[colNum][rowNum, 0] == 1)
{
return (false);
}
}
}
}
return (true);
//return (false);
}
private int[, , ,] LeftLShape = {
{{{1,1},{0,0},{0,0},{0,0}},
{{1,1},{0,0},{0,0},{0,0}},
{{1,1},{1,1},{0,0},{0,0}},
{{0,0},{0,0},{0,0},{0,0}}},
{{{1,1},{1,1},{1,1},{0,0}},
{{1,1},{0,0},{0,0},{0,0}},
{{0,0},{0,0},{0,0},{0,0}},
{{0,0},{0,0},{0,0},{0,0}}},
{{{0,0},{1,1},{1,1},{0,0}},
{{0,0},{0,0},{1,1},{0,0}},
{{0,0},{0,0},{1,1},{0,0}},
{{0,0},{0,0},{0,0},{0,0}}},
{{{0,0},{0,0},{0,0},{0,0}},
{{0,0},{0,0},{1,1},{0,0}},
{{1,1},{1,1},{1,1},{0,0}},
{{0,0},{0,0},{0,0},{0,0}}}
};
private int[, , ,] RightLShape = {
{{{0,0},{0,0},{1,2},{0,0}},
{{0,0},{0,0},{1,2},{0,0}},
{{0,0},{1,2},{1,2},{0,0}},
{{0,0},{0,0},{0,0},{0,0}}},
{{{0,0},{0,0},{0,0},{0,0}},
{{1,2},{0,0},{0,0},{0,0}},
{{1,2},{1,2},{1,2},{0,0}},
{{0,0},{0,0},{0,0},{0,0}}},
{{{1,2},{1,2},{0,0},{0,0}},
{{1,2},{0,0},{0,0},{0,0}},
{{1,2},{0,0},{0,0},{0,0}},
{{0,0},{0,0},{0,0},{0,0}}},
{{{1,2},{1,2},{1,2},{0,0}},
{{0,0},{0,0},{1,2},{0,0}},
{{0,0},{0,0},{0,0},{0,0}},
{{0,0},{0,0},{0,0},{0,0}}}
};
private int[, , ,] LeftZShape = {
{{{1,3},{1,3},{0,0},{0,0}},
{{0,0},{1,3},{1,3},{0,0}},
{{0,0},{0,0},{0,0},{0,0}},
{{0,0},{0,0},{0,0},{0,0}}},
{{{0,0},{1,3},{0,0},{0,0}},
{{1,3},{1,3},{0,0},{0,0}},
{{1,3},{0,0},{0,0},{0,0}},
{{0,0},{0,0},{0,0},{0,0}}},
{{{1,3},{1,3},{0,0},{0,0}},
{{0,0},{1,3},{1,3},{0,0}},
{{0,0},{0,0},{0,0},{0,0}},
{{0,0},{0,0},{0,0},{0,0}}},
{{{0,0},{1,3},{0,0},{0,0}},
{{1,3},{1,3},{0,0},{0,0}},
{{1,3},{0,0},{0,0},{0,0}},
{{0,0},{0,0},{0,0},{0,0}}}
};
private int[, , ,] RightZShape = {
{{{0,0},{1,4},{1,4},{0,0}},
{{1,4},{1,4},{0,0},{0,0}},
{{0,0},{0,0},{0,0},{0,0}},
{{0,0},{0,0},{0,0},{0,0}}},
{{{1,4},{0,0},{0,0},{0,0}},
{{1,4},{1,4},{0,0},{0,0}},
{{0,0},{1,4},{0,0},{0,0}},
{{0,0},{0,0},{0,0},{0,0}}},
{{{0,0},{1,4},{1,4},{0,0}},
{{1,4},{1,4},{0,0},{0,0}},
{{0,0},{0,0},{0,0},{0,0}},
{{0,0},{0,0},{0,0},{0,0}}},
{{{1,4},{0,0},{0,0},{0,0}},
{{1,4},{1,4},{0,0},{0,0}},
{{0,0},{1,4},{0,0},{0,0}},
{{0,0},{0,0},{0,0},{0,0}}}
};
private int[, , ,] lShape = {
{{{1,6},{0,0},{0,0},{0,0}},
{{1,6},{0,0},{0,0},{0,0}},
{{1,6},{0,0},{0,0},{0,0}},
{{1,6},{0,0},{0,0},{0,0}}},
{{{0,0},{0,0},{0,0},{0,0}},
{{1,6},{1,6},{1,6},{1,6}},
{{0,0},{0,0},{0,0},{0,0}},
{{0,0},{0,0},{0,0},{0,0}}},
{{{1,6},{0,0},{0,0},{0,0}},
{{1,6},{0,0},{0,0},{0,0}},
{{1,6},{0,0},{0,0},{0,0}},
{{1,6},{0,0},{0,0},{0,0}}},
{{{0,0},{0,0},{0,0},{0,0}},
{{1,6},{1,6},{1,6},{1,6}},
{{0,0},{0,0},{0,0},{0,0}},
{{0,0},{0,0},{0,0},{0,0}}}
};
private int[, , ,] TuShape = {
{{{0,0},{1,7},{0,0},{0,0}},
{{1,7},{1,7},{1,7},{0,0}},
{{0,0},{0,0},{0,0},{0,0}},
{{0,0},{0,0},{0,0},{0,0}}
},
{{{0,0},{1,7},{0,0},{0,0}},
{{1,7},{1,7},{0,0},{0,0}},
{{0,0},{1,7},{0,0},{0,0}},
{{0,0},{0,0},{0,0},{0,0}}
},
{{{0,0},{0,0},{0,0},{0,0}},
{{1,7},{1,7},{1,7},{0,0}},
{{0,0},{1,7},{0,0},{0,0}},
{{0,0},{0,0},{0,0},{0,0}}
},
{{{0,0},{1,7},{0,0},{0,0}},
{{0,0},{1,7},{1,7},{0,0}},
{{0,0},{1,7},{0,0},{0,0}},
{{0,0},{0,0},{0,0},{0,0}}
},
};
private int[, , ,] TianShape = {
{
{{1,5},{1,5},{0,0},{0,0}},
{{1,5},{1,5},{0,0},{0,0}},
{{0,0},{0,0},{0,0},{0,0}},
{{0,0},{0,0},{0,0},{0,0}}
},
{
{{1,5},{1,5},{0,0},{0,0}},
{{1,5},{1,5},{0,0},{0,0}},
{{0,0},{0,0},{0,0},{0,0}},
{{0,0},{0,0},{0,0},{0,0}}
},
{
{{1,5},{1,5},{0,0},{0,0}},
{{1,5},{1,5},{0,0},{0,0}},
{{0,0},{0,0},{0,0},{0,0}},
{{0,0},{0,0},{0,0},{0,0}}
},
{
{{1,5},{1,5},{0,0},{0,0}},
{{1,5},{1,5},{0,0},{0,0}},
{{0,0},{0,0},{0,0},{0,0}},
{{0,0},{0,0},{0,0},{0,0}}
}
};
}
class GameCtrl
{
public List<int[,]> GameBox = new List<int[,]>();
public int gameSclpe;
public GameCtrl()
{
gameSclpe = 0;
this.InitBox();
}
public void InitBox()
{
int[,] InitZore = new int[18, 2] { { 1, 0 }, { 1, 0 }, { 1, 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 }, { 1, 0 }, { 1, 0 }, { 1, 0 }};
int[,] InitOne = new int[18, 2] { { 1, 0 }, { 1, 0 }, { 1, 0 }, { 1, 0 }, { 1, 0 }, { 1, 0 },
{ 1, 0 }, { 1, 0 }, { 1, 0 }, { 1, 0 }, { 1, 0 }, { 1, 0 },
{ 1, 0 }, { 1, 0 }, { 1, 0 }, { 1, 0 }, { 1, 0 }, { 1, 0 } };
GameBox.Add(InitOne);
GameBox.Add(InitOne);
GameBox.Add(InitOne);
for (int Cnt = 0; Cnt < 20; Cnt++)
GameBox.Add(InitZore);
}
public void ClrBox()
{
GameBox.Clear();
}
public void AddBox(Square landSquare)
{
for (int i = 0; i < 4; i++)
{
int rowNum = 22 - landSquare.PositionY - i;
int[,] ShapeRow = new int[18, 2];
for(int j =0;j<18;j++)
{
if (GameBox[rowNum][j, 0] == 1)
{
ShapeRow[j, 0] = 1;
ShapeRow[j, 1] = GameBox[rowNum][j, 1];
}
}
for (int j = 0; j < 4; j++)
{
int colNum = landSquare.PositionX + j+3;
if (landSquare.Shape[landSquare.State, i, j, 0] == 1)
{
ShapeRow[colNum, 0] = 1;
ShapeRow[colNum, 1] = landSquare.Shape[landSquare.State, i, j, 1];
}
}
GameBox[rowNum] = ShapeRow;
}
SubBox();
}
private void SubBox()
{
int[,] InitZore = new int[18, 2] { { 1, 0 }, { 1, 0 }, { 1, 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 }, { 1, 0 }, { 1, 0 }, { 1, 0 }};
int Cnt;
for (Cnt = 3; Cnt < 23; Cnt++)
{
int ColSum,Cnt2;
for (ColSum = 0, Cnt2 = 3; Cnt2 < 15;Cnt2++ )
ColSum += GameBox[Cnt][Cnt2, 0];
if(ColSum==12)
{
this.gameSclpe++;
GameBox.RemoveAt(3);
GameBox.Add(InitZore);
Cnt--;
}
}
}
public void BoxDraw(ref Graphics dc)
{
for (int Cnt1 = 3; Cnt1 < 23; Cnt1++)
{
for (int Cnt2 = 3; Cnt2 < 15; Cnt2++)
{
if (GameBox[Cnt1][Cnt2, 0] == 1)
{
SolidBrush Brush;
switch (GameBox[Cnt1][Cnt2, 1])
{
case (1):
Brush = new SolidBrush(Color.Red);
break;
case (2):
Brush = new SolidBrush(Color.Blue);
break;
case (3):
Brush = new SolidBrush(Color.Yellow);
break;
case (4):
Brush = new SolidBrush(Color.Green);
break;
case (5):
Brush = new SolidBrush(Color.Tan);
break;
case (6):
Brush = new SolidBrush(Color.Honeydew);
break;
case (7):
Brush = new SolidBrush(Color.ForestGreen);
break;
default:
Brush = new SolidBrush(Color.Red);
break;
}
dc.FillRectangle(Brush, new Rectangle((Cnt2-3)* 16, (22 - Cnt1) * 16, 16, 16));
}
}
}
}
}
}
更多俄罗斯方块精彩文章请点击专题:俄罗斯方块游戏集合 进行学习。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
# C#
# 俄罗斯方块
# C#实现俄罗斯方块基本功能
# C#实现简单俄罗斯方块
# C# 实现俄罗斯方块(附源码)
# 请点击
# 写了
# 大家多多
# lShape
# LeftZShape
# RightZShape
# default
# TianShape
# TuShape
# Shape
# case
# switch
# RightLShape
# break
# LeftLShape
# private
# SolidBrush
# Red
# Color
相关文章:
如何设置并定期更换建站之星安全管理员密码?
厦门模型网站设计制作公司,厦门航空飞机模型掉色怎么办?
创业网站制作流程,创业网站可靠吗?
建站之星安装模板失败:服务器环境不兼容?
如何在西部数码注册域名并快速搭建网站?
ui设计制作网站有哪些,手机UI设计网址吗?
简历在线制作网站免费版,如何创建个人简历?
建站之星安装后如何配置SEO及设计样式?
宝塔建站后网页无法访问如何解决?
如何快速上传自定义模板至建站之星?
内部网站制作流程,如何建立公司内部网站?
专业网站制作服务公司,有哪些网站可以免费发布招聘信息?
安徽网站建设与外贸建站服务专业定制方案
c++怎么使用类型萃取type_traits_c++ 模板元编程类型判断【方法】
专业企业网站设计制作公司,如何理解商贸企业的统一配送和分销网络建设?
如何选择高效便捷的WAP商城建站系统?
如何高效配置IIS服务器搭建网站?
贸易公司网站制作流程,出口贸易网站设计怎么做?
如何在建站宝盒中设置产品搜索功能?
昆明网站制作哪家好,昆明公租房申请网上登录入口?
建站主机CVM配置优化、SEO策略与性能提升指南
怀化网站制作公司,怀化新生儿上户网上办理流程?
网站专业制作公司有哪些,做一个公司网站要多少钱?
英语简历制作免费网站推荐,如何将简历翻译成英文?
保定网站制作方案定制,保定招聘的渠道有哪些?找工作的人一般都去哪里看招聘信息?
关于BootStrap modal 在IOS9中不能弹出的解决方法(IOS 9 bootstrap modal ios 9 noticework)
外汇网站制作流程,如何在工商银行网站上做外汇买卖?
广州建站公司哪家好?十大优质服务商推荐
PHP 500报错的快速解决方法
香港网站服务器数量如何影响SEO优化效果?
北京网站制作费用多少,建立一个公司网站的费用.有哪些部分,分别要多少钱?
已有域名和空间,如何快速搭建网站?
已有域名如何快速搭建专属网站?
如何通过VPS建站实现广告与增值服务盈利?
如何在七牛云存储上搭建网站并设置自定义域名?
如何快速生成ASP一键建站模板并优化安全性?
b2c电商网站制作流程,b2c水平综合的电商平台?
建站之星展会模板:智能建站与自助搭建高效解决方案
如何通过西部建站助手安装IIS服务器?
建站10G流量真的够用吗?如何应对访问高峰?
长春网站建设制作公司,长春的网络公司怎么样主要是能做网站的?
C++如何编写函数模板?(泛型编程入门)
建站主机选购指南与交易推荐:核心配置解析
赚钱网站制作软件,建一个网站怎样才能赚钱?是如何盈利的?
制作充值网站的软件,做人力招聘为什么要自己交端口钱?
北京网站制作网页,网站升级改版需要多久?
重庆市网站制作公司,重庆招聘网站哪个好?
如何正确下载安装西数主机建站助手?
西安大型网站制作公司,西安招聘网站最好的是哪个?
商务网站制作工程师,从哪几个方面把握电子商务网站主页和页面的特色设计?
*请认真填写需求信息,我们会在24小时内与您取得联系。