C# 消除游戏

就是练成3个或者以上就消除的游戏我用C# 没事写的, 可能有漏洞, 主要是练习C#的绘图
代码:

//Program.cs
using System;
using System.Windows.Forms;

namespace ColorRect
{
    /// <summary>
    /// Class with program entry point.
    /// </summary>
    internal sealed class Program
    {
        /// <summary>
        /// Program entry point.
        /// </summary>
        [STAThread]
        private static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());
        }

    }
}
//MainForm.cs
using System;
using System.Collections.Generic;
using System.Drawing.Drawing2D;
using System.Drawing;
using System.Windows.Forms;

namespace ColorRect
{
    /// <summary>
    /// Description of MainForm.
    /// </summary>
    public partial class MainForm : Form
    {
        private int i,j,width = 60,height = 60;
        private Graphics graph;
        private Rectangle[,] rects = new Rectangle[10,10];
        private SolidBrush[] brushes = new SolidBrush[7];
        private int[,] Map = new int[10,10];
        private Random ra = new Random();
        private int posx,posy,cha,chb;

        public MainForm()
        {
            //
            // The InitializeComponent() call is required for Windows Forms designer support.
            //
            InitializeComponent();

            //
            // TODO: Add constructor code after the InitializeComponent() call.
            //
        }

        void 推出ToolStripMenuItemClick(object sender, EventArgs e)
        {
            this.Dispose(true);
        }

        void Rebuild(){
            for(i=0;i<8;i++)
                for(j=0;j<8;j++)
                    if(Map[i,j] == -1)
                        Map[i,j] = ra.Next(0,7);
        }

        void ReBuild2(){
            for(i=0;i<8;i++)
                for(j=0;j<8;j++)
                    if(Map[i,j] == -1){
                int tmp = ra.Next(0,7);
                while(tmp==Map[i+1,j]||tmp==Map[i,j+1])
                    tmp = ra.Next(0,7);
                Map[i,j] = tmp;
            }
        }

        void Init(object sender, EventArgs e){
            this.graph = this.CreateGraphics();
        }

        void Down(){
            int h, t;
            for(j = 0; j < 8; j++) {
                h = 7;
                for(i = 7; i >= 0; i--) {
                    t = Map[i,j];    Map[i,j] = 0;
                    if(t>-1)   Map[h--,j] = t;
                }
            }
        }

        void InitMap(){
            for(i=0;i<8;i++)
                for(j=0;j<8;j++)
                    Map[i,j] = Convert.ToInt32(ra.Next(0,7));
            RePaint();
            DrawFramework();
        }

        void DrawFramework(){
            Pen p = new Pen(Color.Black,5);
            graph.DrawLine(p,0,0,0,590);
            graph.DrawLine(p,60,0,60,590);
            graph.DrawLine(p,120,0,120,590);
            graph.DrawLine(p,180,0,180,590);
            graph.DrawLine(p,240,0,240,590);
            graph.DrawLine(p,300,0,300,590);
            graph.DrawLine(p,360,0,360,590);
            graph.DrawLine(p,420,0,420,590);
            graph.DrawLine(p,480,0,480,590);
            graph.DrawLine(p,0,25,590,25);
            graph.DrawLine(p,0,85,590,85);
            graph.DrawLine(p,0,145,590,145);
            graph.DrawLine(p,0,205,590,205);
            graph.DrawLine(p,0,265,590,265);
            graph.DrawLine(p,0,325,590,325);
            graph.DrawLine(p,0,385,590,385);
            graph.DrawLine(p,0,445,590,445);
            graph.DrawLine(p,0,505,590,505);
        }

        void FillRectangle(){
            for(i=0;i<8;i++)
                for(j=0;j<8;j++)
                        graph.FillRectangle(brushes[Map[i,j]],rects[i,j]);
        }

        void SwapColor(int x,int y,int x2,int y2){
            int tmp = Map[x,y];
            Map[x,y] = Map[x2,y2];
            Map[x2,y2] = tmp;
        }

