Top Banner
1 Reaction Timer Game Session 8.1
20

Session 8.1 Reaction Timer Game - Andrews Universitygreenley/cs2/Slides8.1... · Create a reaction timer game ... Each time Updateis called we can increase the value in the timer,

Mar 13, 2018

Download

Documents

duongxuyen
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Session 8.1 Reaction Timer Game - Andrews Universitygreenley/cs2/Slides8.1... · Create a reaction timer game ... Each time Updateis called we can increase the value in the timer,

1

Reaction Timer GameSession 8.1

Page 2: Session 8.1 Reaction Timer Game - Andrews Universitygreenley/cs2/Slides8.1... · Create a reaction timer game ... Each time Updateis called we can increase the value in the timer,

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

Page 3: Session 8.1 Reaction Timer Game - Andrews Universitygreenley/cs2/Slides8.1... · Create a reaction timer game ... Each time Updateis called we can increase the value in the timer,

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

Page 4: Session 8.1 Reaction Timer Game - Andrews Universitygreenley/cs2/Slides8.1... · Create a reaction timer game ... Each time Updateis called we can increase the value in the timer,

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

Page 5: Session 8.1 Reaction Timer Game - Andrews Universitygreenley/cs2/Slides8.1... · Create a reaction timer game ... Each time Updateis called we can increase the value in the timer,

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;

Page 6: Session 8.1 Reaction Timer Game - Andrews Universitygreenley/cs2/Slides8.1... · Create a reaction timer game ... Each time Updateis called we can increase the value in the timer,

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

Page 7: Session 8.1 Reaction Timer Game - Andrews Universitygreenley/cs2/Slides8.1... · Create a reaction timer game ... Each time Updateis called we can increase the value in the timer,

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;

}

Page 8: Session 8.1 Reaction Timer Game - Andrews Universitygreenley/cs2/Slides8.1... · Create a reaction timer game ... Each time Updateis called we can increase the value in the timer,

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++;

Page 9: Session 8.1 Reaction Timer Game - Andrews Universitygreenley/cs2/Slides8.1... · Create a reaction timer game ... Each time Updateis called we can increase the value in the 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();}

Page 10: Session 8.1 Reaction Timer Game - Andrews Universitygreenley/cs2/Slides8.1... · Create a reaction timer game ... Each time Updateis called we can increase the value in the timer,

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

Page 11: Session 8.1 Reaction Timer Game - Andrews Universitygreenley/cs2/Slides8.1... · Create a reaction timer game ... Each time Updateis called we can increase the value in the 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);

}

Page 12: Session 8.1 Reaction Timer Game - Andrews Universitygreenley/cs2/Slides8.1... · Create a reaction timer game ... Each time Updateis called we can increase the value in the timer,

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

Page 13: Session 8.1 Reaction Timer Game - Andrews Universitygreenley/cs2/Slides8.1... · Create a reaction timer game ... Each time Updateis called we can increase the value in the timer,

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

Page 14: Session 8.1 Reaction Timer Game - Andrews Universitygreenley/cs2/Slides8.1... · Create a reaction timer game ... Each time Updateis called we can increase the value in the timer,

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

Page 15: Session 8.1 Reaction Timer Game - Andrews Universitygreenley/cs2/Slides8.1... · Create a reaction timer game ... Each time Updateis called we can increase the value in the timer,

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

Page 16: Session 8.1 Reaction Timer Game - Andrews Universitygreenley/cs2/Slides8.1... · Create a reaction timer game ... Each time Updateis called we can increase the value in the timer,

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

Page 17: Session 8.1 Reaction Timer Game - Andrews Universitygreenley/cs2/Slides8.1... · Create a reaction timer game ... Each time Updateis called we can increase the value in the timer,

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

Page 18: Session 8.1 Reaction Timer Game - Andrews Universitygreenley/cs2/Slides8.1... · Create a reaction timer game ... Each time Updateis called we can increase the value in the timer,

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;

}

Page 19: Session 8.1 Reaction Timer Game - Andrews Universitygreenley/cs2/Slides8.1... · Create a reaction timer game ... Each time Updateis called we can increase the value in the 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

Page 20: Session 8.1 Reaction Timer Game - Andrews Universitygreenley/cs2/Slides8.1... · Create a reaction timer game ... Each time Updateis called we can increase the value in the timer,

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