//初始化场景 public override void Init() { b = true; m_key = false; fen = 0; Score = new Label(); Score.Content = "总分: 0"; Score.Width = 200; Score.Height = 50; Score.FontSize = 22; Score.FontFamily = new FontFamily("Consolas"); Canvas.SetLeft(Score, 600); Canvas.SetTop(Score, 20); Snake = new CHero(50, 50, 30, 30, 2); Food = new CFood(350, 200, 15, 15); if (MainWindow.On) { Snake.SetSpeed(2); } else { Snake.SetSpeed(30); } background = new CCustomImage(Properties.Resources.initpintu, 795, 415); snake = new CCustomImage(Properties.Resources.head, Snake.GetW(), Snake.GetH()); food = new CCustomImage(Properties.Resources.brick, Food.GetW(), Food.GetH()); pause = new CCustomImage(Properties.Resources.pause1, 30, 30); runback = new CCustomImage(Properties.Resources.runback, 30, 30); pause.SetPos(630, 60); runback.SetPos(680, 60); background.SetPos(-5, 0); food.SetPos(Food.GetX(), Food.GetY()); snake.SetPos(Snake.GetX(), Snake.GetY()); snake.SetAngle(0); pause.MouseDown += pauseRun; runback.MouseDown += runbackRun; m_SceneState = 1; }
public override void Run() { m_SceneState = 0; if (b) { if (MainWindow.On) { Snake.Move(m_key); if (Collide((int)Snake.GetX(), (int)Snake.GetY(), Snake.GetW(), Snake.GetH(), Food.GetX(), Food.GetY(), Food.GetW(), Food.GetH())) { Snake.PushBody(); Snake.ShowBody(); Food.Random(); fen += 5; } Snake.PushHeadPos(); if (Snake.GetBodyLen() * 10 + 20 >= Snake.GetHeadPosLen()) { Snake.PushHeadPos(); } else if (Snake.GetBodyLen() * 10 + 20 < Snake.GetHeadPosLen()) { Snake.EraseHeadPos(); } Snake.ChangeBody(); Score.Content = "总分: " + fen.ToString(); food.SetPos(Food.GetX(), Food.GetY()); snake.SetPos(Snake.GetX(), Snake.GetY()); snake.SetAngle((int)Snake.GetRotate()); if (Snake.GetX() > 750 || Snake.GetY() > 375 || Snake.GetX() < 15 || Snake.GetY() < 15) { MainWindow.SceneManager.SetNextScene("结束场景"); } if (fen < 10) { Snake.SetSpeed(2); } else if (fen > 15 && fen < 40) { Snake.SetSpeed(3); } else if (fen > 40 && fen < 60) { Snake.SetSpeed(4); } else if (fen > 60 && fen < 80) { Snake.SetSpeed(5); } else if (fen > 80 && fen < 150) { Snake.SetSpeed(6); } else if (fen > 150 && fen < 190) { Snake.SetSpeed(7); } else if (fen >= 195) { Snake.SetSpeed(8); } } else { Snake.Move4(); if (Collide((int)Snake.GetX(), (int)Snake.GetY(), Snake.GetW(), Snake.GetH(), Food.GetX(), Food.GetY(), Food.GetW(), Food.GetH())) { Snake.PushBody(); Snake.ShowBody(); Food.Random(); fen += 5; } Score.Content = "总分: " + fen.ToString(); Snake.ChangeBody4(); food.SetPos(Food.GetX(), Food.GetY()); snake.SetPos(Snake.GetX(), Snake.GetY()); if (Snake.GetX() > 750 || Snake.GetY() > 375 || Snake.GetX() < 15 || Snake.GetY() < 15) { MainWindow.SceneManager.SetNextScene("结束场景"); } for (int i = 0; i < Snake.GetBodyLen(); ++i) { if (Snake.GetX() == Snake.GetBodyX(i) && Snake.GetY() == Snake.GetBodyY(i)) { MainWindow.SceneManager.SetNextScene("结束场景"); } } } } }