session 8.1 reaction timer game - andrews universitygreenley/cs2/slides8.1... · create a reaction...
TRANSCRIPT
Session Overview
Find out how an XNA program can measure the passage of time and trigger events at certain points in time
Show how an algorithm can be expressed in terms of a simple flow diagram
Create a reaction timer game
Discover how game play flaws can be discovered, and an algorithm modified to remove them
Chapter 8.1: Reaction Timer Game 2
Game Idea: “Reaction Timer”
The game implements a reaction timer
It measures the time interval between playing a sound sample and the players pressing a button
The fastest player on the button wins
Chapter 8.1: Reaction Timer Game 3
Using Update to Implement a Timer
We know that the Update method is called 60 times a second by XNA
This means that a game can measure time passing
Each time Update is called we can increase the value in the timer, and use certain timer values to trigger game events
We can also use the value in the timer as a way of working out the time when a particular event occurs
Chapter 8.1: Reaction Timer Game 4
Game World Variables for Reaction Timer
These variables are used to implement the Reaction Timer game
They are all integers, since there is no need for a fractional part for any of the values
Chapter 8.1: Reaction Timer Game 5
// Game World
int timer;
// Gamepad 1 scoresint ascore1;int bscore1;int xscore1;int yscore1;
Reaction Timer Flowchart
One way to describe the behavior required from a program is to draw a “flowchart”
This sets out the flow of the program behavior
It shows the decisions that are made, and the actions that are performed as a result
Chapter 8.1: Reaction Timer Game 6
Reaction Timer Code
This is the start behavior
The timer and scores are reset when Start is pressed
Chapter 8.1: Reaction Timer Game 7
if (pad1.Buttons.Start == ButtonState.Pressed)
{timer = -120;ascore1 = 0;bscore1 = 0;xscore1 = 0;yscore1 = 0;
}
Reaction Timer Code
The timer variable maintains the state of the game
When it is less than 0 the game is waiting to start
When the timer reaches 0 the sound is played
When the timer is greater than 0 it is timing players
Chapter 8.1: Reaction Timer Game 8
timer++;
Reaction Timer Code
When the timer reaches 0 it is time for the start sound to play
The dingSound variable is a sound effect that is played in “Fire and Forget” mode
Chapter 8.1: Reaction Timer Game 9
if (timer == 0){
dingSound.Play();}
Reaction Timer Code
This code is for button A
It uses the button edge detection code
Copying the value of timer is how the game records the score for a player
Chapter 8.1: Reaction Timer Game 10
// if A is pressed copy the timerif (oldpad1.Buttons.A == ButtonState.Released &&
pad1.Buttons.A == ButtonState.Pressed){
ascore1 = timer;}
Displaying the Scores
This code should look slightly familiar.
Chapter 8.1: Reaction Timer Game 11
protected override void Draw(GameTime gameTime){
GraphicsDevice.Clear(Color.CornflowerBlue);spriteBatch.Begin();if (pad1.IsConnected){
spriteBatch.DrawString(font, ascore1.ToString(),apos1, Color.Green);
// repeat for other buttons}spriteBatch.End();base.Draw(gameTime);
}
1. Reaction Timer
Chapter 8.1: Reaction Timer Game 12
This version of the Reaction Timer game uses the algorithm described above
The player with the lowest score is the winner
Game Play Bugs
We have uncovered a fault in the algorithm that implements the game
If a player presses their button early they can sneak a peek at the time value
They can also repeatedly press their buttons with no penalty
This is not a problem if everyone plays by the rules
But there is no guarantee that they will.
Chapter 8.1: Reaction Timer Game 13
Finding the Problem
One of these is the fault that allows cheating:1. The fault occurs because the game does not detect
when the player releases the button as well as when it’s pressed.
2. The fault occurs because the game should use level detection on the buttons, not edge detection.
3. The fault occurs because the game should register only the first press of the button.
4. The fault occurs because the game must reset the gamepad after it’s been read.
Chapter 8.1: Reaction Timer Game 14
Finding the Problem
One of these is the fault that allows cheating:1. The fault occurs because the game does not detect
when the player releases the button as well as when it’s pressed.
2. The fault occurs because the game should use level detection on the buttons, not edge detection.
3. The fault occurs because the game should register only the first press of the button.
4. The fault occurs because the game must reset the gamepad after it’s been read.
Chapter 8.1: Reaction Timer Game 15
Detecting the First Button Press
When the game resets the scores are all set to 0
When a button is pressed the game copies the timer value into the score for that button
The second time the button is pressed the score value for that button will not be 0
We can use this to detect when a button has already been pressed, and reject subsequent presses
Chapter 8.1: Reaction Timer Game 16
Improved Flowchart
This flowchart shows the improved version of the algorithm
The extra test is performed when a button is detected
The score value is only updated if the score is zero
Chapter 8.1: Reaction Timer Game 17
Rejecting Extra Button Presses
This version of the program adds an extra test to the button detection
You can combine many conditions using the && operator
In this case the score is only changed if it is zero
Chapter 8.1: Reaction Timer Game 18
if (oldpad1.Buttons.A == ButtonState.Released &&pad1.Buttons.A == ButtonState.Pressed &&ascore1 == 0)
{ascore1 = timer;
}
2. Improved Reaction Timer
Chapter 8.1: Reaction Timer Game 19
This version adds the extra condition so that only the first button press is registered
This could be the basis of a workable game
Summary
You can use the regular calls of Update as the basis of a timer by creating a variable that counts the number of times that Update has been called
You can trigger actions at a particular time by testing for particular values in the timer variable Many games do this during their attract behavior
A flow diagram is a quick way of describing an algorithm
The initial version of an algorithm may not be the best one (we have seen this before)
Chapter 8.1: Reaction Timer Game 20