All right, now we have our variables in place. In case you’ve missed it, you can find page 1 here.
We’ll start by building the bouncing orb, then adding in the start menu, the loading screen and the pause button.
Normally you would load all textures and content in the LoadContent() method, this method is called once when the game launches. This is all good when the game is very small and has no menu. We will build a LoadGame() method that does all the loading of game textures and at the end of this guide the method will be called when the player clicks start.
As said, the menu will be introduced later, for now create the LoadGame() method right under the Draw() method.
1: void LoadGame()
3: //load the game images into the content pipeline
4: orb = Content.Load<Texture2D>(@"orb");
6: //set the position of the orb in the middle of the gamewindow
7: orbPosition = new Vector2((GraphicsDevice.Viewport.Width / 2) - (OrbWidth / 2),
8: (GraphicsDevice.Viewport.Height / 2) - (OrbHeight / 2));
Line number 4 loads an image into the content pipeline as a 2D texture. To do this, we need to add a texture to our project. Save this picture to your pc and remember it’s location.
This is the orb that’ll be moving back and forth. To use it, right-click the content project in your solution explorer, select Add > existing file and select the image.
Line 7 and 8 set the position of the orb, initially this will be the middle of the gamescreen. This is done by taking the width and height from the screen and dividing it by two. Since this will place the upper left corner of the image in the middle we need to substract half the image width and height from the respective width and height.
Now we have a method that loads something in our content pipeline, now we need to call this method. For now we’ll just use the Initialize() method for this. Change it so it looks like this:
1: protected override void Initialize()
3: //enable the mousepointer
4: IsMouseVisible = true;
This will call the LoadGame() method as soon as the game starts. So now we have a method that loads a texture, we call the method when the game launches, all that’s left is to draw the texture 60 times per second. To get this done, change the Draw() method so it looks like this:
1: protected override void Draw(GameTime gameTime)
6: //draw the orb
7: spriteBatch.Draw(orb, orbPosition, Color.White);
SpriteBatch is an XNA specific class that will take care of rendering everything on the screen. It has a Begin() and an End() method that need to be called, everything between those two methods will be rendered onscreen. It takes three parameters, a texture, a position vector and a color. Color.White says the spritebatch to render the texture in it’s original color.
If we run the game now we see a red orb standing in the middle of the screen.
Now let’s get that little red lazy bum moving! Moving thing around in a game is done in the Update() method. Change it so it looks like this:
1: protected override void Update(GameTime gameTime)
3: // Allows the game to exit
4: if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
7: //move the orb
8: orbPosition.X += speed;
10: //prevent out of bounds
11: if (orbPosition.X > (GraphicsDevice.Viewport.Width - OrbWidth) || orbPosition.X < 0)
13: speed *= -1;
Line 8 increments the X value of the orb’s position with the value of the speed variable 60 times every second, at that speed it shows a very fluent animation. The if construction on line 11 checks if the orb goes out of sight and prevents it by inverting the speed value so it makes the orb go the other way.
So our gameplay is finished now, on the next page we will finally introduce the start menu.
Hurry on to page 3!