2000 Prentice Hall, Inc. All rights reserved. 1 Outline 3.1Introduction 3.2Game Loop Components 3.3How to Implement in C# 3.4Adding Image to XNA Project.

Slides:



Advertisements
Similar presentations
Time Sheet Training Press the F5 Key to view the Presentation. It may be helpful to have a printed copy of this slide presentation for the practice sessions.
Advertisements

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 2 Getting Started with Java.
1 Application Software Course Simulink By: Mahdi Akbari 2010.
1 Inheritance Classes and Subclasses Or Extending a Class.
Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
Copyright © 2003 Pearson Education, Inc. Slide 1.
1 Copyright © 2010, Elsevier Inc. All rights Reserved Fig 2.1 Chapter 2.
Tutorial 3 – Creating a Multiple-Page Report
Tutorial 9 – Creating On-Screen Forms Using Advanced Table Techniques
XP New Perspectives on Microsoft Office Word 2003 Tutorial 6 1 Microsoft Office Word 2003 Tutorial 6 – Creating Form Letters and Mailing Labels.
XP New Perspectives on Microsoft Office Word 2003 Tutorial 2 1 Microsoft Office Word 2003 Tutorial 2 – Editing and Formatting a Document.
XP New Perspectives on Microsoft Office Word 2003 Tutorial 7 1 Microsoft Office Word 2003 Tutorial 7 – Collaborating With Others and Creating Web Pages.
Lecture 10 Flow of Control: Loops (Part 2) COMP1681 / SE15 Introduction to Programming.
Game Making with Alice Eat the Hay Game
Mike Scott University of Texas at Austin
Word Lesson 6 Working with Graphics
Office 2003 Introductory Concepts and Techniques M i c r o s o f t Windows XP Project An Introduction to Microsoft Windows XP and Office 2003.
The Advantage Series © 2004 The McGraw-Hill Companies, Inc. All rights reserved Working with Access Microsoft Office Access 2003 Chapter 1.
Inspiration. Copyright Acadia Institute for Teaching and Technology Acadia University, Wolfville, NS, Canada, B0P 1X0 Tel. (902) , Fax (902)
View-Based Application Development Lecture 1 1. Flows of Lecture 1 Before Lab Introduction to the Game to be developed in this workshop Comparison between.
Excel Functions. Part 1. Introduction 2 An Excel function is a formula or a procedure that is performed in the Visual Basic environment, outside the.
POWERPOINT 2007 Introduction to Microsoft Office Skills.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 10 Arrays and Tile Mapping Starting Out with Games & Graphics.
XP New Perspectives on Introducing Microsoft Office 2003 Tutorial 1 1 Using Common Features of Microsoft Office 2003 Tutorial 1.
PowerPoint Basics   Tutorial 5: Navigation
© Paradigm Publishing, Inc Excel 2013 Level 1 Unit 2Enhancing the Display of Worksheets Chapter 8 Adding Visual Interest to Workbooks.
Benchmark Series Microsoft Excel 2013 Level 2
CREATING A PAYMENT REQUEST FOR A NEW VENDOR
Adobe InDesign CS5 - Illustrated Unit F: Working with Layers
Better Interactive Programs
 2000 Prentice Hall, Inc. All rights reserved. Chapter 14 - Advanced C Topics Outline 14.1Introduction 14.2Redirecting Input/Output on UNIX and DOS Systems.
