Update() public méthode

public Update ( ) : void
Résultat void
Exemple #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)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }

            // TODO: Add your update logic here
            if (!gameOver)
            {
                if (collisonHandler.IsAnyColisson())
                {
                    gameOver = true;
                }

                dinosaur.HandleKeyboard(Keyboard.GetState());

                cactus.SpeedX  = GameConfig.WorldStatics.GameSpeed;
                cactus2.SpeedX = GameConfig.WorldStatics.GameSpeed;
                bird.SpeedX    = GameConfig.WorldStatics.GameSpeed;
                cactus.Update();
                cactus2.Update();
                dinosaur.Update();
                bird.Update();
                Console.WriteLine(cactus2.Pos.X);

                if (((int)gameTime.TotalGameTime.TotalSeconds) % 10 == 0)
                {
                    GameConfig.WorldStatics.SetGameSpeed(GameConfig.WorldStatics.GameSpeed + 0.05f);
                }
            }

            base.Update(gameTime);
        }
Exemple #2
0
        public override void Update(GameTime gameTime)
        {
            float delta = (float)gameTime.ElapsedGameTime.TotalSeconds;

            spawnTimer += delta;
            if (spawnTimer > 2)
            {
                float y = MathHelper.Max(-PIPE_HEIGHT + 10,
                                         MathHelper.Min(lastY + random.Next(-20, 20), GameMain.VIRTUAL_HEIGHT - 90 - PIPE_HEIGHT));
                lastY      = y;
                spawnTimer = 0;
                pipePairs.Add(new PipePair(y));
            }

            bird.Update(delta);

            List <PipePair> pipePairsToRemove = new List <PipePair>();

            foreach (PipePair pipePair in pipePairs)
            {
                pipePair.Update(delta);

                if (!pipePair.scored)
                {
                    if (pipePair.X + PIPE_WIDTH < bird.X)
                    {
                        score++;
                        scoreSound.Play();
                        pipePair.scored = true;
                    }
                }


                if (bird.Collides(pipePair.Upper) || bird.Collides(pipePair.Lower))
                {
                    explosionSound.Play();
                    hurtSound.Play();
                    LoadScoreState();
                }

                if (pipePair.remove)
                {
                    pipePairsToRemove.Add(pipePair);
                }
            }
            foreach (PipePair pipePair in pipePairsToRemove)
            {
                pipePairs.Remove(pipePair);
            }

            if (bird.Y > GameMain.VIRTUAL_HEIGHT - 15)
            {
                explosionSound.Play();
                hurtSound.Play();
                LoadScoreState();
            }

            base.Update(gameTime);
        }
Exemple #3
0
    void WhenTimePasses(int frames, float deltaTime)
    {
        var statesInTime = new State[frames];

        for (var i = 0; i < statesInTime.Length; i++)
        {
            statesInTime[i] = bird.Update(deltaTime);
        }

        states = statesInTime;
    }
Exemple #4
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)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }
            //_currentScreen.Update(gameTime);

            bird.Update(gameTime);

            base.Update(gameTime);
        }
Exemple #5
0
        /// <summary>
        /// update method
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gameTimer_Tick(object sender, EventArgs e)
        {
            bird.Update();

            foreach (Pipes pipe in pipes)
            {
                pipe.Update();
            }

            CheckForBound();
            AddScore();

            Invalidate();
        }
Exemple #6
0
    public void Setup(IObservable <float> time, IObservable <Unit> collisions)
    {
        var bird = new Bird(transform.position.y, -gravity, jumpBoost);

        // Transform time into bird positions
        time.Select(n => bird.Update(Time.deltaTime))
        // Get bird position events and actually move the bird
        .Subscribe(state => ChangePosition(state));

        // From every frame, get only the frames where the mouse button was pressed down
        Observable.EveryUpdate()
        .Where(e => Input.GetMouseButtonDown(0))
        // Get the mouse events and make the bird jump every time
        .Subscribe(e => bird.Jump());

        // Get collision events and make the bird react to them
        collisions.Subscribe(c => bird.Collide());
    }
