예제 #1
0
파일: Form1.cs 프로젝트: RomSnow/NewPacman
        public LevelForm(Map levelMap)
        {
            images      = PrepareImage();
            playerImage = images["pacman-left.png"];
            map         = levelMap;
            table       = new TableLayoutPanel();
            var rowCount    = map.Field.GetLength(0);
            var columnCount = map.Field.GetLength(1);

            for (var r = 0; r < rowCount; r++)
            {
                for (var c = 0; c < columnCount; c++)
                {
                    table.RowStyles.Add(
                        new RowStyle(SizeType.Percent, 100f / rowCount)
                        );
                    table.ColumnStyles.Add(
                        new ColumnStyle(SizeType.Percent, 100f / columnCount)
                        );
                }
            }

            var progressBar = new Label
            {
                Location = new Point(0, 0),
                Size     = new Size(ClientSize.Width, 30),
                Text     = "Lives: " + map.HealthPoints.ToString() + "; Score: " + map.Score.ToString() + "; Iteration: " + counter.ToString()
            };

            for (var row = 0; row < rowCount; row++)
            {
                for (var column = 0; column < columnCount; column++)
                {
                    var image = Drawing(row, column);

                    var picture = new PictureBox()
                    {
                        Image    = image,
                        SizeMode = PictureBoxSizeMode.Zoom
                    };

                    picture.Dock = DockStyle.Fill;
                    table.Controls.Add(picture, column, row);
                }
            }
            table.Padding = Padding.Empty;
            table.Dock    = DockStyle.Fill;
            Controls.Add(progressBar);
            Controls.Add(table);

            timer = new Timer()
            {
                Interval = 100
            };
            timer.Tick += (sender, args) =>
            {
                if (this == null)
                {
                    timer.Stop();
                    Close();
                }
                map.Update();
                DrawLevel(progressBar);
                if (map.IsGameOver)
                {
                    timer.Stop();
                    this.DialogResult = DialogResult.OK;
                    Close();
                }
            };
            timer.Start();
            KeyDown += (sender, args) =>
            {
                KeyHandler(args);
            };
        }