Class #3: July 14, 2010
Create New Project Open Visual C#: Start Menu Microsoft XNA Game Studio 3.1 Visual C# Express Edition Create New Project: File New Project Type: XNA Game Studio 3.1 Template: Windows Game 3.1
Classes Classes: Program class in Program.cs Game1 class in Game1.cs Namespace AwesomeGame Like a package in Java, is just a grouping for your classes.
Program class Has a main method that is called at runtime Initializes a new Game1 object game. Calls game.Run() If you check Game1.cs you will not find this method, because it is in the Microsoft.XNA.Framework.Game class. Game1 inherits from this class. Run() begins the game loop.
Game Loop running = true; while (running) { update(); draw(); } As the programmer, it’s up to you to make sure everything gets updated in the update() function and displayed in the draw() function.
Game1 class Update() Get player input Move everything on screen Check for collisions Make decisions based on all the above. (is the player dead? Is the game over?) Draw(), display objects to screen.
Game1 class GraphicsDeviceManager graphics; Handles the management of the graphics device, or what is output to the screen. Defaults to a game window of 800x600. SpriteBatch spriteBatch; Allows us to draw our sprites to the screen. Initialize() Where we initialize our variables. LoadContent() Where we load our content – images, audio, models, etc.
Other Data Types Just like the Java API provides classes like String, the XNA framework is an API and we will be using many of its classes. Today we will be using Vector2 and Texture2D
Vector2 Vector2 is a container for 2 values, an X coordinate and a Y coordinate, both floats. Useful for storing positions. For example if we wanted to store the position of the background texture we would create a Vector2 : Vector2 backgroundPosition = new Vector2(0.0f, 0.0f); To get or set the X coordinate, we do that like this: backgroundPosition.X = backgroundPosition.X +10; backgroundPosition = backgroundPosition +10;
Texture2D class Is a container for an image. After you have added an image to your Content folder: Declare under global variables: Texture2D backgroundTexture; Load the texture under LoadContent(): backgroundTexture = Content.Load <Texture2D>(“Sprites \\ background”);
Draw() Have to use the following syntax to draw your texture in Draw(): spriteBatch.Begin(SpriteBlendMode.AlphaBlend); spriteBatch.Draw(texture, texturePosition,Color.White); spriteBatch.End();
Yesterday’s Answers
Recommend
More recommend