        bool CheckDown(){
            bool[,] vis = new bool[10,10];
            int res = 0;
            for(i = 0; i < 8; i++)
                for(j = 0; j < 8; j++) {
                    if(j < 6) if( Map[i,j] > -1 && Map[i,j] == Map[i,j + 1] && Map[i,j + 1] == Map[i,j + 2])
                        vis[i,j] = vis[i,j + 1] = vis[i,j + 2] = true;
                    if(i < 6) if(Map[i,j] > -1 && Map[i,j] == Map[i + 1,j] && Map[i + 1,j] == Map[i + 2,j])
                        vis[i,j] = vis[i + 1,j] = vis[i + 2,j] = true;
                }
            for(i = 0; i < 8; i++)
                for(j = 0; j < 8; j++)
                    if(vis[i,j]){ Map[i,j] = -1; res++;}
            return res>0;
        }

        void DrawCheck(){
            if(cha!=-1){
                graph.FillRectangle(new SolidBrush(Color.Goldenrod),60*cha+15,60*chb+40,30,30);
            }
        }

        void DrawMouse(){
            graph.FillRectangle(new SolidBrush(Color.Purple),60*posx+15,60*posy+40,30,30);
        }

        void Checked(int x,int y){
            if(cha!=-1){
                if(x == cha&&(y-1==chb||y+1==chb)){
                    SwapColor(cha,chb,x,y);
                    if(CheckDown() == false)
                        SwapColor(cha,chb,x,y);
                    else{
                        RePaint();
                        cha = -1; chb = -1;
                    }
                }else if(y == chb&&(x+1==cha||x-1==cha)){
                    SwapColor(cha,chb,x,y);
                    if(CheckDown() == false)
                        SwapColor(cha,chb,x,y);
                    else{
                        RePaint();
                        cha = -1; chb = -1;
                    }
                }else{
                    cha = x; chb = y;
                }
            }else{
                cha = x;
                chb = y;
            }
        }

        void RePaint(){
            //InitMap();
            bool flag = true;
            do{
                Down();
                ReBuild2();
            }while(CheckDown()&&!flag);
            //graph.FillRectangle(new SolidBrush(Color.White),0,0,600,700);
            ReBuild2();
            FillRectangle();
            DrawFramework();
            DrawCheck();
            DrawMouse();
        }

        void IT(){
                for(i=0;i<8;i++)for(j=0;j<8;j++){rects[i,j] = new Rectangle(i*width,j*height+25,i*width+width,j*height+height+25);}
        }

        void 新游戏ToolStripMenuItemClick(object sender, EventArgs e)
        {
            Rectangle rect1 = new Rectangle(0,0,500,500);
            Brush col = new SolidBrush(Color.White);
            graph.FillRectangle(col,rect1);

            IT();

            brushes[0] = new SolidBrush(Color.White);
            brushes[1] = new SolidBrush(Color.Red);
            brushes[2] = new SolidBrush(Color.Blue);
            brushes[3] = new SolidBrush(Color.Gray);
            brushes[4] = new SolidBrush(Color.Yellow);
            brushes[5] = new SolidBrush(Color.Green);
            brushes[6] = new SolidBrush(Color.Pink);

            InitMap();

            if(CheckDown()){
                Down();
                ReBuild2();
            }

            FillRectangle();
            DrawFramework();

            posx = 0; posy = 0;
            cha = -1; chb = -1;
            DrawMouse();
        }

