Skip to content
This repository was archived by the owner on Jan 10, 2022. It is now read-only.

Commit

Permalink
Endlich neue Bugs eingebaut
Browse files Browse the repository at this point in the history
und Karrrrrrrrrrrrrrrrrrrrrrrrmarrrrrrrrrrrrrrrrrrrrrr wollte irgendwas mit slack
  • Loading branch information
Gallimathias committed Jul 30, 2017
1 parent b9c3a8d commit aeea93a
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 17 deletions.
11 changes: 5 additions & 6 deletions Tetris.Core/Brick.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
namespace Tetris.Core
{
public class Brick
{
public int Width { get; set; }
public int Height { get; set; }

{
public int X { get; set; }
public int Y { get; set; }

Expand All @@ -24,20 +21,22 @@ public void SetPosition(int x, int y)
X = x;
Y = y;
}

public void SetRelativePosition(int x, int y)
{
X += x;
Y += y;
}
public void Rotate() { }

public void Draw(Graphics graphics, int cellWidth, int cellHeight)
{
using (var brush = new SolidBrush(Color.Red))
{
graphics.FillRectangle(brush,
new Rectangle(cellWidth * X, cellHeight * Y, cellWidth * Width, cellHeight * Height));
new Rectangle(cellWidth * X, cellHeight * Y, cellWidth, cellHeight));
}
}

public bool Intersect(int x, int y) => x == X && y == Y;
}
}
40 changes: 40 additions & 0 deletions Tetris.Core/Figures/BaseFigure.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;

namespace Tetris.Core.Figures
{
public abstract class BaseFigure
{
public int Width => Bricks.Max(b => b.X) - Bricks.Min(b => b.X);
public int Height => Bricks.Max(b => b.Y) - Bricks.Min(b => b.Y);
public bool IsActive { get; set; }

protected Brick RotationBrick;

protected List<Brick> Bricks;

public BaseFigure()
{
Bricks = new List<Brick>();
IsActive = true;
}

public abstract void Rotate();

public void Draw(Graphics graphics, int cellWidth, int cellHeight) =>
Bricks.ForEach(b => b.Draw(graphics, cellWidth, cellHeight));

public void SetRelativePosition(int x, int y) =>
Bricks.ForEach(b => b.SetRelativePosition(x, y));

public virtual bool Intersect(int x, int y) => !Bricks.TrueForAll(b => !b.Intersect(x, y));
public virtual bool Intersect(Brick brick) => !Bricks.TrueForAll(b => !b.Intersect(brick.X, brick.Y));

public virtual bool Intersect(BaseFigure figure) => !figure.Bricks.TrueForAll(b => !Intersect(b));

}
}
28 changes: 28 additions & 0 deletions Tetris.Core/Figures/Border.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tetris.Core.Figures
{
public class Border : BaseFigure
{
public Border(int x, int y, int width, int height)
{
IsActive = false;
for (int i = x; i < x + width; i++)
for (int o = y; o < y + height; o++)
Bricks.Add(new Brick { X = i, Y = o });
}

public override void Rotate()
{
}
public new void Draw(Graphics graphics, int cellWidth, int cellHeight)
{

}
}
}
42 changes: 42 additions & 0 deletions Tetris.Core/Figures/Stab.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tetris.Core.Figures
{
public class Stab : BaseFigure
{
public Stab()
{
for (int i = 0; i < 4; i++)
{
var b = new Brick { X = 0, Y = i };
Bricks.Add(b);
if (i == 1)
RotationBrick = b;
}
}

public override void Rotate()
{
if(Bricks.GroupBy(x=>x.Y).ToList().Count > 1)
{
for (int i = -1; i < 3; i++)
{
Bricks[i+1].Y = RotationBrick.Y;
Bricks[i+1].X = RotationBrick.X + i;
}
}
else
{
for (int i = -1; i < 3; i++)
{
Bricks[i + 1].Y = RotationBrick.Y + i;
Bricks[i + 1].X = RotationBrick.X;
}
}
}
}
}
82 changes: 71 additions & 11 deletions Tetris.Core/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Text;
using System.Timers;
using System.Windows.Forms;
using Tetris.Core.Figures;

