Gregory Boudreau / Mbed 2 deprecated Lab6_2036_turkey_greg

Dependencies:   4DGL-uLCD-SE PinDetect mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 //Please note that you must transform this using a 
00002 //polymorphic screen manage like we talked about
00003 //in the lab.  This code is meant to be 'fixed'
00004 //and has some BAD programming choices; however,
00005 //it does  show examples of specific mbed
00006 //functionality that you can use in your final GOOD program. :-)
00007 
00008 #include "mbed.h"
00009 #include "TMP36.h"
00010 #include "uLCD_4DGL.h"
00011 #include "PinDetect.h"
00012 #include "speaker.h"
00013 #include "joystick.h"
00014 #include "gameJingle.h"
00015 #include "gobbleBuzz.h"
00016 
00017 //Put this as C++ constant inside
00018 //your class that contains the turkey!! 
00019 #define TURKEY_HEIGHT 8
00020 #define TURKEY_WIDTH 11
00021 
00022 #define _ BLACK
00023 #define X BROWN
00024 #define R RED
00025 #define Y YELLOW
00026 
00027 //sometimes putting in the global namespace and
00028 //static memory can be a little more efficient for
00029 //embedded programming
00030 
00031 int turkey_sprite[TURKEY_HEIGHT * TURKEY_WIDTH] = {
00032     _,_,_,_,X,X,X,R,R,_,_,
00033     _,X,X,X,X,X,X,X,R,Y,_,
00034     X,X,X,X,X,_,_,X,X,Y,Y,
00035     X,X,X,X,X,_,_,X,X,Y,_,
00036     X,X,X,X,X,X,X,X,R,R,R,
00037     _,_,_,X,X,_,X,X,_,_,_,
00038     _,_,X,X,_,_,_,X,X,_,_,
00039     X,X,_,_,_,X,_,_,_,X,X
00040 };
00041 
00042 
00043 uLCD_4DGL uLCD(p9, p10, p11); // create a global lcd object
00044 TMP36 myTMP36(p17);
00045 
00046 DigitalIn pb1(p21); 
00047 DigitalIn pb2(p22); 
00048 DigitalIn pb3(p23); 
00049 
00050 Speaker mySpeaker(p25);
00051 
00052 Nav_Switch myNav(p16, p13, p14, p12, p15); //pin order on Sparkfun breakout
00053 
00054 int main() {
00055 
00056     //Please have a startup and ending jingle.  I provided
00057     //a wimpy one note startup jingle
00058     GameJingle Jingle;
00059     GobbleBuzz buzz1;
00060     Jingle.playIntro();
00061 
00062     uLCD.cls();
00063     //You can use this following command if you want landscape setup
00064     //uLCD.display_control(LANDSCAPE);
00065     
00066     //This time(0) should return the number of seconds that the mbed
00067     //has been powered on.
00068     srand(time(0));
00069     
00070     //if you are not getting random numbers after each reset try debugging with
00071     //uLCD.printf("Time(0)=%i\n",time(0));
00072     
00073     //setup push buttons
00074     pb1.mode(PullUp);
00075     pb2.mode(PullUp);
00076     pb3.mode(PullUp);
00077     wait(0.3);  //This can make the system more stable as it sets up the pins
00078     
00079     //If you don't have this your system will be really slow.
00080     //This sets the communication rate between the uLCD and the 
00081     //mbed system.
00082     uLCD.baudrate(300000);
00083     wait(0.3);  //Always good to wait as mbed system set itself up!
00084 
00085     
00086     //let's draw a border around the screen in a festive GREEN
00087     //NOTE: In your program make sure the turkey is prevented from
00088     //going outside the bounds set by this boarder!!  
00089     uLCD.cls(); //This clears the screen
00090     uLCD.rectangle(0,0,120,110,GREEN);
00091     
00092     //In this sample program I have one square that randomly
00093     //changes position after it is gobbled up by the gobbler! 
00094     //In your program you will have various choices for 
00095     //your turkey to eat! 
00096     int randXPos = (rand()%(118-TURKEY_WIDTH))+2;
00097     int randYPos = (rand()%(108-TURKEY_HEIGHT))+2;
00098     
00099     int points = 0; //this will keep track of the number of times the 
00100                     //turkey eats the square.
00101     
00102     //In your game you will have to keep track of points in
00103     //a more complex scoring scenario; however, this illustrates
00104     //a way that you can put text anywhere on the screen! In this
00105     //case it is the number of times the turkey eats the square.
00106     //NOTE: The locate function coordinates are not in units of pixels
00107     //but in units of the size of the character printed.
00108     
00109     uLCD.text_width(1); //You can change the size of your text if you want
00110     uLCD.text_height(1); //using these member functions for uLCD
00111     uLCD.locate(1,14); //units are not pixels but character sizes
00112     uLCD.printf("%i",points); //This will print out the points at bottom of screen
00113     
00114     bool GETOUT = false;
00115     int color = WHITE;
00116     
00117     while(!GETOUT) 
00118     {
00119         //You can read about this in the lab. Allows quick printing of graphic sprites
00120         uLCD.BLIT(buzz1.getXPos(), buzz1.getYPos(), TURKEY_WIDTH, TURKEY_HEIGHT, turkey_sprite);
00121         
00122         //This is what the turkey 'eats'. This is so boring; you must spice up the game! 
00123         uLCD.filled_rectangle(randXPos, randYPos,randXPos+TURKEY_WIDTH, randYPos+TURKEY_HEIGHT,color);
00124     
00125         //In order to make the sprite move I must keep track of the old
00126         //position before it is changed by maneveurs on the joystick
00127         buzz1.copyX();
00128         buzz1.copyY();
00129         
00130         //Here is an example on how you can use the joystick using the joystick.h
00131         //You might want to put in a move() function for the GobbleBuzz
00132         if (myNav.up()) buzz1.increaseY();
00133         if (myNav.down()) buzz1.decreaseY();
00134         if (myNav.left()) buzz1.increaseX();
00135         if (myNav.right()) buzz1.decreaseX();
00136         if (myNav.fire())  { GETOUT = true; }
00137         
00138         //You can change the color of your square with the push
00139         //buttons. Notice how they are used because they are in 
00140         //a pull up configuration
00141         if (!pb1) { color = WHITE; }
00142         if (!pb2) {color = BLUE; }
00143         if (!pb3) {color = GREEN; }
00144                      
00145         //Here is an example of the turkey 'eating' the square       
00146         //IDEA: If the turkey sprite and the retangle sprite intersect
00147         //then delete the rectanglar "food" and randomly draw in another 
00148         //location. 
00149         if (buzz1.overlap(randXPos, randYPos, randXPos+TURKEY_WIDTH, randYPos+TURKEY_HEIGHT))
00150         //then you need to delete retangle and redraw in another place
00151         {
00152              //Note I had the edible rectangle and the turkey sprite be the same
00153              //size, but you do not have to do this.
00154              uLCD.filled_rectangle(randXPos, randYPos,randXPos+TURKEY_WIDTH, randYPos+TURKEY_HEIGHT,BLACK);
00155              
00156              //See if you can make a better gobble sound than this!
00157              mySpeaker.PlayNote(450.0,0.1,0.2);
00158              
00159              //Here I pick a new location of the eatable rectangle
00160              randXPos = (rand()%(118-TURKEY_WIDTH))+2;
00161              randYPos = (rand()%(108-TURKEY_HEIGHT))+2;
00162              
00163              //now update the score.  Looks like a cut and paste job...
00164              //make make it a function!! 
00165              uLCD.locate(1,14);
00166              uLCD.text_width(1);
00167              uLCD.text_height(1);
00168              uLCD.printf("%i",++points);
00169         }
00170         
00171         //This will smooth out the movement of your zoombie turkey  
00172         wait(0.1);    
00173         
00174         //If the turkey has been moved with the joystick then you need
00175         //to delete its image at the old location.  At the beginning of the loop
00176         //the turkey sprite will be redrawn again! 
00177         if (( buzz1.getXPos() != buzz1.getOLDXPos()) || (buzz1.getYPos() != buzz1.getOLDYPos()))       
00178             uLCD.filled_rectangle(buzz1.getOLDXPos(), buzz1.getOLDYPos(), 
00179             buzz1.getOLDXPos()+TURKEY_WIDTH, buzz1.getOLDYPos()+ TURKEY_HEIGHT, BLACK);
00180           
00181         }
00182     
00183         //Print a final farewell to indicate win or loss. Also have a nice closing jingle for
00184         //your game! 
00185         uLCD.cls();
00186         uLCD.printf("\n\n\n\n\n       Happy \n   Thanksgiving \n      Break!!");
00187     
00188 
00189 }