Пример #1
0
void main() {
	hal_init();  // Setup IO pins and defaults
	while (1) {  // Repeatedly play games
		wait_start();
		play_start();
		if (single_game()) {
			play_winner();
			game_level++; // Next level
		} else {
			play_loser();
		}
	}
}
Пример #2
0
int main (void)
{
  uint8_t choice;
  uint8_t current_pos;
  uint16_t time_limit;

  ioinit(); //Setup IO pins and defaults

 BEGIN_GAME:

  //Display fancy LED pattern waiting for two buttons to be pressed
  //Wait for user to begin game
  while(1)
    {
      sbi(LED_RED_PORT, LED_RED);
      cbi(LED_BLUE_PORT, LED_BLUE);
      cbi(LED_GREEN_PORT, LED_GREEN);
      cbi(LED_YELLOW_PORT, LED_YELLOW);
      delay_ms(100);
      if(check_button())
        break;

      cbi(LED_RED_PORT, LED_RED);
      sbi(LED_BLUE_PORT, LED_BLUE);
      cbi(LED_GREEN_PORT, LED_GREEN);
      cbi(LED_YELLOW_PORT, LED_YELLOW);
      delay_ms(100);
      if(check_button())
        break;

      cbi(LED_RED_PORT, LED_RED);
      cbi(LED_BLUE_PORT, LED_BLUE);
      sbi(LED_GREEN_PORT, LED_GREEN);
      cbi(LED_YELLOW_PORT, LED_YELLOW);
      delay_ms(100);
      if(check_button())
        break;

      cbi(LED_RED_PORT, LED_RED);
      cbi(LED_BLUE_PORT, LED_BLUE);
      cbi(LED_GREEN_PORT, LED_GREEN);
      sbi(LED_YELLOW_PORT, LED_YELLOW);
      delay_ms(100);
      if(check_button())
        break;
    }

  //Indicate the start of game play
  sbi(LED_RED_PORT, LED_RED);
  sbi(LED_BLUE_PORT, LED_BLUE);
  sbi(LED_GREEN_PORT, LED_GREEN);
  sbi(LED_YELLOW_PORT, LED_YELLOW);
  delay_ms(1000);
  cbi(LED_RED_PORT, LED_RED);
  cbi(LED_BLUE_PORT, LED_BLUE);
  cbi(LED_GREEN_PORT, LED_GREEN);
  cbi(LED_YELLOW_PORT, LED_YELLOW);
  delay_ms(250);

  game_string_position = 0; //Start new game

  while(1)
    {
      add_to_string(); //Add the first button to the string
      play_string(); //Play the current contents of the game string back for the player

      //Wait for user to input buttons until they mess up, reach the end of the current string, or time out
      for(current_pos = 0 ; current_pos < game_string_position ; current_pos++)
        {
          //Button scanning
          //=======================================
          time_limit = 0; //Run timer while we wait for the user to push a button


          int button_state;
          int last_button = check_button();
          while(1)
            {
              button_state = check_button();
              if(button_state != 0 && last_button == button_state)
                break;
              last_button = button_state;

              delay_ms(5);

              time_limit++; //3 second time limit
              if (time_limit > 3000)
                {
                  play_loser(); //Play anoying loser tones
                  delay_ms(1000);
                  play_string();
                  goto BEGIN_GAME;
                }
            }

          // Button debounce.
          delay_ms(10);

          //Wait for user to release button
          choice = 0;
          while(choice == 0)
            {
              int button = check_button();
              if(button == 1) choice = '3'; //Lower left button "Blue"
              if(button == 2) choice = '4'; //Lower right "Yellow"
              if(button == 4) choice = '1'; //Upper left "Red"
              if(button == 8) choice = '2'; //Upper right "Green"

              toner(choice, 150); //Fire the button and play the button tone
            }

          // Debounce
          delay_ms(10);

          //=======================================
          if (choice != game_string[current_pos])
            {
              play_loser(); //Play annoying loser tones
              delay_ms(1000);
              play_string();
              goto BEGIN_GAME;
            }

        }//End user input loop

      //If user reaches the game length of X, they win!
      if (current_pos == 13)
        {
          play_winner(); //Play winner tones
          goto BEGIN_GAME;
        }

      //Otherwise, we need to wait just a hair before we play back the last string
      delay_ms(1000);
    }

  return(0);
}