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

Tuesday, June 5, 2012

WP7 Game Development Tutorial for Absolute Beginner - Updated

I started following this tutorial when I was looking for Windows Phone 7 (WP7) development for absolute beginner. It was for game development. Still trying to finish it. It is a good tutorial, but you may need to tweak some of it a little bit. If you are a real absolute beginner who never touches programming or scripting, you could go here. It's a slow paced tutorial (I never passed the Day 1 mark) but the guy (Bob Tabor) explains really well for you. Anyway, I will discuss the problems I encountered.

Firstly, the position of the player after a few steps changed. It is no longer at the desired position near the center. It will be a little bit up and to the left. Also, the movement is restricted not to the viewport (viewport is the window of the game) but to the left and to the top of the viewport, like this.
____________________
|     _________________|___              Red box is the restricted area of movement
|    |                                  |      |              while black box is the viewport.
|    |                                  |      |
|__|_________________|      |
     |____________________|


To solve it, you need to look for the code which get the value of the player width and height divided by two. I believe it was meant to fix the aforementioned problem, but probably due to version update or something, it CREATES the problem. What is happening is that previously, probably the program calculate the origin of x and y of the player image at the center of the image, while the real thing is at the top left corner of the image. Thus, the problem. By the way, x-axis in the viewport starts from the left while y-axis from the top.

Secondly, you will find that at the tutorial about collision, there is a problem. Sometimes your projectile missed and I can't remember the other problem (I think the starting position of the projectile is wrong). Same problem as above. You need to find out which vector values are wrong. The enemy's rectangle is too high, delete the value of its height divide by 2. If you think the rectangle of the projectile is too to the left, don't, or your projectile will be gone before it touches the enemy's image (when the projectile's image enters the enemy's rectangle but does not touch the enemy's image yet, it is not removed).

Thirdly, after you reach the tutorial about explosion, you will find that the explosions occurs not at the center of the enemy image, but rather at the bottom right corner of the enemy image. I am still figuring out how to fix it, be patient :)

I don't know if the problem is because of the program's version in my computer, but if you encounter the same problems, there you go. :)
And for the record, I am a beginner, although not absolute.


*Update 1:
Seems that the link to the tutorial is gone. I will try too look into it again. If you have any idea where it goes to, could you help me by posting it in the comment? Thanks.

*Update 2:
Okay. I found the link to the tutorial: http://channel9.msdn.com/Blogs/The-XNA-Show/XNA-Game-Development-Intro and http://channel9.msdn.com/Blogs/The-XNA-Show?page=2 . However, it seems that the written tutorial is gone. Can't do much about it. One comment states just go to create.mdsn.com and find the game tutorial there, but I could not find it.

Thursday, May 24, 2012

Blackberry Theme Simply Blocks Red Dark - Updated

My first Blackberry Theme has been approved! It is free. Currently the only version supported is Blackberry Curve 9300. Will be updating the theme and the version supported soon. The idea comes from Windows Phone OS, but cannot fit the portrait screen to the landscape BlackBerry screen, so here it goes.
If you are on your computer, head here. If you are on your mobile phone, type simply blocks in your AppWorld.
If you have suggestions on the theme, comment this post! Other colour will come from request.

*Update 1:
The update for the theme is discontinued.

*Update 2:
I pulled out the theme from the Blackberry AppWorld. I am also not using Blackberry anymore. If you would like to try the theme, you could contact me by leaving a comment. Thanks~