ENGR/CS 101 CS Session Lecture 8 Log into Windows/ACENET (reboot if - - PowerPoint PPT Presentation

engr cs 101 cs session
SMART_READER_LITE
LIVE PREVIEW

ENGR/CS 101 CS Session Lecture 8 Log into Windows/ACENET (reboot if - - PowerPoint PPT Presentation

ENGR/CS 101 CS Session Lecture 8 Log into Windows/ACENET (reboot if in Linux) Start Microsoft Visual Studio 2010 Open DotChaser program Does everyone's game increment the score and move the dot when the dot is clicked? Lecture 8


slide-1
SLIDE 1

ENGR/CS 101 CS Session Lecture 8

 Log into Windows/ACENET (reboot if in

Linux)

 Start Microsoft Visual Studio 2010  Open DotChaser program  Does everyone's game increment the score

and move the dot when the dot is clicked?

Lecture 8 ENGR/CS 101 Computer Science Session 1

slide-2
SLIDE 2

Outline

 Continue Dot Chaser game project

 Timers  Start, Stop, Reset buttons

Lecture 8 ENGR/CS 101 Computer Science Session 2

slide-3
SLIDE 3

Dot Chaser Game

 Specifications of the game

 When the user clicks on the dot, her score

increases by one, and the dot moves to a random place in the game area.

 Otherwise, the dot must move periodically to a

random place in the game area.

 User must be able to start and stop the game.  User must be able to reset the game.

Lecture 8 ENGR/CS 101 Computer Science Session 3

slide-4
SLIDE 4

Timers

 In order for the dot to move periodically, we

need to use a Timer object.

 We need to tell the compiler what library

defines a Timer. This is done with a using statement:

using System.Timers;

 The Timer variable is declared with the

  • ther application variables:

System.Timers.Timer tmr;

Lecture 8 ENGR/CS 101 Computer Science Session 4

slide-5
SLIDE 5

Timers

 A Timer object causes an ElapsedEvent to

  • ccur that we can then handle.

 Just like for the dot's click event, we need to

write a handler for this event. Since there's no GUI element, we write the entire handler method ourselves.

Lecture 8 ENGR/CS 101 Computer Science Session 5

slide-6
SLIDE 6

ElaspedEvent Handler

 Here is the code for the handler for the

ElapsedEvent:

private void timer_Elapsed (object sender, ElapsedEventArgs e) { // move the dot dot_Move(); }

Lecture 8 ENGR/CS 101 Computer Science Session 6

slide-7
SLIDE 7

Adding the Timer Object

 To create and configure the Timer object, we

add the following code to the constructor:

tmr = new System.Timers.Timer(); tmr.Interval = 2000; // 2 seconds tmr.Elapsed += new ElapsedEventHandler(timer_Elapsed);

Lecture 8 ENGR/CS 101 Computer Science Session 7

slide-8
SLIDE 8

Timers

 tmr.Interval is the time between

ElapsedEvents in milliseconds. (1000ms = 1s)

 tmr.Elapsed is the handler to be run when the

ElapsedEvent occurs.

Lecture 8 ENGR/CS 101 Computer Science Session 8

slide-9
SLIDE 9

Start Button

 Add a Start button, rename it to "startBtn",

and style it however you wish

 Double-click on the button to create the click

handler stub. The implementation of this handler is to call the tmr.Start( ) method to start the Timer object.

 Run the program. The button should move

periodically even if the dot is not clicked.

Lecture 8 ENGR/CS 101 Computer Science Session 9

slide-10
SLIDE 10

Stop Button

 Add a Stop button, rename it to "stopBtn",

and style it however you wish.

 Double-click on the button to create the click

handler stub. The implementation of this handler is to call the tmr.Stop( ) method.

 Also, the dot_Click handler should stop the

timer before it updates the game state, then start the timer so that there is a full time period after the dot is moved.

Lecture 8 ENGR/CS 101 Computer Science Session 10

slide-11
SLIDE 11

Reset Button

 Add a Reset button, rename it to "resetBtn",

and style it however you wish.

 Double-click on the button to create the click

handler stub. The implementation of this handler should

 Stop the timer  Set the score to 0  Reposition the dot to its original location

Lecture 8 ENGR/CS 101 Computer Science Session 11

slide-12
SLIDE 12

Resetting the Game

 In order to reposition the dot back to its

  • riginal location, we need to know where it

was when the game started. We do this by

 Adding application variables originalX and

  • riginalY

 Saving the original location in the constructor

  • riginalX = dot.Location.X;
  • riginalY = dot.Location.Y;

 Use dot.setBounds in the Reset button click

handler

Lecture 8 ENGR/CS 101 Computer Science Session 12

slide-13
SLIDE 13

Game State

 What should the game do if the user clicks on

the dot when the game is stopped?

 We can add a boolean variable flag to the

game state that is true when the game is started and false when the game is stopped.

 In C#, bool is the type of a boolean variable

bool started;

 It is initialized to false in the constructor so

that the game is stopped when first launched.

Lecture 8 ENGR/CS 101 Computer Science Session 13

slide-14
SLIDE 14

Game State

 The Start and Stop button click handlers set

the started flag to true and false, respectively.

 The dot Click handler should test that the

game is started before updating the game

  • state. That is, if the game is stopped, the dot

Click handler should do nothing.

Lecture 8 ENGR/CS 101 Computer Science Session 14