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

📄 form1.cs

📁 一个可以在智能手机上运行,完全基于CF的俄罗斯方块游戏
💻 CS
📖 第 1 页 / 共 2 页
字号:
		{
			Point[] b1=blockLast.GetCakes();
			Point[] b2=blockNow.GetCakes();

			int i=0;
			Graphics grfx=this.CreateGraphics();
			for(i=0;i<4;i++)
			{
				fillCake(b1[i],new SolidBrush( Color.AliceBlue ),grfx);
			}
			for(i=0;i<4;i++)
			{
				fillCake(b2[i],new SolidBrush( Color.Red ),grfx);
			}
		}

		/// <summary>
		/// 初始化一组方块
		/// </summary>
		private void Init()
		{
			blockNow=new Block();
			blockLast=new Block();
			blockNow.Shape=blockNext.Shape;
			blockNow.SetBlock(blockNext.GetCakes());
			blockLast.SetBlock(blockNow.GetCakes());
			blockNext=new Block();
			flushNext();
			Point[] p=blockNow.GetCakes();
			for(int i=0;i<4;i++)
			{
				if (!wall.Cakes[p[i].X,p[i].Y])
				{
					this.timer1.Enabled = false ;
					GameOver();
					break;
				}
			}
		}

		/// <summary>
		/// 游戏结束处理
		/// </summary>
		void GameOver()
		{
			//播放鼓掌的声音
			_SouApplause.Play();

			//记录最高分
			if ( mark > _MaxScore )
			{
				GetPlayerName frmName = new GetPlayerName();
				frmName.ShowDialog();
						
				SaveXml( _FilePath,frmName._PlayerName,mark);
				this.labName.Text = frmName._PlayerName;
				this.labScore.Text = mark.ToString();
			}

			gameOver=true;

			for(int  j=19;j>0;j--)
			{
				for(int i=1;i<13;i++)
				{
					wall.Cakes[i,j]=false;
				}
			
				Invalidate();	
				Application.DoEvents();
				label4.Visible=true;
				label4.Text="G a m e    O v e r";
			}
			
			//停止时钟
			this.timer1.Enabled = false;
			this.menuItem5.Enabled = true;
		}

		/// <summary>
		/// 绘制Next方块组
		/// </summary>
		private void flushNext()
		{
			int i,j;
			Graphics grfx=this.CreateGraphics();
			Point[] b1=blockNext.GetCakes();
			

			for(i=14;i<16;i++)
				for(j=5;j<9;j++)
				{
					fillCake(new Point(i,j),new SolidBrush(SystemColors.Control),grfx);
				}
			
			for( i=0;i<4;i++)
			{
				b1[i].X+=8;
				b1[i].Y+=4;
				fillCake(b1[i],new SolidBrush( Color.Red ),grfx);
				b1[i].X-=8;
				b1[i].Y-=4;
			}
		}
		
		/// <summary>
		/// Saves the high score Xml to a given file.
		/// </summary>
		/// <param name="filename">The filename to save the Xml in</param>
		/// <param name="playerName">The name of the player</param>
		/// <param name="score">The value of the score</param>
		/// <returns>returns true if the save worked</returns>
		private bool SaveXml ( string filename, string playerName, int score ) 
		{
			try 
			{
				XmlTextWriter writer;
				writer = new XmlTextWriter( filename, Encoding.UTF8) ;
				writer.Formatting = Formatting.Indented;
				writer.WriteStartDocument();
				writer.WriteStartElement("highscore","www.steelres.com/game");
				writer.WriteAttributeString( "game", "Cheese Breakout");
				writer.WriteStartElement("player");
				try 
				{
					writer.WriteCData(playerName);
				}
				catch 
				{
					writer.WriteCData("Invalid");
				}
				writer.WriteEndElement();
				writer.WriteElementString("score", score.ToString());
				writer.WriteEndElement();
				writer.WriteEndDocument();
				writer.Close();
				writer = null;
			}
			catch 
			{
				return false;
			}
			return true;
		}

		/// <summary>
		/// Loads the Xml high score information. 
		/// </summary>
		/// <param name="filename">File to read the high score from</param>
		/// <param name="playerName">Reference to the name to be set</param>
		/// <param name="highScore">Reference to the high score to be set</param>
		/// <returns>returns true if the load was successful</returns>
		public bool LoadXml ( string filename, ref string playerName, ref int highScore ) 
		{
			// get a new document
			System.Xml.XmlDocument document = new XmlDocument();
			// load it from a file
			try 
			{
				document.Load(filename);
			}
			catch 
			{
				return false;
			}
			// get the document XML element for our data
			System.Xml.XmlElement rootElement = document.DocumentElement;
			// make sure that we have the right element
			if ( rootElement.Name != "highscore" ) 
			{
				return false;
			}
			// make sure it is in the right namespace
			if ( rootElement.NamespaceURI != "www.steelres.com/game" ) 
			{
				return false;
			}
			// check to see if the name is correct
			string gameName = rootElement.GetAttribute("game");

			if ( gameName != "Cheese Breakout"  ) 
			{
				return false ;
			}
			XmlNode playerNameElement = rootElement.FirstChild;
			if ( playerNameElement.Name != "player" ) 
			{
				return false;
			}
			playerName = playerNameElement.FirstChild.Value;

			XmlNode scoreElement = playerNameElement.NextSibling;
			if ( scoreElement.Name != "score" ) 
			{
				return false;
			}
			string highScoreString = scoreElement.FirstChild.Value;
			try 
			{
				highScore = int.Parse(highScoreString);
			}
			catch
			{
				return false;
			}
			return true;
		}
		# endregion

		# region 事件处理
		private void timer1_Tick(object sender, System.EventArgs e)
		{
			int flag=0;
			if(blockNow.CanMove(Block.Direction.DOWN))
			{
				blockLast.SetBlock(blockNow.GetCakes());
				this.blockNow.Move(Block.Direction.DOWN);
				Display();
			}
			else
			{
				Point[] p=blockNow.GetCakes();
				for(int i=0;i<4;i++)
				{
					wall.Cakes[p[i].X,p[i].Y]=false;
					if(wall.CheckRoll(p[i].Y))
					{
						flag=1;
						_SouChimes.Play();
						mark+=10;
						label3.Text=mark.ToString();
						this.Invalidate();
					}
				}
				if(flag==0)
				{
					_SouClick.Play();
				}
				Init();
			}
		}

		private void menuItem5_Click(object sender, System.EventArgs e)
		{
			//播放游戏开始音效
			_SouGong.Play();

			gameOver=false;
			label4.Visible=false;
			mark=0;
			for(int i=1;i<13;i++)
				for(int j=1;j<19;j++)
					wall.Cakes[i,j]=true;
			this.Invalidate();
			Init();
			Display();

			this.menuItem5 .Enabled = false;
			this.timer1.Enabled = true ;
		}

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

		private void menuItem7_Click(object sender, System.EventArgs e)
		{
			this.timer1.Enabled = false;

			MessageBox.Show("作者:方海飞\n昵称:拉丁海~~~~\nQQ:18926355\nhttp://www.steelres.com",
				"说明",MessageBoxButtons.OK,
				MessageBoxIcon.Asterisk,MessageBoxDefaultButton.Button1);
		}

		private void Form1_GotFocus(object sender, System.EventArgs e)
		{
			//blockNow = null说明游戏没有开始
			if (!gameOver && blockNow != null)
			{
				this.timer1.Enabled = true;
			}
		}

		private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			try
			{
				blockLast.SetBlock(blockNow.GetCakes());//重点:当方块移动时消除原来的方块
				switch(e.KeyCode)
				{
					case Keys.Up:
						if(blockNow.CanRotate())
						{
							blockNow.Rotate(blockNow.GetCakes());
							Display();
						}
						break;
					case Keys.Down:
						while(blockNow.CanMove(Block.Direction.DOWN)    )
						{
							blockNow.Move(Block.Direction.DOWN);
						}
						Display();
						break;
					case Keys.Left:
						blockNow.Move(Block.Direction.LEFT);
						Display();
						break;
					case Keys.Right:
						blockNow.Move(Block.Direction.RIGHT);
						Display();
						break;
					default:
						break;
				}
			}
			catch
			{}
		}

		private void Form1_LostFocus(object sender, System.EventArgs e)
		{
			if (!gameOver)
			{
				this.timer1.Enabled = false;
			}
		}
		# endregion
	}
}

⌨️ 快捷键说明

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