Working with Tables for Page Design – Lesson 41 Working with Tables for Page Design Lesson 4.
25 seconds left…...
Review – Presentations with Graphics & Multimedia © 2010, 2006 South-Western, Cengage Learning.
Abstract Class, Packages and interface from Chapter 9
Chapter 8 Improving the User Interface
Sprites, User Input, and Collision COSC 315 Fall 2014 Bridget M. Blodgett.
Windfall Web Throughout this slide show there will be hyperlinks (highlighted in blue). Follow the hyperlinks to navigate to the specified Topic or Figure.
COMPUTER PROGRAMMING 2 Chapter 7 Sound. Objectives Find out how to prepare sounds for inclusion in Microsoft XNA projects. Incorporate sounds into XNA.
Game Design and Programming. Objectives Classify the games How games are design How games are implemented What are the main components of a game engine.
Chapter 9 Introduction to ActionScript 3.0. Chapter 9 Lessons 1.Understand ActionScript Work with instances of movie clip symbols 3.Use code snippets.
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
CHAPTER 1 XNA Game Studio 4.0. Your First Project A computer game is not just a program—it is also lots of other bits and pieces that make playing the.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
CHAPTER 4 Images XNA Game Studio 4.0. Objectives Find out how the Content Manager lets you add pictures to Microsoft XNA games. Discover how pictures.
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
Chapter 11 Adding Sound and Video. Chapter 11 Lessons 1.Work with sound 2.Specify synchronization options 3.Modify sounds 4.Use ActionScript with sound.
11 A First Game Program Session Session Overview  Begin the creation of an arcade game  Learn software design techniques that apply to any form.
Getting Started. XNA Game Studio 4.0 To download XNA Game Studio 4.0 itself, go to XNA Game.
Using Namepsaces  This section lists the namespaces that the application will be using frequently. Saves the programmer from specifying a fully qualified.
Developing the Game User Interface (UI) Lesson 5.
Alice 2.0 Introductory Concepts and Techniques Project 1 Exploring Alice and Object-Oriented Programming.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 5 Working with Images Starting Out with Games & Graphics in.
Adobe InDesign CS5 – Illustrated Unit E: Using Master Pages Adobe InDesign CS5 – Illustrated Unit E: Using Master Pages.
XNA An Introduction. What XNA is… Microsoft® XNA™ is composed of industry- leading software, services, resources, and communities focused on enabling.
11 Making a Sprite Session 4.2. Session Overview  Describe the principle of a game sprite, and see how to create a sprite in an XNA game  Learn more.
XNA Basic Displaying Image & Collision Detect. What’s format image that XNA support? XNA support only.bmp.png and.jpg image..PNG have transparent region.
CHAPTER 14 Classes, Objects, and Games XNA Game Studio 4.0.
CHAPTER 2 The Game Loop - Variables, Types, Classes and Objects in XNA XNA Game Studio 4.0.
© 2011 Delmar, Cengage Learning Chapter 11 Adding Sound and Video.
11 Writing Text Session 5.1. Session Overview  Show how fonts are managed in computers  Discover the difference between bitmap fonts and vector fonts.
Alice: A Free 3D Animation World for Teaching Programming.
Copyright © Texas Education Agency, All rights reserved.1 Web Technologies Motion Graphics & Animation.
XNA ● Proprietary Microsoft framework ● C#. Interface.
11 Using the Keyboard in XNA Session 9.1. Session Overview  Discover more detail on how the XNA keyboard is implemented  Find out how to use arrays.
XNA Tutorial 1 For CS134 Lecture. Overview Some of the hard work has already been done for you. If you build and run your game now, the GraphicsDeviceManager.
CHAPTER 5 Text XNA Game Studio 4.0. Objectives Discover how text is drawn using Microsoft XNA. Add some font resources to your XNA program. Draw some.
Graphical Output Basic Images.
Using Procedures and Exception Handling
MTA-4201 Game Programming Chapter 8 : Scrolling Background & Font
Presentation transcript:

2000 Prentice Hall, Inc. All rights reserved. 1 Outline 3.1Introduction 3.2Game Loop Components 3.3How to Implement in C# 3.4Adding Image to XNA Project 3.5Drawing Sprite on the Screen 3.6Making a Sprite Class 3.7Using the New Sprite Class 3.8Positioning the Sprite 3.9Drawing Multiple Sprites MTA-4201 Game Programming Chapter 3: Concept of Game Loop & Using Resources

2000 Prentice Hall, Inc. All rights reserved Introduction Basically, from programmers perspective, a video game is a continuous loop that performs logic and draws an image on the screen. In that aspect, video game is very similar to how a movie is displayed, except that you are creating the movie as you go.

2000 Prentice Hall, Inc. All rights reserved Game Loop Components Game Loop is consist of eight sections. 1.Initialization 2.Enter Game Loop 3.Retrieve Player Input 4.Perform AI and Game Logic 5.Render Next Frame 6.Synchronize Display 7.Loop 8.Shutdown

2000 Prentice Hall, Inc. All rights reserved. 4 Initialization -Allocate memory -Load files -Build tables Main event loop call windows stuff Init timing Retrieve player input Render next frame to off-screen buffer Main logic -Game AI -Collision detection -Physics Exit to O/S Cleanup -Reallocate -Close files Copy image to display Time sync lock to 30 FPS Handle windows events Off-screen memory Keyboard Mouse Joystick Loop Wait Exit? General Game Loop Architecture

2000 Prentice Hall, Inc. All rights reserved. 5 All game loops pretty much follow this State transition diagram Game Run Game Init Game Menu Game Restart Game Exit Game Starting State transition diagram for a game loop

2000 Prentice Hall, Inc. All rights reserved How to Implement in C# The following is a more detailed version of what a C# game loop might look like in real code.

