public Body(BodyView view, Vector2 position, DirectionSnake direction) { View = view; IsPause = false; Position = position; Direction = NextDirection = direction; NextPosition = GetNextPosition(); }
public void SetNextDirection(DirectionSnake direction) { if (_moveTimeRest > Constants.CELL_LENGTH_HALF) { return; } var newDirection = direction; if (newDirection == DirectionSnake.NONE) { return; } if (_gameData.IsCamera2D) { var isNewDirectionVertical = newDirection == DirectionSnake.UP || newDirection == DirectionSnake.DOWN; var isCurDirectionVertical = _headData.IsVecticalMoving(); if (isCurDirectionVertical ^ isNewDirectionVertical) { _headData.SetNextDirection(newDirection); } } else { if (newDirection == DirectionSnake.LEFT) { switch (_headData.Direction) { case DirectionSnake.LEFT: _headData.SetNextDirection(DirectionSnake.DOWN); break; case DirectionSnake.UP: _headData.SetNextDirection(DirectionSnake.LEFT); break; case DirectionSnake.RIGHT: _headData.SetNextDirection(DirectionSnake.UP); break; case DirectionSnake.DOWN: _headData.SetNextDirection(DirectionSnake.RIGHT); break; default: throw new Exception("wtf"); } } else if (newDirection == DirectionSnake.RIGHT) { switch (_headData.Direction) { case DirectionSnake.LEFT: _headData.SetNextDirection(DirectionSnake.UP); break; case DirectionSnake.UP: _headData.SetNextDirection(DirectionSnake.RIGHT); break; case DirectionSnake.RIGHT: _headData.SetNextDirection(DirectionSnake.DOWN); break; case DirectionSnake.DOWN: _headData.SetNextDirection(DirectionSnake.LEFT); break; default: throw new Exception("wtf"); } } } }
public GameViewModel() { CanvasWidth = 400; CanvasHeight = 400; currPosX = 0; currPosY = 0; sizeRect = 20; currColor = "Black"; directionSnake = DirectionSnake.Right; backgroundOk = false; CountFoodOnField = 3; SnakeSpeed = 500; numberCellsHoriz = CanvasWidth / sizeRect; numberCellsVert = CanvasHeight / sizeRect; randomPos = new Random(); elements = new ObservableCollection <Figure>(); gameTickTimer = new System.Windows.Threading.DispatcherTimer(); UpCommand = new RelayCommand(new Action <object>(btn_UpPressed)); DownCommand = new RelayCommand(new Action <object>(btn_DownPressed)); LeftCommand = new RelayCommand(new Action <object>(btn_LeftPressed)); RightCommand = new RelayCommand(new Action <object>(btn_RightPressed)); TestСommand = new RelayCommand(new Action <object>(Test)); // Риусем фон. while (!backgroundOk) { while (currPosY < CanvasHeight) { if (currColor == "Black") { currColor = "Yellow"; } else { currColor = "Black"; } while (currPosX < CanvasWidth) { elements.Add(new RectField { Left = currPosX, Top = currPosY, Color = currColor }); currPosX += sizeRect; if (currColor == "Black") { currColor = "Yellow"; } else { currColor = "Black"; } } currPosX = 0; currPosY += sizeRect; } backgroundOk = true; } // Нарисуем первоначальное положение змейки в начале игры. for (int i = 0; i < 3; i++) { snakeHeadX++; elements.Add(new SnakePart { Left = snakeHeadX * sizeRect, Top = snakeHeadY * sizeRect, Color = "Green", IsHead = false, ElementType = "Snake" }); currSnakeLength++; } // Добавим заданное кол-во еды на игровое поле. for (int i = 0; i < CountFoodOnField; i++) { AddFood(); } // Запустим таймер, который будет периодически вызывать метод DrawPartSnake - для движения змейки. gameTickTimer.Tick += DrawPartSnake; gameTickTimer.Interval = TimeSpan.FromMilliseconds(SnakeSpeed); gameTickTimer.IsEnabled = true; }
private void btn_RightPressed(object sender) { //MessageBox.Show("Вправо"); directionSnake = DirectionSnake.Right; }
private void btn_LeftPressed(object sender) { //MessageBox.Show("Влево"); directionSnake = DirectionSnake.Left; }
private void btn_DownPressed(object sender) { //MessageBox.Show("Вниз"); directionSnake = DirectionSnake.Down; }
private void btn_UpPressed(object sender) { //essageBox.Show("Вверх"); directionSnake = DirectionSnake.Up; }
public void SetNextDirection(DirectionSnake direction) { NextDirection = direction; }