Beispiel #1
0
 private void btnStart_Click(object sender, EventArgs e)
 {
     if (sgv != null)
     {
         DisposeGame();
     }
     try
     {
         Snake.Debug("create view");
         sgv = new SnakeGameView(40, 40);
         Snake.Debug("create model");
         sgm = new SnakeGameModel(40, 40);
         Snake.Debug("create controller");
         sgc = new SnakeGameController();
         Snake.Debug("attach model");
         sgc.AddModel(sgm);
         Snake.Debug("attach view");
         sgm.AttachObserver(sgv);
         sgm.AttachObserver(this);
         Snake.Debug("set controller");
         sgv.setController(sgc);
         Snake.Debug("Start the controller");
         sgc.Start();
         sgv.Run();
         SnakeGameModel.Speed = 1;
     } catch
     {
         Snake.Debug("Error starting game");
     }
 }
Beispiel #2
0
 private void button1_Click(object sender, EventArgs e)
 {
     if (sgv2x != null)
     {
         DisposeGame();
     }
     try
     {
         Snake.Debug("create view");
         sgv2x = new SnakeGameView2x(80, 80);
         Snake.Debug("create model");
         sgm = new SnakeGameModel(80, 80);
         Snake.Debug("create controller");
         sgc = new SnakeGameController();
         Snake.Debug("attach model");
         sgc.AddModel(sgm);
         Snake.Debug("attach view");
         sgm.AttachObserver(sgv2x);
         sgm.AttachObserver(this);
         Snake.Debug("set controller");
         sgv2x.setController(sgc);
         Snake.Debug("Start the controller");
         sgm.resetSpeed();
         sgc.Start();
         sgv2x.Run();
     }
     catch
     {
         Snake.Debug("Error starting game");
     }
 }
Beispiel #3
0
 public void DisposeGame()
 {
     sgm = null;
     sgv.Dispose();
     sgv = null;
     sgv = null;
 }
        public void KeyUpHandled(KeyboardState ks)
        {
            int direction = -1;

            Keys[] keys = ks.GetPressedKeys();

            if (keys.Contains(Keys.Up))
            {
                direction = SnakeGameModel.MOVE_UP;
            }
            else if (keys.Contains(Keys.Down))
            {
                direction = SnakeGameModel.MOVE_DOWN;
            }
            else if (keys.Contains(Keys.Left))
            {
                direction = SnakeGameModel.MOVE_LEFT;
            }
            else if (keys.Contains(Keys.Right))
            {
                direction = SnakeGameModel.MOVE_RIGHT;
            }
            // Find all snakeboard model we know
            if (direction == -1)
            {
                return;
            }
            SnakeGameModel sbm = (SnakeGameModel)model;

            sbm.SetDirection(direction);
        }
        public void KeyUpHandled(KeyboardState ks)
        {
            int direction = -1;

            Keys[] keys = ks.GetPressedKeys();
            if (keys.Contains(Keys.Space))
            {
                Stop();
                //System.Threading.Thread.Sleep(1000);
            }
            else if (keys.Contains(Keys.Up))
            {
                Start();
                direction = SnakeGameModel.MOVE_UP;
            }
            else if (keys.Contains(Keys.Down))
            {
                Start();
                direction = SnakeGameModel.MOVE_DOWN;
            }
            else if (keys.Contains(Keys.Left))
            {
                Start();
                direction = SnakeGameModel.MOVE_LEFT;
            }
            else if (keys.Contains(Keys.Right))
            {
                Start();
                direction = SnakeGameModel.MOVE_RIGHT;
            }

            // Find all snakeboard model we know


            /*  if (direction == -5 && pause==false) {
             * // memtime = timer;
             *  Stop();
             *  pause = true;
             * }
             * else if (direction == -5 && pause == true)
             * {
             * // timer = memtime;
             *  Start();
             *  pause = false;
             * }*/
            if (direction == -1)
            {
                return;
            }
            foreach (Model m in mList)
            {
                if (m is SnakeGameModel)
                {
                    // Tell the model to update
                    SnakeGameModel sbm = (SnakeGameModel)m;
                    sbm.SetDirection(direction);
                }
            }
        }
Beispiel #6
0
 public void Notify(Model m)
 {
     if (m is SnakeGameModel)
     {
         SnakeGameModel sbm = m as SnakeGameModel;
         SetScore((sbm.SnakeLength() - SnakeGameModel.SNAKE_INIT_SIZE).ToString());
     }
 }
        private void OnTimedEvent(Object source, ElapsedEventArgs e)
        {
            Snake.Debug("TE");
            SnakeGameModel sbm = (SnakeGameModel)model;

            sbm.Move();
            sbm.Update();
            timer.Interval = SnakeGameModel.TIME_BASE / sbm.Speed;
        }
