基本信息
源码名称:C#做的俄罗斯方块 游戏源码下载
源码大小:0.02M
文件格式:.rar
开发语言:C#
更新时间:2017-07-12
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍

使用C#做的俄罗斯方块小游戏


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace Tetris_CS
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class MainForm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Panel screenPanel;
		private System.Windows.Forms.Panel nextPanel;
		private System.Windows.Forms.Timer timer;
		private System.Windows.Forms.MainMenu mainMenu;
		private System.Windows.Forms.MenuItem menuItem4;
		private System.Windows.Forms.MenuItem gameMenu;
		private System.Windows.Forms.MenuItem startMenu;
		private System.Windows.Forms.MenuItem stopMenu;
		private System.Windows.Forms.MenuItem exitMenu;
		private System.Windows.Forms.MenuItem helpMenu;
		private System.Windows.Forms.MenuItem aboutMenu;
		private System.ComponentModel.IContainer components;
		private Random rndShape = new Random();
		private Shape nextShape;
		private Body mainBody = new Body();
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Label speedLabel;
		private System.Windows.Forms.Label scoreLabel;
		private GAME_STATUS gameStatus;
		
		private int speed;
		private int score;
		private System.Windows.Forms.Label linesLabel;
		private System.Windows.Forms.PictureBox pictureBox1;
		private int lines;

		enum GAME_STATUS {GAME_STOP, GAME_RUN, GAME_OVER};

		public MainForm()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();

			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
			Shape.InitTetrisDefine();
		}

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

		#region Windows Form Designer generated code
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(MainForm));
			this.screenPanel = new System.Windows.Forms.Panel();
			this.nextPanel = new System.Windows.Forms.Panel();
			this.timer = new System.Windows.Forms.Timer(this.components);
			this.mainMenu = new System.Windows.Forms.MainMenu();
			this.gameMenu = new System.Windows.Forms.MenuItem();
			this.startMenu = new System.Windows.Forms.MenuItem();
			this.stopMenu = new System.Windows.Forms.MenuItem();
			this.menuItem4 = new System.Windows.Forms.MenuItem();
			this.exitMenu = new System.Windows.Forms.MenuItem();
			this.helpMenu = new System.Windows.Forms.MenuItem();
			this.aboutMenu = new System.Windows.Forms.MenuItem();
			this.label1 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.label4 = new System.Windows.Forms.Label();
			this.speedLabel = new System.Windows.Forms.Label();
			this.linesLabel = new System.Windows.Forms.Label();
			this.scoreLabel = new System.Windows.Forms.Label();
			this.pictureBox1 = new System.Windows.Forms.PictureBox();
			this.SuspendLayout();
			// 
			// screenPanel
			// 
			this.screenPanel.BackColor = System.Drawing.Color.White;
			this.screenPanel.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.screenPanel.Dock = System.Windows.Forms.DockStyle.Left;
			this.screenPanel.Name = "screenPanel";
			this.screenPanel.Size = new System.Drawing.Size(206, 305);
			this.screenPanel.TabIndex = 0;
			// 
			// nextPanel
			// 
			this.nextPanel.BackColor = System.Drawing.Color.White;
			this.nextPanel.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.nextPanel.Location = new System.Drawing.Point(280, 0);
			this.nextPanel.Name = "nextPanel";
			this.nextPanel.Size = new System.Drawing.Size(104, 96);
			this.nextPanel.TabIndex = 1;
			// 
			// timer
			// 
			this.timer.Interval = 300;
			this.timer.Tick  = new System.EventHandler(this.OnTimer);
			// 
			// mainMenu
			// 
			this.mainMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					 this.gameMenu,
																					 this.helpMenu});
			// 
			// gameMenu
			// 
			this.gameMenu.Index = 0;
			this.gameMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					 this.startMenu,
																					 this.stopMenu,
																					 this.menuItem4,
																					 this.exitMenu});
			this.gameMenu.Text = "文件(&F)";
			// 
			// startMenu
			// 
			this.startMenu.Index = 0;
			this.startMenu.Text = "开始(&S)";
			this.startMenu.Click  = new System.EventHandler(this.startMenu_Click);
			// 
			// stopMenu
			// 
			this.stopMenu.Enabled = false;
			this.stopMenu.Index = 1;
			this.stopMenu.Text = "停止(&T)";
			this.stopMenu.Click  = new System.EventHandler(this.stopMenu_Click);
			// 
			// menuItem4
			// 
			this.menuItem4.Index = 2;
			this.menuItem4.Text = "-";
			// 
			// exitMenu
			// 
			this.exitMenu.Index = 3;
			this.exitMenu.Text = "退出(&X)";
			this.exitMenu.Click  = new System.EventHandler(this.exitMenu_Click);
			// 
			// helpMenu
			// 
			this.helpMenu.Index = 1;
			this.helpMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					 this.aboutMenu});
			this.helpMenu.Text = "帮助(&H)";
			// 
			// aboutMenu
			// 
			this.aboutMenu.Index = 0;
			this.aboutMenu.Text = "关于Tetris...(&A)";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(216, 8);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(56, 16);
			this.label1.TabIndex = 2;
			this.label1.Text = "下一个:";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(216, 120);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(56, 16);
			this.label2.TabIndex = 3;
			this.label2.Text = "分 数:";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(216, 144);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(56, 16);
			this.label3.TabIndex = 4;
			this.label3.Text = "行 数:";
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(216, 168);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(56, 16);
			this.label4.TabIndex = 5;
			this.label4.Text = "速 度:";
			// 
			// speedLabel
			// 
			this.speedLabel.Location = new System.Drawing.Point(288, 168);
			this.speedLabel.Name = "speedLabel";
			this.speedLabel.Size = new System.Drawing.Size(56, 16);
			this.speedLabel.TabIndex = 8;
			this.speedLabel.Text = "0";
			// 
			// linesLabel
			// 
			this.linesLabel.Location = new System.Drawing.Point(288, 144);
			this.linesLabel.Name = "linesLabel";
			this.linesLabel.Size = new System.Drawing.Size(56, 16);
			this.linesLabel.TabIndex = 7;
			this.linesLabel.Text = "0";
			// 
			// scoreLabel
			// 
			this.scoreLabel.Location = new System.Drawing.Point(288, 120);
			this.scoreLabel.Name = "scoreLabel";
			this.scoreLabel.Size = new System.Drawing.Size(56, 16);
			this.scoreLabel.TabIndex = 6;
			this.scoreLabel.Text = "0";
			// 
			// pictureBox1
			// 
			this.pictureBox1.Image = ((System.Drawing.Bitmap)(resources.GetObject("pictureBox1.Image")));
			this.pictureBox1.Location = new System.Drawing.Point(276, 258);
			this.pictureBox1.Name = "pictureBox1";
			this.pictureBox1.Size = new System.Drawing.Size(36, 28);
			this.pictureBox1.TabIndex = 9;
			this.pictureBox1.TabStop = false;
			// 
			// MainForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(386, 305);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.pictureBox1,
																		  this.speedLabel,
																		  this.linesLabel,
																		  this.scoreLabel,
																		  this.label4,
																		  this.label3,
																		  this.label2,
																		  this.label1,
																		  this.nextPanel,
																		  this.screenPanel});
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.MaximizeBox = false;
			this.Menu = this.mainMenu;
			this.Name = "MainForm";
			this.Text = "Tetris";
			this.KeyDown  = new System.Windows.Forms.KeyEventHandler(this.MainForm_KeyDown);
			this.Load  = new System.EventHandler(this.MainForm_Load);
			this.Paint  = new System.Windows.Forms.PaintEventHandler(this.MainFormPaint);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new MainForm());
		}

		public void DrawScreen()
		{
			if (gameStatus == GAME_STATUS.GAME_RUN || gameStatus == GAME_STATUS.GAME_OVER)
			{
				ReDrawNextShape();

				Graphics grMain = screenPanel.CreateGraphics();
				grMain.FillRectangle(new SolidBrush(Color.White), 0, 0, screenPanel.Width, screenPanel.Height);
				mainBody.Draw(grMain);
			}
			if (gameStatus == GAME_STATUS.GAME_STOP || gameStatus == GAME_STATUS.GAME_OVER)
			{
				Graphics grMain = screenPanel.CreateGraphics();
				string logo = "TETRIS";
				
				DrawText(logo, grMain, new Point(20, (int)(screenPanel.Height*0.28)), 30);
			}
			if (gameStatus == GAME_STATUS.GAME_OVER)
			{
				Graphics grMain = screenPanel.CreateGraphics();
				string logo = "GAME OVER";
				
				DrawText(logo, grMain, new Point(20, (int)(screenPanel.Height*0.42)), 21);
			}
		}

		private void MainFormPaint(object sender, System.Windows.Forms.PaintEventArgs e)
		{
			DrawScreen();
		}

		private void OnTimer(object sender, System.EventArgs e)
		{
			if (gameStatus == GAME_STATUS.GAME_RUN)
			{
				Graphics grMain = screenPanel.CreateGraphics();
				if (mainBody.MoveShape(grMain, Body.MOVE_TYPE.MOVE_DOWN))
				{
					DisposeShapeDown();
				}
			}
		}

		private void startMenu_Click(object sender, System.EventArgs e)
		{
			StartGame();
		}

		private void stopMenu_Click(object sender, System.EventArgs e)
		{
			GameOver();
		}

		private void exitMenu_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		public void StartGame()
		{
			score = 0;
			speed = 0;
			lines = 0;

			ChangeLines(0);
			timer.Interval = SpeedToTime(speed);
			timer.Enabled = true;
			startMenu.Enabled = false;
			stopMenu.Enabled = true;
			gameStatus = GAME_STATUS.GAME_RUN;
			mainBody.Reset();
			GetNextShape(true);
			DrawScreen();
		}

		public bool GetNextShape()
		{
			return GetNextShape(false);
		}

		private void MainForm_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			int key = e.KeyValue;
			bool ret;
			Graphics grMain = screenPanel.CreateGraphics();
			if (gameStatus == GAME_STATUS.GAME_RUN)
			{
				switch (key)
				{
					case 38:	//	up
						ret = mainBody.MoveShape(grMain, Body.MOVE_TYPE.MOVE_ROATE);
						break;
					case 37:	//	left
						ret = mainBody.MoveShape(grMain, Body.MOVE_TYPE.MOVE_LEFT);
						break;
					case 39:	//	right
						ret = mainBody.MoveShape(grMain, Body.MOVE_TYPE.MOVE_RIGHT);
						break;
					case 40:	//	fall down
						ret = mainBody.MoveShape(grMain, Body.MOVE_TYPE.MOVE_FALL);
						break;
					default:
						ret = false;
						break;
				}
				if (ret && key == 40)
				{
					DisposeShapeDown();
				}
			}
		}

		private void MainForm_Load(object sender, System.EventArgs e)
		{
		
		}

		public bool GetNextShape(bool initGame)
		{
			int shapeCount = 7;
			if (initGame)
			{
				int indexShape = rndShape.Next(shapeCount);
				nextShape = new Shape(indexShape);
			}

			bool ret = mainBody.SetNextShape(nextShape);		
			int indNextShape = rndShape.Next(shapeCount);
			nextShape = new Shape(indNextShape);
			return ret;
		}

		public void DisposeShapeDown()
		{
			int count = mainBody.ClearLines();
			if (GetNextShape())
			{
				GameOver();
			}
			
			if (count > 0)
			{
				ChangeLines(count);
				DrawScreen();
			}
			else
			{
				ReDrawNextShape();
			}
		}

		public void ReDrawNextShape()
		{
			Graphics grNext = nextPanel.CreateGraphics();
			grNext.FillRectangle(new SolidBrush(Color.White), 0, 0, nextPanel.Width, nextPanel.Height);
			nextShape.Draw(grNext, nextPanel.Size);

			Graphics grMain = screenPanel.CreateGraphics();
			mainBody.DrawNextShape(grMain);
		}

		public void GameOver()
		{
			gameStatus = GAME_STATUS.GAME_OVER;
			timer.Enabled = false;
			startMenu.Enabled = true;
			stopMenu.Enabled = false;
			DrawScreen();
		}

		public void DrawText(string text, Graphics g, Point pt, int font)
		{
			Font drawFont = new Font("Courier new", font, FontStyle.Bold);

			for (int i=0; i<text.Length; i  )
			{
				int corIndex = i;
				if (i >= 7) 
					corIndex = i % 7;
				SolidBrush drawBrush = new SolidBrush(Block.GetColor(corIndex));
				string drawText = new String(' ', i);
				drawText  = text.Substring(i, 1);
				g.DrawString(drawText, drawFont, drawBrush, pt);
			}
		}

		public void ChangeLines(int count)
		{
			switch (count)
			{
				case 1:
					score  = 100;
					break;
				case 2:
					score  = 300;
					break;
				case 3:
					score  = 500;
					break;
				case 4:
					score  = 1000;
					break;
				default:
					break;
			}
			if ((lines count) / 30 > lines / 30)
			{
				speed  ;
				timer.Interval = SpeedToTime(speed);
			}
			lines  = count;

			scoreLabel.Text = score.ToString();
			speedLabel.Text = speed.ToString();
			linesLabel.Text = lines.ToString();
		}

		public int SpeedToTime(int nSpeed)
		{
			switch (nSpeed)
			{
				case 0:
					return(1000);
				case 1:
					return(900);
				case 2:
					return(800);
				case 3:
					return(700);
				case 4:
					return(600);
				case 5:
					return(500);
				case 6:
					return(400);
				case 7:
					return(300);
				case 8:
					return(200);
				case 9:
					return(150);
				default:
					return(150);
			}
		}
	}
}