        void KeyUpListener(object sender,KeyEventArgs e){
            string key = e.KeyCode.ToString();
            if(key == "Up"){
                if(posy > 0)
                    posy--;
            }
            else if(key == "Down"){
                if(posy < 7)
                    posy++;
            }
            else if(key == "Right"){
                if(posx < 7)
                    posx++;
            }
            else if(key == "Left"){
                if(posx>0)
                    posx--;
            }
            else if(key == "Space"){
                Checked(posx,posy);
            }
            RePaint();
        }
    }
}
//MainForm.Designer
namespace ColorRect
{
    partial class MainForm
    {
        /// <summary>
        /// Designer variable used to keep track of non-visual components.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Disposes resources used by the form.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing) {
                if (components != null) {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        /// <summary>
        /// This method is required for Windows Forms designer support.
        /// Do not change the method contents inside the source code editor. The Forms designer might
        /// not be able to load this method if it was changed manually.
        /// </summary>
        private void InitializeComponent()
        {
            this.statusStrip1 = new System.Windows.Forms.StatusStrip();
            this.menuStrip1 = new System.Windows.Forms.MenuStrip();
            this.游戏ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.新游戏ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.推出ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.帮助ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.帮助ToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
            this.关于ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.toolStripStatusLabel1 = new System.Windows.Forms.ToolStripStatusLabel();
            this.statusStrip1.SuspendLayout();
            this.menuStrip1.SuspendLayout();
            this.SuspendLayout();
            //
            // statusStrip1
            //
            this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
                                    this.toolStripStatusLabel1});
            this.statusStrip1.Location = new System.Drawing.Point(0, 505);
            this.statusStrip1.Name = "statusStrip1";
            this.statusStrip1.Size = new System.Drawing.Size(481, 22);
            this.statusStrip1.TabIndex = 0;
            this.statusStrip1.Text = "statusStrip1";
            //
            // menuStrip1
            //
            this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
                                    this.游戏ToolStripMenuItem,
                                    this.帮助ToolStripMenuItem});
            this.menuStrip1.Location = new System.Drawing.Point(0, 0);
            this.menuStrip1.Name = "menuStrip1";
            this.menuStrip1.Size = new System.Drawing.Size(481, 25);
            this.menuStrip1.TabIndex = 1;
            this.menuStrip1.Text = "menuStrip1";
            //
            // 游戏ToolStripMenuItem
            //
            this.游戏ToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
                                    this.新游戏ToolStripMenuItem,
                                    this.推出ToolStripMenuItem});
            this.游戏ToolStripMenuItem.Name = "游戏ToolStripMenuItem";
            this.游戏ToolStripMenuItem.Size = new System.Drawing.Size(44, 21);
            this.游戏ToolStripMenuItem.Text = "游戏";
            //
            // 新游戏ToolStripMenuItem
            //
            this.新游戏ToolStripMenuItem.Name = "新游戏ToolStripMenuItem";
            this.新游戏ToolStripMenuItem.Size = new System.Drawing.Size(112, 22);
            this.新游戏ToolStripMenuItem.Text = "新游戏";
            this.新游戏ToolStripMenuItem.Click += new System.EventHandler(this.新游戏ToolStripMenuItemClick);
            //
            // 推出ToolStripMenuItem
            //
            this.推出ToolStripMenuItem.Name = "推出ToolStripMenuItem";
            this.推出ToolStripMenuItem.Size = new System.Drawing.Size(112, 22);
            this.推出ToolStripMenuItem.Text = "退出";
            this.推出ToolStripMenuItem.Click += new System.EventHandler(this.推出ToolStripMenuItemClick);
            //
            // 帮助ToolStripMenuItem
            //
            this.帮助ToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
                                    this.帮助ToolStripMenuItem1,
                                    this.关于ToolStripMenuItem});
            this.帮助ToolStripMenuItem.Name = "帮助ToolStripMenuItem";
            this.帮助ToolStripMenuItem.Size = new System.Drawing.Size(44, 21);
            this.帮助ToolStripMenuItem.Text = "帮助";
            //
            // 帮助ToolStripMenuItem1
            //
            this.帮助ToolStripMenuItem1.Name = "帮助ToolStripMenuItem1";
            this.帮助ToolStripMenuItem1.Size = new System.Drawing.Size(152, 22);
            this.帮助ToolStripMenuItem1.Text = "帮助";
            //
            // 关于ToolStripMenuItem
            //
            this.关于ToolStripMenuItem.Name = "关于ToolStripMenuItem";
            this.关于ToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            this.关于ToolStripMenuItem.Text = "关于";
            //
            // toolStripStatusLabel1
            //
            this.toolStripStatusLabel1.Name = "toolStripStatusLabel1";
            this.toolStripStatusLabel1.Size = new System.Drawing.Size(131, 17);
            this.toolStripStatusLabel1.Text = "toolStripStatusLabel1";
            //
            // MainForm
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(481, 527);
            this.Controls.Add(this.statusStrip1);
            this.Controls.Add(this.menuStrip1);
            this.MainMenuStrip = this.menuStrip1;
            this.Name = "MainForm";
            this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
            this.Text = "ColorRect";
            this.Shown += new System.EventHandler(this.Init);
            this.statusStrip1.ResumeLayout(false);
            this.statusStrip1.PerformLayout();
            this.menuStrip1.ResumeLayout(false);
            this.menuStrip1.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();
        }
        private System.Windows.Forms.ToolStripMenuItem 关于ToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem 帮助ToolStripMenuItem1;
        private System.Windows.Forms.ToolStripMenuItem 帮助ToolStripMenuItem;
        private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel1;
        private System.Windows.Forms.ToolStripMenuItem 推出ToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem 新游戏ToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem 游戏ToolStripMenuItem;
        private System.Windows.Forms.MenuStrip menuStrip1;
        private
