rob miles. creating a working moodlight we know that colours in xna are stored as values which...
TRANSCRIPT
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 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
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
• This program uses the mended version of UpdateUpdate
• The brightness values are increased 60 times a second up to their limit
Fixed Fading Up
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
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
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
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
Boolean Variables
• The boolbool type is part of C# and is either true or false
• We could make a variable called countingUpcountingUp which is either truetrue or falsefalse
• This holds the state of our flashing light
Game Data and Counting State
• The Game Data now holds the counting state• It is initially set to truetrue to indicate that the
brightness is increasing• The UpdateUpdate 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;
Update Method with state
• A boolbool value can be used directly in a condition
• An ifif condition can have an elseelse part which is performed if the condition is falsefalse
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);}
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
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);}
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}
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}
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}
Counting Down
• What does the block that counts down have to do?
• See if we can write the code to do this
• Finally we have our pulsing light
• It changes between getting brighter and getting darker
Pulsing Light
• 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
• 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
• 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
• 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
• 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
• 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
Multi Colours
• We now need to create a MoodLight that displays ever changing colours rather than just changing between black and white
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