SlideShare a Scribd company logo
Windows Phone 7 XNA A different kind of phone, designed for a life in motion
XNA OVerviewA frameworktocreategameapplications
XNA XNA is a framework for writing gamesIt includes a set of professional tools for game production and resource managementWindows Phone uses version 4.0 of the frameworkThis is included as part of the Windows Phone SDK
2D and 3D gamesXNA provides full support for 3D gamesIt allows a game program to make full use of the underlying hardware acceleration provided by the graphics hardwareFor the purpose of this course we are going to focus on 2D gamingWindows Phone games be either 2D or 3D4
XNA and SilverlightXNA is completely different from SilverlightThe way that programs are constructed and execute in XNA is quite differentXNA has been optimised for game creationIt does not have any elements for user interface or data bindingInstead it has support for 3D rendering and game content management5
XNA and ProgramsXNA provides a set of classes which allow you to create gameplayThe classes represent game information and XNA resourcesXNA  is also a very good example of how you construct and deploy a set of software resources in a Framework
Creating a GameThe Windows Phone SDK provides a Windows Phone game project type
The Game ProjectThe solution explorer shows the items that make up our game projectThe solution will also contain any content that we add to the game project
Empty Game DisplayAt the moment all our game does is display a blue screenThis is because the behaviour of the Draw method in a brand new project is to clear the screen to blue
Demo 1: New GameDemo10
What a Game Does When it RunsInitialise all the resources at the startfetch all textures, models, scripts etcRepeatedly run the game:Update the game worldread the user input, update the state and position of game elementsDraw the game worldrender the game elements on the viewing device
XNA Game Class MethodspartialclassPongGame : Microsoft.Xna.Framework.Game{protectedoverridevoidLoadContent                              (boolloadAllContent)    {    }protectedoverridevoid Update(GameTimegameTime)    {    }protectedoverridevoid Draw(GameTimegameTime)    {    }}
XNA Methods and gamesNote that our program never calls the LoadContent, Draw and Update methodsThey are called by the XNA Framework LoadContent is called when the game startsDraw is called as often as possibleUpdate is called 30 times a secondNote that this is not the same as an XNA game on Windows PC or Xbox, where Update is called 60 times a second13
Loading Game ContentprotectedoverridevoidLoadContent(){// Create a new SpriteBatch, which can be used to // draw textures.spriteBatch = newSpriteBatch(GraphicsDevice);}LoadContent is called when our game startsIt is where we put the code that loads the content into our gameContent includes images, sounds, models etc.
Game ContentGames are not just programs, they also contain other content:Images for textures and backgroundsSound Effects3D Object MeshesWe need to add this content to our projectThe XNA framework provides a content management system which is integrated into Visual Studio
Image ResourcesThis is a Ball imageI have saved it as a PNG fileThis allows the image to use transparencyYou can use any image resource you like The resources are added to the Visual Studio projectThey are held in the Content directory as part of your project
Using the Content PipelineEach resource is given an asset name The Load method of Content Manager provides access to the resource using the Asset Name that we gave itballTexture = Content.Load<Texture2D>("WhiteDot");
Storing the Ball Texture in the game// Game WorldTexture2D ballTexture;XNA provides a Texture2Dtype which holds a 2D (flat) texture to be drawn on the displayThe game class needs to contain a member variable to hold the ball texture that is to be drawn when the game runsThis variable will be shared by all the methods in the game
Loading Content into the Ball TextureprotectedoverridevoidLoadContent(){// Create a new SpriteBatch, which can be used to     // draw textures.spriteBatch = newSpriteBatch(GraphicsDevice);ballTexture = Content.Load<Texture2D>("WhiteDot");}LoadContentis called when a game startsIt loads an image into the ball textureThe content manager fetches the images which are automatically sent to the target device
Making a “sprite”A sprite is an object on the screen of a gameIt has both a texture and a position on the screen We are creating a sprite object for the ball in our gameWe now have the texture for the objectThe next thing to do is position it on the screen20
Coordinates and PixelsWhen drawing in XNA the position of an object on the screen is given using coordinates based on pixelsA standard Windows Phone screen is 800 pixels wide and 480 pixels highThis gives the range of possible values for display coordinatesIf you draw things off the screen this does not cause XNA problems, but nothing is drawn21
X and Y in XNAThe coordinate system used by XNA sprite drawing puts the origin (0,0) in the top left hand corner of the screenIncreasing X moves an object across the screen towards the rightIncreasing Y moves an object down the screen towards the bottomIt is important that you remember this22
Positioning the Ball using a Rectangle// Game WorldTexture2DballTexture;RectangleballRectangle;We can add a rectangle value to the game to manage the position of the ball on the screenWe will initialise it in the LoadContent method
The Rectangle structureRectangle ballRectangle = newRectangle(    0, 0,ballTexture.Width, ballTexture.Height),Color.White);Rectangle is a struct type which contains a position and a sizeThe code above creates a rectangle positioned at 0,0 (top left hand corner) the same size as ballTextureWe could move our ball by changing the content of the rectangle
Drawing a SpriteIn game programming terms a “sprite” is a texture that can be positioned on the screenWe now have a ball spriteWe have the texture that contains an image of the ballWe have a rectangle that gives the position and size of the sprite itself25
XNA Game Draw Methodprotectedoverridevoid Draw(GameTimegameTime){GraphicsDevice.Clear(Color.CornflowerBlue);base.Draw(gameTime);}The Draw method is called repeatedly when an XNA game is runningIt has the job of drawing the display on the screenA brand new XNA game project  contains a Draw method that clears the screen to CornflowerBlueWe must add our own code to the method to draw the ball
Sprite Batching2D Graphics drawing is handled by a set of “sprite” drawing methods provided by XNAThese create commands that are passed to the graphics deviceThe graphics device will not want to draw everything on a piecemeal basisIdeally all the drawing information, textures and transformations should be provided as a single itemThe SpriteBatch class looks after this for us
SpriteBatch Begin and Endprotectedoverridevoid Draw(GameTimegameTime){GraphicsDevice.Clear(Color.CornflowerBlue);spriteBatch.Begin();    // Code that uses spriteBatch to draw the displayspriteBatch.End();base.Draw(gameTime);}The call to the Begin method tells SpriteBatch to begin a assembling a new set of drawing operationsThe call to the End method tells SpriteBatch that the there are no more operations and causes the rendering to take place
Using SpriteBatch.DrawspriteBatch.Draw(ballTexture, ballRectangle, Color.White);The SpriteBatch class provides a Draw method to do the sprite drawingIt is given parameters to tell it what to do:Texture to drawPosition (expressed as a Rectangle)Draw color
Rectangle Funnew Rectangle(  0, 0,ballTexture.Width, ballTexture.Height)new Rectangle(  0, 0,      // position  200,100)   // sizenew Rectangle(  50, 50,     // position  60, 60)     // size
Demo 2: Dot SizesDemo31
Screen Size and ScalingWe need to make our game images fit the size of the screenWe can find out the size of the screen from the GraphicsDevice used to draw itGraphicsDevice.Viewport.WidthGraphicsDevice.Viewport.HeightWe can use this information to scale the images on the screen
Creating the ballRectangle variableballRectangle = newRectangle(    0, 0,GraphicsDevice.Viewport.Width / 20,GraphicsDevice.Viewport.Width/ 20);If we use this rectangle to draw our ball texture it will be drawn as a square which is a twentieth of the width of the screenWe can then set the position parts of the rectangle to move the ball around the screen
Moving the ball around the screenAt the moment the ball is drawn in the same position each time Draw runsTo move the ball around we need to make this position change over timeWe need to give the game an update behaviourWe must add code to the Update method in the gameThe Update method is where we manage the state of the “game world”
The Update Methodprotectedoverridevoid Update(GameTimegameTime){// TODO: Add your update logic herebase.Update(gameTime);}The Update method is automatically called 30 times a second when a game is runningIt is in charge of managing the “game world”In a pong game this means updating the bat and the ball positions and checking for collisions
Simple Update Methodprotectedoverridevoid Update(GameTimegameTime){ballRectangle.X++;ballRectangle.Y++;base.Update(gameTime);}Each time the game updates the X and Y position of the ball rectangle is increasedThis will cause it to move across and down the screenNote that I call the base method to allow my parent object to update too
GameTimeAt the moment the Update method is called sixty time a second or once every 16.66 millisecondsWe can also let the update "free run", in which case we need to know the time since the last call so we can move objects the right distanceThis is what the GameTime parameter is for, it gives the time at which the method was called
Demo 2: Moving DotDemo38
SummaryXNA is a Framework of methods that are used to write gamesYou create the games using Visual Studio Games have initialise, update and draw behavioursGame objects are held as textures objects and their position and dimensions as rectangle structures

More Related Content

PPTX
WP7 HUB_XNA overview
DOC
XNA coding series
PDF
The Ring programming language version 1.7 book - Part 53 of 196
PDF
The Ring programming language version 1.5.4 book - Part 48 of 185
DOCX
2d game engine workflow
PDF
The Ring programming language version 1.5.1 book - Part 47 of 180
PDF
The Ring programming language version 1.5.3 book - Part 48 of 184
PDF
Make a match3
WP7 HUB_XNA overview
XNA coding series
The Ring programming language version 1.7 book - Part 53 of 196
The Ring programming language version 1.5.4 book - Part 48 of 185
2d game engine workflow
The Ring programming language version 1.5.1 book - Part 47 of 180
The Ring programming language version 1.5.3 book - Part 48 of 184
Make a match3

What's hot (20)

PDF
Introduction to Game Programming: Using C# and Unity 3D - Chapter 3 (Preview)
PDF
Introduction to Game Programming: Using C# and Unity 3D - Chapter 6 (Preview)
PDF
The Ring programming language version 1.9 book - Part 80 of 210
PDF
Introduction to Game Programming: Using C# and Unity 3D - Chapter 7 (Preview)
PDF
The Ring programming language version 1.2 book - Part 50 of 84
PDF
Introduction to Game Programming: Using C# and Unity 3D - Chapter 2 (Preview)
PDF
The Ring programming language version 1.5.2 book - Part 49 of 181
PDF
Pygame presentation
PDF
Game Programming I - GD4N
PDF
The Ring programming language version 1.6 book - Part 52 of 189
PDF
The Ring programming language version 1.3 book - Part 38 of 88
PDF
The Ring programming language version 1.5.1 book - Part 48 of 180
PDF
The Ring programming language version 1.7 book - Part 74 of 196
DOCX
3d game engine
PDF
Lecture 2: C# Programming for VR application in Unity
PDF
The Ring programming language version 1.8 book - Part 56 of 202
PDF
The Ring programming language version 1.5.2 book - Part 48 of 181
PPTX
Unity - Building Your First Real-Time 3D Project - All Slides
PDF
The Ring programming language version 1.4.1 book - Part 19 of 31
PDF
libGDX: User Input and Frame by Frame Animation
Introduction to Game Programming: Using C# and Unity 3D - Chapter 3 (Preview)
Introduction to Game Programming: Using C# and Unity 3D - Chapter 6 (Preview)
The Ring programming language version 1.9 book - Part 80 of 210
Introduction to Game Programming: Using C# and Unity 3D - Chapter 7 (Preview)
The Ring programming language version 1.2 book - Part 50 of 84
Introduction to Game Programming: Using C# and Unity 3D - Chapter 2 (Preview)
The Ring programming language version 1.5.2 book - Part 49 of 181
Pygame presentation
Game Programming I - GD4N
The Ring programming language version 1.6 book - Part 52 of 189
The Ring programming language version 1.3 book - Part 38 of 88
The Ring programming language version 1.5.1 book - Part 48 of 180
The Ring programming language version 1.7 book - Part 74 of 196
3d game engine
Lecture 2: C# Programming for VR application in Unity
The Ring programming language version 1.8 book - Part 56 of 202
The Ring programming language version 1.5.2 book - Part 48 of 181
Unity - Building Your First Real-Time 3D Project - All Slides
The Ring programming language version 1.4.1 book - Part 19 of 31
libGDX: User Input and Frame by Frame Animation
Ad

Viewers also liked (6)

PDF
Adobe Illustrator Tutorials: Digital Photography Flyer
DOCX
Multimedia system 3 rd sem 2012aug.ASSIGNMENT
PDF
PDF
DOCX
Java 3 rd sem. 2012 aug.ASSIGNMENT
PDF
Adobe Illustrator Tutorials: Digital Photography Flyer
Multimedia system 3 rd sem 2012aug.ASSIGNMENT
Java 3 rd sem. 2012 aug.ASSIGNMENT
Ad

Similar to WP7 HUB_XNA (20)

PDF
The Ring programming language version 1.2 book - Part 36 of 84
PPTX
Galactic Wars XNA Game
PDF
The Ring programming language version 1.4 book - Part 14 of 30
PDF
School For Games 2015 - Unity Engine Basics
PPTX
Unity workshop
PDF
The Ring programming language version 1.5.3 book - Part 58 of 184
PDF
The Ring programming language version 1.8 book - Part 55 of 202
PPTX
XNA And Silverlight
PDF
Useful Tools for Making Video Games - XNA (2008)
PDF
Presentación Unity
PPTX
98 374 Lesson 05-slides
PDF
Game Programming I - Introduction
PDF
Unity 101
PPTX
Silverlight as a Gaming Platform
PPTX
Game Development Session - 3 | Introduction to Unity
PPTX
Beginning Game Development in XNA
PPTX
Beginning Game Development in XNA
PDF
Monogame and xna
PDF
The Ring programming language version 1.6 book - Part 51 of 189
PPTX
Vanmathy python
The Ring programming language version 1.2 book - Part 36 of 84
Galactic Wars XNA Game
The Ring programming language version 1.4 book - Part 14 of 30
School For Games 2015 - Unity Engine Basics
Unity workshop
The Ring programming language version 1.5.3 book - Part 58 of 184
The Ring programming language version 1.8 book - Part 55 of 202
XNA And Silverlight
Useful Tools for Making Video Games - XNA (2008)
Presentación Unity
98 374 Lesson 05-slides
Game Programming I - Introduction
Unity 101
Silverlight as a Gaming Platform
Game Development Session - 3 | Introduction to Unity
Beginning Game Development in XNA
Beginning Game Development in XNA
Monogame and xna
The Ring programming language version 1.6 book - Part 51 of 189
Vanmathy python

More from MICTT Palma (20)

PPTX
Active directory ds ws2008 r2
PPTX
Office 365
PPTX
Ad ds ws2008 r2
PPTX
Sharepoint 2010. Novedades y Mejoras.
PPTX
¿Qué es la nube?
PPTX
Introduction to wcf solutions
PPTX
Introducción a web matrix
PPTX
Ie9 + html5
PPTX
WP7 HUB_Consuming Data Services
PPTX
WP7 HUB_Introducción a Visual Studio
PPTX
WP7 HUB_Creando aplicaciones de Windows Phone
PPTX
WP7 HUB_Diseño del interfaz con Silverlight
PPTX
WP7 HUB_Platform overview
PPTX
WP7 HUB_Introducción a Silverlight
PPTX
WP7 HUB_Overview and application platform
PPTX
WP7 HUB_Marketplace
PPTX
WP7 HUB_Launch event WP7
PPTX
WP7 HUB_Launch event Windows Azure
PPTX
WP7 HUB_Launch event introduction
PPTX
Presentacion share point 2010
Active directory ds ws2008 r2
Office 365
Ad ds ws2008 r2
Sharepoint 2010. Novedades y Mejoras.
¿Qué es la nube?
Introduction to wcf solutions
Introducción a web matrix
Ie9 + html5
WP7 HUB_Consuming Data Services
WP7 HUB_Introducción a Visual Studio
WP7 HUB_Creando aplicaciones de Windows Phone
WP7 HUB_Diseño del interfaz con Silverlight
WP7 HUB_Platform overview
WP7 HUB_Introducción a Silverlight
WP7 HUB_Overview and application platform
WP7 HUB_Marketplace
WP7 HUB_Launch event WP7
WP7 HUB_Launch event Windows Azure
WP7 HUB_Launch event introduction
Presentacion share point 2010

Recently uploaded (20)

PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Yogi Goddess Pres Conference Studio Updates
PDF
Weekly quiz Compilation Jan -July 25.pdf
PDF
A systematic review of self-coping strategies used by university students to ...
PPTX
master seminar digital applications in india
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Computing-Curriculum for Schools in Ghana
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Yogi Goddess Pres Conference Studio Updates
Weekly quiz Compilation Jan -July 25.pdf
A systematic review of self-coping strategies used by university students to ...
master seminar digital applications in india
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
2.FourierTransform-ShortQuestionswithAnswers.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
VCE English Exam - Section C Student Revision Booklet
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
Final Presentation General Medicine 03-08-2024.pptx
Microbial diseases, their pathogenesis and prophylaxis
Abdominal Access Techniques with Prof. Dr. R K Mishra
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
O5-L3 Freight Transport Ops (International) V1.pdf
Computing-Curriculum for Schools in Ghana
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
202450812 BayCHI UCSC-SV 20250812 v17.pptx

WP7 HUB_XNA

  • 1. Windows Phone 7 XNA A different kind of phone, designed for a life in motion
  • 3. XNA XNA is a framework for writing gamesIt includes a set of professional tools for game production and resource managementWindows Phone uses version 4.0 of the frameworkThis is included as part of the Windows Phone SDK
  • 4. 2D and 3D gamesXNA provides full support for 3D gamesIt allows a game program to make full use of the underlying hardware acceleration provided by the graphics hardwareFor the purpose of this course we are going to focus on 2D gamingWindows Phone games be either 2D or 3D4
  • 5. XNA and SilverlightXNA is completely different from SilverlightThe way that programs are constructed and execute in XNA is quite differentXNA has been optimised for game creationIt does not have any elements for user interface or data bindingInstead it has support for 3D rendering and game content management5
  • 6. XNA and ProgramsXNA provides a set of classes which allow you to create gameplayThe classes represent game information and XNA resourcesXNA is also a very good example of how you construct and deploy a set of software resources in a Framework
  • 7. Creating a GameThe Windows Phone SDK provides a Windows Phone game project type
  • 8. The Game ProjectThe solution explorer shows the items that make up our game projectThe solution will also contain any content that we add to the game project
  • 9. Empty Game DisplayAt the moment all our game does is display a blue screenThis is because the behaviour of the Draw method in a brand new project is to clear the screen to blue
  • 10. Demo 1: New GameDemo10
  • 11. What a Game Does When it RunsInitialise all the resources at the startfetch all textures, models, scripts etcRepeatedly run the game:Update the game worldread the user input, update the state and position of game elementsDraw the game worldrender the game elements on the viewing device
  • 12. XNA Game Class MethodspartialclassPongGame : Microsoft.Xna.Framework.Game{protectedoverridevoidLoadContent (boolloadAllContent) { }protectedoverridevoid Update(GameTimegameTime) { }protectedoverridevoid Draw(GameTimegameTime) { }}
  • 13. XNA Methods and gamesNote that our program never calls the LoadContent, Draw and Update methodsThey are called by the XNA Framework LoadContent is called when the game startsDraw is called as often as possibleUpdate is called 30 times a secondNote that this is not the same as an XNA game on Windows PC or Xbox, where Update is called 60 times a second13
  • 14. Loading Game ContentprotectedoverridevoidLoadContent(){// Create a new SpriteBatch, which can be used to // draw textures.spriteBatch = newSpriteBatch(GraphicsDevice);}LoadContent is called when our game startsIt is where we put the code that loads the content into our gameContent includes images, sounds, models etc.
  • 15. Game ContentGames are not just programs, they also contain other content:Images for textures and backgroundsSound Effects3D Object MeshesWe need to add this content to our projectThe XNA framework provides a content management system which is integrated into Visual Studio
  • 16. Image ResourcesThis is a Ball imageI have saved it as a PNG fileThis allows the image to use transparencyYou can use any image resource you like The resources are added to the Visual Studio projectThey are held in the Content directory as part of your project
  • 17. Using the Content PipelineEach resource is given an asset name The Load method of Content Manager provides access to the resource using the Asset Name that we gave itballTexture = Content.Load<Texture2D>("WhiteDot");
  • 18. Storing the Ball Texture in the game// Game WorldTexture2D ballTexture;XNA provides a Texture2Dtype which holds a 2D (flat) texture to be drawn on the displayThe game class needs to contain a member variable to hold the ball texture that is to be drawn when the game runsThis variable will be shared by all the methods in the game
  • 19. Loading Content into the Ball TextureprotectedoverridevoidLoadContent(){// Create a new SpriteBatch, which can be used to // draw textures.spriteBatch = newSpriteBatch(GraphicsDevice);ballTexture = Content.Load<Texture2D>("WhiteDot");}LoadContentis called when a game startsIt loads an image into the ball textureThe content manager fetches the images which are automatically sent to the target device
  • 20. Making a “sprite”A sprite is an object on the screen of a gameIt has both a texture and a position on the screen We are creating a sprite object for the ball in our gameWe now have the texture for the objectThe next thing to do is position it on the screen20
  • 21. Coordinates and PixelsWhen drawing in XNA the position of an object on the screen is given using coordinates based on pixelsA standard Windows Phone screen is 800 pixels wide and 480 pixels highThis gives the range of possible values for display coordinatesIf you draw things off the screen this does not cause XNA problems, but nothing is drawn21
  • 22. X and Y in XNAThe coordinate system used by XNA sprite drawing puts the origin (0,0) in the top left hand corner of the screenIncreasing X moves an object across the screen towards the rightIncreasing Y moves an object down the screen towards the bottomIt is important that you remember this22
  • 23. Positioning the Ball using a Rectangle// Game WorldTexture2DballTexture;RectangleballRectangle;We can add a rectangle value to the game to manage the position of the ball on the screenWe will initialise it in the LoadContent method
  • 24. The Rectangle structureRectangle ballRectangle = newRectangle( 0, 0,ballTexture.Width, ballTexture.Height),Color.White);Rectangle is a struct type which contains a position and a sizeThe code above creates a rectangle positioned at 0,0 (top left hand corner) the same size as ballTextureWe could move our ball by changing the content of the rectangle
  • 25. Drawing a SpriteIn game programming terms a “sprite” is a texture that can be positioned on the screenWe now have a ball spriteWe have the texture that contains an image of the ballWe have a rectangle that gives the position and size of the sprite itself25
  • 26. XNA Game Draw Methodprotectedoverridevoid Draw(GameTimegameTime){GraphicsDevice.Clear(Color.CornflowerBlue);base.Draw(gameTime);}The Draw method is called repeatedly when an XNA game is runningIt has the job of drawing the display on the screenA brand new XNA game project contains a Draw method that clears the screen to CornflowerBlueWe must add our own code to the method to draw the ball
  • 27. Sprite Batching2D Graphics drawing is handled by a set of “sprite” drawing methods provided by XNAThese create commands that are passed to the graphics deviceThe graphics device will not want to draw everything on a piecemeal basisIdeally all the drawing information, textures and transformations should be provided as a single itemThe SpriteBatch class looks after this for us
  • 28. SpriteBatch Begin and Endprotectedoverridevoid Draw(GameTimegameTime){GraphicsDevice.Clear(Color.CornflowerBlue);spriteBatch.Begin(); // Code that uses spriteBatch to draw the displayspriteBatch.End();base.Draw(gameTime);}The call to the Begin method tells SpriteBatch to begin a assembling a new set of drawing operationsThe call to the End method tells SpriteBatch that the there are no more operations and causes the rendering to take place
  • 29. Using SpriteBatch.DrawspriteBatch.Draw(ballTexture, ballRectangle, Color.White);The SpriteBatch class provides a Draw method to do the sprite drawingIt is given parameters to tell it what to do:Texture to drawPosition (expressed as a Rectangle)Draw color
  • 30. Rectangle Funnew Rectangle( 0, 0,ballTexture.Width, ballTexture.Height)new Rectangle( 0, 0, // position 200,100) // sizenew Rectangle( 50, 50, // position 60, 60) // size
  • 31. Demo 2: Dot SizesDemo31
  • 32. Screen Size and ScalingWe need to make our game images fit the size of the screenWe can find out the size of the screen from the GraphicsDevice used to draw itGraphicsDevice.Viewport.WidthGraphicsDevice.Viewport.HeightWe can use this information to scale the images on the screen
  • 33. Creating the ballRectangle variableballRectangle = newRectangle( 0, 0,GraphicsDevice.Viewport.Width / 20,GraphicsDevice.Viewport.Width/ 20);If we use this rectangle to draw our ball texture it will be drawn as a square which is a twentieth of the width of the screenWe can then set the position parts of the rectangle to move the ball around the screen
  • 34. Moving the ball around the screenAt the moment the ball is drawn in the same position each time Draw runsTo move the ball around we need to make this position change over timeWe need to give the game an update behaviourWe must add code to the Update method in the gameThe Update method is where we manage the state of the “game world”
  • 35. The Update Methodprotectedoverridevoid Update(GameTimegameTime){// TODO: Add your update logic herebase.Update(gameTime);}The Update method is automatically called 30 times a second when a game is runningIt is in charge of managing the “game world”In a pong game this means updating the bat and the ball positions and checking for collisions
  • 36. Simple Update Methodprotectedoverridevoid Update(GameTimegameTime){ballRectangle.X++;ballRectangle.Y++;base.Update(gameTime);}Each time the game updates the X and Y position of the ball rectangle is increasedThis will cause it to move across and down the screenNote that I call the base method to allow my parent object to update too
  • 37. GameTimeAt the moment the Update method is called sixty time a second or once every 16.66 millisecondsWe can also let the update "free run", in which case we need to know the time since the last call so we can move objects the right distanceThis is what the GameTime parameter is for, it gives the time at which the method was called
  • 38. Demo 2: Moving DotDemo38
  • 39. SummaryXNA is a Framework of methods that are used to write gamesYou create the games using Visual Studio Games have initialise, update and draw behavioursGame objects are held as textures objects and their position and dimensions as rectangle structures

Editor's Notes

  • #3: Start Visual StudioSelect File&gt;New&gt;Project to open the New Project dialogSelect XNA Game Studio 4 from the list of available templates on the leftSelect Windows Phone Game (4.0) from the template selection.Change the name to PongGame.Click OK.The project will now be created.Click Run to run the project. The emulator will start and display the blue screen.Click Stop to stop the program.Close Visual Studio and return to the presentation.
  • #19: Open the PongGame project in Demo 2 Dot Sizes.Run the program.Rotate the emulator display so that it is landscape with the controls on the right. Explain that this is the default orientation for Windows Phone games.Open the file Game1.cs and navigate to the Draw method.Change the draw position and size of the dot in response to suggestions from the class.Try to draw the dot off the screen. Ask what will happen.Answer: XNA will just clip the display. This will work fineTry to draw a really big dot. Ask what will happen.Answer: The dot will be clipped again.Put the
  • #20: Open the PongGame project in Demo 2 Moving Dot.Run the program.Show that the dot moves slowly down the screen.Ask what controls the speed of movement:Answer It is the rate at which Update is called and the size of the change applied to the position of the dot.Open the file Game1.cs and navigate to the Update method.Change the code to:ballRectangle.X++;ballRectangle.X++;Ask what this would do to the game.Answer: It would cause the ball to move across the screen and not down. The ball will also move twice as fast.Make the point that the game is presently being