rob miles. creating a working moodlight we know that colours in xna are stored as values which...

27
Rob Miles

Upload: emil-small

Post on 02-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 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

Rob Miles

Page 2: 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

Creating a Working MoodLight

Page 3: 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

• 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

Page 4: 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

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

Page 5: 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

• This program uses the mended version of UpdateUpdate

• The brightness values are increased 60 times a second up to their limit

Fixed Fading Up

Page 6: 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

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

Page 7: 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

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

Page 8: 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

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

Page 9: 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

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

Page 10: 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

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

Page 11: 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

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;

Page 12: 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

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);}

Page 13: 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

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

Page 14: 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

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);}

Page 15: 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

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}

Page 16: 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

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}

Page 17: 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

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}

Page 18: 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

Counting Down

• What does the block that counts down have to do?

• See if we can write the code to do this

Page 19: 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

• Finally we have our pulsing light

• It changes between getting brighter and getting darker

Pulsing Light

Page 20: 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

• 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

Page 21: 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

• 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

Page 22: 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

• 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

Page 23: 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

• 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

Page 24: 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

• 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

Page 25: 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

• 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

Page 26: 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

Multi Colours

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

Page 27: 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

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