Example #1
0
        private static void CheckKey()
        {
            if (NativeKeyboard.IsKeyDown(KeyCode.Left))
            {
                _moveCommand = new MoveLeft();
            }
            else if (NativeKeyboard.IsKeyDown(KeyCode.Right))
            {
                _moveCommand = new MoveRight();
            }

            if (NativeKeyboard.IsKeyDown(KeyCode.Space))
            {
                if (!IsSpaceDown)
                {
                    PlaySound(Sound.Shoot);
                    _shootCommand = new Shoot();
                    IsSpaceDown   = true;
                }
            }
            else
            {
                // recharge
                IsSpaceDown = false;
            }
        }
Example #2
0
 private static void KeyboardDemo(string[] args)
 {
     do
     {
         Console.WriteLine(NativeKeyboard.IsKeyDown(ConsoleKey.A));
     }while (true);
 }
Example #3
0
 static void Input()
 {
     if (NativeKeyboard.IsKeyDown(KeyCode.Right))
     {
         table.Dx += 1;
     }
     if (NativeKeyboard.IsKeyDown(KeyCode.Left))
     {
         table.Dx -= 1;
     }
 }
Example #4
0
        private bool UpdatePieceLocation(bool forceDown)
        {
            if (NativeKeyboard.IsKeyDown(KeyCode.Left))
            {
                _currentX -= DoesPieceFit(_currentPiece, _currentRotation, _currentX - 1, _currentY) ? 1 : 0;
            }

            if (NativeKeyboard.IsKeyDown(KeyCode.Right))
            {
                _currentX += DoesPieceFit(_currentPiece, _currentRotation, _currentX + 1, _currentY) ? 1 : 0;
            }

            if (NativeKeyboard.IsKeyDown(KeyCode.Down))
            {
                if (DoesPieceFit(_currentPiece, _currentRotation, _currentX, _currentY + 1))
                {
                    _currentY++;
                }
                else
                {
                    // move down failed, ask for next piece
                    return(false);
                }
            }

            if (NativeKeyboard.IsKeyDown(KeyCode.Up))
            {
                _currentRotation += !_rotateHold && DoesPieceFit(_currentPiece, _currentRotation + 1, _currentX, _currentY) ? 1 : 0;
                _rotateHold       = true;
            }
            else
            {
                _rotateHold = false;
            }

            if (forceDown)
            {
                // Test if piece can be moved down
                if (DoesPieceFit(_currentPiece, _currentRotation, _currentX, _currentY + 1))
                {
                    _currentY++; // It can, so do it!
                }
                else
                {
                    // move down failed, ask for next piece
                    return(false);
                }
            }

            return(true);
        }
