ENGR/CS 101 CS Session Lecture 6 Log into Windows/ACENET (reboot if in Linux) Start Microsoft Visual Studio 2012 Open DotChaser project Can everyone's game have a dot that moves periodically, and can start, stop, and reset? Lecture 6 ENGR/CS 101 Computer Science Session 1
Outline Continue Dot Chaser game project Game state If-statements Enhancements Lecture 6 ENGR/CS 101 Computer Science Session 2
Game State What should the game do if the user clicks on the dot when the game is stopped? Lecture 6 ENGR/CS 101 Computer Science Session 3
If-Statements We only want to execute the dot click handler code only when the game has been started. We do this with an if-statement . Lecture 6 ENGR/CS 101 Computer Science Session 4
If-Statements An if-statement has: A condition test A body to execute when the test is true An optional body to execute when the test is false We can used it to decide whether to process a dot click 1. If the game is active 1.1 Compute the new score 1.2 Update the score display 1.3 Move the dot Lecture 6 ENGR/CS 101 Computer Science Session 5
If-Statements The syntax of a C# if-statement is: if (<condition>) { <body to execute when condition is true> } else // this part is optional { <body to execute when condition is false> } Lecture 6 ENGR/CS 101 Computer Science Session 6
Game State A condition is an expression that is either true or false We can add a boolean variable flag to represent the game state. The flag 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 active; // game state Lecture 6 ENGR/CS 101 Computer Science Session 7
Game State The values a C# boolean variable may have are true and false . The started flag is initialized to false in the constructor so that the game is stopped when first launched. The Start and Stop button click handlers set the active flag to true and false, respectively. Lecture 6 ENGR/CS 101 Computer Science Session 8
Game State 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. if (active) { // process the dot click } Lecture 6 ENGR/CS 101 Computer Science Session 9
Game Enhancements Here are some of the previously suggested other game rules Occasionally have a "bomb" (a dot of a different color that you don't want to click) that either ends game or loses points Make the dot move faster after some number of points Lose points if don't click the dot before move or clicks in the game area Change colors after some number of clicks Lecture 6 ENGR/CS 101 Computer Science Session 10
Game Enhancements Here are some other ideas for game enhancements: Dot becomes smaller after some number of clicks Game ends or "pauses" (i.e. stops) if no click after some number of moves Game ends if fewer than 20 points in 1 minute Lecture 6 ENGR/CS 101 Computer Science Session 11
Game Enhancements Here are some ideas for implementing these new rules Add integer variables to count things Add Timer variables to keep track of time Will need to add a handler for each Timer object Update counters or Timers when handling dot clicks or Timer moves Test counter values in an if-statement to determine if something needs to be done Lecture 6 ENGR/CS 101 Computer Science Session 12
Recommend
More recommend