예제 #1
0
    // Start is called before the first frame update
    void Start()
    {
        numWins = 0;

        moving   = false;
        rotating = false;

        pStepDistance = 1f / fpp;
        Debug.Log(pStepDistance);

        rStepDistance = 1f / fpr;
        Debug.Log(rStepDistance);

        currentKey = MoveKey.none;

        moveNow = true;
    }
예제 #2
0
        public Point GetNewPoint(MoveKey e, Point oldPoint)
        {
            switch (e)
            {
            case MoveKey.Up:
                return(new Point(oldPoint.X, oldPoint.Y - 1));

            case MoveKey.Down:
                return(new Point(oldPoint.X, oldPoint.Y + 1));

            case MoveKey.Left:
                return(new Point(oldPoint.X - 1, oldPoint.Y));

            case MoveKey.Right:
                return(new Point(oldPoint.X + 1, oldPoint.Y));

            default:
                throw new ArgumentException("Invalid move key value: " + e);
            }
        }
예제 #3
0
파일: Game.cs 프로젝트: andre197/SnakeGame
        private void Move(object sender, MoveKey e)
        {
            lock (_lockObject)
            {
                if (_cancellationToken.IsCancellationRequested)
                {
                    return;
                }

                Point newPoint = _newPointFactory.GetNewPoint(e, _snake.HeadCoordinates);
                var   result   = _snake.MoveHead(newPoint);
                if (!result)
                {
                    return;
                }

                _lastMoveKeyPressed = e;
                _skipAutoMove       = true;
                if (newPoint != _foodPosition)
                {
                    _snake.MoveTail();
                }
                else
                {
                    _score++;
                    WriteScore();
                    SpawnNewFood();
                }

                _snake.Draw(_writerService);

                if (_snake.HasBittenHerself || _gameArea.IsPointOnEdgeOfGameArea(_snake.HeadCoordinates))
                {
                    _cancellationToken.Cancel();
                }
            }
        }
