//******************************************************** //* Project Name: helicopter //* Date: July 28, 2017 //* Author: Dr. Meier //* Provides: a simple helicopter game for kids //* //* Requirements //* - present welcome message //* - play welcome music //* - move helicopter to row 0 when up button pressed //* - move helicopter to row 1 when down button pressed //* - play helicopter propeller sound while flying //* - play crash music when helicopter crashes //* - keep track of user score and high score //* - offers three lives // //* How to Use //* - attach speaker right between A1 and just left of VIN //* - apply usb power //* - use up/dn button to fly helicopter through maze //* - press rst button at game over to start again //******************************************************** // include lcd subroutine library #include // bring lcd panel object to life // - objects are data types that include subroutines // - research object-oriented programming for more info // - Discover LCD attaches to computer pins 8,9,4,5,6,7 // - object constructor needs pin numbers LiquidCrystal lcd(8,9,4,5,6,7); // declare other global variables used in the game // - global variables are visible to all subroutines // // - length = brick maze length // - up = pin A0 voltage range when up button pressed // - down = pin A0 voltage range when dn button pressed // - track = brick maze icon matrix sized to length // - left = left end of 16 brick icon window on lcd screen const int length=500; const int up=100; const int down=300; int track[length]; int left=0; // create helicopter icons as custom LCD characters // - LCD characters are 5 dots wide // - LCD characters are 7 dots tall // - LCD character definition uses 1 = pixel on // - LCD character definition uses 0 = pixel off // - define first icon: blade horizontal // - define second icon: blade coming out of LCD // - define third icon: blade crashed down on cockpit // - each definition is a collection {} of bytes // - B = binary number // - each binary number represents a character row byte helicopter0[8]={ B11111, B00100, B11110, B10001, B11110, B10000, B11111, }; byte helicopter1[8]={ B00100, B00100, B11110, B10001, B11110, B10000, B11111, }; byte helicopter2[8]={ B10001, B01110, B11110, B11111, B11110, B10000, B11111, }; // Arduino mandatory setup function - runs once at reset // - setup game environment: make brick maze // - setup lcd panel: size, custom characters // - display welcome message // - play welcome music // - display instructions // - clear LCD panel to enter operational mode void setup() { createTrack(); lcd.createChar(0,helicopter0); lcd.createChar(1,helicopter1); lcd.createChar(2,helicopter2); lcd.begin(16,2); welcomeMessage(); welcomeMusic(); instructions(); welcomeFlight(); lcd.clear(); } // Arduino mandatory loop function - runs after setup // and runs continuously until Arduino is reset again // // local variables // - int heli: row helicopter icon prints on: 0 or 1 // - int icon: blade position icon to print: 0 or 1 // - int button: user button press voltage from pin A0 // - int brickColumn: brick maze column being displayed // // control algorithm // - do while user hasn't flown through entire maze // - // - *** Handle Button Press and Helicopter Icon *** // - clear LCD panel // - read user button press // - if up button // - clear icon from bottom row with blank space // - adjust heli position variable to row 0 // - else if down button // - clear icon from top row with blank space // - adjust heli position variable to row 1 // - print helicopter at heli position variable row // - (print icon uses divide-by-2 remainder) // - increment icon variable to prepare next icon // - // - *** Draw Track Onto Screen *** // - for each column of maze to be displayed // - check maze to determine which row for brick // - draw brick at brickColumn and appropriate row // - // - ** Check for Helicopter Collision *** // - if heli row=brick row of left end of maze window // - draw crash icon // - make crash music // - display game over message // - enter infinite loop forcing press of reset // - else // - delay one quarter second for scroll speed // - adjust left end of window to be displayed next void loop() { int heli=0; int icon=0; int button; int brickColumn=0; int life=3; int best=0; do { // handle button press and helicopter icon draw lcd.clear(); button=analogRead(A0); if(button>up && buttondown && button<1000) { lcd.setCursor(0,0); lcd.print(" "); heli=1; } lcd.setCursor(0,heli); lcd.write(byte(icon%2)); if((icon%2)==0) tone(A1,17); else tone(A1,19); icon=icon+1; // draw track on screen brickColumn=0; for(int i=left; i 0 // - set track at current column to row number // - decrement n toward 0 as bricks are added // - increment column toward end of maze // - switch rows using if-else // - insert blank to fly through as rows switch // - while column is less than maze length void createTrack() { int column=0; int row=0; int n; randomSeed(analogRead(A5)); // fill first 16 bricks with spaces do { track[column]=2; column=column+1; }while(column<16); do { // fill random n columns with bricks on row n=random(10); while(n>0) { track[column]=row; n=n-1; column=column+1; } // adjust row if(row==0) row=1; else row=0; // insert blank to fly through as rows switch track[column]=2; column=column+1; }while(column