public void Update(Input input) { if (input.WasPressed(Keys.Left)) { cursor.position.X--; if (!isCursorValid(cursor)) cursor.position.X++; } if (input.WasPressed(Keys.Right)) { cursor.position.X++; if (!isCursorValid(cursor)) cursor.position.X--; } if (input.WasPressed(Keys.Space)) { Cursor rotated = cursor.Rotate(); if (isCursorValid(rotated)) cursor = rotated; } cursor.position.Y++; if (!isCursorValid(cursor)) { cursor.position.Y--; for (int x = 0; x < cursor.Width; x++) for (int y = 0; y < cursor.Height; y++) if (cursor.blocks[x, y] != null) blocks[cursor.position.X + x, cursor.position.Y + y] = cursor.blocks[x, y]; resetCursor(); if (!isCursorValid(cursor)) TetrisGame.isGameOver = true; } checkRows(); }
public PlayingGrid(int width, int height) : base(width, height) { cursor = new Cursor(); resetCursor(); }
public Cursor Mirror() { Cursor result = new Cursor(); result.blocks = new Block[Width, Height]; result.position = position; for (int x = 0; x < Width; x++) for (int y = 0; y < Height; y++) result.blocks[x, y] = blocks[Width - x - 1, y]; return result; }
bool isCursorValid(Cursor cursor) { Point pos = cursor.position; if (pos.X < 0 || pos.X > Width - cursor.Width || pos.Y > Height - cursor.Height) return false; for (int x = 0; x < cursor.Width; x++) for (int y = 0; y < cursor.Height; y++) if (cursor.blocks[x, y] != null && blocks[pos.X + x, pos.Y + y] != null) return false; return true; }
public Cursor Rotate() { Cursor result = new Cursor(); result.blocks = new Block[Height, Width]; result.position = position; for (int x = 0; x < Height; x++) for (int y = 0; y < Width; y++) result.blocks[x, y] = blocks[y, x]; result = result.Mirror(); return result; }