//MainForm.resx
<?xml version="1.0" encoding="utf-8"?>
<root>
  <!--
    Microsoft ResX Schema

    Version 2.0

    The primary goals of this format is to allow a simple XML format
    that is mostly human readable. The generation and parsing of the
    various data types are done through the TypeConverter classes
    associated with the data types.

    Example:

    ... ado.net/XML headers & schema ...
    <resheader name="resmimetype">text/microsoft-resx</resheader>
    <resheader name="version">2.0</resheader>
    <resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
    <resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
    <data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
    <data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
    <data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
        <value>[base64 mime encoded serialized .NET Framework object]</value>
    </data>
    <data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
        <value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
        <comment>This is a comment</comment>
    </data>

    There are any number of "resheader" rows that contain simple
    name/value pairs.

    Each data row contains a name, and value. The row also contains a
    type or mimetype. Type corresponds to a .NET class that support
    text/value conversion through the TypeConverter architecture.
    Classes that don't support this are serialized and stored with the
    mimetype set.

    The mimetype is used for serialized objects, and tells the
    ResXResourceReader how to depersist the object. This is currently not
    extensible. For a given mimetype the value must be set accordingly:

    Note - application/x-microsoft.net.object.binary.base64 is the format
    that the ResXResourceWriter will generate, however the reader can
    read any of the formats listed below.

    mimetype: application/x-microsoft.net.object.binary.base64
    value   : The object must be serialized with
            : System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
            : and then encoded with base64 encoding.

    mimetype: application/x-microsoft.net.object.soap.base64
    value   : The object must be serialized with
            : System.Runtime.Serialization.Formatters.Soap.SoapFormatter
            : and then encoded with base64 encoding.

    mimetype: application/x-microsoft.net.object.bytearray.base64
    value   : The object must be serialized into a byte array
            : using a System.ComponentModel.TypeConverter
            : and then encoded with base64 encoding.
    -->
  <xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
    <xsd:element name="root" msdata:IsDataSet="true">
      <xsd:complexType>
        <xsd:choice maxOccurs="unbounded">
          <xsd:element name="metadata">
            <xsd:complexType>
              <xsd:sequence>
                <xsd:element name="value" type="xsd:string" minOccurs="0" />
              </xsd:sequence>
              <xsd:attribute name="name" use="required" type="xsd:string" />
              <xsd:attribute name="type" type="xsd:string" />
              <xsd:attribute name="mimetype" type="xsd:string" />
              <xsd:attribute ref="xml:space" />
            </xsd:complexType>
          </xsd:element>
          <xsd:element name="assembly">
            <xsd:complexType>
              <xsd:attribute name="alias" type="xsd:string" />
              <xsd:attribute name="name" type="xsd:string" />
            </xsd:complexType>
          </xsd:element>
          <xsd:element name="data">
            <xsd:complexType>
              <xsd:sequence>
                <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
                <xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
              </xsd:sequence>
              <xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
              <xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
              <xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
              <xsd:attribute ref="xml:space" />
            </xsd:complexType>
          </xsd:element>
          <xsd:element name="resheader">
            <xsd:complexType>
              <xsd:sequence>
                <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
              </xsd:sequence>
              <xsd:attribute name="name" type="xsd:string" use="required" />
            </xsd:complexType>
          </xsd:element>
        </xsd:choice>
      </xsd:complexType>
    </xsd:element>
  </xsd:schema>
  <resheader name="resmimetype">
    <value>text/microsoft-resx</value>
  </resheader>
  <resheader name="version">
    <value>2.0</value>
  </resheader>
  <resheader name="reader">
    <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
  </resheader>
  <resheader name="writer">
    <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
  </resheader>
  <metadata name="statusStrip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
    <value>17, 17</value>
  </metadata>
  <metadata name="menuStrip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
    <value>143, 17</value>
  </metadata>