namespace Tetris.Core
{
Expand All @@ -17,11 +18,19 @@ public class Game

public int CellWidth { get; set; }
public int CellHeight { get; set; }
public BaseFigure CurrentFigure { get; set; }
public List<BaseFigure> AllFigures { get; set; }

/// <summary>
/// Used for Input. Check if Key is pressed
/// </summary>
Dictionary<Keys, KeyValuePair<bool, sbyte>> KeyDictionary;

System.Timers.Timer timer;
Brick brick;
Stab stab;
Border bottom;
Border left;
Border right;

ulong frames = 0;

Expand All @@ -38,12 +47,13 @@ public Game(int width, int height)
[Keys.Up] = new KeyValuePair<bool, sbyte>(false, 0)
};

brick = new Brick()
{
Width = 2,
Height = 2
};

stab = new Stab();
CurrentFigure = stab;
left = new Border(-1, 0, 1, Height);
right = new Border(Width, 0, 1, Height);
bottom = new Border(0, Height, Width, 1);
AllFigures = new List<BaseFigure> { stab, bottom, left, right };

timer = new System.Timers.Timer()
{
Interval = 33
Expand All @@ -57,18 +67,68 @@ public Game(int width, int height)
private void Timer_Elapsed(object sender, ElapsedEventArgs e)
{
frames++;
if(frames % 10 == 0)
brick.SetRelativePosition(0, brick.Y + 3 > Height ? 0 : 1);
KeyDictionary.Where(x => x.Value.Key).Select(x => x.Value.Value).ToList().ForEach(x => brick.SetRelativePosition(x, 0));

//KeyDictionary.Where(
// x => x.Key == Keys.Right || x.Key == Keys.Left).Where(
// x => x.Value.Key).Select(
// x => x.Value.Value).ToList().ForEach(
// x => CurrentFigure.SetRelativePosition(
// x > 0 ?
// CurrentFigure.Intersect(right) ? 0 : 1 :// stab.X + stab.Width + 1 > Width ? 0 : x :
// CurrentFigure.Intersect(left) ? 0 : -1, 0));

if (KeyDictionary[Keys.Down].Key)
if (!CurrentFigure.Intersect(bottom))
CurrentFigure.SetRelativePosition(0, 1);

if (frames % 4 == 0)
{
if (KeyDictionary[Keys.Up].Key)
{
KeyDictionary[Keys.Up] = new KeyValuePair<bool, sbyte>(false, 0);
CurrentFigure.Rotate();
}
}

if (frames % 10 == 0)
CurrentFigure.SetRelativePosition(0, 1);

bool intersect = false;
foreach (var item in AllFigures.Where(x => x != CurrentFigure))
{
if (intersect)
continue;
intersect = CurrentFigure.Intersect(item);
}
if(!intersect)
KeyDictionary.Where(
x => x.Key == Keys.Right || x.Key == Keys.Left).Where(
x => x.Value.Key).Select(
x => x.Value.Value).ToList().ForEach(
x => CurrentFigure.SetRelativePosition(
x > 0 ? intersect ? -1 : 1 : intersect ? 1 : -1, 0));

while (CurrentFigure.Intersect(bottom))
{
CurrentFigure.SetRelativePosition(0, -1);
CurrentFigure.IsActive = false;
CurrentFigure = new Stab();
AllFigures.Add(CurrentFigure);
}
}

public void OnDraw(Graphics graphics)
{
brick.Draw(graphics, CellWidth, CellHeight);
AllFigures.ForEach(f => {
if (!(f is Border))
f.Draw(graphics, CellWidth, CellHeight);
});
}

public void MoveBrick(KeyEventArgs keyEventArgs, bool keyUp)
{
if (keyEventArgs.KeyCode == Keys.Up && !keyUp)
return;
if (KeyDictionary.TryGetValue(keyEventArgs.KeyCode, out var val))
KeyDictionary[keyEventArgs.KeyCode] = new KeyValuePair<bool, sbyte>(keyUp, val.Value);
}
Expand Down
4 changes: 4 additions & 0 deletions Tetris.Core/Tetris.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,12 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Brick.cs" />
<Compile Include="Figures\BaseFigure.cs" />
<Compile Include="Figures\Border.cs" />
<Compile Include="Figures\Stab.cs" />
<Compile Include="Game.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

0 comments on commit aeea93a

Please sign in to comment.