Showing posts with label XNA Tutorial: Make a Game in 60 Minutes. Show all posts
Showing posts with label XNA Tutorial: Make a Game in 60 Minutes. Show all posts

Friday, June 22, 2012

XNA Tutorial: Make a Game in 60 Minutes #2

Hello. This post is the continuation to the previous post on an XNA Tutorial: Make a Game in 60 Minutes.

I have continued from the previous step. Now, there are more problem arising from the tutorial.

Firstly, when the asteroids drifted on top or bottom, the shifting to the other side is smooth. On the other hand, when it happens left to right, you could see the jerking shift. The solution if to change the "if" so that the asteroids will be shifted when they drift more out of the edge and are shifted to more out of the edge as well. I am not sure how to do it smoothly, but I will be doing it roughly. (This amendment will change if you change the viewport resolution or the camera projection setting)

In Asteroid.cs, replace the following:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if (position.X > GameConstants.PlayfieldSizeX)
    position.X -= 2 * GameConstants.PlayfieldSizeX;
if (position.X < -GameConstants.PlayfieldSizeX)
    position.X += 2 * GameConstants.PlayfieldSizeX;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

with the following:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if (position.X > GameConstants.PlayfieldSizeX + 2000)
    position.X -= 2 * GameConstants.PlayfieldSizeX + 4000;
if (position.X < -GameConstants.PlayfieldSizeX - 2000)
    position.X += 2 * GameConstants.PlayfieldSizeX + 4000;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

You might want to ask why for the if condition it is + and - while for the code called for the condition it is both +. The reason is due to the use of -= and +=. Thus, the value after them should be the same for symmetry.

Another change I implement is on the starting point of the asteroids. If you follow the tutorial, you will see that the asteroids appear more on top half then evenly across the screen. This is because the code states that there is only two values, 0 and 1. If it is 0, it will appear on the center of the screen height while if it is 1, top of the screen height. Those are the top and bottom limit. To make the asteroids also appear on the bottom of the screen height, we need to make the random values called to be -1 and 1 instead of 0 and 1 so as the bottom limit is not the center of the height but the bottom of the screen height.

In Game1.cs, replace the following:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
(This can be found under private void ResetAsteroids())

yStart = (float)random.NextDouble() * GameConstants.PlayfieldSizeY;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

with the following:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
yStart = ((float)random.NextDouble() * 2 - 1) * GameConstants.PlayfieldSizeY;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

It is actually simple math. If I get 0, it will be multiplied by 2 (= 0) then subtracted by 1 (= -1). If I get 1, it will be multiplied by 2 (= 2) then subtracted by 1 (= 1). I found out how to change the 0 and 1 to -1 and 1 from this page.

I just finished step 3, will continue later. Gotta go to Bandung for this weekend :). Hope this post help you. Oh, and by the way, Windows Phone 8 support C++ language. I am not sure about C# support (this tutorial is in C#), hope it is continued! I am also amateur in terms of understanding the different languages and their compatibilities. Anyway, cheers!

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. :)