Skip to content

Commit

Permalink
reformate to git
Browse files Browse the repository at this point in the history
  • Loading branch information
LaVieon committed Aug 28, 2022
1 parent 83417b9 commit a87c190
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 30 deletions.
3 changes: 2 additions & 1 deletion GameState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -54,6 +54,7 @@ private bool BlockFits()
return true;
}

//Temporarily disable this feature
//public void HoldBlock()
//{
// if (!CanHold)
Expand Down
61 changes: 36 additions & 25 deletions TetrisComponent.cs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -38,7 +38,8 @@ public TetrisComponent()
/// </summary>
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);
}

Expand Down Expand Up @@ -72,7 +73,7 @@ public async override void AddedToDocument(GH_Document document)
{
base.AddedToDocument(document);
imageControls = SetupGameCanvas(gameState.GameGrid);
await GameLoop();
await GameLoop(document);
}


Expand All @@ -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";
Expand All @@ -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++)
Expand All @@ -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);
Expand All @@ -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++)
{
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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<IGH_DocumentObject> 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);

Expand All @@ -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++)
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
}

Expand Down
5 changes: 1 addition & 4 deletions TetrisControlComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ protected override void RegisterInputParams(GH_Component.GH_InputParamManager pM
/// </summary>
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;
Expand All @@ -52,7 +52,6 @@ protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager
/// to store data in output parameters.</param>
protected override void SolveInstance(IGH_DataAccess DA)
{
//由于expire机制,所以这里需要先去除订阅,再增加事件订阅
Instances.DocumentEditor.KeyDown -= this.GhKeydown;
Instances.DocumentEditor.KeyDown += this.GhKeydown;

Expand Down Expand Up @@ -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;
}
}
Expand Down

0 comments on commit a87c190

Please sign in to comment.