2000 Prentice Hall, Inc. All rights reserved Adding Image to XNA project The image types you can add that are supported by the Content Pipeline are.bmp,.jpg,.png,.tga,.dds and a few texture file formats. I would recommend to stick with.png files to take advantage of the transparency those files provide. The images you add to your projects may either be single images of objects or spritesheets which contain multiple images and are often used for creating animations or used for tiles. Adding the images to your project makes them available to the XNA framework's Content Importer. This will compile them as ".xnb" files when you build your game.

2000 Prentice Hall, Inc. All rights reserved Adding the image 1)right-click on the Content Folder (the Content Folder is created automatically in your project when you make a new XNA game project) and select "Add -> Existing Item" from the right-click menu. This will open the "Add Existing Item - Content" dialog. 2)Next, navigate to the location of your images. You can select a single image or select multiple images to add to your project at once. Then click the "Add" button. 3)If you look at the project tree, you should see the image or images that you added listed there as well under the "Content" node. 4)Adding the images to the game project does not immediately start drawing them to the screen, you will need to do that later with some code.

2000 Prentice Hall, Inc. All rights reserved Drawing the Sprite on the Screen Create some objects Add this code to the top of the Game1.cs class. Vector2 mPosition = new Vector2(0, 0); Texture2D mSpriteTexture; Vector2 is a object provided by the XNA framework. It is used to store 2D positional information. Texture2D is another object provided by the XNA framework. It is used to hold image content. Both Vector2 and Texture2D objects will be used a lot in 2D game development with the XNA framework so it's good to become very familiar with them.

