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!

No comments:

Post a Comment