基于C#实现俄罗斯方块游戏

最近在看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));
     }
    }
   }

  }
 }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • C#在Unity游戏开发中进行多线程编程的方法

    在这之前,有很多人在质疑Unity支不支持多线程,事实上Unity是支持多线程的.而提到多线程就要提到Unity非常常用的协程,然而协程并非真正的多线程.协程其实是等某个操作完成之后再执行后面的代码,或者说是控制代码在特定的时机执行.而多线程在Unity渲染和复杂逻辑运算时可以高效的使用多核CPU,帮助程序可以更高效的运行.本篇主要介绍在Unity中如何使用多线程. 首先引入C#中使用多线程的类库 using System.Threading; 创建线程实例的四种方式 一.线程执行无参方法 构造

  • C#实现的算24点游戏算法实例分析

    本文实例讲述了C#实现的算24点游戏算法.分享给大家供大家参考.具体如下: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace Calc24Points { public class Cell { public enum Type { Number, Signal } public int Number; public ch

  • C#实现简单的井字游戏实例

    本文实例讲述了C#实现简单的井字游戏.分享给大家供大家参考.具体如下: /* * Created using: SharpDevelop * Created by: Tony Misner * Date: 1/2/2007 * Time: 2:34 PM * */ using System; using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; namespace TicTacToe

  • C#拼图游戏编写代码(2)

    前言:在C#拼图游戏编写代码程序设计 之 C#实现<拼图游戏>(上),上传了各模块代码,而在本文中将详细剖析原理,使读者更容易理解并学习,程序有诸多问题,欢迎指出,共同学习成长! 正文: 拼图是一个非常经典的游戏,基本每个人都知道他的玩法,他的开始,运行,结束.那么,当我们想要做拼图的时候如何入手呢?答案是:从现实出发,去描述需求(尽量描述为文档),当我们拥有了全面的需求,就能够提供可靠的策略,从而在代码中实现,最终成为作品! (一)需求: (这个需求书写较为潦草,为广大小白定制,按照最最最普

  • C#面向对象编程之猜拳游戏实现方法

    本文实例讲述了C#面向对象编程之猜拳游戏实现方法.分享给大家供大家参考.具体实现方法如下: 1.需求 现在要制作一个游戏,玩家与计算机进行猜拳游戏,玩家出拳,计算机出拳,计算机自动判断输赢. 2.需求分析 根据需求,来分析一下对象,可分析出:玩家对象(Player).计算机对象(Computer).裁判对象(Judge). 玩家出拳由用户控制,使用数字代表:1石头.2剪子.3布 计算机出拳由计算机随机产生 裁判根据玩家与计算机的出拳情况进行判断输赢. 3.类对象的实现 ①.玩家类示例代码: 复制

  • C#实现的24点游戏实例详解

    本文实例分析了C#实现的24点游戏.分享给大家供大家参考.具体如下: 1. 24点游戏规则及算法 规则:给出4个自然数,找出能够求出24的四则运算式,要求数字不能重复使用 分析: 本算法是一种暴力求解法: 给出任意两个数字,可以进行6种四则运算,求出最多6个值.以数字a和b为例,有: 加(a+b).减(a-b).被减(b-a).乘以(a*b).除以(a/b)和除(b/a) abcd共计四个数,如果顺序固定,则有5种计算顺序(★代表上面6种四则运算中的一种): ((a★b)★c)★d.(a★b)★

  • C#十五子游戏编写代码

    本文实例为大家分享了C#十五子游戏的具体代码,供大家参考,具体内容如下 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms;

  • C#实现洗牌游戏实例

    棋牌类游戏是目前比较火的游戏之一.今天本文就以实例形式实现洗牌游戏.本文实例所采用的算法是:遍历每个位置上的牌,然后与随机位置上的牌交换. 运行结果如下图所示: 对于牌来讲,2个关键的因素是面值和类型(如红桃.梅花等). 代码如下: public class Card { private string mianzhi; private string leixin; public Card(string m, string l) { mianzhi = m; leixin = l; } publi

  • C#拼图游戏编写代码

    本文设计了C#拼图游戏程序,供大家参考,具体内容如下 功能描述: 1.用户自定义上传图片 2.游戏难度选择:简单(3*3).一般(5*5).困难(9*9)三个级别 3.纪录完成步数 模块: 1.拼图类 2.配置类 3.游戏菜单窗口 4.游戏运行窗口 代码文件VS2013版本: 下载链接: 拼图游戏 --------------------------------------------------我叫分割线---------------------------------------------

  • C#利用控件拖拽技术制作拼图游戏

    主要实现的功能: 1.程序附带多张拼图随机拼图. 2.可手动添加拼图. 3.游戏成功判断. 4.30秒超时判断. Puzzle.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms

随机推荐