2000 Prentice Hall, Inc. All rights reserved Drawing the Sprite on the Screen Load the content To load the texture. Well do this by adding some code to the LoadContent method. You don't need to create the "LoadContent" method, it's already created as part of your Game1.cs class when you create your Windows game project. We will just be adding a new line of code to this method to load our sprite texture. protected override void LoadContent() { // Create a new SpriteBatch to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); // TODO: use this.Content to load your game content here mSpriteTexture = this.Content.Load ("SquareGuy"); } The line we added, uses the Content Manager to load the "SquareGuy" image we added to the project into our Texture2D object.

2000 Prentice Hall, Inc. All rights reserved Drawing the Sprite on the Screen Draw the sprite With the Texture2D object loaded, we can now draw our sprite to the screen. All of your drawing should be done in the "Draw" method in your Game1.cs Add the following code to the "Draw" method. protected override void Draw(GameTime gameTime) { graphics.GraphicsDevice.Clear(Color.CornflowerBlue); // background color // TODO: Add your drawing code here spriteBatch.Begin(); spriteBatch.Draw(mSpriteTexture, mPosition, Color.White); spriteBatch.End(); base.Draw(gameTime); } The spriteBatch object is automatically created when you make a new Windows game. SpriteBatch objects are what is used to draw 2D images to the screen. To use them, you first have to initiate "drawing" by calling "Begin". Next, you draw all the images you want. In our case, we're currently just drawing a single sprite to the screen, but some games might have hundreds or thousands of "Draw" calls. With our "Draw" call, we pass in the sprite texture we want drawn. The position in the game screen we want it drawn in (our current position is X=0 and Y=0) and what color we want to shade our sprite (using White, tells the SpriteBatch object that you don't want to apply any shading to the texture). Finally, you tell the SpriteBatch object that you've added all the things to the scene that you want drawn and you tell it to go ahead and draw them all by calling "End".

2000 Prentice Hall, Inc. All rights reserved Drawing the Sprite on the Screen Build your game and give it a go. You should see the Square Guy sitting in the upper left of the game window. Just click the Close button on the game window to stop the game.

2000 Prentice Hall, Inc. All rights reserved Drawing the Sprite on the Screen Changing the Position of the Sprite Change this line of code. Vector2 mPosition = new Vector2(250, 100);

2000 Prentice Hall, Inc. All rights reserved Making a Sprite Class A "Sprite" object makes it easier for us to work with multiple sprites. Start by right-clicking on the project, pick Add -> New Item. Select "Class" from the "Add New Item" dialog and then type in the name "Sprite" into the Name text box. Click the "Add" button when you are finished. When you created the new class, the code window for the class should have opened. We are going to be working with some XNA Framework objects, so we will need to add the following "using" statements to the top of the class file. using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Graphics;

2000 Prentice Hall, Inc. All rights reserved Making a Sprite Class Next, just like we did in the Game1.cs class when we were learning to draw an image to the screen, we are going to add two storage objects to the Sprite class to be used in the same way. Add the following code to the top of the Sprite class class Sprite { //The current position of the Sprite public Vector2 Position = new Vector2(0,0); //The texture object used when drawing the sprite private Texture2D mSpriteTexture; You can see that there is a little difference for how we declared our objects. We made the Position object Public and the Texture2D object private. This means that the Position can be changed by things outside of the class and the Texture can not.

2000 Prentice Hall, Inc. All rights reserved Drawing the Sprite on the Screen We are now going to mimic the Game1.cs class a bit and create our own "LoadContent" method for our sprite class. This method will do for the sprite exactly what the LoadContent method does for the Game1.cs class. Add the following method to the Sprite.cs class. //Load the texture for the sprite using the Content Pipeline public void LoadContent(ContentManager theContentManager, string theAssetName) { mSpriteTexture = theContentManager.Load (theAssetName); } You can see that the LoadContent method for our Sprite class takes in some parameters. The Sprite class does not have a ContentManager to do it's loading so it "asks" for one. It also wants to know the name of the asset that it should load into it's Texture2D object. Both of those values will be passed in when the LoadContent method is called. So now our content is loaded for the sprite, we now need to Draw it. We will once again copy the way the Game1.cs class works and add a "Draw" method to our Sprite class.

2000 Prentice Hall, Inc. All rights reserved Drawing the Sprite on the Screen Add the following method to the Sprite.cs clas //Draw the sprite to the screen public void Draw(SpriteBatch theSpriteBatch) { theSpriteBatch.Draw(mSpriteTexture, Position, Color.White); } We have now created our Sprite class. You should be able to Build the project with no errors now and the Image should still draw BUT it's not being drawn by our new Sprite class.

2000 Prentice Hall, Inc. All rights reserved Using the new Sprite Class We will now modify the Game1.cs class to use the new Sprite object we have created. To begin with, delete the following lines of code from the top of the Game1.cs class. Vector2 mPosition = new Vector2(250, 100); Texture2D mSpriteTexture; Now, add the following line of code to the Game1.cs class. This will be our Sprite object. Sprite mSprite; Next we want to "create" our Sprite object so that it's ready to be used in our Game1.cs class. To do that, add the following line of code to the "Initialize" method. The Initialize method is another one of those methods already created for you when you make a new Windows game. protected override void Initialize() { // TODO: Add your initialization logic here mSprite = new Sprite(); base.Initialize(); }

2000 Prentice Hall, Inc. All rights reserved Drawing the Sprite on the Screen Modify the LoadContent method to look like this. protected override void LoadContent() { // Create a new SpriteBatch to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); // TODO: use this.Content to load your game content here mSprite.LoadContent(this.Content, "SquareGuy"); }

2000 Prentice Hall, Inc. All rights reserved Drawing the Sprite on the Screen Calling that will load the SquareGuy asset into the Texture2D object in our Sprite class. Now we just need to Draw the Sprite to the screen. To do that, modify the Game1.cs Draw method to look like this. protected override void Draw(GameTime gameTime) { graphics.GraphicsDevice.Clear(Color.CornflowerBlue); // TODO: Add your drawing code here spriteBatch.Begin(); mSprite.Draw(this.spriteBatch); spriteBatch.End(); base.Draw(gameTime); }

2000 Prentice Hall, Inc. All rights reserved Positioning the Sprite Object This changes the Position of the sprite so that when the Sprite draws itself, it uses the new positioning information you gave it. You can also change just an individual X and or Y position of the Sprite by writing code like this. mSprite.Position.X = 200; mSprite.Position.Y = 300; Just another way of accessing and changing the positioning information of your sprite. With the new positioning information you have given the Sprite, go ahead and Build now and watch where the Sprite is drawn.

2000 Prentice Hall, Inc. All rights reserved Drawing Multiple Sprites Add a new Sprite object to the top of the class, maybe call this one mSpriteTwo. Sprite mSpriteTwo; Instantiate a new instance of the Sprite in the Initialize method of the Game1.cs class. mSpriteTwo = new Sprite(); Call the LoadContent method of your new sprite object in the LoadContent method of the Game1.cs class. mSpriteTwo.LoadContent(this.Content, "SquareGuy"); Give the new sprite a new position (if you give it the same position, you won't see it since they will be drawn on top of each other). mSpriteTwo.Position = new Vector2( 300, 100); Call the Draw method of your sprite object in the Draw method of the Game1.cs class. mSpriteTwo.Draw(this.spriteBatch); Now go ahead and Build your game, you should now see two sprites drawn to the screen in the locations you have given them. Do you think you could add a third?

2000 Prentice Hall, Inc. All rights reserved Drawing Multiple Sprites