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

Slides:



Advertisements
Similar presentations
Games in Python – the easy way
Advertisements

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.
Constructor and New Fields // Don't synch draw() with vertical retrace of monitor graphics.SynchronizeWithVerticalRetrace = false; IsFixedTimeStep = true;
Creating Games For Windows, Xbox 360, and Windows Phone 7 Ryan Plemons
Dan Waters, Academic Relations Manager, Microsoft.
3.1. G RAPHICS I The use of images within games. Reflections and advice on the games proposed in the Week 2 Hand-in.
2D Graphics in XNA Game Studio Express (Modeling a Class in UML) Game Design Experience Professor Jim Whitehead February 5, 2008 Creative Commons Attribution.
Create slices and hotspots Create links in Web pages Create rollovers from slices Create basic animation Add tweening symbol instances to create animation.
Sketchify Tutorial Graphics and Animation in Sketchify sketchify.sf.net Željko Obrenović
LO: Learn how to develop your game further to include interactions with the device.
2D Physics and Camera Systems For CSE 3902 By: Matt Boggus.
© 2011 Delmar, Cengage Learning Chapter 8 Building Complex Animations.
2 What is pyGame? A set of Python modules to make it easier to write games. –home page: –documentation:
Pygame Dick Steflik.
Guide to Programming with Python
I can haz gamez?. Bret Stateham Microsoft Developer Evangelist Blog:
Scratch the Cat. Object Oriented Programing Writing computer programs Based on Objects Instead of Actions Based on Data Instead of Logic.
Finishing 2D COSC 315 Fall 2014 Bridget M. Blodgett.
Computer Science – Game DesignUC Santa Cruz CMPS 20: Game Design Experience January 14, 2010 Arnav Jhala.
AI & 2D Development COSC 315 Fall 2014 Bridget M. Blodgett.
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.
Locally Edited Animations We will need 3 files to help get us started at
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.
User Input and Collisions COSC 315 Fall 2014 Bridget M. Blodgett.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 7 The Game Loop and Animation Starting Out with Games & Graphics.
Introduction to XNA Graphics Programming Asst. Prof. Rujchai Ung-arunyawee COE, KKU.
3.2. G RAPHICS I Alpha blending within games. An exploration of the use of alpha blending within games.
CHAPTER 10 Using C# Methods to Solve Problem XNA Game Studio 4.0.
11 Working with Images Session Session Overview  Find out more about image manipulation and scaling when drawing using XNA  Start to implement.
11 Adding Tomato Targets Session Session Overview  We now have a game which lets a player bounce a piece of cheese on a bread bat  Now we have.
XNA An Introduction. What XNA is… Microsoft® XNA™ is composed of industry- leading software, services, resources, and communities focused on enabling.
Object Oriented Design COSC 315 Fall 2014 Bridget M. Blodgett.
XNA Game Studio 4.0 Keyboard and Mouse Controls + more on Animated Sprites.
MAEK GAEM III: SDL ● Simple DirectMedia Layer ● Input/Graphics/Audio using OpenGL ● Not nearly as difficult as OpenGL ● Cross Platform and Open Sauce ●
Programming Video Games
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.
Game Maker Terminology
2.1. T HE G AME L OOP Central game update and render processes.
Sprites, User Input, and Collision COSC 315 Fall 2014 Bridget M. Blodgett.
Reference: The Game Loop Animation / Game loop 1. Update variables 2. [Get input from the user] (GameLoop only) 3. Draw (using variables)
Guide to Programming with Python Chapter Twelve Sound, Animation, and Program Development: The Astrocrash Game.
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.
Computer Science I Recap: variables & functions. Images. Pseudo-random processing.
Tutorial 7 Creating Animations. XP Objectives Learn about animation Create a timeline Add AP divs and graphics to a timeline Move and resize animation.
11 Writing Text Session 5.1. Session Overview  Show how fonts are managed in computers  Discover the difference between bitmap fonts and vector fonts.
Playing with Sprites. XNA Game Lifecycle In the faceBall demo program we bounced a smiley face around the graphical display against a background image.
XNA ● Proprietary Microsoft framework ● C#. Interface.
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.
PyGame - Unit 2 PyGame Unit – – Animation.
Enhancing JavaScript Using Application Programming Interfaces (APIs), Lecture #3.
The Stingray Example Program CMT3311. Stingray - an example 2D game May be useful as a simple case study Most 2D games need to solve generic problems.
2D Graphics CMT3311. This covers... How to make a transparent sprite How to add a sprite to your project and draw it Properties of sprites and how to.
Game Maker Tutorials Introduction Clickball IntroductionClickball Where is it? Shooting Where is it?Shooting.
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.
Sprites (Images) and Sounds
Sound and more Animations
Reading and Writing Image Files
2D Physics and Camera Systems
Graphical Output Basic Images.
Animation Frame Animation.
MOM! Phineas and Ferb are … Aims:
Pixels, Colors and Shapes
Using and Creating Sprites
2D Graphics and Animations in Unity 3D
Introduction to Object-Oriented Programming
Game Loop Update & Draw.
Chapter 7 The Game Loop and Animation
Animation Translation.
Presentation transcript:

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

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?

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?

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

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()

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

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

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

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);

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

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);

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;

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);

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; }

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;

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