| Mike Sikes's profileMike's XNA Game Studio B...BlogLists | Help |
|
Mike's XNA Game Studio BlogCreating games with XNA Game Studio December 18 Tutorial #4 - Adding basic movement to the ball...Now that we have a ball for the game and can display it on screen, we need to write some code to give the ball some motion, but we also need to look at some issues before we start coding this method. First of all, the game is going to be displayed inside of a window. If we set the ball in motion, it will start to move in a certain direction at a given speed, which is what we want it to do. The problem is, it will continue to move in that direction and eventually disappear as it moves beyond the edge of the window. In order to prevent this from happening, we need to set some boundaries, in other words, the ball movement must be contained within the window. If the ball hits the edge of the window, it should bounce off and start moving in another direction. For now, we are going to write a rather simple version, which will be modified later as the game progresses. In future tutorials, we will be adding paddles, blocks, power-ups and other neat things to the game, so we will need a more advanced version of this method. But for now, this will enable you to get a feel of how to give objects a basic movement within certain boundaries, and it will also give us something that we can build upon and improve later.
With all that in mind, load up your project and let's begin. In the file Game1.cs, find the line that has, "base.Update(gameTime);" in it. Directly below that line should be a bracket, you need to type in the following code (new code is in Bold) directly beneath the bracket so that it looks like this:
}// Type your code below this bracket
void UpdateBall(GameTime gameTime)
{
ballPosition += ballSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
// Set up the window boundaries for the ball
int RightSide = graphics.GraphicsDevice.Viewport.Width - ballTexture.Width;
int LeftSide = 0;
int Top = graphics.GraphicsDevice.Viewport.Height - ballTexture.Height;
int Bottom = 0;
// Test to see if ball has hit the sides, top or bottom of the window
if (ballPosition.X > RightSide)
{
ballSpeed.X *= -1;
ballPosition.X = RightSide;
}
else if (ballPosition.X < LeftSide)
{
ballSpeed.X *= -1;
ballPosition.X = LeftSide;
}
if (ballPosition.Y > Top)
{
ballSpeed.Y *= -1;
ballPosition.Y = Top;
}
else if (ballPosition.Y < Bottom)
{
ballSpeed.Y *= -1;
ballPosition.Y = Bottom;
}
}
After you are finished with the above code, you need to find the method, Update(GameTime gameTime) in the code, and just above it add the following lines:
// Sets the speed of the ball
Vector2 ballSpeed = new Vector2(100.0f, 100.0f);
Then add the following code within the brackets:
// Move the ball to a new position
UpdateBall(gameTime);
The whole section should look like this:
/// <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>
// Sets the speed of the ball
Vector2 ballSpeed = new Vector2(100.0f, 100.0f);
protected override void Update(GameTime gameTime)
{
// Allows the default game to exit on Xbox 360 and Windows
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
// TODO: Add your update logic here
// Move the ball to a new position
UpdateBall(gameTime);
base.Update(gameTime);
}
Compile your game and if you get no errors, hit Ctrl-F5 and run it. You should see your ball moving and bouncing around your screen. If you recieved errors when compiling, make sure you check your code. Also make sure you placed the code in the right place. If you still have problems and can't figure it out, post a comment and I will try to help you with it. In fact, feel free to post any comments as we go through the tutorials. See you in Tutorial #5. ;)
December 15 Tutorial #3 - Displaying a sprite on the screenBy now you're probably sick of looking at that CornFlowerBlue screen, and I don't blame you! Since we will be needing one anyway, I will show you how to add a ball to the game and later in Tutorial #4 we will give it some basic logic and motion. The first thing you will need to do is obtain a ball. You can create one yourself using a graphics program or you can can look for one on the Internet it's up to you. If you don't have a good graphics package, there are many that can be downloaded for free. My favorites are Paint.NET, and The Gimp, both are free and very powerful. For those of you that are not very artistic, I will place a copy of the ball I am using at the end of this post, feel free to download it for use in your game.
Once we have our ball, it's time to add it to our project. Open up Game Studio and load your project. In the Solution Explorer (the right side pane by default) right click on the name of your project, it should be the one in Bold at the top of the list. From the menu choose Add -->Existing Item and a dialog box will pop up. Make sure you select Content Pipeline Files in the Files of type selection box. Browse to wherever you saved your graphics file, click the file and then click add. An entry will be made for the file in Solution Explorer. Now that we have some content for our game, it's time to write some code that will load it and display it on our screen. The file Game1.cs is the file we need to work with right now. It will be listed in Solution Explorer, so once you find it just double click on it and the file will open up in the center pane. You will need to find a method called LoadGraphicsContent, and add the following lines of code, the ones in Bold: // This is the texture for the ball
Texture2D ballTexture;
// This is the position of the ball
Vector2 ballPosition = Vector2.Zero;
// This is the object that will draw the ball
SpriteBatch gameBall;
/// <summary>
/// </summary>
/// <param name="loadAllContent"></param>
protected override void LoadGraphicsContent(bool loadAllContent)
{
if (loadAllContent)
{
// TODO: Load any ResourceManagementMode.Automatic content
// Load the texture for the ball
ballTexture = content.Load<Texture2D>("gameball");
// Create a new ball object
gameBall = new SpriteBatch(graphics.GraphicsDevice);
}
// TODO: Load any ResourceManagementMode.Manual content
}
You will need to use the asset name of the resource in the above code, mine was "gameball", yours will probably be different. To find the asset name just right click on the entry for the ball in Solution Explorer and select Properties. The asset name should be listed at the top, simply remove "gameball" from the above line of code and replace it with your asset name.
The above lines of code will load your sprite and prepare it to be drawn on the screen, it will not however draw the sprite. We need to write some code in the Draw loop that will draw the ball on the screen. I would also suggest changing the screen color from Blue to Black, unless of course your ball is black, then you might not want to do that. So to do all of this you must find the Draw method and then add the following lines of code, the ones in Bold:
protected override void Draw(GameTime gameTime)
{
// Change screen color to Black
graphics.GraphicsDevice.Clear(Color.Black);
// TODO: Add your drawing code here
// Draw the ball
gameBall.Begin(SpriteBlendMode.AlphaBlend);
gameBall.Draw(ballTexture, ballPosition, Color.White);
gameBall.End();
base.Draw(gameTime);
}
You can now select Build from the menu and recompile your code. Press Ctrl-F5 and the progam should run. You should see a screen very similar to mine. In Tutorial #4 we will write some simple logic and get the ball moving on the screen. I will see you then.
December 11 XNA Game Studio final version available today...You can get the final release of XNA Game Studio from Microsoft as of today. Time to roll up the sleeves and start some serious coding! December 08 Tutorial #2 - Rendering a blank screen...Now that we have everything installed, it's time to start some coding! Fire up XNA Game Studio, and select New Project from the File menu. When the dialog box pops up make sure you choose "Windows Game" as your template and then type in the name of your game. Then, make sure the "Create directory for this solution" checkbox is checked and click the OK button. A bunch of starter files will be created and the panes on the left and right will be populated with file names, class names etc. Don't be concerned about these files just yet, you will learn about them as we progress through the tutorials.
Several of the files contain source code, which is what we will be writing throughout the tutorials. What we will do now is compile the source to see what it does. From the Build menu select Build Solution, and the code will be compiled into an executable file. Now we can press Ctrl-F5 and the program will run.
We're basically rendering a blank screen, so you should see something like the following: December 07 Tutorial #1 - Setting up XNA Game StudioFirst things first, download and install Microsoft Visual C# Express Edition. You can get it here. Once you have that installed you can now download and install XNA Game Studio Express Edition from this location. Once you have done the above we are pretty much ready to start coding.
For this game I am going to keep things rather simple and concentrate more on graphics quality, playability and functionality. You have no doubt played countless computer games and enjoyed playing them, which is probably why you want to learn how to create one of your own. Most games as you know do pretty much the same thing and work the same way. The difference between a good game and a bad game is not so much in the mechanics of the game but in the quality of the game, the presentation, and the fresh ideas that are added to the mix.
With this in mind, we are going to create a remake of the famous Breakout game. This game is realitively simple to implement, yet it can be made to look and behave in ways that simply were not possible when it was first written. So let's give this great classic a makeover and see what happens! See you in Tutorial #2... December 06 Begin coding of an XNA game...Today I am starting to code a game using the XNA Game Studio Express Edition. The resulting code will run on the PC platform and the XBOX 360 game console. I have been using the beta 2 version of XNA for over a month now, but I am waiting for the final release sometime this month (around the 11th I think) to do any serious work with XNA. Once it is released, and I have played around with it a little I will start to develop the final version of this game.
For those that wish to follow along, I will start with Tutorial #1 in a few days and will continue with the tutorials until we have a finished commercial quality game. |
Useful links about XNA game development...
|
|||||||||||||||||||
|
|