Create your own
teaching resources

Sign up for a free account to try! It takes less than 20 seconds

Teacherbot 09 Jan, 17:25:36

Sure! Here’s an example of a Tetris game implemented in C# using WPF:

```csharp using System; using System.Collections.Generic; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Media; using System.Windows.Shapes; using System.Windows.Threading;

namespace TetrisGame { public partial class MainWindow : Window { private const int Rows = 20; private const int Columns = 10; private const int SquareSize = 30; private const int GameSpeed = 500;

    private readonly DispatcherTimer _gameTimer;
    private readonly List<List<Rectangle>> _grid;
    private readonly Random _random;
    private readonly Shape[] _shapes;

    private Shape _currentShape;
    private int _currentRow;
    private int _currentColumn;

    public MainWindow()
    {
        InitializeComponent();

        _gameTimer = new DispatcherTimer();
        _gameTimer.Tick += GameTimer_Tick;
        _gameTimer.Interval = TimeSpan.FromMilliseconds(GameSpeed);

        _grid = new List<List<Rectangle>>();
        _random = new Random();
        _shapes = new Shape[]
        {
            new Shape(new int[,]
            {
                { 1, 1, 1, 1 }
            }, Brushes.Cyan),
            new Shape(new int[,]
            {
                { 1, 1 },
                { 1, 1 }
            }, Brushes.Yellow),
            new Shape(new int[,]
            {
                { 1, 1, 0 },
                { 0, 1, 1 }
            }, Brushes.Magenta),
            new Shape(new int[,]
            {
                { 0, 1, 1 },
                { 1, 1, 0 }
            }, Brushes.Orange),
            new Shape(new int[,]
            {
                { 1, 1, 1 },
                { 0, 0, 1 }
            }, Brushes.Blue),
            new Shape(new int[,]
            {
                { 1, 1, 1 },
                { 1, 0, 0 }
            }, Brushes.Green),
            new Shape(new int[,]
            {
                { 1, 1, 1 },
                { 0, 1, 0 }
            }, Brushes.Red)
        };

        InitializeGrid();
        StartGame();
    }

    private void InitializeGrid()
    {
        for (int row = 0; row < Rows; row++)
        {
            var rowList = new List<Rectangle>();
            for (int column = 0; column < Columns; column++)
            {
                var square = new Rectangle
                {
                    Width = SquareSize,
                    Height = SquareSize,
                    Fill = Brushes.Transparent,
                    Stroke = Brushes.Black,
                    StrokeThickness = 1
                };

                Grid.SetRow(square, row);
                Grid.SetColumn(square, column);
                GameGrid.Children.Add(square);

                rowList.Add(square);
            }
            _grid.Add(rowList);
        }
    }

    private void StartGame()
    {
        _currentShape = GetRandomShape();
        _currentRow = 0;
        _currentColumn = Columns / 2 - _currentShape.Width / 2;

        _gameTimer.Start();
    }

    private void GameTimer_Tick(object sender, EventArgs e)
    {
        MoveShapeDown();
    }

    private void MoveShapeDown()
    {
        if (CanMoveShape(_currentRow + 1, _currentColumn))
        {
            _currentRow++;
            UpdateShapePosition();
        }
        else
        {
            PlaceShape();
            CheckLines();
            _currentShape = GetRandomShape();
            _currentRow = 0;
            _currentColumn = Columns / 2 - _currentShape.Width / 2;
        }
    }

    private void MoveShapeLeft()
    {
        if (CanMoveShape(_currentRow, _currentColumn - 1))
        {
            _currentColumn--;
            UpdateShapePosition();
        }
    }

    private void MoveShapeRight()
    {
        if (CanMoveShape(_currentRow, _currentColumn + 1))
        {
            _currentColumn++;
            UpdateShapePosition();
        }
    }

    private void RotateShape()
    {
        var rotatedShape = _currentShape.Rotate();
        if (CanMoveShape(_currentRow, _currentColumn, rotatedShape))
        {
            _currentShape = rotatedShape;
            UpdateShapePosition();
        }
    }

    private bool CanMoveShape(int row, int column, Shape shape = null)
    {
        shape ??= _currentShape;

        for (int shapeRow = 0; shapeRow < shape.Height; shapeRow++)
        {
            for (int shapeColumn = 0; shapeColumn < shape.Width; shapeColumn++)
            {
                if (shape.Grid[shapeRow, shapeColumn] == 1)
                {
                    int newRow = row + shapeRow;
                    int newColumn = column + shapeColumn;

                    if (newRow < 0 || newRow >= Rows || newColumn < 0 || newColumn >= Columns || _grid[newRow][newColumn].Fill != Brushes.Transparent)
                    {
                        return false;
                    }
                }
            }
        }

        return true;
    }

    private void UpdateShapePosition()
    {
        ClearShape();

        for (int shapeRow = 0; shapeRow < _currentShape.Height; shapeRow++)
        {
            for (int shapeColumn = 0; shapeColumn < _currentShape.Width; shapeColumn++)
            {
                if (_currentShape.Grid[shapeRow, shapeColumn] == 1)
                {
                    int row = _currentRow + shapeRow;
                    int column = _currentColumn + shapeColumn;

                    _grid[row][column].Fill = _currentShape.Color;
                }
            }
        }
    }

    private void ClearShape()
    {
        for (int row = 0; row < Rows; row++)
        {
            for (int column = 0; column < Columns; column++)
            {
                if (_grid[row][column].Fill == _currentShape.Color)
                {
                    _grid[row][column].Fill = Brushes.Transparent;
                }
            }
        }
    }

    private void PlaceShape()
    {
        for (int shapeRow = 0; shapeRow < _currentShape.Height; shapeRow++)
        {
            for (int shapeColumn = 0; shapeColumn < _currentShape.Width; shapeColumn++)
            {
                if (_currentShape.Grid[shapeRow, shapeColumn] == 1)
                {
                    int row = _currentRow + shapeRow;
                    int column = _currentColumn + shapeColumn;

                    _grid[row][column].Fill = _currentShape.Color;
                }
            }
        }
    }

    private void CheckLines()
    {
        for (int row = Rows - 1; row >= 0; row--)
        {
            bool isLineComplete = true;
            for (int column = 0; column < Columns; column++)
            {
                if (_grid[row][column].Fill == Brushes.Transparent)
                {
                    isLineComplete = false;
                    break;
                }
            }

            if (isLineComplete)
            {
                RemoveLine(row);
                MoveLinesDown(row);
            }
        }
    }

    private void RemoveLine(int row)
    {
        for (int column = 0; column < Columns; column++)
        {
            _grid[row][column].Fill = Brushes.Transparent;
        }
    }

    private void MoveLinesDown(int row)
    {
        for (int currentRow = row - 1; currentRow >= 0; currentRow--)
        {
            for (int column = 0; column < Columns; column++)
            {
                _grid[currentRow + 1][column].Fill = _grid[currentRow][column].Fill;
            }
        }
    }

    private Shape GetRandomShape()
    {
        int randomIndex = _random.Next(_shapes.Length);
        return _shapes[randomIndex];
    }

    private void Window_KeyDown(object sender, KeyEventArgs e)
    {
        switch (e.Key)
        {
            case Key.Left:
                MoveShapeLeft();
                break;
            case Key.Right:
                MoveShapeRight();
                break;
            case Key.Down:
                MoveShapeDown();
                break;
            case Key.Up:
                RotateShape();
                break;
        }
    }
}

public class Shape
{
    public int[,] Grid { get; }
    public int Width => Grid.GetLength(1);
    public int Height => Grid.GetLength(0);
    public Brush Color { get; }

    public Shape(int[,] grid, Brush color)
    {
        Grid = grid;
        Color = color;
    }

    public Shape Rotate()
    {
        int[,] rotatedGrid = new int[Width, Height];

        for (int row = 0; row < Height; row++)
        {
            for (int column = 0; column < Width; column++)
            {
                rotatedGrid[column, Height - row - 1] = Grid[row, column];
            }
        }

        return new Shape(rotatedGrid, Color);
    }
} } ```

To run this code, create a new WPF Application project in Visual Studio, replace the contents of the MainWindow.xaml file with the following XAML code:

```xaml <Window x:Class=”TetrisGame.MainWindow” xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation” xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml” Title=”Tetris” Height=”600