Presentation is loading. Please wait.

Presentation is loading. Please wait.

Rob Miles. Using data in an XNA game program An XNA game program Draw and Update methods that are called to run the game Colours are held in XNA as four.

Similar presentations


Presentation on theme: "Rob Miles. Using data in an XNA game program An XNA game program Draw and Update methods that are called to run the game Colours are held in XNA as four."— Presentation transcript:

1 Rob Miles

2 Using data in an XNA game program

3 An XNA game program Draw and Update methods that are called to run the game Colours are held in XNA as four values (Red, Green. Blue and Transparency (Alpha) Data in programs is held in storage locations of a particular type XNA provides a type called Color which can hold colour information

4 A MoodLight Program We are building a colour changing moodlight Now we to make it work – store the colour of the light as data in the program Update – make the Update method set this colour Draw – make the Draw method draw with the colour

5 How Games Work: 1 Update The Update method changes the game data each time the clock ticks Update Game Data Draw

6 How Games Work: 2 Draw The Draw method reads the game data and draws the display Update Game Data Draw

7 How Games Work: 3 DrawUpdate The Draw and Update methods in XNA are called sixty times a second – The XNA framework does this for us automatically Update If Update changes the data each time the game can give the appearance of movement

8 More About Data Data is what computers work with It is really just patterns of signals inside the computer hardware But we can use these patterns to represent things in the real world

9 Computer Programs and Data Programs work on Data – The colour of the screen – Your name – Your number of MySpace friends – The things in your Amazon shopping cart These must be held in the computer and manipulated by programs

10 Data and Variables Data in a program is held in variables These are named locations which hold something the program works with Different types of data are held in different types of variables – Your age is held in an integer – Your name is held in a string – A colour value is held in a special “colour” type provided as part of XNA This type is called Color because Microsoft is an American company

11 Declaring a Colour Variable A declaration statement tells the compiler about a variable we want to use in our program – A declaration gives the type of variable followed by the name of the variable backColor We want to store the value of the backgrooun colour in a variable called backColor You must declare a variable before you use it – Note the American spelling of the Color type Color backColor;

12 Names for Variables In programs names of things are called identifiers The compiler doesn’t care what identifiers you use as long as they are legal – Must start with letter and only contain letters, digits or _ (the underscore character) xX – Case is significant. x is a different identifier from X You should invent a name that fits the contents of the variable Color backColor;

13 Variables in an XNA Game The game data is declared inside the game and is part of it – called a data member UpdateDraw It can then be used by the Update and Draw methods Large games have lots of Game Data Game Data: Color backColor Update action Draw action Other actions...

14 A Color Member Variable Color backColor This code declares a new Color variable with the identifier backColor DrawUpdate This is declared as a member in the game so that Draw and Update can both work with this variable – Update sets it – Draw uses it Color backColor;

15 The XNA Update method Update This is the Update method created as part of an empty XNA game project protected override void Update(GameTime gameTime) { // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); // TODO: Add your update logic here base.Update(gameTime); } protected override void Update(GameTime gameTime) { // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); // TODO: Add your update logic here base.Update(gameTime); }

16 Setting the backColor value in Update backColor Color This statement sets backColor to the value of a new Color with 200 red, 200 blue and no green protected override void Update(GameTime gameTime) { // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); backColor = new Color(200,200,0); base.Update(gameTime); } protected override void Update(GameTime gameTime) { // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); backColor = new Color(200,200,0); base.Update(gameTime); }

17 Creating a Colour Variable backColor Color This statement sets the value of backColor to a new Color value The statement is called an assignment – It assigns a value to the variable The value on the left is assigned the value on the right backColor = new Color(200,200,0);

18 Creating a Color Variable Color This code constructs a new Color value – It actually calls a special method called a constructor The values in the brackets are parameters that are fed into the construction process, given in the order red, green and blue – We can leave out the alpha value if we want a solid colour backColor = new Color(200,200,0);

19 Using our Colour Variable in Draw backColorClear The method uses the value stored in backColor and passes it into the Clear method protected override void Draw(GameTime gameTime) { graphics.GraphicsDevice.Clear(backColor); base.Draw(gameTime); } protected override void Draw(GameTime gameTime) { graphics.GraphicsDevice.Clear(backColor); base.Draw(gameTime); }

20 This program shows us what (200,200,0) gives as a colour I think it looks like mustard Colour Program

21 A variable is a named storage location of a particular type – C# contains rules which determine what makes a valid variable name (identifier) We can create our own data variables inside an XNA game These hold information which is updated using Update and controls the behaviour of the Draw method

22 HelloWorld is a valid identifier The Draw method updates the game data The Update method updates the game data The Xbox holds data variables on the hard disk Fred and FRED are the same identifier in C# XNA identifiers always start with X

23 HelloWorld is a valid identifier The Draw method updates the game data The Update method updates the game data The Xbox holds data variables on the hard disk Fred and FRED are the same identifier in C# XNA identifiers always start with X

24 HelloWorld is a valid identifier The Draw method updates the game data The Update method updates the game data The Xbox holds data variables on the hard disk Fred and FRED are the same identifier in C# XNA identifiers always start with X

25 HelloWorld is a valid identifier The Draw method updates the game data The Update method updates the game data The Xbox holds data variables on the hard disk Fred and FRED are the same identifier in C# XNA identifiers always start with X

26 HelloWorld is a valid identifier The Draw method updates the game data The Update method updates the game data The Xbox holds data variables on the hard disk Fred and FRED are the same identifier in C# XNA identifiers always start with X

27 HelloWorld is a valid identifier The Draw method updates the game data The Update method updates the game data The Xbox holds data variables on the hard disk Fred and FRED are the same identifier in C# XNA identifiers always start with X

28 HelloWorld is a valid identifier The Draw method updates the game data The Update method updates the game data The Xbox holds data variables on the hard disk Fred and FRED are the same identifier in C# XNA identifiers always start with X


Download ppt "Rob Miles. Using data in an XNA game program An XNA game program Draw and Update methods that are called to run the game Colours are held in XNA as four."

Similar presentations


Ads by Google