예제 #4
0
    void Move()
    {
        if (!p1KeyBoard)
        {
            horizontalAxis = Input.GetAxisRaw("HorizontalJoy");
            verticalAxis   = Input.GetAxisRaw("VerticalJoy");
            if (!inverseControl)
            {
                if (horizontalAxis == 1)
                {
                    lastKey = MoveKey.Right;
                }
                else if (horizontalAxis == -1)
                {
                    lastKey = MoveKey.Left;
                }
                else if (verticalAxis == 1)
                {
                    lastKey = MoveKey.Up;
                }
                else if (verticalAxis == -1)
                {
                    lastKey = MoveKey.Down;
                }
            }
            else
            {
                if (horizontalAxis == -1)
                {
                    lastKey = MoveKey.Right;
                }
                else if (horizontalAxis == 1)
                {
                    lastKey = MoveKey.Left;
                }
                else if (verticalAxis == -1)
                {
                    lastKey = MoveKey.Up;
                }
                else if (verticalAxis == 1)
                {
                    lastKey = MoveKey.Down;
                }
            }
        }

        if (p1KeyBoard)
        {
            if (!inverseControl)
            {
                if (Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.D))
                {
                    lastKey = MoveKey.Right;
                }
                else if (Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.A))
                {
                    lastKey = MoveKey.Left;
                }
                else if (Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.W))
                {
                    lastKey = MoveKey.Up;
                }
                else if (Input.GetKeyDown(KeyCode.DownArrow) || Input.GetKeyDown(KeyCode.S))
                {
                    lastKey = MoveKey.Down;
                }
            }
            else
            {
                if (Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.A))
                {
                    lastKey = MoveKey.Right;
                }
                else if (Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.D))
                {
                    lastKey = MoveKey.Left;
                }
                else if (Input.GetKeyDown(KeyCode.DownArrow) || Input.GetKeyDown(KeyCode.S))
                {
                    lastKey = MoveKey.Up;
                }
                else if (Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.W))
                {
                    lastKey = MoveKey.Down;
                }
            }
        }



        if (lastKey != MoveKey.None)
        {
            CountDown.started = true;
        }


        if (timeCounter > 0)
        {
            timeCounter -= Time.deltaTime;
            return;
        }

        if (herbasFlying)
        {
            GetComponent <Rigidbody> ().useGravity = false;
            flyingTime = (Time.time - trueTime);
            if (flyingTime >= flyingDuration)
            {
                herbasFlying = false;
                flyingTime   = 0f;
            }
        }

        if (!herbasFlying)
        {
            GetComponent <Rigidbody> ().useGravity = true;
            if (onAir)
            {
                return;
            }
        }

        if (lastKey == MoveKey.Right)
        {
            TargetPosition     = new Vector3(targetX + 1f, jumpHeight, targetZ);
            targetX           += 1f;
            transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(new Vector3(1f, 0f, 0f)), 1f);
            shinyCubePosition  = new Vector3(targetX, 0, targetZ);
            shinyCubePosition2 = new Vector3(targetX + 1f, 0, targetZ);
        }

        if (lastKey == MoveKey.Left)
        {
            TargetPosition     = new Vector3(targetX - 1f, jumpHeight, targetZ);
            targetX           -= 1f;
            transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(new Vector3(-1f, 0f, 0f)), 1f);
            shinyCubePosition  = new Vector3(targetX, 0, targetZ);
            shinyCubePosition2 = new Vector3(targetX - 1f, 0, targetZ);
        }

        if (lastKey == MoveKey.Up)
        {
            TargetPosition     = new Vector3(targetX, jumpHeight, targetZ + 1f);
            targetZ           += 1f;
            transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(new Vector3(0f, 0f, 1f)), 1f);
            shinyCubePosition  = new Vector3(targetX, 0, targetZ);
            shinyCubePosition2 = new Vector3(targetX, 0, targetZ + 1f);
        }

        if (lastKey == MoveKey.Down)
        {
            TargetPosition     = new Vector3(targetX, jumpHeight, targetZ - 1f);
            targetZ           -= 1f;
            transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(new Vector3(0f, 0f, -1f)), 1f);
            shinyCubePosition  = new Vector3(targetX, 0, targetZ);
            shinyCubePosition2 = new Vector3(targetX, 0, targetZ - 1f);
        }

        if (horizontalAxis == 0)
        {
            isHorizontalAxisInUse = false;
        }

        if (verticalAxis == 0)
        {
            isVerticalAxisInUse = false;
        }

        timeCounter = jumpTime;
    }
예제 #5
0
    // Update is called once per frame
    void Update()
    {
        if (gameActive)
        {
            if (moving)
            {
                Move();
            }
            if (rotating)
            {
                Rotate();
            }
        }
        else
        {
            moving   = false;
            rotating = false;
        }

        if (Input.GetKeyDown(manager.keys["up"]))
        {
            currentKey = MoveKey.up;

            if (!moving)
            {
                moveNow = true;
            }
        }
        else if (Input.GetKeyDown(manager.keys["down"]))
        {
            currentKey = MoveKey.down;

            if (!moving)
            {
                moveNow = true;
            }
        }
        else if (Input.GetKeyDown(manager.keys["left"]))
        {
            currentKey = MoveKey.left;

            if (!moving)
            {
                moveNow = true;
            }
        }
        else if (Input.GetKeyDown(manager.keys["right"]))
        {
            currentKey = MoveKey.right;

            if (!moving)
            {
                moveNow = true;
            }
        }
        else if (!Input.anyKey)
        {
            currentKey = MoveKey.none;
        }

        if (gameActive)
        {
            if (!moving && moveNow && currentKey != MoveKey.none)
            {
                if (currentKey == MoveKey.up)
                {
                    StartMove(Vector2.up);
                }
                if (currentKey == MoveKey.down)
                {
                    StartMove(Vector2.down);
                }
                if (currentKey == MoveKey.left)
                {
                    StartMove(Vector2.left);
                }
                if (currentKey == MoveKey.right)
                {
                    StartMove(Vector2.right);
                }

                currentKey = MoveKey.none;
                moveNow    = false;
            }

            timer += Time.deltaTime;
        }
    }
예제 #6
0
 private bool GetKeyDown(MoveKey key)
 {
     return(Input.GetKeyDown((KeyCode)controlKeys[key]));
 }