public static void Main() { var path = Directory.GetCurrentDirectory(); var field = new Field(); var gamer = new Gamer(); var enemy = new Enemy(); var box = new RandomBox(); Application.Run(new MyForm(field, gamer, enemy, box) { ClientSize = new Size(750, 950), BackColor = Color.Black, BackgroundImage = Image.FromFile(path + "\\Sprites\\background.png") }); }
private MyForm(Field field, Gamer gamer, Enemy enemy, RandomBox box) { FormBorderStyle = FormBorderStyle.Fixed3D; MaximizeBox = false; var countCoins = 0; var timerEnemy = new Timer(); var timerPlayer = new Timer(); var timerCoin = new Timer(); var path = AppDomain.CurrentDomain.BaseDirectory; var direction = ""; var pointCount = new Label() { ForeColor = Color.Red, Location = new Point(355, 415), Size = new Size(42, 20), TextAlign = ContentAlignment.MiddleCenter }; pointCount.Font = new Font(pointCount.Font.FontFamily, 15.0f, pointCount.Font.Style); MakeMusic(path); Controls.Add(pointCount); Controls.Add(enemy.EnemyPicYellow); Controls.Add(enemy.EnemyPicRed); Controls.Add(gamer.PlayerPic); Controls.Add(box.boxPic); field.FillField(); CreateField(field); timerEnemy.Start(); timerEnemy.Interval = 450; timerPlayer.Start(); timerPlayer.Interval = 200; timerCoin.Start(); timerCoin.Interval = 300; this.KeyDown += (sender, args) => { direction = gamer.DirectionPlayer(field, gamer, args, direction); }; timerCoin.Tick += (sender, args) => { if (countCoins != 50) { return; } enemy.TransformEnemy(); timerEnemy.Interval = 350; timerCoin.Stop(); }; timerPlayer.Tick += (sender, args) => { gamer.AutoMoving(field.Dic, direction, enemy.EnemyPicRed, enemy.EnemyPicYellow, box); pointCount.Text = countCoins.ToString(); }; timerEnemy.Tick += (sender, args) => { Enemy.Moving(gamer, field.Dic, enemy.EnemyPicYellow); Enemy.Moving(gamer, field.Dic, enemy.EnemyPicRed); }; gamer.PlayerChanged += (sender) => { gamer.PlayerPic.Image = Image.FromFile(path + "\\Sprites\\" + sender); }; gamer.CoinChanged += (dic, playerX, playerY) => { if (!dic[new Point(playerX, playerY)].Visible) { return; } dic[new Point(playerX, playerY)].Hide(); countCoins++; }; box.BoxChanged += (random) => { var label = new Label() { Location = new Point(0, 0), ForeColor = Color.FromArgb(255, Color.Aqua), TextAlign = ContentAlignment.MiddleCenter, Size = new Size(ClientSize.Width, 30) }; label.Font = new Font(pointCount.Font.FontFamily, 15.0f, pointCount.Font.Style); var timer = new Timer(); var flag = true; switch (random) { case 0: label.Text = "STAND"; Controls.Add(label); timer.Start(); timer.Interval = 1; timer.Tick += (sender, args) => { if (flag) { timer.Interval = 5000; timerEnemy.Stop(); flag = false; } else { timerEnemy.Start(); timer.Stop(); label.Hide(); } }; break; case 1: label.Text = "SPEED"; Controls.Add(label); SwitchBox(timer, timerPlayer, null, label); break; case 2: label.Text = "UPGRADE"; Controls.Add(label); SwitchBox(timer, null, enemy, label); break; } }; }
public void AutoMoving(Dictionary <Point, PictureBox> dic, string direction, PictureBox enemyRed, PictureBox enemyYellow, RandomBox box) { var playerX = PlayerPic.Location.X; var playerY = PlayerPic.Location.Y; if (PlayerPic.Location == enemyRed.Location) { Application.Exit(); } if (PlayerPic.Location == enemyYellow.Location) { Application.Exit(); } if (PlayerPic.Location == box.boxPic.Location) { box.Roulete(); } switch (direction) { case "Right" when playerX + 50 == 750 && playerY == 400: CoinChanged?.Invoke(dic, playerX, playerY); PlayerPic.Location = new Point(0, 400); PlayerChanged?.Invoke("playerRightGIF.gif"); break; case "Right" when CanMove(dic, playerX + 50, playerY): CoinChanged?.Invoke(dic, playerX, playerY); PlayerPic.Left += 50; break; case "Left" when playerX - 50 == -50 && playerY == 400: CoinChanged?.Invoke(dic, playerX, playerY); PlayerPic.Location = new Point(700, 400); PlayerChanged?.Invoke("playerLeftGIF.gif"); break; case "Left" when CanMove(dic, playerX - 50, playerY): CoinChanged?.Invoke(dic, playerX, playerY); PlayerPic.Left -= 50; break; case "Up" when CanMove(dic, playerX, playerY - 50): CoinChanged?.Invoke(dic, playerX, playerY); PlayerPic.Top -= 50; break; case "Down" when CanMove(dic, playerX, playerY + 50): CoinChanged?.Invoke(dic, playerX, playerY); PlayerPic.Top += 50; break; } }