From a87c1909b52b9765f18fb75240b7d871e36911a3 Mon Sep 17 00:00:00 2001 From: 707238270 <707238270@qq.com> Date: Mon, 29 Aug 2022 00:04:56 +0800 Subject: [PATCH] reformate to git --- GameState.cs | 3 +- TetrisComponent.cs | 61 +++++++++++++++++++++++---------------- TetrisControlComponent.cs | 5 +--- 3 files changed, 39 insertions(+), 30 deletions(-) diff --git a/GameState.cs b/GameState.cs index b9b3e0f..7a1735c 100644 --- a/GameState.cs +++ b/GameState.cs @@ -42,7 +42,7 @@ public GameState() private bool BlockFits() { - //判断是否可以容纳这个块,可以容纳则为true + //Determine whether the block can be accommodated, and true if it can be accommodated foreach (Position p in CurrentBlock.TilePositions()) { if (!GameGrid.IsEmpty(p.Row, p.Column)) @@ -54,6 +54,7 @@ private bool BlockFits() return true; } + //Temporarily disable this feature //public void HoldBlock() //{ // if (!CanHold) diff --git a/TetrisComponent.cs b/TetrisComponent.cs index cdd3dc6..2db0c79 100644 --- a/TetrisComponent.cs +++ b/TetrisComponent.cs @@ -1,13 +1,13 @@ using Grasshopper; using Grasshopper.Kernel; +using Grasshopper.Kernel.Special; using Rhino.Geometry; using System; using System.Collections.Generic; -using System.Windows.Forms; -using Grasshopper.Kernel.Special; using System.Drawing; using System.Linq; using System.Threading.Tasks; +using System.Windows.Forms; // In order to load the result of this wizard, you will also need to @@ -38,7 +38,8 @@ public TetrisComponent() /// protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager) { - pManager.AddTextParameter("control", "control", "", GH_ParamAccess.item, ""); + pManager.AddTextParameter("control", "control", "Input control string:\ndirections=ctrl+directions\nhome=counter CW\nend=Drop\ndelete=pause", GH_ParamAccess.item, ""); + //added restart feature in to right click button event //pManager.AddBooleanParameter("restart", "restart", "", GH_ParamAccess.item,false); } @@ -72,7 +73,7 @@ public async override void AddedToDocument(GH_Document document) { base.AddedToDocument(document); imageControls = SetupGameCanvas(gameState.GameGrid); - await GameLoop(); + await GameLoop(document); } @@ -85,7 +86,7 @@ protected override void SolveInstance(IGH_DataAccess DA) { string control = string.Empty; DA.GetData("control", ref control); - //由于expire机制,所以这里需要先去除订阅,再增加事件订阅 + //Due to the EXPIRE mechanism, the subscription needs to be removed first, and then the event subscription needs to be added. if (control == "") { Message = "Inside Control Mode"; @@ -112,19 +113,19 @@ protected override void AppendAdditionalComponentMenuItems(ToolStripDropDown men gameState = new GameState(); RemoveExpiredGrid(OnPingDocument()); imageControls = SetupGameCanvas(gameState.GameGrid); - }); } public override void RemovedFromDocument(GH_Document document) { base.RemovedFromDocument(document); RemoveExpiredGrid(document); + } private GH_Markup[,] SetupGameCanvas(GameGrid grid) { var doc = OnPingDocument(); - //设置网格 + //Setup Grid GH_Markup[,] imageControls = new GH_Markup[grid.Rows, grid.Columns]; int cellSize = 50; for (int r = 0; r < grid.Rows; r++) @@ -139,7 +140,7 @@ public override void RemovedFromDocument(GH_Document document) doc.AddObject(comp, false); } - //设置分数 + //Setup Score scoreText = new GH_Scribble(); scoreText.CreateAttributes(); scoreText.Attributes.Pivot = new PointF(300, 100); @@ -148,7 +149,7 @@ public override void RemovedFromDocument(GH_Document document) scoreText.Text = "Score : " + gameState.Score.ToString(); doc.AddObject(scoreText, false); - //设置NextBlockCells + //Setup NextBlockCells nextBlockCells = new GH_Markup[4, 4]; for (int r = 0; r < nextBlockCells.GetLength(0); r++) { @@ -163,7 +164,7 @@ public override void RemovedFromDocument(GH_Document document) } - //设置Next + //Setup Next nextText = new GH_Scribble(); nextText.CreateAttributes(); nextText.Attributes.Pivot = new PointF(800, 400); @@ -194,25 +195,21 @@ private GH_Markup ConstructGrid(int cellSize, int pivotX, int pivotY, Color colo markGrid.Marks.Add(markLine); markGrid.Attributes.Pivot = new PointF(0, 0); ChangeCellColor(ref markGrid, color, width); - //var attributes = (GH_MarkupAttributes)markGrid.Attributes; - //var prop= new GH_MarkupProperties(); - //prop.Width = width; - //prop.Colour= color; - //attributes.Properties= prop; markGrid.Attributes.Pivot = new PointF(pivotX, pivotY); return markGrid; } - private async Task GameLoop() + private async Task GameLoop(GH_Document document) { Draw(gameState); while (!gameState.GameOver) { + //change delay to set level of difficulty int delay = 500; await Task.Delay(delay); - //后续加入暂停功能 + //add Pause feature later //if (gameState.Pause) //{ // await Task.Delay(2000); @@ -222,14 +219,30 @@ private async Task GameLoop() Draw(gameState); } - //GameOverMenu.Visibility = Visibility.Visible; - //FinalScoreText.Text = $"Score: {gameState.Score}"; + //if the component has been removed, Game over should not displayed + List tetrisComponents = document.Objects.Where(x => x.GetType() == typeof(TetrisComponent)).ToList(); + if (tetrisComponents.Count > 0) + { + bool isThisCompInDoc = false; + foreach (var item in tetrisComponents) + { + if (item.InstanceGuid == this.InstanceGuid) + { + isThisCompInDoc = true; + break; + } + } + if (isThisCompInDoc) + { + MessageBox.Show("Game Over!\nScore : " + gameState.Score.ToString()); + } + } } private void Draw(GameState gameState) { DrawGrid(gameState.GameGrid); - //先画ghostBlock,这样GhostBlock会在Block的后面 + //It's Painter Mode,Draw ghostBlock first,so that the GhostBlock will be behind the Block DrawGhostBlock(gameState.CurrentBlock); DrawBlock(gameState.CurrentBlock); @@ -239,13 +252,13 @@ private void Draw(GameState gameState) scoreText.Attributes.ExpireLayout(); scoreText.OnDisplayExpired(true); - //终于解决了画面更新的问题 + //Update Screen Display,Important Instances.RedrawAll(); } private void DrawNextBlock(BlockQueue blockQueue) { - //setup next cells + //Setup next cells for (int r = 0; r < nextBlockCells.GetLength(0); r++) { for (int c = 0; c < nextBlockCells.GetLength(1); c++) @@ -278,7 +291,7 @@ private Color GetGhostColor(Color color) Color result = color; if (color.A > 100) { - result = Color.FromArgb(100, color); + result = Color.FromArgb(70, color); } return result; } @@ -402,9 +415,7 @@ protected override System.Drawing.Bitmap Icon { get { - // You can add image files to your project resources and access them like this: return Properties.Resources.T; - //return null; } } diff --git a/TetrisControlComponent.cs b/TetrisControlComponent.cs index 5dc7bd3..389513a 100644 --- a/TetrisControlComponent.cs +++ b/TetrisControlComponent.cs @@ -40,7 +40,7 @@ protected override void RegisterInputParams(GH_Component.GH_InputParamManager pM /// protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager) { - pManager.AddTextParameter("string", "string", "", GH_ParamAccess.item); + pManager.AddTextParameter("string", "string", "Parse Keyboard input.\nIt can be understood as the interface left by the extensibility", GH_ParamAccess.item); } string output = String.Empty; public bool isPress = false; @@ -52,7 +52,6 @@ protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager /// to store data in output parameters. protected override void SolveInstance(IGH_DataAccess DA) { - //由于expire机制,所以这里需要先去除订阅,再增加事件订阅 Instances.DocumentEditor.KeyDown -= this.GhKeydown; Instances.DocumentEditor.KeyDown += this.GhKeydown; @@ -109,8 +108,6 @@ protected override System.Drawing.Bitmap Icon { get { - // You can add image files to your project resources and access them like this: - //return Resources.IconForThisComponent; return null; } }