예제 #1
0
        /// <summary>
        /// Method updating the player based on user input.
        /// </summary>
        /// <param name="input">Object holding the state of all hardware keys.</param>
        public void Update(Input input)
        {
            if(bulletTick > 0)
                bulletTick--;

            if(speedX < desiredSpeedX)
                speedX += (desiredSpeedX - speedX) / 4;
            else if(speedX > desiredSpeedX && speedX > 1)
                speedX += (desiredSpeedX - speedX) / 4;

            if(speedY < desiredSpeedY)
                speedY += (desiredSpeedY - speedY) / 4;
            else if(speedY > desiredSpeedY && speedY > 1)
                speedY += (desiredSpeedY - speedY) / 4;

            if(input.KeyPressed((int)Keys.Up))
            {
                desiredSpeedY = (int)maxSpeed;
                movingUp = true;
            }
            else if(input.KeyJustReleased((int)Keys.Up))
            {
                desiredSpeedY = 0;
                movingUp = true;
            }

            if(input.KeyPressed((int)Keys.Down))
            {
                desiredSpeedY = (int)maxSpeed;
                movingUp = false;
            }
            else if(input.KeyJustReleased((int)Keys.Down))
            {
                desiredSpeedY = 0;
                movingUp = false;
            }

            if(input.KeyPressed((int)Keys.Left))
            {
                desiredSpeedX = (int)maxSpeed;
                movingLeft = true;
            }
            else if(input.KeyJustReleased((int)Keys.Left))
            {
                desiredSpeedX = 0;
                movingLeft = true;
            }

            if(input.KeyPressed((int)Keys.Right))
            {
                desiredSpeedX = (int)maxSpeed;
                movingLeft = false;
            }
            else if(input.KeyJustReleased((int)Keys.Right))
            {
                desiredSpeedX = 0;
                movingLeft = false;
            }

            if(
                input.KeyJustPressed(194) ||
                input.KeyPressed(194)
                )
            {
                if(CanFire())
                {
                    GameForm.CurrentLevel.Bullets.Add(Fire());
                    bulletTick = (int)(0.25 * GameForm.FramesPerSecond);
                }
            }

            if(movingUp)
            {
                if(y > 0)
                    y = y - (int)speedY;
            }
            else
            {
                if(GameForm.GameArea.Height > y + (spriteSize.Height - 6))
                    y = y + (int)speedY;
            }

            if(movingLeft)
            {
                if(x > -(spriteSize.Width / 2))
                    x = x - (int)speedX;
            }
            else
            {
                if(GameForm.GameArea.Width > x + (spriteSize.Width / 2))
                    x = x + (int)speedX;
            }
        }
예제 #2
0
        /// <summary>
        /// Instanciates the main game form.
        /// </summary>
        public GameForm()
        {
            TickCount = 0;
            this.Visible = true;
            this.WindowState = FormWindowState.Maximized;
            offScreenBitmap = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height);
            offScreenGraphics = Graphics.FromImage(offScreenBitmap);
            offScreenGraphics.Clear(Color.Black);
            GameArea = this.ClientRectangle;

            SpriteList.Instance.LoadSprites();

            Randomizer = new Random(DateTime.Now.Millisecond);
            Player = new Player();
            input = new Input();
            levelGui = new LevelGui();
            levelFiles = GetLevels();
            levelFont = new GameFont(Color.Yellow, string.Empty, new Font(FontFamily.GenericSansSerif, 24, FontStyle.Bold), 60, 130);
            exitFont = new GameFont(Color.Red, string.Empty, new Font(FontFamily.GenericSansSerif, 10, FontStyle.Bold), 70, 160);

            titleTick = 120;
            DoGameLoop();
        }