Presentation is loading. Please wait.

Presentation is loading. Please wait.

Rob Miles. Creating a Working MoodLight We know that colours in XNA are stored as values which represent the amount of red, blue, green and transparency.

Similar presentations


Presentation on theme: "Rob Miles. Creating a Working MoodLight We know that colours in XNA are stored as values which represent the amount of red, blue, green and transparency."— Presentation transcript:

1 Rob Miles

2 Creating a Working MoodLight

3 We know that colours in XNA are stored as values which represent the amount of red, blue, green and transparency of that colour We know that the Update and Draw methods in an XNA game are called 60 times a second By adding data to a game program and statements to the Update and Draw methods we can create a mood light display that fades smoothly up from black to white

4 A Changing Mood Light When we started we wanted to make a mood light that displayed a range of different colours What we have at the moment is a start, it is time to finish the job

5 Update This program uses the mended version of Update The brightness values are increased 60 times a second up to their limit Fixed Fading Up

6 Fading Up and Down We want the light to fade up and down Once it has got as bright as it can it should start getting darker Then once it has gone dim, it will grow brighter again This behaviour will be the basis of our colour changing light

7 Lamp Fading Update Behaviour If you are fading up: – Make the light brighter – If it is as bright as it can be, change to fading down If you are fading down: – Make the light dimmer – If it is as dim as it can be, change to fading up

8 State and Variables The light needs to “know” what to do when the Update occurs It must “remember” whether it is counting up and down Program writers call this state – The program is either in the counting up state or the counting down state

9 Adding Variables Programs “remember” things by using variables – This is how our program remembered the red, green and blue intensity values We need to add another variable to remember if we are fading up or down – It has two possible state values

10 Boolean Variables bool The bool type is part of C# and is either true or false countingUp truefalse We could make a variable called countingUp which is either true or false This holds the state of our flashing light

11 Game Data and Counting State The Game Data now holds the counting state true It is initially set to true to indicate that the brightness is increasing Update The Update method will use this variable to remember the state of the game Color backColor; byte redIntensity = 0; byte greenIntensity = 0; byte blueIntensity = 0; bool countingUp = true; Color backColor; byte redIntensity = 0; byte greenIntensity = 0; byte blueIntensity = 0; bool countingUp = true;

12 Update Method with state bool A bool value can be used directly in a condition ifelse false An if condition can have an else part which is performed if the condition is false protected override void Update(GameTime gameTime) { if (countingUp) // do counting up behaviour else // do counting down behaviour base.Update(gameTime); } protected override void Update(GameTime gameTime) { if (countingUp) // do counting up behaviour else // do counting down behaviour base.Update(gameTime); }

13 Making a Block We want to perform more than one statement if we are counting up – Update colours – Check the limit of the colours We can do this by creating a block of statements that are controlled by the test

14 The Counting Up Block Curly brackets { and } can be used to make blocks protected override void Update(GameTime gameTime) { if (countingUp) { redIntensity++ ; // add more red greenIntensity++; // add more green blueIntensity++; // add more blue if ( redIntensity == 255 ) countingUp = false; } else // do counting down behaviour base.Update(gameTime); } protected override void Update(GameTime gameTime) { if (countingUp) { redIntensity++ ; // add more red greenIntensity++; // add more green blueIntensity++; // add more blue if ( redIntensity == 255 ) countingUp = false; } else // do counting down behaviour base.Update(gameTime); }

15 Updating the Intensity We have seen these updates before protected override void Update(GameTime gameTime) { // create backColor from intensity values if (countingUp) { redIntensity++ ; // add more red greenIntensity++; // add more green blueIntensity++; // add more blue if ( redIntensity == 255 ) countingUp = false; } else // do counting down behaviour } protected override void Update(GameTime gameTime) { // create backColor from intensity values if (countingUp) { redIntensity++ ; // add more red greenIntensity++; // add more green blueIntensity++; // add more blue if ( redIntensity == 255 ) countingUp = false; } else // do counting down behaviour }

16 Changing the State This test changes the state of the light protected override void Update(GameTime gameTime) { // create backColor from intensity values if (countingUp) { redIntensity++ ; // add more red greenIntensity++; // add more green blueIntensity++; // add more blue if ( redIntensity == 255 ) countingUp = false; } else // do counting down behaviour } protected override void Update(GameTime gameTime) { // create backColor from intensity values if (countingUp) { redIntensity++ ; // add more red greenIntensity++; // add more green blueIntensity++; // add more blue if ( redIntensity == 255 ) countingUp = false; } else // do counting down behaviour }

17 Testing for Equality The == logical operator tests for equality protected override void Update(GameTime gameTime) { // create backColor from intensity values if (countingUp) { redIntensity++ ; // add more red greenIntensity++; // add more green blueIntensity++; // add more blue if ( redIntensity == 255 ) countingUp = false; } else // do counting down behaviour } protected override void Update(GameTime gameTime) { // create backColor from intensity values if (countingUp) { redIntensity++ ; // add more red greenIntensity++; // add more green blueIntensity++; // add more blue if ( redIntensity == 255 ) countingUp = false; } else // do counting down behaviour }

18 Counting Down What does the block that counts down have to do? See if we can write the code to do this

19 Finally we have our pulsing light It changes between getting brighter and getting darker Pulsing Light

20 We can add variables to a game program that represent the “state” of the game Each time the Update method is called it can use such a state variable to decide what to do The game program can change its behaviour over time by modifying the value in the state variable

21 A Boolean variable can only hold the values 0 and 1 The state of a game program changes each time Update is called A block of C# code can hold another block The number of { characters and } characters in a C# program must be the same

22 A Boolean variable can only hold the values 0 and 1 The state of a game program must change each time Update is called A block of C# code can hold another block The number of { characters and } characters in a C# program must be the same

23 A Boolean variable can only hold the values 0 and 1 The state of a game program must change each time Update is called A block of C# code can hold another block The number of { characters and } characters in a C# program must be the same

24 A Boolean variable can only hold the values 0 and 1 The state of a game program must change each time Update is called A block of C# code can hold another block The number of { characters and } characters in a C# program must be the same

25 A Boolean variable can only hold the values 0 and 1 The state of a game program must change each time Update is called A block of C# code can hold another block The number of { characters and } characters in a C# program must be the same

26 Multi Colours We now need to create a MoodLight that displays ever changing colours rather than just changing between black and white

27 Taking Over You are going to create this version of the program after the original developer stepped out for a cup of coffee and has not been seen since


Download ppt "Rob Miles. Creating a Working MoodLight We know that colours in XNA are stored as values which represent the amount of red, blue, green and transparency."

Similar presentations


Ads by Google