</root>
//ColorRect.csproj
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build">
  <PropertyGroup>
    <ProjectGuid>{DE63FB41-2BB2-4F04-B676-3D5B90D75344}</ProjectGuid>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <OutputType>WinExe</OutputType>
    <RootNamespace>ColorRect</RootNamespace>
    <AssemblyName>ColorRect</AssemblyName>
    <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
    <TargetFrameworkProfile>Client</TargetFrameworkProfile>
    <AppDesignerFolder>Properties</AppDesignerFolder>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Platform)' == 'AnyCPU' ">
    <PlatformTarget>x86</PlatformTarget>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
    <OutputPath>bin\Debug\</OutputPath>
    <DebugSymbols>True</DebugSymbols>
    <DebugType>Full</DebugType>
    <Optimize>False</Optimize>
    <CheckForOverflowUnderflow>True</CheckForOverflowUnderflow>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
    <OutputPath>bin\Release\</OutputPath>
    <DebugSymbols>False</DebugSymbols>
    <DebugType>None</DebugType>
    <Optimize>True</Optimize>
    <CheckForOverflowUnderflow>False</CheckForOverflowUnderflow>
    <DefineConstants>TRACE</DefineConstants>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="System" />
    <Reference Include="System.Core">
      <RequiredTargetFramework>3.5</RequiredTargetFramework>
    </Reference>
    <Reference Include="System.Data" />
    <Reference Include="System.Data.DataSetExtensions">
      <RequiredTargetFramework>3.5</RequiredTargetFramework>
    </Reference>
    <Reference Include="System.Drawing" />
    <Reference Include="System.Windows.Forms" />
    <Reference Include="System.Xml" />
    <Reference Include="System.Xml.Linq">
      <RequiredTargetFramework>3.5</RequiredTargetFramework>
    </Reference>
  </ItemGroup>
  <ItemGroup>
    <Compile Include="MainForm.cs" />
    <Compile Include="MainForm.Designer.cs">
      <DependentUpon>MainForm.cs</DependentUpon>
    </Compile>
    <Compile Include="Program.cs" />
    <Compile Include="Properties\AssemblyInfo.cs" />
  </ItemGroup>
  <ItemGroup>
    <EmbeddedResource Include="MainForm.resx">
      <DependentUpon>MainForm.cs</DependentUpon>
    </EmbeddedResource>
  </ItemGroup>
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

posted on 2015-02-24 22:25  JeremyGuo  阅读(341)  评论(0编辑  收藏  举报

导航