Beispiel #8
0
        public void KeyUpHandled(KeyboardState ks)
        {
            int direction = -1;

            Keys[] keys = ks.GetPressedKeys();

            if (keys.Contains(Keys.Up))
            {
                direction = SnakeGameModel.MOVE_UP;
            }
            else if (keys.Contains(Keys.Down))
            {
                direction = SnakeGameModel.MOVE_DOWN;
            }
            else if (keys.Contains(Keys.Left))
            {
                direction = SnakeGameModel.MOVE_LEFT;
            }
            else if (keys.Contains(Keys.Right))
            {
                direction = SnakeGameModel.MOVE_RIGHT;
            }
            else if (keys.Contains(Keys.Space))
            {
                if (!ispause)
                {
                    Stop();
                    ispause = true;
                    System.Threading.Thread.Sleep((int)System.TimeSpan.FromSeconds(0.25).TotalMilliseconds);
                }
                else
                {
                    Start();
                    ispause = false;
                    System.Threading.Thread.Sleep((int)System.TimeSpan.FromSeconds(0.25).TotalMilliseconds);
                }
            }
            // Find all snakeboard model we know
            if (direction == -1)
            {
                return;
            }
            foreach (Model m in mList)
            {
                if (m is SnakeGameModel)
                {
                    // Tell the model to update
                    SnakeGameModel sbm = (SnakeGameModel)m;
                    sbm.SetDirection(direction);
                }
            }
        }
        public void KeyUpHandled(KeyboardState ks)
        {
            int direction = -1;

            Keys[] keys = ks.GetPressedKeys();

            if (keys.Contains(Keys.Up))
            {
                direction = SnakeGameModel.MOVE_UP;
            }
            else if (keys.Contains(Keys.Down))
            {
                direction = SnakeGameModel.MOVE_DOWN;
            }
            else if (keys.Contains(Keys.Left))
            {
                direction = SnakeGameModel.MOVE_LEFT;
            }
            else if (keys.Contains(Keys.Right))
            {
                direction = SnakeGameModel.MOVE_RIGHT;
            }
            else if (keys.Contains(Keys.Space))
            {
                if (!TimeSpace)
                {
                    Stop();
                    TimeSpace = true;
                }
                else
                {
                    Start();
                    TimeSpace = false;
                }
            }

            // Find all snakeboard model we know
            if (direction == -1)
            {
                return;
            }
            foreach (Model m in mList)
            {
                if (m is SnakeGameModel)
                {
                    // Tell the model to update
                    SnakeGameModel sbm = (SnakeGameModel)m;
                    sbm.SetDirection(direction);
                }
            }
        }
Beispiel #10
0
 private void OnTimedEvent(Object source, ElapsedEventArgs e)
 {
     Snake.Debug("TE");
     foreach (Model m in mList)
     {
         if (m is SnakeGameModel)
         {
             SnakeGameModel sbm = (SnakeGameModel)m;
             sbm.Move();
             sbm.Update();
         }
     }
     timer.Interval = SnakeGameModel.TIME_BASE / SnakeGameModel.Speed;
 }
Beispiel #11
0
        public void Notify(Model m)
        {
            // if it's a SnakeBoardModel, then we know how to handle it
            if (m is SnakeGameModel)
            {
                sbm = (SnakeGameModel)m;
                if (sbm.isHit)
                {
                    controller.Stop();
                    MessageBox.Show("Game over!!, your score is " + (sbm.SnakeLength() - SnakeGameModel.SNAKE_INIT_SIZE));
                }

                if (sbm.isEating)
                {
                    Snake.Debug("Eating");
                }
            }
        }
Beispiel #12
0
 private void RunGame()
 {
     Snake.Debug("create view");
     sgv = new SnakeGameView(40, 40);
     Snake.Debug("create model");
     sgm = new SnakeGameModel(40, 40);
     Snake.Debug("create controller");
     sgc = new SnakeGameController();
     Snake.Debug("attach model");
     sgc.AddModel(sgm);
     Snake.Debug("attach view");
     sgm.AttachObserver(sgv);
     sgm.AttachObserver(this);
     Snake.Debug("set controller");
     sgv.setController(sgc);
     Snake.Debug("Start the controller");
     sgc.Start();
     sgv.Run();
 }