예제 #1
0
        public void Run()
        {
            while (window.IsOpen)
            {
                float deltaT = Configuration.IsDebugFrameTime
                    ? Configuration.DebugFrameTime
                    : clock.Restart().AsMicroseconds() / 1000000f;

                // Clear the previous frame
                window.Clear(Configuration.Background);

                // Process events
                window.DispatchEvents();

                // Update all the screens
                screenManager.Update(deltaT);

                // Draw all the screens
                screenManager.Draw(deltaT);

                // Update the window
                window.Display();
            }
        }
예제 #2
0
        public void Run()
        {
            // Spawn our initial populaiton of individuals
            world.Spawn();

            Thread.Sleep(3000);

            // Set our generation count to zero and restart our timer
            int generation = 0;

            generationClock.Restart();

            while (window.IsOpen)
            {
                // Get the amount of time that has passed since we drew the last frame.
                float deltaT = clock.Restart().AsMicroseconds() / 1000000f;

                // Clear the previous frame
                window.Clear(Configuration.Background);

                // Process events
                window.DispatchEvents();

                // Camera movement is now separate from our other screen update code to minimise overhead.
                screenManager.UpdateCamera(deltaT);

                screenManager.Draw(deltaT);

                // Update the window
                window.Display();

                // If one second has passed, breed the next generation
                if (generationClock.ElapsedTime.AsSeconds() > generationTime &&
                    (doGeneration?.IsCompleted ?? true) &&
                    !world.HasConverged)
                {
                    doGeneration = Task.Run(() =>
                    {
                        // Do one generation of breeding & culling.
                        world.DoGeneration();

                        // Restart our generation timer
                        generationClock.Restart();

                        // Update the screen to show the new generation
                        pathScreen.GenerationString.StringText = $"Generation: {++generation}";

                        // Draw the paths of the current best individual
                        pathScreen.UpdateSequence(world.GetBestIndividual());

                        // Update all the screen components that are unrelated to our sequence.
                        screenManager.Update(deltaT);
                    });
                }

                // Our GA has either hit the max generations or has stopped improving, set the GA as complete
                // and copy the history of fitness values to the clipboard
                if (world.GenerationCount == GAConfig.MaxGenerations ||
                    world.NoImprovementCount == GAConfig.MaxNoImprovementCount)
                {
                    pathScreen.SetGACompleted();

                    // Copy the fitness to clipboard
                    Clipboard.SetText(string.Join(",", world.FitnessOverTime));
                }

                // Process any key presses that the user has made.
                this.ProcessUserInput();

                // Check to see if the user wants to stop the simulation.
                if (Keyboard.IsKeyPressed(Configuration.QuitKey) ||
                    Keyboard.IsKeyPressed(Keyboard.Key.Escape))
                {
                    return;
                }
            }
        }
예제 #3
0
        public static void Update()
        {
            if (screenManager.IsTransitioning())
            {
                screenManager.Update(gameScene);

                // Transition Finished, load relevant data
                if (!screenManager.IsTransitioning())
                {
                    if (screenManager.GetScreen() == Screens.Game)
                    {
                        SetupGame();
                        soundManager.PlayBGM();
                    }
                    else if (screenManager.GetScreen() == Screens.GameOver)
                    {
                        gameScene.Camera2D.SetViewY(new Vector2(0.0f, Director.Instance.GL.Context.GetViewport().Height *0.5f),
                                                    new Vector2((Director.Instance.GL.Context.GetViewport().Width *0.5f), Director.Instance.GL.Context.GetViewport().Height *0.5f));
                    }
                }
            }
            else
            {
                UpdateTouchData();

                switch (screenManager.GetScreen())
                {
                case Screens.Splash:
                    screenManager.ChangeScreenTo(Screens.Menu);
                    break;

                case Screens.Game:
                    if (!tutorialManager.HasPopUp())
                    {
                        GameUpdate();
                    }
                    else
                    {
                        UpdateTouchData();
                    }
                    break;

                case Screens.GameOver:
                    Vector2 touchPos = Input2.Touch00.Pos;

                    if (touchPos.X >= 0)
                    {
                        touchPos = new Vector2((touchPos.X * 450) + 450, (touchPos.Y * 272) + 272);
                    }
                    else
                    {
                        touchPos = new Vector2(((touchPos.X + 1) * 450), ((touchPos.Y + 1) * 272));
                    }
                    Bounds2 touchBox = new Bounds2(touchPos, touchPos);
                    if (touchBox.Overlaps(rSprite.GetlContentLocalBounds()))
                    {
                        screenManager.ChangeScreenTo(Screens.Menu);
                        gameScene.RemoveChild(rSprite, false);
                    }

                    break;
                }
            }
        }
