Friday, June 15, 2012

XNA Tutorial: Make a Game in 60 Minutes #1

Hello, everybody. I am following up the previous post by doing the 3D tutorial.

There is the tutorial about a simple asteroid game in XNA. Although the tutorial is for XNA Game Studio 3.1, I found no trouble up to now using XNA 4.0. Click here for the link to the tutorial.

I am still halfway through the tutorial (finished Step 2). I paused because I was looking for a way to make it work with keyboard rather than Xbox controller because I don't have one. If you are trying to do the same thing as me, you could refer below.

In Ship.cs, replace the following:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

public void Update(GamePadState controllerState)
        {
            // Rotate the model using the left thumbstick, and scale it down.
            Rotation -= controllerState.ThumbSticks.Left.X * 0.10f;

            // Finally, add this vector to our velocity.
            Velocity += RotationMatrix.Forward * 1.0f * 
                controllerState.Triggers.Right;
        }

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

with the following:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public void Update(GamePadState controllerState)
        {
            //Rotate the model using A and D
            if (keyboardState.IsKeyDown(Keys.A))
                Rotation += 0.10f;
            else if (keyboardState.IsKeyDown(Keys.D))
                Rotation -= 0.10f;

            if (keyboardState.IsKeyDown(Keys.W))
                Velocity += RotationMatrix.Forward * VelocityScale;
            else if (keyboardState.IsKeyDown(Keys.S))
                Velocity -= RotationMatrix.Forward * VelocityScale; 
         }
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


In Game1.cs, replace the following:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

protected void UpdateInput()
{
    // Get the game pad state.
    GamePadState currentState = GamePad.GetState(PlayerIndex.One);
    if (currentState.IsConnected)
    {
        ship.Update(currentState);

        //Play engine sound only when the engine is on.
        if (currentState.Triggers.Right > 0)
        {

            if (soundEngineInstance.State == SoundState.Stopped)
            {
                soundEngineInstance.Volume = 0.75f;
                soundEngineInstance.IsLooped = true;
                soundEngineInstance.Play();
            }
            else
                soundEngineInstance.Resume();
        }
        else if (currentState.Triggers.Right == 0)
        {
            if (soundEngineInstance.State == SoundState.Playing)
                soundEngineInstance.Pause();
        }


        // In case you get lost, press A to warp back to the center.
        if (currentState.Buttons.A == ButtonState.Pressed)
        {
            ship.Position = Vector3.Zero;
            ship.Velocity = Vector3.Zero;
            ship.Rotation = 0.0f;
            soundHyperspaceActivation.Play();
        }
    }
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

with the following:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

protected void UpdateInput()
{
            // Get the keyboard state.
            KeyboardState currentKeyState = Keyboard.GetState();


            ship.Update(currentKeyState);

            //Play engine sound only when the engine is on.
            if (currentKeyState.IsKeyDown(Keys.W) || currentKeyState.IsKeyDown(Keys.S))
            {
                    if (soundEngineInstance.State == SoundState.Stopped)
                    {
                        soundEngineInstance.Volume = 0.75f;
                        soundEngineInstance.IsLooped = true;
                        soundEngineInstance.Play();
                    }
                    else
                        soundEngineInstance.Resume();
            }
            else if (currentKeyState.IsKeyUp(Keys.W) || currentKeyState.IsKeyUp(Keys.S))
            {
                    if (soundEngineInstance.State == SoundState.Playing)
                        soundEngineInstance.Pause();
            }


            // In case you get lost, press A to warp back to the center.
            if (currentKeyState.IsKeyDown(Keys.Space))
            {
                    ship.Position = Vector3.Zero;
                    ship.Velocity = Vector3.Zero;
                    ship.Rotation = 0.0f;
                    soundHyperspaceActivation.Play();
            }
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I did several changes. I change the reset button from Enter (in previous tutorial) to Space so that I can use Enter to shoot, if I decide to do so. Also, I enable moving backward by pressing S which was not implemented in any of the tutorials.

My job here is done :) Sorry about the colours and indentations. It appears black after copy paste from my Visual Studio. If you got questions, you could post them in comment section. I am still thinking about how to make it work both with gamepad and with keyboard, probably changing the update in Ship.cs to Update(Gametime Gametime), but I am unsure about it. For now, I'll stick with the keyboard. :)

No comments:

Post a Comment