Example #5
0
        public static string ReadInput()
        {
            Console.SetCursorPosition(2, 22);
            // Check if the first keypress is an arrow key
            string readline_output = "";

            if (NativeKeyboard.GetKeyState(0x26) < 0)
            {
                Player.Move(1);
            }
            else if (NativeKeyboard.GetKeyState(0x27) < 0)
            {
                Player.Move(2);
            }
            else if (NativeKeyboard.GetKeyState(0x28) < 0)
            {
                Player.Move(3);
            }
            else if (NativeKeyboard.GetKeyState(0x25) < 0)
            {
                Player.Move(4);
            }
            else if (NativeKeyboard.GetKeyState(0x31) < 0)
            {
                // Show inventory
                UI.Log("Inventory Contents:");
                foreach (Item item in Player.inventory)
                {
                    UI.Log(String.Format("{0}, qty {1}", item.name, item.qty));
                }
            }
            else if (NativeKeyboard.GetKeyState(0x32) < 0)
            {
                // Repeat action
                Player.HandleInput(Player.last_command);
            }
            else if (NativeKeyboard.GetKeyState(0x33) < 0)
            {
                // Repeat action
                Player.HandleInput(Player.second_last_command);
            }
            // The following line is too damn long
            foreach (KeyValuePair <char, int> pair in key_list)
            {
                if (NativeKeyboard.GetKeyState(pair.Value) < 0)
                {
                    readline_output = ReadLine(pair.Key);
                }
            }
            return(readline_output.ToLower().TrimEnd('\r', '\n'));
        }
        private List <KeyCode> GetPressedKeys()
        {
            var pressedKeys = new List <KeyCode>();

            foreach (var key in this.allKeys)
            {
                if (NativeKeyboard.IsKeyDown(key))
                {
                    pressedKeys.Add(key);
                }
                ;
            }

            return(pressedKeys);
        }
 public static void ModifierPressedCheckerEvent(object source, EventArgs e)
 {
     lock (locker)
     {
         if (NativeKeyboard.IsKeyDown((int)TimerModifierMap[(Timer)source]))
         {
             //Console.WriteLine(KeyTimerMapReverse[(Timer)source].ToString()+ " pressed");
             inputMap[(int)TimerModifierMap[(Timer)source]] = TimerModifierMap[(Timer)source].ToString() + " ";
         }
         else
         {
             // Console.WriteLine(KeyTimerMapReverse[(Timer)source].ToString() + " released");
             inputMap[(int)TimerModifierMap[(Timer)source]] = "";
         }
     }
 }
 public static void KeyPressedCheckerEvent(object source, EventArgs e)
 {
     lock (locker)
     {
         if (NativeKeyboard.IsKeyDown((int)TimerKeyMap[(Timer)source]))
         {
             if (TimerKeyMap[(Timer)source] == ConsoleKey.Escape)
             {
                 continueLoop = false;
             }
             //Console.WriteLine(KeyTimerMapReverse[(Timer)source].ToString()+ " pressed");
             inputMap[(int)TimerKeyMap[(Timer)source]] = TimerKeyMap[(Timer)source].ToString() + " ";
         }
         else
         {
             // Console.WriteLine(KeyTimerMapReverse[(Timer)source].ToString() + " released");
             inputMap[(int)TimerKeyMap[(Timer)source]] = "";
         }
     }
 }
Example #9
0
        public static void MainGameLoop()
        {
            var invMovingDirections = Direction.Right;

            InvadersMoveSize = Console.BufferWidth - (InvaderCount * (_invaderSize.Width + 1));
            var invMovingCounter = InvadersMoveSize;
            var loopCounter      = LoopWaitingBound;

            while (true)
            {
                if (NativeKeyboard.IsKeyDown(KeyCode.Escape))
                {
                    break;
                }

                if (NativeKeyboard.IsKeyDown(KeyCode.F10))
                {
                    EasterEgg();
                    while (Console.ReadKey(true).Key != ConsoleKey.Enter)
                    {
                        ;
                    }
                    Reset();
                }

                if (_ship.Exploded || MiceReachedBottom())
                {
                    GameOver();
                    while (Console.ReadKey(true).Key != ConsoleKey.Escape)
                    {
                        ;
                    }
                    break;
                }
                else if (GameObjects.GameObjects.OfType <Invader>().Count() == 0)
                {
                    if (_level >= Constants.LevelCount)
                    {
                        Win();
                        while (Console.ReadKey(true).Key != ConsoleKey.Escape)
                        {
                            ;
                        }
                        break;
                    }
                    else
                    {
                        SetInvaders(++_level);
                        invMovingDirections = Direction.Right;
                        invMovingCounter    = InvadersMoveSize;
                    }
                }
                else
                {
                    CommandExecute();

                    if (--loopCounter <= 0)
                    {
                        loopCounter = LoopWaitingBound;

                        if (--invMovingCounter > 0)
                        {
                            MoveInvaders(invMovingDirections);
                        }
                        else
                        {
                            invMovingCounter    = InvadersMoveSize;
                            invMovingDirections = invMovingDirections == Direction.Left ? Direction.Right : Direction.Left;
                            MoveInvaders(Direction.Down);
                        }
                    }
                    InvadersBomb();
                    DetectCollisions();
                    Render();
                }
                Thread.Sleep(LoopWaitingTimeMs);

                CheckKey();
            }
        }
