Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sprites, User Input, and Collision COSC 315 Fall 2014 Bridget M. Blodgett.

Similar presentations

Presentation on theme: "Sprites, User Input, and Collision COSC 315 Fall 2014 Bridget M. Blodgett."— Presentation transcript:

1 Sprites, User Input, and Collision COSC 315 Fall 2014 Bridget M. Blodgett

2 Basic XNA Game Open XNA and make a new project – You can leave the default name Open and examine program.cs and Game1.cs (or YourGamesName.cs) – What does it look like each of these methods does? What are GraphicsDeviceManager and SpriteBatch and why are they important?

3 Game Loops The logic of programming a game relies upon a game loop – This loop usually consists of a couple of methods that actually control that play of the game The key methods are Update() and Draw() – From the names what do you think goes in these two methods?

4 Registering vs Polling Most programs wait for user input to perform any actions – They can be said to register user changes and respond Games often have to keep running regardless of whether the user is interacting – This is called polling the input devices for changes – It is one reason that most games use a game loop to run

5 Game State The game state is the log of what is currently happening in the game Games usually have several basic states that correspond to the major play phases – There can be minor states that address play related states as well The game state is tracked and changed in Update() – The impacts and visible outcome are then shown using Draw()

6 Initialize()LoadContent() Update() Draw() UnloadContent()

7 Game Time Draw() accepts one parameter: gameTime – gameTime keeps track of how long has passed since the game was started – This is important because the only other method of timing is based upon the processor speed Try changing the background color to something other than blue

8 Adding Sprites XNA will allow you to use several graphics formats for images in your game – You load them into the content pipeline which then converts them to a format that is usable by the program Looking at the solution explorer you should be able to add the existing images from the source code to your project In Game1.cs write Texture2D texture; –texture = Content.Load (@”Images/logo”);

9 Drawing the Sprite The texture has been loaded into the program To draw it on the screen type: spriteBatch.Begin(); spriteBatch.Draw(texture,Vector2.Zero, Color.White); spriteBatch.End(); If you wanted to center the image you can use: –spriteBatch.Draw(texture, new Vector2(Window.ClientBounds.Width/2) – (texture.Width/2), (Window.ClientBounds.Height/2) – (texture.Height/2)), Color.White);

10 Additional Options When drawing more than one sprite it is optimal to draw as many as possible within one spriteBatch call – To make the background transparent you can turn it pure magenta (auto transparent in XNA) or use a format like PNG that handles transparency You can also flip and scale the sprites, set their order or rotate them around an axis point

11 Layer Depth Typically the sprites will layer on top of one another in the order that they are drawn on the screen Setting the layer depth property to 0 will put them on top and 1 will put them behind Changing this requires a change to spriteBatch.Begin(SpriteSortMode.FrontToBa ck, BlendState.AlphaBlend);

12 Moving Sprites Make two Vector2 variables named pos1 and pos2 and set both to Vector2.Zero ; Make two float variables named speed1 and speed2 and set them to 2f and 3f respectively. To make the movement happen you need to change the values of pos 1 and 2 in Update(). pos1.X += speed1; if(pos1.X > Window.ClientBounds.Width – texture.Width || pos1.X < 0) speed1 *= -1; pos2.Y += speed2; if(pos2.Y > Window.ClientBounds.Height – texture.Height || pos2.Y < 0) speed2 *= -1;

13 Sprite Sheets Load in the animated sprites three rings sheet like we did with the XNA logos To move between the different sprites to show motion you need to set up some basic information at the class level Point frameSize = new Point(75,75); Point currentFrame = new Point(0,0); Point sheetSize = new Point(6,8); In Draw add: spriteBatch.Draw(texture,Vector2.Zero, new Rectangle(currentFrame.X*frameSize.X, currentFrame.Y * frameSize.Y, frameSize.X, frameSize.Y), Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0);

14 Sprite Sheets Set the background color to white in the Draw method We need to get the sprite to animate through the different images In Update() before base.Update ++currentFrame.X; if(currentFrame.X >= sheetSize.X){ currentFrame.X=0; ++currentFrame.Y; if (currentFrame.Y >= sheetSize.Y){ currentFrame.Y = 0; }

15 Timing Animations By default the animation speed is equal to the framerate To change the framerate add the following code at the end of the Game1 constructor –TargetElapsedTime = new TimeSpan(0,0,0,0,50); To change the animation speed remove the previous line of code and make two new variables: int timeSinceLastFrame=0; and int millisecondsPerFrame = 50;

16 Timing Animations Now alter the code in Update to make use of these on the frame progression statements: timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds; if(timeSinceLastFrame > millisecondsPerFrame){ timeSinceLastFrame -= millisecondsPerFrame; //continue with existing code

Download ppt "Sprites, User Input, and Collision COSC 315 Fall 2014 Bridget M. Blodgett."

Similar presentations

Ads by Google