Presentation is loading. Please wait.

Presentation is loading. Please wait.

Rob Miles. Creating a Broken MoodLight An XNA game contains game data which is used by the Draw and Update methods – Update updates the game data – Draw.

Similar presentations


Presentation on theme: "Rob Miles. Creating a Broken MoodLight An XNA game contains game data which is used by the Draw and Update methods – Update updates the game data – Draw."— Presentation transcript:

1 Rob Miles

2 Creating a Broken MoodLight

3 An XNA game contains game data which is used by the Draw and Update methods – Update updates the game data – Draw uses the data to draw the game display We can create a fixed colour light by setting a game data value with the colour we want to be drawn

4 A Changing Mood Light We can now create any colour moodlight by changing the values that are used to create the colour Next we want to make the colour of the light change automatically over time

5 Changing Colours A colour can be constructed out of three values (red, green, blue) If we change these values we will draw different colours This is how our changing moodlight will work

6 Game Data for the Mood Light We need some more game data variables These will hold the intensity of red, green and blue in our mood light Update backColor The Update method will change these values and create a backColor value using them Draw backColor The Draw method will clear the screen with the backColor

7 Colour Intensity Game Data The intensity of each part of a colour is held as a value in a data byte byte A byte is an area of computer memory that can hold values in the range 0 to 255 byte backColor We need to declare 3 byte variables for the game in addition to the backColor value Color backColor; byte redIntensity; byte greenIntensity; byte blueIntensity; Color backColor; byte redIntensity; byte greenIntensity; byte blueIntensity;

8 More Game Data We now have four different variables as game data They are shared by all the methods inside the game Color byte One variable is of type Color, the others are of type byte Game Data Color backColor; byte redIntensity; byte greenIntensity; byte blueIntensity; Update method Draw method

9 Game Data and Methods Game Data Color backColor; byte redIntensity; byte greenIntensity; byte blueIntensity; protected override void Update(GameTime gameTime) { } protected override void Draw(GameTime gameTime) { }

10 The Game Class public class Game1 : Microsoft.Xna.Framework.Game { } public class Game1 : Microsoft.Xna.Framework.Game { } Color backColor; byte redIntensity; byte greenIntensity; byte blueIntensity; protected override void Update(GameTime gameTime) { } protected override void Draw(GameTime gameTime) { }

11 The Game Class public class Game1 : Microsoft.Xna.Framework.Game { Color backColor; byte redIntensity; byte greenIntensity; byte blueIntensity; protected override void Update(GameTime gameTime) { } protected override void Draw(GameTime gameTime) { } } public class Game1 : Microsoft.Xna.Framework.Game { Color backColor; byte redIntensity; byte greenIntensity; byte blueIntensity; protected override void Update(GameTime gameTime) { } protected override void Draw(GameTime gameTime) { } }

12 What is a Class? A class is what C# calls a lump of data and actions Things which are placed in a class are called “members” of the class Our game class contains the Game world data and the methods that use

13 Blocks and Brackets – Game1 public class Game1 : Microsoft.Xna.Framework.Game { Color backColor; byte redIntensity; byte greenIntensity; byte blueIntensity; protected override void Update(GameTime gameTime) { } protected override void Draw(GameTime gameTime) { } } public class Game1 : Microsoft.Xna.Framework.Game { Color backColor; byte redIntensity; byte greenIntensity; byte blueIntensity; protected override void Update(GameTime gameTime) { } protected override void Draw(GameTime gameTime) { } }

14 Blocks and Brackets - Update public class Game1 : Microsoft.Xna.Framework.Game { Color backColor; byte redIntensity; byte greenIntensity; byte blueIntensity; protected override void Update(GameTime gameTime) { } protected override void Draw(GameTime gameTime) { } } public class Game1 : Microsoft.Xna.Framework.Game { Color backColor; byte redIntensity; byte greenIntensity; byte blueIntensity; protected override void Update(GameTime gameTime) { } protected override void Draw(GameTime gameTime) { } }

