Пример #1
0
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>

        protected override void Update(GameTime gameTime)
        {
            GamePadState  gamePadState  = GamePad.GetState(PlayerIndex.One);
            KeyboardState keyboardState = Keyboard.GetState();
            MouseState    mouseState    = Mouse.GetState();

            mousePos = new Vector2(mouseState.X, mouseState.Y);

            SoundHelper.Update();
            if (!songStart)
            {
                SoundHelper.PlaySound(0);
                songStart = true;
            }
            // Allows the game to exit
            if (gamePadState.Buttons.Back == ButtonState.Pressed)
            {
                this.Exit();
            }

            //Decrement the delay by the number of seconds that have elapsed since
            //the last time that the Update method was called
            mFadeDelay -= gameTime.ElapsedGameTime.TotalSeconds;

            //If the Fade delays has dropped below zero, then it is time to
            //fade in/fade out the image a little bit more.
            if (mFadeDelay <= 0)
            {
                //Reset the Fade delay
                mFadeDelay = .035;

                //Increment/Decrement the fade value for the image
                mAlphaValue += mFadeIncrement;

                //If the AlphaValue is equal or above the max Alpha value or
                //has dropped below or equal to the min Alpha value, then
                //reverse the fade
                if (mAlphaValue >= 255 || mAlphaValue <= 0)
                {
                    mFadeIncrement *= -1;
                }
            }

            switch (this.gameState)
            {
            case GameState.Setup:
                background.BackgroundOffset += 1;
                background.ParallaxOffset   += 2;

                if (canType)
                {
                    CheckKeys();
                }
                else
                {
                    this.gameState = GameState.Title;
                }

                break;

            case GameState.Title:
                //songStart = false;
                if (Keyboard.GetState().IsKeyDown(Keys.Space))
                {
                    BeginGame();
                    this.gameState = GameState.LevelChange;
                }
                else if (Keyboard.GetState().IsKeyDown(Keys.Escape))
                {
                    Exit();
                }

                break;

            case GameState.LevelChange:
                //Thread.Sleep(3000);

                getReadyTimer += (float)gameTime.ElapsedGameTime.TotalSeconds;
                if (getReadyTimer >= 2.0f)
                {
                    BeginLevel();
                    this.gameState = GameState.Playing;
                }

                break;

            case GameState.Playing:
                float elapsed = (float)gameTime.ElapsedRealTime.TotalMilliseconds;
                deltaFPSTime += elapsed;

                //Parallax
                background.BackgroundOffset += 1;
                background.ParallaxOffset   += 2;

                if (deltaFPSTime > 1000)
                {
                    float fps = 1000 / elapsed;
                    framerate     = fps.ToString("F2");
                    deltaFPSTime -= 1;
                }

                for (int i = Sprite.Sprites.Count - 1; i >= 0; i--)
                {
                    Sprite actor = Sprite.Sprites[i];
                    actor.Update(gameTime);



                    if (actor is Player)
                    {
                        Vector2 direction = new Vector2(mouseState.X, mouseState.Y);

                        actor.Position = direction;


                        if (actor.Position.X < actor.Origin.X)
                        {
                            actor.position.X = actor.Origin.X;
                        }
                        if (actor.Position.Y < actor.Origin.Y)
                        {
                            actor.position.Y = actor.Origin.Y;
                        }
                        if (actor.Position.X > graphics.PreferredBackBufferWidth - actor.Origin.X)
                        {
                            actor.position.X = graphics.PreferredBackBufferWidth - actor.Origin.X;
                        }
                        if (actor.Position.Y > graphics.PreferredBackBufferHeight - actor.Origin.Y)
                        {
                            actor.position.Y = graphics.PreferredBackBufferHeight - actor.Origin.Y;
                        }

                        continue;
                    }

                    Enemy enemy = actor as Enemy;
                    //Update enemies
                    enemyCount = (int)Sprite.Sprites.Count() - 1;


                    if (gameTime.TotalGameTime.TotalSeconds > timer)
                    {
                        ENEMY_VELOCITY_X += .5f;
                        ENEMY_VELOCITY_Y += .5f;
                        Enemy.AddEnemies(1, enemyTexture, random.Next(0, 200), ENEMY_BASE_SPEED, ENEMY_SPEED_VARIATION, ENEMY_VELOCITY_X, ENEMY_VELOCITY_Y);
                        score += 143;
                        timer  = gameTime.TotalGameTime.TotalSeconds + 10d;
                    }

                    if (enemy != null)
                    {
                        if (!player.IsInvincible && enemy.IsHarmful && Sprite.CheckCollision(enemy, this.player))
                        {
                            SoundHelper.PlaySound(2);
                            if (score > highscore)
                            {
                                newHighscore = true;
                                highscore    = score;
                                IPHostEntry ip = null;
                                try
                                {
                                    ip = Dns.GetHostEntry("highscore.ettfyratre.se");
                                    Highscores.sendScore("1", text, "Enemies: " + enemyCount, highscore);
                                }
                                catch (Exception ex)
                                {
                                    Console.WriteLine(ex.Message);
                                }
                                //takeScreenshot = true;
                                //Screenshot();
                            }

                            this.lives--;
                            this.gameState = GameState.Gameover;
                        }
                    }
                }

                break;

            case GameState.Died:
                Thread.Sleep(3000);
                this.gameState = GameState.Highscores;

                break;

            case GameState.Gameover:
                Thread.Sleep(3000);
                this.gameState = GameState.Highscores;
                break;

            case GameState.Highscores:
                if (score > highscore)
                {
                    takeScreenshot = true;
                    Screenshot();
                }
                Thread.Sleep(5000);
                this.gameState = GameState.Title;
                break;
            }

            base.Update(gameTime);
        }
Пример #2
0
        public override void Update(GameTime gameTime)
        {
            if (this.enemyType < 100)
            {
                if (moveTime <= 0)
                {
                    this.SetRandomMove();
                }

                if (this.IsHarmful)
                {
                    amount       += (float)gameTime.ElapsedGameTime.TotalSeconds / moveTime;
                    this.Position = Vector2.SmoothStep(this.pointA, this.pointB, this.amount);
                }

                if (this.amount >= 1f)
                {
                    this.SetRandomMove();
                }
            }
            else if (this.enemyType >= 100)
            {
                if (moveTime <= 0)
                {
                    this.SetBouncingMove();
                }

                if (this.IsHarmful)
                {
                    amount += (float)gameTime.ElapsedGameTime.TotalSeconds / moveTime;

                    this.position.X += this.vX;
                    this.position.Y += this.vY;
                }

                if (this.amount >= 1f)
                {
                    this.SetBouncingMove();
                }

                if (this.position.X < (this.Origin.X + 10))
                {
                    SoundHelper.PlaySound(1); this.vX = -this.vX; SoundHelper.PlaySound(1);
                }
                if (this.position.Y < (this.Origin.Y))
                {
                    SoundHelper.PlaySound(1); this.vY = -this.vY; SoundHelper.PlaySound(1);
                }
                if (this.position.X > (Game1.SCREEN_WIDTH - this.Origin.X - 10))
                {
                    SoundHelper.PlaySound(1); this.vX = -this.vX; SoundHelper.PlaySound(1);
                }
                if (this.position.Y > (Game1.SCREEN_HEIGHT - this.Origin.Y))
                {
                    SoundHelper.PlaySound(1); this.vY = -this.vY; SoundHelper.PlaySound(1);
                }
            }

            angle += 0.010f;

            this.rotation = angle;

            base.Update(gameTime);
        }