Unit 6 Lesson 9: Clicker Counter Game

One of my students is trying to make a game where if someone gets a certain amount of clicks using the left or right buttons on the circuit first that they will win but we cannot seem to figure out what to do for this. Any suggestions?

Hi @lacey.lloyd,

I can get you started and hopefully that will help you and your student see it to the end. I did make the assumption you wanted a two player game. One player on the left button and the other player on the right button. Here is how I broke it down.

  1. You need to capture the number of clicks on the left button. Usually when you have to “capture” something, you create a variable. So, create a variable that captures how many times someone pressed down on the left button and use the onBoardEvent to do that.
    var cntPlayer1 = 0;
    onBoardEvent(buttonL, “down”, function(event) {
    cntPlayer1 = cntPlayer1+1;
    });

  2. Repeat for the right button. (I didn’t do that but mostly identical to code above but for the buttonR)

  3. Create two text boxes (labels) for the scores for each player. Use the setProperty box to show those scores and add the code to the onBoardEvent for the left button. Here is an example for player 1:
    var cntPlayer1 = 0;
    onBoardEvent(buttonL, “down”, function(event) {
    cntPlayer1 = cntPlayer1+1;
    setProperty(“player_1_score”, “text”, cntPlayer1);
    });

  4. Finally, you need to evaluate which player reaches the number first, stop the counting, and declare a winner. I would probably evaluate when either one reaches the number (say it is 20) and then hide the text element of the other box and set property in the winner player box to declare the winner. You would add that to the same onBoardEvent.
    var cntPlayer1 = 0;
    onBoardEvent(buttonL, “down”, function(event) {
    cntPlayer1 = cntPlayer1+1;
    setProperty(“player_1_score”, “text”, cntPlayer1);
    if (cntPlayer1==20) {
    setProperty(“player_2_score”, “hidden”, true);
    setProperty(“player_1_score”, “text”, “Winner!”);
    }
    });

  5. Repeat for both the other button as in #2 above.

I didn’t have a board to test so I’ll let you do that for me :slight_smile:

Michelle

1 Like

Thank you so much @melynn! It works and when I have him in class again I know he will be very excited to add this to his project.

1 Like