예제 #4
0
파일: TankGame.cs 프로젝트: Stran6/TankGame
        protected override void Update(GameTime gameTime)
        {
            //if (!gameRunning)
            //{
            //    if (!soundManager.introPlaying)
            //        soundManager.playIntro();
            //}
            //else
            //{
            //    if (!soundManager.backgroundPlaying)
            //        soundManager.playBackground();
            //}

            if (turnsTaken == 0 & !shotFired)
            {
                cameraFollow(currentTank.getTankPos());
            }

            player1Tank.checkAngle();
            player2Tank.checkAngle2();

            if (currentTank == player1Tank)
            {
                currentTank.turretPosition = new Vector2(currentTank.tankPosition.X + 45, currentTank.tankPosition.Y + 10);
                controller = GamePad.GetState(PlayerIndex.One);
                currentTank.recieveController(GamePad.GetState(PlayerIndex.One));
            }

            if (currentTank == player2Tank)
            {
                currentTank.turretPosition = new Vector2(currentTank.tankPosition.X + 30, currentTank.tankPosition.Y + 10);
                controller = GamePad.GetState(PlayerIndex.Two);
                currentTank.recieveController(GamePad.GetState(PlayerIndex.Two));
            }

            if ((currentTank.tankPosition.X > 180 && currentTank.tankPosition.X < 468) || (currentTank.tankPosition.X > 1160 && currentTank.tankPosition.X < 1260))
            {
                currentTank.turretPosition.X = currentTank.tankPosition.X + 50;
                currentTank.turretPosition.Y = currentTank.tankPosition.Y + 15;
            }
            else if ((currentTank.tankPosition.X > 705 && currentTank.tankPosition.X < 950) || (currentTank.tankPosition.X > 1540 && currentTank.tankPosition.X < 1795))
            {
                currentTank.turretPosition.X = currentTank.tankPosition.X + 30;
                currentTank.turretPosition.Y = currentTank.tankPosition.Y + 15;
            }
            else
            {
                currentTank.turretPosition.X = currentTank.tankPosition.X + 40;
                currentTank.turretPosition.Y = currentTank.tankPosition.Y + 10;
            }

            if (controller.Buttons.Back == ButtonState.Pressed)
            {
                this.Exit();
            }
            if (Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                this.Exit();
            }

            screenManager.Update(gameTime);

            player1Tank.tempSpeed = 1f;
            player2Tank.tempSpeed = 1f;

            if (player1Tank.intersectPixels(background.layers[1].Sprites[0].Position, background.terrainTextureData))
            {
                player1Tank.tempSpeed = 0;
            }

            if (player2Tank.intersectPixels(background.layers[1].Sprites[0].Position, background.terrainTextureData))
            {
                player2Tank.tempSpeed = 0;
            }

            if (gameRunning)
            {
                //if (soundManager.introPlaying)
                //    soundManager.stopIntro();

                uiManager.Update(gameTime);
                background.Update(gameTime);

                takeTurn(gameTime);

                tankCollision(player1Tank, player2Tank);
                powerUpManager.PowerUpCollision(player1Tank, player2Tank);

                if (isGameOver() || Keyboard.GetState().IsKeyDown(Keys.P))
                {
                    gameRunning = false;
                    GamePad.SetVibration(PlayerIndex.One, 0.0f, 0.0f);
                    GamePad.SetVibration(PlayerIndex.Two, 0.0f, 0.0f);
                    screenManager.ChangeGameState(ScreenManager.GameState.END);
                }

                if (currentTank.turnTime <= 5 && currentTank.turnTime > 0)
                {
                    if (countdownTimer >= 1)
                    {
                        soundManager.timeBeep.Play();
                        countdownTimer = 0;
                    }
                    else
                    {
                        countdownTimer += (float)gameTime.ElapsedGameTime.TotalSeconds;
                    }
                }
            }

            if (screenManager.currentState == ScreenManager.GameState.END)
            {
                resetSetting();
            }

            uiManager.moveFloatingText();

            base.Update(gameTime);
        }