⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 一个简单的C# 做的俄罗斯方块
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace MyRussianRock
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Timer timer1;
		private System.ComponentModel.IContainer components;
		private int[,] table=new int[15,15];
		private int x,y;//方块的坐标
	//	private int[] shape=new int[9]{1,2,3,4,5,6,7,8,9};//nine type of block
		private int currentshape;//当前的形状
		public void block (int i)//i 为方块的形状
		{   
			x=150;//初始的坐标
			y=10;
			switch(i)
			{
				case 1:currentshape=1;//田字形
					paintBlock(1,Color.Yellow);
					break;
				case 2:currentshape=2;//T字形
					paintBlock(2,Color.Yellow);
					break;
				case 3:currentshape=3;//长条形
					paintBlock(3,Color.Yellow);
					break;
				case 4:currentshape=4;//Z字形
					paintBlock(4,Color.Yellow);
					break;
			}
		}
		private void rotate()//旋转方法
		{
			Graphics g=this.CreateGraphics();
			int a,b;//获得在table中的坐标
			a=(y-10)/20;
			b=(x-10)/20;
				switch(currentshape)
				{
					case 1:break;
					case 2:					
						if(a>=1&&a<=12&&b<=11&&table[a-1,b+1]==0&&table[a+1,b+2]==0&&table[a+1,b]==0&&table[a-1,b]==0)
						{
							paintBlock(2,Color.Black);
							paintBlock(5,Color.Yellow);
						}
						break;
					case 3:					
						if(a<=10&&table[a+1,b]==0&&table[a+2,b]==0&&table[a+3,b]==0)
						{
							paintBlock(3,Color.Black);
							paintBlock(8,Color.Yellow);
						}
						break;
					case 4:
						
						if(a<=10&&b>=1&&table[a+1,b]==0&&table[a+1,b-1]==0&&table[a+2,b-1]==0)
						{
							paintBlock(4,Color.Black);
							paintBlock(9,Color.Yellow);
						}
						break;
					case 5:
						if(a<=12&&b>=1&&table[a+1,b]==0&&table[a+1,b-1]==0)
						{
							paintBlock(5,Color.Black);
							paintBlock(6,Color.Yellow);
						}
						break;
					case 6:
						
						if(a<=11&&table[a+2,b]==0)
						{
							paintBlock(6,Color.Black);
							paintBlock(7,Color.Yellow);
						}
						break;
					case 7:
						if(b<=12&&table[a,b+1]==0&&table[a,b+2]==0)
						{
							paintBlock(7,Color.Black);
							paintBlock(2,Color.Yellow);
						}
						break;
					case 8:
						if(b<=10&&table[a,b+1]==0&&table[a,b+2]==0&&table[a,b+3]==0)
						{
							paintBlock(8,Color.Black);
							paintBlock(3,Color.Yellow);
						}
						break;
					case 9:
						if(a<=12&&b<=11&&table[a+1,b+1]==0&&table[a+1,b+2]==0)
						{
							paintBlock(9,Color.Black);
							paintBlock(4,Color.Yellow);
						}
						break;
				} 
			}
		private int getRandomBlock()//得到随机的形状
		{
			Random rd=new Random();
			return rd.Next(1,4);//
		}
		public Form1()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();
			//this.KeyPress+=new KeyPressEventHandler(Form1_KeyPress);
			this.KeyDown+=new KeyEventHandler(Form1_KeyDown);
			inite();
			block(getRandomBlock());
			this.timer1.Start();
			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			this.timer1 = new System.Windows.Forms.Timer(this.components);
			// 
			// timer1
			// 
			this.timer1.Enabled = true;
			this.timer1.Interval = 500;
			this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(320, 317);
			this.Enabled = false;
			this.Name = "Form1";
			this.Text = "Form1";
		}
		#endregion

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main() 
		{   
			Application.Run(new Form1());
		}
		protected override void OnPaint(PaintEventArgs e)
		{  
			base.OnPaint (e);
			Graphics g=e.Graphics;
			g.FillRectangle(new SolidBrush(Color.Blue),10,10,300,300);
			g.FillRectangle(new SolidBrush(Color.Black),30,10,260,280);	
			for(int i=0;i<14;i++)
				for(int j=1;j<14;j++)
				{
					if(table[i,j]==1)
						g.FillRectangle(new SolidBrush(Color.Yellow),j*20+10,i*20+10,20,20);
				}
		}
		private void paintBlock(int z,Color w)//画方块的函数
		{
			//  if(x>13||x<0||y<1||y>13) return;
			Graphics g=this.CreateGraphics();
			int a,b;//in table
			int c;
			Color d;
			if(w==Color.Black)
			{
				c=0;
			    d=Color.Black;
			}
			else
			{
				c=1;
				d=Color.Green;
			}
			Pen drawPen=new Pen(d,2);
			SolidBrush sBrush=new SolidBrush(w);
			switch(z)
			{
				case 1: if(x>=30&&x<=250&&y>=10&&y<=250)
						{
							a=(y-10)/20;
							b=(x-10)/20;
							g.FillRectangle(sBrush,x,y,40,40);
                            g.DrawRectangle(drawPen,x,y,40,20);
							g.DrawRectangle(drawPen,x,y+20,40,20);
							g.DrawRectangle(drawPen,x+20,y,20,40);
							table[a,b]=c;
							table[a+1,b]=c;
							table[a,b+1]=c;
							table[a+1,b+1]=c;
							currentshape=1;
						}
					break;
				case 2: if(x>=30&&x<=230&&y>=10&&y<=250)
						{
							g.FillRectangle(sBrush,x,y,60,20);
							g.FillRectangle(sBrush,x+20,y+20,20,20);
							g.DrawRectangle(drawPen,x,y,60,20);
							g.DrawRectangle(drawPen,x+20,y,20,40);
							a=(y-10)/20;
							b=(x-10)/20;
							table[a,b]=c;
							table[a,b+1]=c;
							table[a,b+2]=c;
							table[a+1,b+1]=c;
							currentshape=2;
						}
					break;
				case 3:if(x>=30&&x<=210&&y>=10&&y<=270)
					   {
						   g.FillRectangle(sBrush,x,y,80,20);
						   g.DrawRectangle(drawPen,x,y,80,20);
						   g.DrawRectangle(drawPen,x+20,y,20,20);
						   g.DrawRectangle(drawPen,x+40,y,20,20);
						   a=(y-10)/20;
						   b=(x-10)/20;
						   table[a,b]=c;
						   table[a,b+1]=c;
						   table[a,b+2]=c;
						   table[a,b+3]=c;
						   currentshape=3;
					   }
					break;	
				case 4:if(x>=30&&x<=230&&y>=10&&y<=250)
					   {
						   g.FillRectangle(sBrush,x,y,40,20);
						   g.FillRectangle(sBrush,x+20,y+20,40,20);
						   g.DrawRectangle(drawPen,x,y,40,20);
						   g.DrawRectangle(drawPen,x+20,y+20,40,20);
						   g.DrawRectangle(drawPen,x+20,y,20,40);
						   a=(y-10)/20;
						   b=(x-10)/20;
						   table[a,b]=c;
						   table[a,b+1]=c;
						   table[a+1,b+1]=c;
						   table[a+1,b+2]=c;
						   currentshape=4;
					   }
					break;
				case 5:if(x>=30&&x<=250&&y>=30&&y<=250)
					   {
						   g.FillRectangle(sBrush,x,y,20,20);
						   g.FillRectangle(sBrush,x+20,y-20,20,60);
						   g.DrawRectangle(drawPen,x,y,40,20);
						   g.DrawRectangle(drawPen,x+20,y-20,20,60);
						   a=(y-10)/20;
						   b=(x-10)/20;
						   table[a,b]=c;
						   table[a-1,b+1]=c;
						   table[a,b+1]=c;
						   table[a+1,b+1]=c;
						   currentshape=5;
					   }
					break;
				case 6:if(x>=50&&x<=250&&y>=10&&y<=250)
					   {
						   g.FillRectangle(sBrush,x,y,20,20);
						   g.FillRectangle(sBrush,x-20,y+20,60,20);
						   g.DrawRectangle(drawPen,x,y,20,40);
						   g.DrawRectangle(drawPen,x-20,y+20,60,20);
						   a=(y-10)/20;
						   b=(x-10)/20;
						   table[a,b]=c;
						   table[a+1,b-1]=c;
						   table[a+1,b]=c;
						   table[a+1,b+1]=c;
						   currentshape=6;
					   }
					break;
				case 7:if(x>=30&&x<=250&&y>=10&&y<=230)
					   {
						   g.FillRectangle(sBrush,x,y,20,60);
						   g.FillRectangle(sBrush,x+20,y+20,20,20);
						   g.DrawRectangle(drawPen,x,y,20,60);
						   g.DrawRectangle(drawPen,x,y+20,40,20);
						   a=(y-10)/20;
						   b=(x-10)/20;
						   table[a,b]=c;
						   table[a+1,b]=c;
						   table[a+2,b]=c;
						   table[a+1,b+1]=c;
						   currentshape=7;
					   }
					break;
				case 8:if(x>=30&&x<=270&&y>=10&&y<=210)
					   {
						   g.FillRectangle(sBrush,x,y,20,80);
                           g.DrawRectangle(drawPen,x,y,20,80);
                           g.DrawRectangle(drawPen,x,y+20,20,20);
						   g.DrawRectangle(drawPen,x,y+40,20,20);
						   a=(y-10)/20;
						   b=(x-10)/20;
						   table[a,b]=c;
						   table[a+1,b]=c;
						   table[a+2,b]=c;
						   table[a+3,b]=c;
						   currentshape=8;
					   }
					break;
				case 9:if(x>=30&&x<=270&&y>=30&&y<=250)
					   {
						   g.FillRectangle(sBrush,x,y,20,40);
						   g.FillRectangle(sBrush,x+20,y-20,20,40);
						   g.DrawRectangle(drawPen,x,y,20,40);
						   g.DrawRectangle(drawPen,x+20,y-20,20,40);
						   g.DrawRectangle(drawPen,x,y,40,20);
						   a=(y-10)/20;
						   b=(x-10)/20;
						   table[a,b]=c;
						   table[a,b+1]=c;
						   table[a+1,b]=c;
						   table[a-1,b+1]=c;
						   currentshape=9;
					   }
					break;
			}
			g.Dispose();
		}
		private void dropDown()//下落方法
		{
			Graphics g=this.CreateGraphics();	
			switch(currentshape)
			{
				case 1: if(x>=30&&x<=250&&y>=10&&y<=230)
						{
							paintBlock(1,Color.Black);
							y+=20;
							paintBlock(1,Color.Yellow);
						}
					break;
				case 2: if(x>=30&&x<=230&&y>=10&&y<=230)
						{
							paintBlock(2,Color.Black);
							y+=20;
							paintBlock(2,Color.Yellow);
						}
					break;
				case 3: if(x>=30&&x<=210&&y>=10&&y<=250)
						{
							paintBlock(3,Color.Black);
							y+=20;
							paintBlock(3,Color.Yellow);
						}
					break;
				case 4: if(x>=30&&x<=210&&y>=10&&y<=230)
						{
							paintBlock(4,Color.Black);
							y+=20;
							paintBlock(4,Color.Yellow);
						}
					break;
				case 5:if(x>=30&&x<=250&&y>=30&&y<=230)
					   {
						   paintBlock(5,Color.Black);
						   y+=20;
						   paintBlock(5,Color.Yellow);
					   }
					break;
				case 6:if(x>=50&&x<=250&&y>=10&&y<=230)
					   {
						   paintBlock(6,Color.Black);
						   y+=20;
						   paintBlock(6,Color.Yellow);
					   }
					break;
				case 7:if(x>=30&&x<=250&&y>=10&&y<=210)
					   {
						   paintBlock(7,Color.Black);
						   y+=20;
						   paintBlock(7,Color.Yellow);
					   }
					break;
				case 8:if(x>=30&&x<=270&&y>=10&&y<=190)
					   {
						   paintBlock(8,Color.Black);
						   y+=20;
						   paintBlock(8,Color.Yellow);
					   }
					break;
				case 9:if(x>=30&&x<=250&&y>=30&&y<=230)
					   {
						   paintBlock(9,Color.Black);
						   y+=20;
						   paintBlock(9,Color.Yellow);
					   }
					break;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -