コード例 #1
0
        public static void Update()
        {
            //Determine whether the player tapped the screen
            var touches = Touch.GetData(0);

            //If tapped, inform the bird.
            if (touches.Count > 0)
            {
                bird.Tapped();
            }

            //Update the bird.
            bird.Update(0.0f);

            if (bird.Alive)
            {
                //Move the background.
                background.Update(0.0f);

                //Update the obstacles.
                foreach (Obstacle obstacle in obstacles)
                {
                    obstacle.Update(0.0f);
                }
            }
        }
コード例 #2
0
        public static void Update()
        {
            //Determine whether the player tapped the screen.
            var touches = Touch.GetData(0);

            //If tapped, inform the bird.
            if (touches.Count > 0)
            {
                bird.Tapped();
            }

            // timer that decreases air supply of bird by one every 2 seconds.
            if (airLoss.Seconds() > 2)
            {
                air--;
                airLabel.Text = "Air: " + air.ToString();
                airLoss.Reset();
            }

            //Update the bird.
            bird.Update(0.0f);

            //Update the bubble.
            foreach (Bubble b in bubble)
            {
                b.Update(0.0f);
            }

            if (bird.Alive)
            {
                //Move the background.
                background.Update(0.0f);

                //Update chains and mines.
                for (int i = 0; i < OBSTACLE_COUNT; i++)
                {
                    chain[i].Update(0.0f);
                    seamine[i].SetPosition((chain[i].GetX - 60), (chain[i].GetY + chain[i].GetMaxY) - 20);
                    seamine[i].Update(0.0f);
                }
            }

            Collision();
        }
コード例 #3
0
ファイル: Game.cs プロジェクト: bulatkhaziev/FlappyBird
        public static void Init()
        {
            Bird bird = new Bird();

            bird.ReadHighScore();

            // Используем поток для отлова прыжка птицы.
            Thread birdThread = new Thread(bird.ThreadJumping);

            birdThread.Start();

            // Используем поток для создания стен.
            Wall   wall        = new Wall();
            Thread wallsThread = new Thread(wall.Add);

            wallsThread.Start();

            while (true)
            {
                // Если птица ударилась о границы карты или о препятствие.
                if (bird.IsCrashed(wall.Walls))
                {
                    // Завершаем потоки и выходим из цикла
                    wallsThread.Abort();
                    birdThread.Abort();
                    break;
                }
                // Обновляем координаты птицы, преград и отрисовываем карту заново.
                bird.Update();

                Map.Print(bird);
                wall.Print(wallsThread);

                Thread.Sleep(UpdateDelay);
            }
            // Завершаем игру
            Destruct(bird);
        }
コード例 #4
0
        public static void Update()
        {
            //Determine whether the player tapped the screen
            var touches = Touch.GetData(0);

            if (Director.Instance.CurrentScene == startScene)
            {
                scoreLabel.Text = "Touch to start";


                if (touches.Count > 0 && !keyPressed)
                {
                    Touch.GetData(0).Clear();
                    Director.Instance.ReplaceScene(gameScene);
                    keyPressed = true;
                }
            }
            else if (Director.Instance.CurrentScene == gameScene)
            {
                //If tapped, inform the bird.
                if (touches.Count > 0)
                {
                    bird.Tapped();
                }

                //Update the bird.
                bird.Update(0.0f);

                if (bird.Alive)
                {
                    //Move the background.
                    background.Update(0.0f);

                    //Update the obstacles.
                    foreach (Obstacle obstacle in obstacles)
                    {
                        obstacle.Update(0.0f);
                    }
                    foreach (Obstacle obstacle in obstacles)
                    {
                        if (obstacle.HasCollidedWith(bird.Sprite))
                        {
                            bird.Alive        = false;
                            bird.Sprite.Color = Colors.Black;
                        }
                        if (obstacle.HasPassed(bird.Sprite))
                        {
                            score++;
                        }
                    }
                    scoreLabel.Text = "" + score;
                }
                if (!bird.Alive)
                {
                    Director.Instance.ReplaceScene(endScene);
                }
            }
            else if (Director.Instance.CurrentScene == endScene)
            {
                scoreLabel.Text = "You dead";
                score           = 0;
                bird.reset();
                bird.Sprite.Color = Colors.White;

                obstacles[0].reset(Director.Instance.GL.Context.GetViewport().Width *0.5f);
                obstacles[1].reset(Director.Instance.GL.Context.GetViewport().Width);

                if (touches.Count > 0 && !keyPressed)
                {
                    Director.Instance.ReplaceScene(startScene);
                    keyPressed = true;
                }
            }
            if (touches.Count == 0 && keyPressed)
            {
                keyPressed = false;
            }
        }
