基于C#实现俄罗斯方块游戏
程序员文章站
2023-11-17 10:17:28
最近在看c#,写了一个很水的方块游戏练手。
代码:
namespace game
{
class square
{
public squar...
最近在看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)); } } } } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。