基于C#實(shí)現(xiàn)俄羅斯方塊游戲
最近在看C#,寫了一個(gè)很水的方塊游戲練手。
代碼:
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)
{
//判斷右側(cè)
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)
{
//判斷右側(cè)
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)
{
//判斷右側(cè)
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)
{
//判斷左側(cè)
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));
}
}
}
}
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:C#中SQL參數(shù)傳入空值報(bào)錯(cuò)解決方案
欄 目:C#教程
下一篇:DevExpress GridView自動(dòng)滾動(dòng)效果
本文標(biāo)題:基于C#實(shí)現(xiàn)俄羅斯方塊游戲
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/5690.html
您可能感興趣的文章
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新聞效果的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已安裝軟件變化的方法
- 01-10C#實(shí)現(xiàn)多線程下載文件的方法
- 01-10C#實(shí)現(xiàn)Winform中打開網(wǎng)頁(yè)頁(yè)面的方法
- 01-10C#實(shí)現(xiàn)遠(yuǎn)程關(guān)閉計(jì)算機(jī)或重啟計(jì)算機(jī)的方法
- 01-10C#自定義簽名章實(shí)現(xiàn)方法
- 01-10C#文件斷點(diǎn)續(xù)傳實(shí)現(xiàn)方法
- 01-10winform實(shí)現(xiàn)創(chuàng)建最前端窗體的方法


閱讀排行
- 1C語言 while語句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語言實(shí)現(xiàn)“百馬百擔(dān)”問題方法
- 4C語言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁(yè)無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-10delphi制作wav文件的方法
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10C#中split用法實(shí)例總結(jié)
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 04-02jquery與jsp,用jquery