コード例 #5
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)
        {
            // Check exit action
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }

            // Update the base class
            base.Update(gameTime);

            // Update game components
            _Bird.Update(gameTime);


            // Check the game state
            switch (_GameState)
            {
            case GameState.Ready:
            {
                // Update the floor positions and maintain the floor list
                foreach (Floor floor in _Floors)
                {
                    floor.PositionX -= _SceneSpeed * gameTime.ElapsedGameTime.Milliseconds / 1000;
                }
                _MaintainFloors();

                // Check if game starts
                if (Keyboard.GetState().IsKeyDown(Keys.Space))
                {
                    // Reset the score
                    _Score = 0;

                    // Set the vertical speed of the bird
                    _BirdVerticalSpeed = 400;

                    // Switch the game state
                    _GameState = GameState.Start;
                }
                else
                {
                    _BirdVerticalSpeed = 0;
                    _Bird.PositionY    = ScreenHeight / 2;
                }
            }
            break;


            case GameState.Start:
            {
                // Update the floor positions and maintain the floor list
                foreach (Floor floor in _Floors)
                {
                    floor.PositionX -= _SceneSpeed * gameTime.ElapsedGameTime.Milliseconds / 1000;
                }
                _MaintainFloors();

                // Update the motion of the bird
                if (Keyboard.GetState().IsKeyDown(Keys.Space))
                {
                    _BirdVerticalSpeed = 400;
                }
                else
                {
                    _BirdVerticalSpeed += -_Gravity * gameTime.ElapsedGameTime.Milliseconds / 1000;
                }
                _Bird.PositionY += _BirdVerticalSpeed * gameTime.ElapsedGameTime.Milliseconds / 1000;
                if (_Bird.PositionY < Floor.Height)
                {
                    _Bird.PositionY    = Floor.Height;
                    _BirdVerticalSpeed = 0;
                }
                if (_Bird.PositionY > ScreenHeight)
                {
                    _Bird.PositionY    = ScreenHeight;
                    _BirdVerticalSpeed = 0;
                }

                // Update the pipe positions and maintain the pipe list
                foreach (Pipe pipe in _Pipes)
                {
                    pipe.PositionX -= _SceneSpeed * gameTime.ElapsedGameTime.Milliseconds / 1000;
                }
                _MaintainPipes();

                // Update the score and check if game over
                _UpdateScore();
            }
            break;


            case GameState.GameOver:
            {
                // Check if game gets ready
                if (Keyboard.GetState().IsKeyDown(Keys.Enter))
                {
                    // Reset the pipes
                    _Pipes.Clear();
                    _Pipes.Add(new Pipe(this, 0, Floor.Height));
                    _Pipes[0].Initialize();
                    _Pipes[0].PositionX       = ScreenWidth + Pipe.Width + 200;
                    _Pipes[0].OpeningAltitude = _Random.Next(_Pipes[0].OpeningMinAltitude, _Pipes[0].OpeningMaxAltitude);

                    // Switch the game state to ready
                    _GameState = GameState.Ready;
                }

                // Update the motion of the bird
                _BirdVerticalSpeed += -_Gravity * gameTime.ElapsedGameTime.Milliseconds / 1000;
                _Bird.PositionY    += _BirdVerticalSpeed * gameTime.ElapsedGameTime.Milliseconds / 1000;
                if (_Bird.PositionY < Floor.Height)
                {
                    _Bird.PositionY    = Floor.Height;
                    _BirdVerticalSpeed = 0;
                }
                if (_Bird.PositionY > ScreenHeight)
                {
                    _Bird.PositionY    = ScreenHeight;
                    _BirdVerticalSpeed = 0;
                }
            }
            break;
            }
        }