Esempio n. 1
0
File: VM.cs Progetto: luxiki/Tetris
        public VM()
        {
            figure = new Figure();
            FigureRotateCommand = new DelegateCommand(obj => figure.Rotate());
            FigureDownCommand   = new DelegateCommand(obj => figure.Down());
            FigureLeftCommand   = new DelegateCommand(obj => figure.Left());
            FigureRightCommand  = new DelegateCommand(obj => figure.Right());
            figure.Paint       += Paint;

            for (int y = 0; y < Figure.VERTICAL; y++)
            {
                for (int x = 0; x < Figure.HORIZONTAL; x++)
                {
                    if (x == 0 || x == Figure.HORIZONTAL - 1 || y == Figure.VERTICAL - 1)
                    {
                        // TODO Можно нарисовать стены другим блоком!
                    }

                    image        = new Image();
                    image.Source = bitmap;
                    blocks.Add(image);
                    Canvas.SetLeft(blocks.Last(), x * 20);
                    Canvas.SetTop(blocks.Last(), y * 20);
                }
            }

            timer.Elapsed  += GameTick;
            timer.AutoReset = true;
            timer.Enabled   = true;

            //RoutedEvent Ke

            //Paint();
        }
Esempio n. 2
0
        //конструктор
        public Form1()
        {
            InitializeComponent();

            nextFigure = new NextFigure(this);

            //инициализация таймера
            timer = new Timer {
                Interval = speed
            };
            timer.Tick += (sender, args) =>
            {
                if (figure.Down())
                {
                    ;
                }
                else
                {
                    HorizontalRow();
                    GameOver();
                    figure = nextFigure.GetNext(this);
                }
                pictureBox1.Invalidate();
                PictureNextFigure.Invalidate();
            };

            //инициализация цвета отрисовки подквадрата
            myPen = new Pen(Color.Black)
            {
                LineJoin = LineJoin.Miter,
                Width    = 2.0F
            };

            //отрисовка стакана и сетки
            pictureBox1.Image = (Image) new Bitmap(pictureBox1.Width, pictureBox1.Height);
            var g = Graphics.FromImage(pictureBox1.Image);
            var p = new Pen(Color.DarkSlateGray);

            for (var i = 0; i < 10; i++)
            {
                g.DrawLine(p, pictureBox1.Width / 10 * (i + 1), 0, pictureBox1.Width / 10 * (i + 1), pictureBox1.Height);
            }
            for (var i = 0; i < 20; i++)
            {
                g.DrawLine(p, 0, pictureBox1.Height / 20 * (i + 1), pictureBox1.Width, pictureBox1.Height / 20 * (i + 1));
            }

            //красная линия
            g.DrawLine(Pens.Red, 0, pictureBox1.Height / 20 * 3, pictureBox1.Width, pictureBox1.Height / 20 * 3);

            //инициализация массива/поля
            InitializeField();

            //текущий уровень сложности
            labelLevel.Text = $"Level: {level}";

            //инициализация массива координат следущей фигуры
            for (var i = 0; i < 3; i++)
            {
                for (var j = 0; j < 4; j++)
                {
                    smallField[i, j] = new Component((j * width) + 1, (i * width) + 1, Brushes.Black);
                }
            }

            //pictureBox на котором будет отображаться следущая фигура
            PictureNextFigure = new PictureBox
            {
                Location  = new Point(324, 91),
                Size      = new Size(101, 76),
                BackColor = Color.Black
            };

            //добавляем контрол к форме
            this.Controls.Add(PictureNextFigure);

            //обработчик перерисовки поля
            PictureNextFigure.Paint += (s, e) =>
            {
                for (var i = 0; i < 3; i++)
                {
                    for (var j = 0; j < 4; j++)
                    {
                        //если в текущей клетке стоит фигура рисуем квадрат
                        if (smallField[i, j].brush != Brushes.Black)
                        {
                            e.Graphics.FillRectangle(smallField[i, j].brush
                                                     , smallField[i, j].x, smallField[i, j].y, width - 1, width - 1);
                            //и подквадрат
                            e.Graphics.DrawRectangle(myPen, smallField[i, j].x + 3, smallField[i, j].y + 3,
                                                     width - 7, width - 7);
                        }
                    }
                }
            };

            labelPause.Text    = @"TETRIS";
            labelPause.Visible = true;
            soundControl.playMain();
        }