Exemple #7
0
        public override async Task Update(TimeSpan dt)
        {
            // update timer for pipe spawning
            timer = timer + dt.TotalSeconds;

            // spawn a new pipe pair every second and a half
            if (timer > 2)
            {
                // modify the last Y coordinate we placed so pipe gaps aren't too far apart
                // no higher than 10 pixels below the top edge of the screen,
                // and no lower than a gap length (90 pixels) from the bottom
                var y = Math.Max(-PipeHeight + 10, Math.Min(lastY + FiftyBird.Instance.Random.Next(-20, 20 + 1), Game.Instance.VirtualHeight));
                lastY = y;

                // add a new pipe pair at the end of the screen at our new Y
                var pipePair = new PipePair(y);
                await pipePair.Load();

                pipePairs.Add(pipePair);

                // reset timer
                timer = 0;
            }

            // for every pair of pipes..
            foreach (var pair in pipePairs)
            {
                // score a point if the pipe has gone past the bird to the left all the way
                // be sure to ignore it if it's already been scored
                if (!pair.Scored)
                {
                    if (pair.X + PipeWidth < bird.X)
                    {
                        score++;
                        pair.Scored = true;
                        await FiftyBird.Instance.Sounds["score"].Play();
                    }
                }

                // Update posiiton of pair
                pair.Update(dt);
            }

            // we need this second loop, rather than deleting in the previous loop, because
            // modifying the table in-place without explicit keys will result in skipping the
            // next pipe, since all implicit keys (numerical indices) are automatically shifted
            // down after a table removal
            pipePairs.RemoveAll(pair => pair.Remove);

            // simple collision between bird and all pipes in pairs
            foreach (var pair in pipePairs)
            {
                foreach (var pipe in pair.Pipes.Values)
                {
                    if (bird.Collides(pipe))
                    {
                        await FiftyBird.Instance.Sounds["explosion"].Play();
                        await FiftyBird.Instance.Sounds["hurt"].Play();

                        await FiftyBird.Instance.StateMachine.Change("score", new Dictionary <string, object>
                        {
                            ["score"] = score
                        });
                    }
                }
            }

            // update bird based on gravity and input
            bird.Update(dt);

            // reset if we get to the ground
            if (bird.Y > FiftyBird.Instance.VirtualHeight - 15)
            {
                await FiftyBird.Instance.Sounds["explosion"].Play();
                await FiftyBird.Instance.Sounds["hurt"].Play();

                await FiftyBird.Instance.StateMachine.Change("score", new Dictionary <string, object>
                {
                    ["score"] = score
                });
            }
        }
Exemple #8
0
        // METHODS

        // UPDATE & DRAW
        public override void Update(GameTime gameTime)
        {
            presentKey = Keyboard.GetState();
            base.Update(gameTime);
            if (presentKey.IsKeyDown(Keys.Escape) && pastKey != presentKey && started)
            {
                paused = !paused;
            }
            if (!paused)
            {
                if (started)
                {
                    pauseButton.Update(gameTime);
                    if (pauseButton.Clicked)
                    {
                        paused = true;
                    }
                    player.Update(gameTime, true);
                    foreach (Pipe pipe in pipes)
                    {
                        pipe.Update(gameTime);
                    }
                    for (int i = 0; i < 3; i++)
                    {
                        if ((!player.Intersect(pipes[i].Hole) && player.XPosition + Bird.Width / 2 > pipes[i].XpositionUp && player.XPosition - Bird.Width / 2 < pipes[i].XpositionUp + pipes[i].WidthUp))
                        {
                            died = true;
                            RessourcesManager.hurt.Play();
                            player.Flap(false);
                        }
                        else if (player.XPosition >= pipes[i].Hole.X + pipes[i].Hole.Width && player.XPosition <= pipes[i].Hole.X + pipes[i].Hole.Width + 1 && addScore)
                        {
                            totalScore++;
                            addScore = false;
                            RessourcesManager.pipePassed.Play();
                        }
                        else
                        {
                            addScore = true;
                        }
                    }
                    if (player.YPosition + Bird.Height / 2 >= Game1.screenHeight - ground.Height || died)
                    {
                        died            = true;
                        started         = false;
                        pauseBackground = true;
                        RessourcesManager.hurt.Play();
                        player.Flap(false);
                    }
                    else if (player.YPosition - Bird.Height / 2 < 0)
                    {
                        player.YPosition = 0 + Bird.Height / 2;
                    }
                }
                else if (died)
                {
                    // ################ DIED ##############
                    if (player.YPosition - Bird.Height / 2 > Game1.screenHeight + Bird.Height)
                    {
                        if (!over)
                        {
                            gameOver = new MenuGameOver();
                            over     = true;
                        }
                        gameOver.Update(gameTime);
                    }
                    else
                    {
                        player.Update(gameTime, false);
                        if (!writed)
                        {
                            if (totalScore > highScore)
                            {
                                if (totalScore < 10)
                                {
                                    txtScore[1] = "high score=" + "00" + totalScore;
                                }
                                else if (totalScore < 100)
                                {
                                    txtScore[1] = "high score=" + "0" + totalScore;
                                }
                                else
                                {
                                    txtScore[1] = "high score=" + totalScore;
                                }
                                System.IO.File.WriteAllLines(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\Flappy.txt", txtScore);
                                highScore = totalScore;
                                newScore  = true;
                            }
                            writed = true;
                        }
                    }
                }
                else
                {
                    menuButton.Update(gameTime);
                    if (menuButton.Clicked)
                    {
                        GameMain.ChangeMenu = "main";
                    }

                    if (Keyboard.GetState().IsKeyDown(Keys.Space))
                    {
                        started = true;
                    }
                    player   = new Bird();
                    addScore = true;
                    pipes    = new Pipe[3];
                    for (int i = 0; i < 3; i++)
                    {
                        pipes[i] = new Pipe(i * 267);
                    }
                }
            }
            else
            {
                menuButton.Update(gameTime);
                if (menuButton.Clicked)
                {
                    GameMain.ChangeMenu = "main";
                }
                resumeButton.Update(gameTime);
                if (resumeButton.Clicked)
                {
                    paused = false;
                }
            }
            pastKey = presentKey;
        }
Exemple #9
0
        public override void Update()
        {
            CheckForInput();


            Statics.EEG_SIGNAL    = 0;
            Statics.EEG_ATTENTION = 100;

            //if (MindSetConn.RealtimeData.Quality > 0)
            //{
            //    Statics.GAME_STATE = Statics.STATE.Paused;
            //}
            //else
            //{
            //    Statics.GAME_STATE = Statics.STATE.Playing;
            //}



            if (Statics.GAME_STATE == Statics.STATE.Playing)
            {
                // MindWave Controller
                if (pathlist.Count > 0)
                {
                    if (Math.Ceiling(_entityBird.Position.Y) != pathlist[list_position])
                    {
                        if (Math.Ceiling(_entityBird.Position.Y) >= pathlist[list_position])
                        {
                            if (Statics.EEG_ATTENTION > 40)
                            {
                                _entityBird._ySpeed = -1.000f;
                            }
                            if (Statics.EEG_ATTENTION > 45)
                            {
                                _entityBird._ySpeed = -1.000f;
                            }
                            if (Statics.EEG_ATTENTION > 50)
                            {
                                _entityBird._ySpeed = -1.200f;
                            }
                            if (Statics.EEG_ATTENTION > 55)
                            {
                                _entityBird._ySpeed = -1.200f;
                            }
                            if (Statics.EEG_ATTENTION > 60)
                            {
                                _entityBird._ySpeed = -1.400f;
                            }
                            if (Statics.EEG_ATTENTION > 65)
                            {
                                _entityBird._ySpeed = -1.800f;
                            }
                            if (Statics.EEG_ATTENTION > 75)
                            {
                                _entityBird._ySpeed = -2.000f;
                            }
                            if (Statics.EEG_ATTENTION > 80)
                            {
                                _entityBird._ySpeed = -2.400f;
                            }
                            if (Statics.EEG_ATTENTION > 85)
                            {
                                _entityBird._ySpeed = -3.000f;
                            }
                            if (Statics.EEG_ATTENTION > 90)
                            {
                                _entityBird._ySpeed = -3.500f;
                            }
                            if (Statics.EEG_ATTENTION > 95)
                            {
                                _entityBird._ySpeed = -4.000f;
                            }
                        }
                        else
                        {
                            if (Statics.EEG_ATTENTION > 40)
                            {
                                _entityBird._ySpeed = +1.000f;
                            }
                            if (Statics.EEG_ATTENTION > 45)
                            {
                                _entityBird._ySpeed = +1.000f;
                            }
                            if (Statics.EEG_ATTENTION > 50)
                            {
                                _entityBird._ySpeed = +1.200f;
                            }
                            if (Statics.EEG_ATTENTION > 55)
                            {
                                _entityBird._ySpeed = +1.200f;
                            }
                            if (Statics.EEG_ATTENTION > 60)
                            {
                                _entityBird._ySpeed = +1.400f;
                            }
                            if (Statics.EEG_ATTENTION > 65)
                            {
                                _entityBird._ySpeed = +1.800f;
                            }
                            if (Statics.EEG_ATTENTION > 75)
                            {
                                _entityBird._ySpeed = +2.000f;
                            }
                            if (Statics.EEG_ATTENTION > 80)
                            {
                                _entityBird._ySpeed = +2.400f;
                            }
                            if (Statics.EEG_ATTENTION > 85)
                            {
                                _entityBird._ySpeed = +3.000f;
                            }
                            if (Statics.EEG_ATTENTION > 90)
                            {
                                _entityBird._ySpeed = +3.500f;
                            }
                            if (Statics.EEG_ATTENTION > 95)
                            {
                                _entityBird._ySpeed = +4.000f;
                            }
                        }
                    }
                    else
                    {
                        _entityBird._ySpeed = 0;
                    }
                }



                // Increase game difficulty
                if (Statics.TIME_ACTUALGAMETIME - _previousDifficultyTime > _difficultyTime)
                {
                    _previousDifficultyTime = Statics.TIME_ACTUALGAMETIME;

                    Statics.GAME_SPEED_DIFFICULTY += 0.5f;
                    Statics.GAME_LEVEL++;

                    foreach (ParallaxBackground layer in Statics.GAME_BACKGROUND.BackgroundLayer_Stack.Values)
                    {
                        layer.MoveSpeed -= Statics.GAME_SPEED_DIFFICULTY;
                    }

                    foreach (ParallaxBackground layer in Statics.GAME_FOREGROUND.ForegroundLayer_Stack.Values)
                    {
                        layer.MoveSpeed -= Statics.GAME_SPEED_DIFFICULTY;
                    }

                    _refreshTime = TimeSpan.FromMilliseconds(_refreshTime.TotalMilliseconds - (Statics.GAME_SPEED_DIFFICULTY * 5f));
                }

                // Add new obstacle
                if (Statics.TIME_ACTUALGAMETIME - _previousRefreshTime > _refreshTime && Statics.EEG_SIGNAL == 0)
                {
                    _previousRefreshTime = Statics.TIME_ACTUALGAMETIME;

                    switch (Statics.GAME_WORLD)
                    {
                    case Statics.WORLD.Pipes:
                    {
                        // Add Pipes
                        _entityObstacles.Add(new Entities.Pipe(Entity.Type.Pipe, Statics.GAME_SPEED_DIFFICULTY));

                        List <Rectangle> entita = _entityObstacles[_entityObstacles.Count - 1].Bounds;
                        pathlist.Add(entita[entita.Count - 1].Y - 36);

                        break;
                    }
                    }
                }

                foreach (Entities.Entity entity in _entityObstacles.ToList())
                {
                    List <Rectangle> bounds = entity.Bounds;

                    if (!_isCheckingCollision)
                    {
                        foreach (Rectangle bound in bounds)
                        {
                            _isCheckingCollision = true;

                            if (Statics.COLLISION_USESLOPPY)
                            {
                                if (Helpers.Collision.IsSloppyCollision(bound, _entityBird.Bounds[0]))
                                {
                                    SetGameState(Statics.STATE.GameOver);
                                }
                                else if (_entityBird.Position.X > bound.X + bound.Width && !entity.IsScored)
                                {
                                    Statics.MANAGER_SOUND.Play("Player\\Score");
                                    list_position++;
                                    entity.IsScored = true;
                                    Statics.GAME_SCORE++;
                                }
                            }
                            else
                            {
                                if (Helpers.Collision.IsPixelCollision(bound, _entityBird.Bounds[0], entity.ColorData, _entityBird.ColorData))
                                {
                                    SetGameState(Statics.STATE.GameOver);
                                }
                                else if (_entityBird.Position.X > bound.X + 50 + bound.Width && !entity.IsScored)
                                {
                                    Statics.MANAGER_SOUND.Play("Player\\Score");
                                    list_position++;
                                    entity.IsScored = true;
                                    Statics.GAME_SCORE++;
                                }
                            }

                            if (entity.Position.X <= -entity.Width)
                            {
                                this._entityObstacles.Remove(entity);
                            }

                            _isCheckingCollision = false;
                        }
                    }
                }

                if (Statics.GAME_SCORE > Statics.GAME_HIGHSCORE)
                {
                    Statics.GAME_HIGHSCORE    = Statics.GAME_SCORE;
                    Statics.GAME_NEWHIGHSCORE = true;
                }

                Statics.DEBUG_ENTITIES = _entityObstacles.Count;
                Statics.DEBUG_PLAYER   = _entityBird.Position;

                _entityBird.Update();

                foreach (var obstacle in _entityObstacles)
                {
                    obstacle.Update();
                }
            }

            base.Update();
        }
        public override void Update()
        {
            CheckForInput();

            if (Statics.GAME_STATE == Statics.STATE.Playing)
            {
                // Increase game difficulty
                if (Statics.TIME_ACTUALGAMETIME - _previousDifficultyTime > _difficultyTime)
                {
                    _previousDifficultyTime = Statics.TIME_ACTUALGAMETIME;

                    Statics.GAME_SPEED_DIFFICULTY += 0.1f;
                    Statics.GAME_LEVEL++;

                    foreach (ParallaxBackground layer in Statics.GAME_BACKGROUND.BackgroundLayer_Stack.Values)
                    {
                        layer.MoveSpeed -= Statics.GAME_SPEED_DIFFICULTY;
                    }

                    foreach (ParallaxBackground layer in Statics.GAME_FOREGROUND.ForegroundLayer_Stack.Values)
                    {
                        layer.MoveSpeed -= Statics.GAME_SPEED_DIFFICULTY;
                    }

                    _refreshTime = TimeSpan.FromMilliseconds(_refreshTime.TotalMilliseconds - (Statics.GAME_SPEED_DIFFICULTY * 5f));

                    Console.WriteLine("Refresh time: " + _refreshTime.TotalMilliseconds);
                    Console.WriteLine("Background move speed: " + Statics.GAME_BACKGROUND.BackgroundLayer_Stack["Houses"].MoveSpeed);
                }

                if (DateTime.UtcNow - startTime > TimeSpan.FromSeconds(5))
                {
                    Statics.GAME_USESLOWMODE = false;
                }

                // Add new obstacle
                if (Statics.TIME_ACTUALGAMETIME - _previousRefreshTime > _refreshTime)
                {
                    _previousRefreshTime = Statics.TIME_ACTUALGAMETIME;

                    switch (Statics.GAME_WORLD)
                    {
                    case Statics.WORLD.Pipes:
                    {
                        // Add Pipes
                        _entityObstacles.Add(new Entities.Pipe(Entity.Type.Pipe, Statics.GAME_SPEED_DIFFICULTY));
                        break;
                    }

                    case Statics.WORLD.Bullets:
                    {
                        // Play Sound
                        Statics.MANAGER_SOUND.Play("Bullet\\Explode");

                        // Add Bullet
                        _entityObstacles.Add(new Entities.Bullet(Entity.Type.Bullet, Statics.GAME_SPEED_DIFFICULTY));
                        break;
                    }

                    case Statics.WORLD.Paratroopas:
                    {
                        // Add Paratroopa
                        _entityObstacles.Add(new Entities.Paratroopa(Entity.Type.Paratroopa, Statics.GAME_SPEED_DIFFICULTY));
                        break;
                    }
                    }
                }

                foreach (Entities.Entity entity in _entityObstacles.ToList())
                {
                    List <Rectangle> bounds = entity.Bounds;

                    if (!_isCheckingCollision)
                    {
                        foreach (Rectangle bound in bounds)
                        {
                            _isCheckingCollision = true;

                            if (Statics.COLLISION_USESLOPPY)
                            {
                                if (Helpers.Collision.IsSloppyCollision(bound, _entityBird.Bounds[0]))
                                {
                                    SetGameState(Statics.STATE.GameOver);
                                }
                                else if (_entityBird.Position.X > bound.X + bound.Width && !entity.IsScored)
                                {
                                    Statics.MANAGER_SOUND.Play("Player\\Score");

                                    entity.IsScored = true;
                                    Statics.GAME_SCORE++;
                                }
                            }
                            else
                            {
                                if (Helpers.Collision.IsPixelCollision(bound, _entityBird.Bounds[0], entity.ColorData, _entityBird.ColorData))
                                {
                                    SetGameState(Statics.STATE.GameOver);
                                }
                                else if (_entityBird.Position.X > bound.X + bound.Width && !entity.IsScored)
                                {
                                    Statics.MANAGER_SOUND.Play("Player\\Score");

                                    entity.IsScored = true;
                                    Statics.GAME_SCORE++;
                                }
                            }

                            if (entity.Position.X <= -entity.Width)
                            {
                                this._entityObstacles.Remove(entity);
                            }

                            _isCheckingCollision = false;
                        }
                    }
                }

                if (Statics.GAME_SCORE > Statics.GAME_HIGHSCORE)
                {
                    Statics.GAME_HIGHSCORE    = Statics.GAME_SCORE;
                    Statics.GAME_NEWHIGHSCORE = true;
                }

                Statics.DEBUG_ENTITIES = _entityObstacles.Count;
                Statics.DEBUG_PLAYER   = _entityBird.Position;

                _entityBird.Update();

                foreach (var obstacle in _entityObstacles)
                {
                    obstacle.Update();
                }
            }

            base.Update();
        }