110 likes | 226 Views
This lecture provides a comprehensive overview of key methods used in constructing an XNA game. It covers the essential processes such as initializing and setting up variables, loading content like textures and fonts, and managing user input. The update and draw methods are explained, emphasizing that all drawing should occur in the Draw method, while user input and state changes should be handled in the Update method. Additionally, instructions for managing assets using classes like Texture2D and SpriteBatch are included, alongside the use of random numbers and lists for dynamic item storage.
E N D
Lecture 4 XNA Review Richard Gesick
TheMajor Methods of an XNA Game Constructor - initialize and set up variables as usual LoadContent - load SpriteFont, Texture2D, etc. Update - handle all user input and update game state Draw - handle drawing the images/graphics to the screen
NO drawing should happen in Update and No input/state changes should happen in Draw
Content Draw your assets (using transparent backgrounds if desired) in your favorite paint program. GIMP and Paint.NET are both free Add your assets to the Content folder and then add them to your project (via "Project | Add | Add Existing...")
Create attributes within the class to hold the assets Texture2D bowl; then Load the assets into the attributes in the LoadContent method bowl = Content.Load<Texture2D>("bowl");
User Input Handle user input (via the GetState method of the Keyboard class) currentKeyboardState= Keyboard.GetState(PlayerIndex.One); if (currentKeyboardState.IsKeyDown(Keys.Left)) { bowl_position.X -= 3; if (bowl_position.X < 0) bowl_position.X = 0; } . . .
Draw(1) Draw icons/sprites to the screen (via the Draw method of the SpriteBatch class) GraphicsDevice.Clear(Color.CornflowerBlue); spriteBatch.Begin(); spriteBatch.Draw(bowl,bowl_position, Color.White);
Draw (2) Draw strings to the screen (via the DrawString method of the SpriteBatch class) spriteBatch.DrawString(stats_font, "Food consumed: " + FoodCaught, pos, Color.White); Be sure to place all screen manipulation inside the Begin/End methods of the SpriteBatch class
Random numbers Random r; … r = new Random(); … = r.Next(1, 4); // values 1 to 3 = r.Next(-100, 100); //values -100 to 99 = r.Next(100) //values 0 to 99
List<T> List<> is a parameterized collection class that is useful for storing dynamic collections of items; use the Add, Remove, RemoveAt, etc. methods falling_food = new List<FoodItem>(); … falling_food.Add(fi);
Tutorial Link Tutorial courtesy of Dr. J. Preston. audio/video tutorials cse1302_Lecture4_FoodWars.zip