Example #10
0
        /*protected override bool OnUserUpdateTest(float elapsedTime)
         * {
         *  DrawText($"{DateTime.Now}", 10, 10, AnsiColor.Red);
         *  DrawText($"elapsedTime: {elapsedTime}", 10, 11, AnsiColor.Green);
         *  DrawText($"Framerate: {1.0 / (elapsedTime / _ticksPerSecond)}", 10, 13, AnsiColor.Yellow);
         *
         *
         *  long millisecondNow = DateTime.Now.Ticks;
         *  if (millisecondNow - _lastFramerateUpdate >= _ticksPerSecond) {
         *      _lastFramerateCount = _framerateCount;
         *      _framerateCount = 0;
         *      _lastFramerateUpdate = millisecondNow;
         *  }
         *
         *  _framerateCount++;
         *
         *  DrawText($"Framerate count: {_lastFramerateCount}", 10, 15, AnsiColor.Yellow);
         *
         *  if (NativeKeyboard.IsKeyDown(KeyCode.Z)) {
         *      return false;
         *  }
         *
         *  return true;
         * }*/

        protected override bool OnUserUpdate(long elapsedTime)
        {
            if (!_gameOver)
            {
                // GAME TIMING =============================
                _elapsedMilliseconds += (float)elapsedTime / TicksPerMillisecond;
                if (_elapsedMilliseconds > 50)
                {
                    _elapsedMilliseconds = 0;
                    _speedCount++;

                    bool forceDown = _speedCount == _speed;

                    // INPUT ===================================

                    // GAME LOGIC ==============================
                    bool updatePieceSuccess = UpdatePieceLocation(forceDown);

                    if (forceDown)
                    {
                        _speedCount = 0;
                    }

                    if (!updatePieceSuccess)
                    {
                        LockPiece();
                        CheckForLines();
                        SpawnPiece();

                        // update score
                        _score += 25;
                        if (_fullLines.Count > 0)
                        {
                            // for every line 2^<num of lines> *100
                            _score += (1 << _fullLines.Count) * 100;
                        }

                        // Update difficulty every 50 pieces
                        _pieceCount++;
                        if (_pieceCount % 50 == 0 && _speed >= 10)
                        {
                            _speed--;
                        }
                    }
                }
            }

            // RENDER OUTPUT ===========================
            DrawText("Hello World!", 0, 0);

            // draw tetromino field
            Draw2D(_playingField, FieldWidth, FieldHeight, FieldStartX, FieldStartY);

            // draw score
            DrawText($"Score: {_score}", FieldStartX + FieldWidth + 6, 16, AnsiColor.Green);
            DrawText($"Level: {21 - _speed}", FieldStartX + FieldWidth + 6, 17, AnsiColor.Green);
            DrawText($"Blocks: {_pieceCount}", FieldStartX + FieldWidth + 6, 18, AnsiColor.Green);
            DrawText($"Lines: {_linesRemoved}", FieldStartX + FieldWidth + 6, 19, AnsiColor.Green);

            // draw current piece
            Draw2D(GetRotatedPiece(_currentPiece, _currentRotation), 4, 4, FieldStartX + _currentX, FieldStartY + _currentY, '.');

            //draw line wait then move stuff down
            if (_fullLines.Count > 0)
            {
                // Display Frame (cheekily to draw lines)
                Thread.Sleep(400); // Delay a bit

                _linesRemoved += _fullLines.Count;
                RemoveFullLines();
            }

            if (_gameOver)
            {
                DrawText($"Game Over! ", FieldStartX + FieldWidth + 6, 14, AnsiColor.Red);
                DrawText("Press Z key to exit.", FieldStartX + FieldWidth + 6, 21, AnsiColor.Magenta);

                if (NativeKeyboard.IsKeyDown(KeyCode.Z))
                {
                    DrawText("Byeee. Shutting down application...", FieldStartX + FieldWidth + 6, 22, AnsiColor.Cyan);
                    return(false);
                }
            }

            return(true);
        }