/// <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) { // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) { this.Exit(); } // Check if the x force on the Zune has changed by more than 0.4g in the past half second - if so, it's shaking ShakeTime += gameTime.TotalGameTime.Milliseconds; if (ShakeTime >= 500) { Vector3 acceleration = Accelerometer.GetState().Acceleration; if (Math.Abs(acceleration.X - LastAccelX) > 0.4) { Cls = true; } LastAccelX = acceleration.X; ShakeTime = 0; } // Update touch panel state touchStateCollection = TouchPanel.GetState(); base.Update(gameTime); }
/// <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) { // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) { this.Exit(); } //Update the Touches BEFORE updating the Game Objects TouchCollection touch = TouchPanel.GetState(); AccelerometerState accelstate = Accelerometer.GetState(); phsX.Gravity = (float)9.8 * (new Vector2(accelstate.Acceleration.X, -accelstate.Acceleration.Y)); foreach (TouchLocation tl in touch) { foreach (Geom b in phsX.GeomList) { b.Body.ApplyForce((tl.Position - b.Position) / (float)2.0); } } phsX.Update((float)(1.0 / 30.0)); //Update physX base.Update(gameTime); }
/// <summary> /// Get the ship position /// </summary> protected void HandleInput() { GamePadState gamepadstatus = GamePad.GetState(PlayerIndex.One); // Check the thumbstick position.Y += (int)(gamepadstatus.ThumbSticks.Left.Y * -4); position.X += (int)(gamepadstatus.ThumbSticks.Left.X * 4); // Check the accelerometer position.Y += (int)(Accelerometer.GetState().Acceleration.Y * -4); position.X += (int)(Accelerometer.GetState().Acceleration.X * 4); }
private void HandleInput(GameTime gameTime) { // get all of our input states keyboardState = Keyboard.GetState(); touchState = TouchPanel.GetState(); gamePadState = virtualGamePad.GetState(touchState, GamePad.GetState(PlayerIndex.One)); accelerometerState = Accelerometer.GetState(); if (keyboardState.IsKeyDown(Keys.Escape) || keyboardState.IsKeyDown(Keys.Q)) { Exit(); } #if !NETFX_CORE // Exit the game when back is pressed. if (gamePadState.Buttons.Back == ButtonState.Pressed) { Exit(); } #endif bool continuePressed = keyboardState.IsKeyDown(Keys.Space) || gamePadState.IsButtonDown(Buttons.A) || touchState.AnyTouch(); // Perform the appropriate action to advance the game and // to get the player back to playing. if (!wasContinuePressed && continuePressed) { if (!level.Player.IsAlive) { level.StartNewLife(); } else if (level.TimeRemaining == TimeSpan.Zero) { if (level.ReachedExit) { LoadNextLevel(); } else { ReloadCurrentLevel(); } } } wasContinuePressed = continuePressed; virtualGamePad.Update(gameTime); }
public void Update(GameTime gameTime) { Vector2 newPosition; #if ZUNE if (Math.Abs(Accelerometer.GetState().Acceleration.X) > 0.05) { movementSpeed = Accelerometer.GetState().Acceleration.X *movementMulitplier; isFacingRight = (movementSpeed > 0); newPosition = new Vector2(Position.X + movementSpeed, Position.Y); if (newPosition.X < 272 - this.Texture.Width / 2 && newPosition.X > 0) { Position = newPosition; } if (gameTime.TotalRealTime.Milliseconds % 7 == 0) { frame = (frame == 0) ? 1 : 0; } } #else if (Keyboard.GetState().IsKeyDown(Keys.Right)) { newPosition = new Vector2(Position.X + movementSpeed, Position.Y); if (newPosition.X < 272 - this.Texture.Width / 2) { Position = newPosition; } isFacingRight = true; if (gameTime.TotalRealTime.Milliseconds % 7 == 0) { frame = (frame == 0) ? 1 : 0; } } if (Keyboard.GetState().IsKeyDown(Keys.Left)) { newPosition = new Vector2(Position.X - movementSpeed, Position.Y); if (newPosition.X > 0) { Position = newPosition; } isFacingRight = false; if (gameTime.TotalRealTime.Milliseconds % 7 == 0) { frame = (frame == 0) ? 1 : 0; } } #endif }
/// <summary> /// This is called when the game should draw itself. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Draw(GameTime gameTime) { graphics.GraphicsDevice.Clear(Color.CornflowerBlue); spriteBatch.Begin(); spriteBatch.Draw(caracter, position, caracterColor); spriteBatch.DrawString(font, GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.ToString(), Vector2.One, Color.Black); spriteBatch.DrawString(font, Accelerometer.GetState().Acceleration.ToString(), new Vector2(1, 40), Color.Black); // Draw the virtual GamePad GamePad.Draw(gameTime, spriteBatch); spriteBatch.End(); base.Draw(gameTime); }
/// <summary> /// Handles the input base. /// </summary> /// <param name="input">Input.</param> public override void HandleInputBase(vxInputManager input) { // get all of our input states keyboardState = Keyboard.GetState(); touchState = TouchPanel.GetState(); //gamePadState = virtualGamePad.GetState(touchState, GamePad.GetState(PlayerIndex.One)); accelerometerState = Accelerometer.GetState(); //virtualGamePad.Update(gameTimeBase); // #if !NETFX_CORE // // Exit the game when back is pressed. // if (gamePadState.Buttons.Back == ButtonState.Pressed) // vxEngine.Game.Exit(); // #endif }
/// <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) { // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) { this.Exit(); } // TODO: Add your update logic here base.Update(gameTime); // Save the previous state of the keyboard and game pad so we can determinesingle key/button presses previousGamePadState = currentGamePadState; previousKeyboardState = currentKeyboardState; // Read the current state of the keyboard and gamepad and store it currentKeyboardState = Keyboard.GetState(); currentGamePadState = GamePad.GetState(PlayerIndex.One); //Update the player UpdatePlayer(gameTime); // Update the parallaxing background bgLayer1.Update(); bgLayer2.Update(); // Update the enemies UpdateEnemies(gameTime); // Update the explosions UpdateExplosions(gameTime); #if MONOGAME var state = Accelerometer.GetState(); player.Position.X += (float)state.Acceleration.Y * playerMoveSpeed; player.Position.Y += (float)state.Acceleration.X * playerMoveSpeed; #endif }
/// <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) { base.Update(gameTime); touchCollection = TouchPanel.GetState(); accelerometerState = Accelerometer.GetState(); // Use X axis to determine if the Zune is being viewed from the // left or right side. // Leave fixed for now, need to rethink recalculating marble // positions when screen is rotated. if (accelerometerState.Acceleration.X >= 0.0 && accelerometerState.Acceleration.X < 1.0) { screenOrientation = ScreenOrientation.LandscapeRight; screenRotation = 4.72f; } else { screenOrientation = ScreenOrientation.LandscapeLeft; screenRotation = 1.57f; } if (screenOrientation != priorScreenOrientation) { if (GameState == GameState.Play2D) { mainGame.RecalculateMarblePositions(); } priorScreenOrientation = screenOrientation; } if (NextGameState != GameState.None) { ChangeGameState(); } }
// internal because we want our game types to call this. internal static void Update(LandscapeOrientation landscapeOrientation) { #if !WINDOWS // On Zune we can just use the TouchPanel class and fill // our ZuneTouch structures with the same data. // clear out the old touches touches.Clear(); // for each touch on the touch panel foreach (var t in TouchPanel.GetState()) { Vector2 position = t.Position; // adjust the touch's position based on device orientation switch (landscapeOrientation) { case LandscapeOrientation.Right: position = new Vector2(position.Y, 272 - position.X); break; case LandscapeOrientation.Left: position = new Vector2(480 - position.Y, position.X); break; default: break; } // add a new ZuneTouch instance that duplicates the data touches.Add(new ZuneTouch { ID = t.Id, Position = position, State = (ZuneTouchState)(int)t.State, Pressure = t.Pressure }); } // Then we just update our accelerometer property from the Accelerometer class AccelerometerState accState = Accelerometer.GetState(); Acceleration = accState.Acceleration; #else // On PC we use the current and last mouse state's to position our // fake touch as well as update its state. MouseState mouseState = Mouse.GetState(); // we want to make sure we're in bounds of the screen, so figure out if we're // actually in landscape mode or not. int screenWidth = 272, screenHeight = 480; if (landscapeOrientation == LandscapeOrientation.Left || landscapeOrientation == LandscapeOrientation.Right) { screenWidth = 480; screenHeight = 272; } // first we check to see if we have a touch in the collection with a state of released, // and if so, we clear the list out if (touches.Count > 0 && touches[0].State == ZuneTouchState.Released) { touches.Clear(); } // if this is a new press of the left mouse button and the mouse is inside the window... if (mouseState.LeftButton == ButtonState.Pressed && lastMouseState.LeftButton == ButtonState.Released && mouseState.X >= 0 && mouseState.X <= screenWidth && mouseState.Y >= 0 && mouseState.Y <= screenHeight) { // add a new ZuneTouch instance with our data touches.Add(new ZuneTouch { ID = id++, Position = new Vector2(mouseState.X, mouseState.Y), State = ZuneTouchState.Pressed, Pressure = 1 }); } // otherwise if the left button isn't newly pressed and we are tracking a touch already... else if (touches.Count > 0) { // get the touch var touch = touches[0]; // update the position touch.Position = new Vector2(mouseState.X, mouseState.Y); // we assume the touch has moved touch.State = ZuneTouchState.Moved; // if the mouse was released inside the window OR moved outside of the window... if ((mouseState.LeftButton == ButtonState.Released && lastMouseState.LeftButton == ButtonState.Pressed) || mouseState.X < 0 || mouseState.X > screenWidth || mouseState.Y < 0 || mouseState.Y > screenHeight) { // set the State to Released touch.State = ZuneTouchState.Released; } // set the touch back onto the list touches[0] = touch; } lastMouseState = mouseState; // to fake accelerometer data, we use a GamePad's thumbsticks and set the acceleration // using this system: // X Axis of Left Thumbstick = X axis // Y Axis of Left Thumbstick = Y axis // X Axis of Right Thumbstick = Z axis GamePadState gamePadState = GamePad.GetState(PlayerIndex.One); Acceleration = new Vector3( gamePadState.ThumbSticks.Left.X, gamePadState.ThumbSticks.Left.Y, gamePadState.ThumbSticks.Right.X); #endif }
public override void Update(GameTime gameTime) { m_iTicks++; #if !ZUNE KeyboardState keyState = Keyboard.GetState(); if (keyState.IsKeyDown(Keys.Left) && m_vecPosition.X > 2) { m_vecVelocity = new Vector2(-5, 0); m_plyrDirection = PlayerDirection.Left; } else if (keyState.IsKeyDown(Keys.Right) && m_vecPosition.X < (272 - 2 - (m_texImage.Width / m_iMaxFrames))) { m_vecVelocity = new Vector2(5, 0); m_plyrDirection = PlayerDirection.Right; } else { m_vecVelocity = Vector2.Zero; } #else if (Accelerometer.GetState().Acceleration.X > .05 && m_vecPosition.X < (272 - 2 - (m_texImage.Width / m_iMaxFrames))) { m_plyrDirection = PlayerDirection.Right; m_vecVelocity = new Vector2(5, 0); } else if (Accelerometer.GetState().Acceleration.X < -0.05 && m_vecPosition.X > 2) { m_plyrDirection = PlayerDirection.Left; m_vecVelocity = new Vector2(-5, 0); } else { m_vecVelocity = Vector2.Zero; } if (m_vecPosition.X > (272 - 2 - (m_texImage.Width / m_iMaxFrames))) { m_vecPosition.X = (272 - 2 - (m_texImage.Width / m_iMaxFrames)); } else if (m_vecPosition.X < 2) { m_vecPosition.X = 2; } #endif #if !ZUNE if (keyState.IsKeyDown(Keys.Right) || keyState.IsKeyDown(Keys.Left)) #else if (Math.Abs(Accelerometer.GetState().Acceleration.X) > .05) #endif { if (m_iTicks > m_iFrameRate) { m_iFrame++; if (m_iFrame >= m_iMaxFrames) { m_iFrame = 0; } m_iTicks = 0; } } base.Update(gameTime); }
/// <summary> /// Input helper method provided by GameScreen. Packages up the various input /// values for ease of use. Here it checks for pausing and handles controlling /// the player's tank. /// </summary> /// <param name="input">The state of the gamepads</param> public override void HandleInput(InputState input) { if (input == null) { throw new ArgumentNullException("input"); } if (input.PauseGame) { if (gameOver == true) { MediaPlayer.Stop(); foreach (GameScreen screen in ScreenManager.GetScreens()) { screen.ExitScreen(); } ScreenManager.AddScreen(new BackgroundScreen()); ScreenManager.AddScreen(new MainMenuScreen()); } else { ScreenManager.AddScreen(new PauseMenuScreen()); } } else { bool touchShoot = false; #if TARGET_IPHONE_SIMULATOR // This section handles tank movement. We only allow one "movement" action // to occur at once so that touchpad devices don't get double hits. player.Velocity.X = MathHelper.Min(input.CurrentGamePadStates.ThumbSticks.Left.X * 2.0f, 1.0f); touchShoot = input.MenuSelect; #else // Add the accelerometer support player.Velocity.X = MathHelper.Min(Accelerometer.GetState().Acceleration.X * 2.0f, 1.0f); // tap the screen to shoot foreach (TouchLocation location in input.TouchStates) { switch (location.State) { case TouchLocationState.Pressed: touchShoot = true; break; case TouchLocationState.Moved: break; case TouchLocationState.Released: break; } } #endif if (touchShoot) { //Mouse.SetPosition(0,0); if (player.FireTimer <= 0.0f && player.IsAlive && !gameOver) { Bullet bullet = CreatePlayerBullet(); bullet.Position = new Vector2((int)(player.Position.X + player.Width / 2) - bulletTexture.Width / 2, player.Position.Y - 4); bullet.Velocity = new Vector2(0, -256.0f); player.FireTimer = 0.5f; particles.CreatePlayerFireSmoke(player); playerFired.Play(); } } } }
/// <summary> /// Updates the state of the game. This method checks the GameScreen.IsActive /// property, so the game will stop updating when the pause menu is active, /// or if you tab away to a different application. /// </summary> public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen) { base.Update(gameTime, otherScreenHasFocus, false); #region SomaAd // if the ad panel was tapped, show the click thru ad if (ScreenManager.enableAd == true) { while (TouchPanel.IsGestureAvailable) { GestureSample gestureSample = TouchPanel.ReadGesture(); if (gestureSample.GestureType == GestureType.Tap) { Vector2 touchPosition = gestureSample.Position; if (touchPosition.X >= 0 && touchPosition.X < somaAdSize.X && touchPosition.Y >= somaAdPosition.Y && touchPosition.Y < (somaAdPosition.Y + somaAdSize.Y)) { WebBrowserTask webBrowserTask = new WebBrowserTask(); webBrowserTask.Uri = new Uri(ScreenManager.getSoma.Uri); webBrowserTask.Show(); } } } } // if there is a new ad, get it from Isolated Storage and show it if (ScreenManager.getSoma.Status == "success" && ScreenManager.getSoma.AdImageFileName != null && ScreenManager.getSoma.ImageOK) { try { if (currentAdImageFileName != ScreenManager.getSoma.AdImageFileName) { currentAdImageFileName = ScreenManager.getSoma.AdImageFileName; IsolatedStorageFile myIsoStore = IsolatedStorageFile.GetUserStoreForApplication(); IsolatedStorageFileStream myAd = new IsolatedStorageFileStream(ScreenManager.getSoma.AdImageFileName, FileMode.Open, myIsoStore); textureSomaAd = Texture2D.FromStream(ScreenManager.GraphicsDevice, myAd); myAd.Close(); myAd.Dispose(); myIsoStore.Dispose(); } } catch (IsolatedStorageException ise) { string message = ise.Message; } } #endregion // Gradually fade in or out depending on whether we are covered by the pause screen. if (coveredByOtherScreen) { pauseAlpha = Math.Min(pauseAlpha + 1f / 32, 1); } else { pauseAlpha = Math.Max(pauseAlpha - 1f / 32, 0); } if (IsActive) { // Prompt player to start if (ScreenManager.playerReady == false) { ScreenManager.enableAd = true; ScreenManager.AddScreen(new ReadyScreen(readyCrashText), PlayerIndex.One); readyCrashText = "Ready?"; resetGame(); } // Reset game every new game if (ScreenManager.resetGame == true) { ScreenManager.resetGame = false; fuelExist = true; resetGame(); } if (ScreenManager.playerReady == true) { TrollFacePos = new Vector2(-100, -100); #region Accelerometer if (ScreenManager.enableAccelerometer == true && ScreenManager.enableAnimation == false) { //poll the acceleration value Vector3 acceleration = Accelerometer.GetState().Acceleration; if (playerPosition.X >= 72 && playerPosition.X <= 300) { // Sudden change of direction if (distance.X < 0 && acceleration.X > 0) { distance.X = 0; } else if (distance.X > 0 && acceleration.X < 0) { distance.X = 0; } distance.X = acceleration.X * 25.0f; distance.Y = 0; playerPosition += distance; playerRect.X += Convert.ToInt32(distance.X); if (playerPosition.X <= 72) { playerPosition.X = 72; distance.X = 0; } else if (playerPosition.X + carWidth >= 300) { playerPosition.X = 300 - carWidth; distance.X = 0; } } } #endregion #region Engine Sound if (ScreenManager.enableSoundEffect == true) { if (ScreenManager.engineSoundBool == true) { if (EngineInstance.State != SoundState.Playing) { EngineInstance.Play(); } } else { EngineInstance.Pause(); EngineInstance.Pitch = 0; } switch (gearNumEngine) { case 1: if (EngineInstance.Pitch < 0.8) { EngineInstance.Pitch += 0.01f; } else { EngineInstance.Pitch = -0.1f; gearNumEngine++; } break; case 2: if (EngineInstance.Pitch < 0.7) { EngineInstance.Pitch += 0.005f; } else { EngineInstance.Pitch = -0.2f; gearNumEngine++; } break; case 3: if (EngineInstance.Pitch < 0.6) { EngineInstance.Pitch += 0.001f; } break; } } #endregion playerCar = playerCarNotDestroy; timer -= (float)gameTime.ElapsedGameTime.TotalSeconds; if (timer <= 0) { timer = 1.0f; fuelCounter -= 2; if (fuelCounter <= 0) { EngineInstance.Pause(); ScreenManager.engineSoundBool = false; ScreenManager.AddScreen(new GameOverScreen(), PlayerIndex.One); } } // // //Win Statement //If goes through successfully, the game will end //UITimer -= (float)gameTime.ElapsedGameTime.TotalSeconds; //if (UITimer != 0) //{ // UITimer = 0.1f; // UICarVect.Y -= 1; // if (UICarVect.Y <= 115) // { // roadEndVect.Y = -800; // clearCars(); // } //} //UICarVect.Y -= 1; //if (UICarVect.Y <= 100) //{ // clearCars(); //} //if (roadEndVect.Y == 300) //{ // // Win Animation // ScreenManager.enableAnimation = true; // if (playerPosition.Y >= -100) // { // playerPosition.Y += 5; // } // else // { // ScreenManager.playerReady = false; // EngineInstance.Pause(); // WinMusicInstance.Play(); // ScreenManager.AddScreen(new WinScreen(), PlayerIndex.One); // } //} if (fuelCounter <= 0) { ScreenManager.AddScreen(new GameOverScreen(), PlayerIndex.One); } gameTimer -= (float)gameTime.ElapsedGameTime.TotalSeconds; // // Road Movement // if (ScreenManager.enableAnimation == false) { roadOneVect.Y += 50; roadTwoVect.Y += 50; roadEndVect.Y += 50; UICarVect.Y -= roadLength; // Fuel movement fuelVect.Y += 20; fuelRect.Y += 20; // Enemy car movement for (int i = 0; i < enemyCarNum; i++) { enemyCarsVect[i].Y += 20; enemyRect[i].Y += 20; } // Respawn car when they leave bottom of the screen enemyPositionCounter += 20; if (enemyPositionCounter >= 1600) { fuelPerTurn--; if (fuelPerTurn < 1) { fuelPerTurn = 3; fuelTransparency = 1.0f; fuelExist = true; } else { fuelTransparency = 0.0f; fuelExist = false; } carSpawner(); enemyPositionCounter = -800; } for (int i = 0; i < enemyCarNum; i++) { hitTest(i); if (fuelExist == true) { fuelPickUp(); } } } // Looping of two road sprite if (roadOneVect.Y >= 800) { roadOneVect.Y = -800; } else if (roadTwoVect.Y >= 800) { roadTwoVect.Y = -800; } if (roadEndVect.Y == -1600) { clearCars(); } // When ending reached, put ending sprite in between two road sprite if (roadEndVect.Y == -800) { roadOneVect.Y = 0; roadTwoVect.Y = -1600; clearCars(); } // Actual end if (roadEndVect.Y == 150) { if (ScreenManager.enableAnimation == false) { EngineInstance.Pause(); WinMusicInstance.Play(); } ScreenManager.enableAnimation = true; if (endingAnimationDeccelaration > 0) { playerPosition.Y -= endingAnimationDeccelaration; endingAnimationDeccelaration--; } else { ScreenManager.playerReady = false; ScreenManager.AddScreen(new WinScreen(), PlayerIndex.One); } } // // } } }