Пример #1
0
        public static SnakeForm ForAutomatedControl(int speedUpRate = 1)
        {
            var form = new SnakeForm {
                _isAutomatedControlled = true
            };

            form.GameTimer.Interval /= speedUpRate;
            return(form);
        }
Пример #2
0
 public SnakePlayer(SnakeForm Form)
 {
     m_SnakeParts.Add(new BodyPart(100, 0, Direction.right));
     m_SnakeParts.Add(new BodyPart(80, 0, Direction.right));
     m_SnakeParts.Add(new BodyPart(60, 0, Direction.right));
     m_MoveDirection = Direction.right;
     m_PendingSegments = 0;
     GameForm = Form;
 }
Пример #3
0
        public GameController(SnakeForm form)
        {
            gameView = form;
            gameStick = new GameStick(this);
            gameStick.OpenPort("COM5");

            MAX_WIDTH = gameView.getCanvas().Width / TILE_WIDTH;
            MAX_HEIGHT = gameView.getCanvas().Height / TILE_WIDTH;

            gameTimer = new Timer();
            gameTimer.Interval = 100;
            gameTimer.Tick += new EventHandler(Step);
        }
Пример #4
0
        //alt:

        /*public void SetSpawnBody2(int start, String dir)
         * {
         *  int max = 300;
         *  int s = (start < 60) ? 60 : start;
         *  s = (s > (max - 60)) ? (max - 60) : s;
         *
         *  if (dir == "right")
         *  {
         *      m_SnakeParts.Add(new BodyPart(s, 0, Direction.right));
         *      m_SnakeParts.Add(new BodyPart(s - 20, 0, Direction.right));
         *      m_SnakeParts.Add(new BodyPart(s - 40, 0, Direction.right));
         *  }
         *  else if (dir == "left")
         *  {
         *      m_SnakeParts.Add(new BodyPart(s, 0, Direction.left));
         *      m_SnakeParts.Add(new BodyPart(s + 20, 0, Direction.left));
         *      m_SnakeParts.Add(new BodyPart(s + 40, 0, Direction.left));
         *  }
         *  else if (dir == "down")
         *  {
         *      m_SnakeParts.Add(new BodyPart(0, s, Direction.down));
         *      m_SnakeParts.Add(new BodyPart(0, s - 20, Direction.down));
         *      m_SnakeParts.Add(new BodyPart(0, s - 40, Direction.down));
         *  }
         *  else if (dir == "up")
         *  {
         *      m_SnakeParts.Add(new BodyPart(0, s, Direction.up));
         *      m_SnakeParts.Add(new BodyPart(0, s + 20, Direction.up));
         *      m_SnakeParts.Add(new BodyPart(0, s + 40, Direction.up));
         *  }
         *
         * }*/
        //End
        public SnakePlayer(SnakeForm Form, int startX, int startY, Direction dir)
        {
            // Add 3 body parts to the snake because the snake begins small
            SetSpawnBody(startX, startY, dir);

            // Need to give an initial direction
            m_MoveDirection = dir;
            this.SetDirection(dir);//Sam: redundancy in attempt to avoid miss directed start

            // Currently no body parts queued to be added
            m_PendingSegments = 0;
            GameForm          = Form;
        }
Пример #5
0
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            SnakeForm snakeForm = new SnakeForm();
            GameController game = new GameController(snakeForm);
            snakeForm.game = game;

            game.start();

            Application.Run(snakeForm);
        }
Пример #6
0
        private SnakeForm GameForm = null;                            // Stores the GUI form

        /// <summary>
        /// Object constructor
        /// </summary>
        /// <param name="Form">GUI form for the game</param>
        public SnakePlayer(SnakeForm Form)
        {
            // Add 3 body parts to the snake because the snake begins small
            m_SnakeParts.Add(new BodyPart(100, 0, Direction.right));
            m_SnakeParts.Add(new BodyPart(80, 0, Direction.right));
            m_SnakeParts.Add(new BodyPart(60, 0, Direction.right));

            // Need to give an initial direction
            m_MoveDirection = Direction.right;

            // Currently no body parts queued to be added
            m_PendingSegments = 0;
            GameForm          = Form;
        }
Пример #7
0
        // 物件生成
        public SnakePlayer(SnakeForm Form)
        {
            // 蛇身初始化,3粒
            m_SnakeParts.Add(new BodyPart(100, 0, Direction.right));
            m_SnakeParts.Add(new BodyPart(80, 0, Direction.right));
            m_SnakeParts.Add(new BodyPart(60, 0, Direction.right));

            // 初始方向
            m_MoveDirection = Direction.right;

            // 無待生成蛇身
            m_PendingSegments = 0;
            GameForm          = Form;
        }
Пример #8
0
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            // !!!!! Manual !!!!!
            //Application.Run(new SnakeForm());

            // !!!!! Automated !!!!!
            _gameForm = SnakeForm.ForAutomatedControl(1); // 2x

            Evolution.GeneticEvolution.BeforeNaturalSelection += (individual, generation) =>
            {
                //individual.AvoidLevel = 1;
                //individual.EagerLevel = 100;
                //individual.WallAwareness = new[] { 100d, 100, 100, 100 };
                //individual.FoodAwareness = new[] { 100d, 100, 100, 100 };

                individual.SetGeneration(generation);
                _gameForm.OnSnakeMove += individual.SnakeGame_OnSnakeMove;
                _gameForm.OnGameOver  += individual.SnakeGame_OnGameOver;
                _gameForm.StartNewGame();
            };

            Evolution.GeneticEvolution.AfterNaturalSelection += (individual, generation) =>
            {
                _gameForm.OnSnakeMove -= individual.SnakeGame_OnSnakeMove;
                _gameForm.OnGameOver  -= individual.SnakeGame_OnGameOver;
            };

            Evolution.GeneticEvolution.GenerationEvolved += result =>
            {
                _gameForm.Invoke((Action)(() => _gameForm.SetGenerationResult(result)));
            };

            Evolution.Start();
            Application.Run(_gameForm);
        }
Пример #9
0
        /// <summary>
        /// Object constructor
        /// </summary>
        /// <param name="Form">GUI form for the game</param>
        public SnakePlayer(SnakeForm Form)
        {
            GameForm = Form;
            //randomize snake starting position
            int headX, headY, bodyX, bodyY, tailX, tailY;

            headX = random.Next(GameForm.Canvas.Left + 60, GameForm.Canvas.Right - 60);
            headY = random.Next(GameForm.Canvas.Top + 60, GameForm.Canvas.Bottom - 60);
            bodyX = headX - 20;
            bodyY = headY;
            tailX = bodyX - 20;
            tailY = bodyY;
            // Add 3 body parts to the snake because the snake begins small
            m_SnakeParts.Add(new BodyPart(headX, headY, Direction.right));
            m_SnakeParts.Add(new BodyPart(bodyX, bodyY, Direction.right));
            m_SnakeParts.Add(new BodyPart(tailX, tailY, Direction.right));

            // Need to give an initial direction
            m_MoveDirection = Direction.right;

            // Currently no body parts queued to be added
            m_PendingSegments = 0;
        }
Пример #10
0
 private void btn2Player_Click(object sender, EventArgs e)
 {
     s = new SnakeForm(this, true);
     s.Show();
     this.Hide();
 }
Пример #11
0
        static void Main(string[] args)
        {
            SnakeForm MainForm = new SnakeForm();

            MainForm.ShowDialog();
        }