15 Blocks and Brackets - Update public class Game1 : Microsoft.Xna.Framework.Game { Color backColor; byte redIntensity; byte greenIntensity; byte blueIntensity; protected override void Update(GameTime gameTime) { } protected override void Draw(GameTime gameTime) { } } public class Game1 : Microsoft.Xna.Framework.Game { Color backColor; byte redIntensity; byte greenIntensity; byte blueIntensity; protected override void Update(GameTime gameTime) { } protected override void Draw(GameTime gameTime) { } }

16 Comments Comments do not tell the computer to do anything They make programs easier to understand // At the start of a comment there is a // sequence to mark the rest of that line as a comment // Game Data Color backColor; byte redIntensity; byte greenIntensity; byte blueIntensity; // Game Data Color backColor; byte redIntensity; byte greenIntensity; byte blueIntensity;

17 Big Comments /**/ If you want to write a really large comment you put the text in-between /* and */ The compiler (the thing that makes your program into something you can run) ignores all comments Lots of comments make code easy to understand /* This program displays a mood light which changes colour and looks really nice */ /* This program displays a mood light which changes colour and looks really nice */

18 Coloured Code Visual Studio shows comments in green It also shows keywords in dark blue – A keyword is something which is part of C# Things that have been added to C# (e.g. parts of XNA) are shown in light blue This makes programs easier to read // Game Data Color backColor; byte redIntensity; byte greenIntensity; byte blueIntensity; // Game Data Color backColor; byte redIntensity; byte greenIntensity; byte blueIntensity;

19 Initial Values for Variables A program can set the initial value of a variable when the variable is declared backColorUpdate We don’t need to set an initial value of backColor as this will be set by Update // Game Data – start with a black screen Color backColor; byte redIntensity = 0; byte greenIntensity = 0; byte blueIntensity = 0; // Game Data – start with a black screen Color backColor; byte redIntensity = 0; byte greenIntensity = 0; byte blueIntensity = 0;

20 A Boring Update Method UpdatebackColor This version of Update create a backColor values based on the intensity values The screen will always be black protected override void Update(GameTime gameTime) { // game exit test backColor = new Color(redIntensity, greenIntensity, blueIntensity); base.Update(gameTime); } protected override void Update(GameTime gameTime) { // game exit test backColor = new Color(redIntensity, greenIntensity, blueIntensity); base.Update(gameTime); }

21 An Interesting Update Method protected override void Update(GameTime gameTime) { // game exit test backColor = new Color(redIntensity, greenIntensity, blueIntensity); redIntensity++ ; // add more red greenIntensity++; // add more green blueIntensity++; // add more blue base.Update(gameTime); } protected override void Update(GameTime gameTime) { // game exit test backColor = new Color(redIntensity, greenIntensity, blueIntensity); redIntensity++ ; // add more red greenIntensity++; // add more green blueIntensity++; // add more blue base.Update(gameTime); }

22 The Game Draw Method Draw This is exactly the same Draw method we used before Update The game behaves differently because Update does different things to the game data 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); }

23 Update This program uses the “interesting” version of Update The brightness values are increased 60 times a second Fading Up

24 Something is Broken The screen gets brighter, and then it goes black This seems to happen every four seconds or so Apart from that the program runs fine – There are no errors reported What is happening? – More next time......

25 The Update method contains the program’s class The start and end of a block is marked with curly brackets Comments in programs tell the compiler what to do Comments in programs are blue The Update method is only called once when a game runs

26 The Update method contains the program’s class The start and end of a block is marked with curly brackets Comments in programs tell the compiler what to do Comments in programs are blue The Update method is only called once when a game runs

27 The Update method contains the program’s class The start and end of a block is marked with curly brackets Comments in programs tell the compiler what to do Comments in programs are blue The Update method is only called once when a game runs

28 The Update method contains the program’s class The start and end of a block is marked with curly brackets Comments in programs tell the compiler what to do Comments in programs are blue The Update method is only called once when a game runs

29 The Update method contains the program’s class The start and end of a block is marked with curly brackets Comments in programs tell the compiler what to do Comments in programs are blue The Update method is only called once when a game runs

30 The Update method contains the program’s class The start and end of a block is marked with curly brackets Comments in programs tell the compiler what to do Comments in programs are blue The Update method is only called once when a game runs


Download ppt "Rob Miles. Creating a Broken MoodLight An XNA game contains game data which is used by the Draw and Update methods – Update updates the game data – Draw."

Similar presentations


Ads by Google