Gregory Boudreau / Mbed 2 deprecated Lab6_2036_turkey_greg

Dependencies:   4DGL-uLCD-SE PinDetect mbed

Committer:
gboudreau6
Date:
Tue Nov 22 16:42:02 2022 +0000
Revision:
0:56c7173428ac
For ECE 2036 Fall 2022 Lab 6;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gboudreau6 0:56c7173428ac 1 //Please note that you must transform this using a
gboudreau6 0:56c7173428ac 2 //polymorphic screen manage like we talked about
gboudreau6 0:56c7173428ac 3 //in the lab. This code is meant to be 'fixed'
gboudreau6 0:56c7173428ac 4 //and has some BAD programming choices; however,
gboudreau6 0:56c7173428ac 5 //it does show examples of specific mbed
gboudreau6 0:56c7173428ac 6 //functionality that you can use in your final GOOD program. :-)
gboudreau6 0:56c7173428ac 7
gboudreau6 0:56c7173428ac 8 #include "mbed.h"
gboudreau6 0:56c7173428ac 9 #include "TMP36.h"
gboudreau6 0:56c7173428ac 10 #include "uLCD_4DGL.h"
gboudreau6 0:56c7173428ac 11 #include "PinDetect.h"
gboudreau6 0:56c7173428ac 12 #include "speaker.h"
gboudreau6 0:56c7173428ac 13 #include "joystick.h"
gboudreau6 0:56c7173428ac 14 #include "gameJingle.h"
gboudreau6 0:56c7173428ac 15 #include "gobbleBuzz.h"
gboudreau6 0:56c7173428ac 16
gboudreau6 0:56c7173428ac 17 //Put this as C++ constant inside
gboudreau6 0:56c7173428ac 18 //your class that contains the turkey!!
gboudreau6 0:56c7173428ac 19 #define TURKEY_HEIGHT 8
gboudreau6 0:56c7173428ac 20 #define TURKEY_WIDTH 11
gboudreau6 0:56c7173428ac 21
gboudreau6 0:56c7173428ac 22 #define _ BLACK
gboudreau6 0:56c7173428ac 23 #define X BROWN
gboudreau6 0:56c7173428ac 24 #define R RED
gboudreau6 0:56c7173428ac 25 #define Y YELLOW
gboudreau6 0:56c7173428ac 26
gboudreau6 0:56c7173428ac 27 //sometimes putting in the global namespace and
gboudreau6 0:56c7173428ac 28 //static memory can be a little more efficient for
gboudreau6 0:56c7173428ac 29 //embedded programming
gboudreau6 0:56c7173428ac 30
gboudreau6 0:56c7173428ac 31 int turkey_sprite[TURKEY_HEIGHT * TURKEY_WIDTH] = {
gboudreau6 0:56c7173428ac 32 _,_,_,_,X,X,X,R,R,_,_,
gboudreau6 0:56c7173428ac 33 _,X,X,X,X,X,X,X,R,Y,_,
gboudreau6 0:56c7173428ac 34 X,X,X,X,X,_,_,X,X,Y,Y,
gboudreau6 0:56c7173428ac 35 X,X,X,X,X,_,_,X,X,Y,_,
gboudreau6 0:56c7173428ac 36 X,X,X,X,X,X,X,X,R,R,R,
gboudreau6 0:56c7173428ac 37 _,_,_,X,X,_,X,X,_,_,_,
gboudreau6 0:56c7173428ac 38 _,_,X,X,_,_,_,X,X,_,_,
gboudreau6 0:56c7173428ac 39 X,X,_,_,_,X,_,_,_,X,X
gboudreau6 0:56c7173428ac 40 };
gboudreau6 0:56c7173428ac 41
gboudreau6 0:56c7173428ac 42
gboudreau6 0:56c7173428ac 43 uLCD_4DGL uLCD(p9, p10, p11); // create a global lcd object
gboudreau6 0:56c7173428ac 44 TMP36 myTMP36(p17);
gboudreau6 0:56c7173428ac 45
gboudreau6 0:56c7173428ac 46 DigitalIn pb1(p21);
gboudreau6 0:56c7173428ac 47 DigitalIn pb2(p22);
gboudreau6 0:56c7173428ac 48 DigitalIn pb3(p23);
gboudreau6 0:56c7173428ac 49
gboudreau6 0:56c7173428ac 50 Speaker mySpeaker(p25);
gboudreau6 0:56c7173428ac 51
gboudreau6 0:56c7173428ac 52 Nav_Switch myNav(p16, p13, p14, p12, p15); //pin order on Sparkfun breakout
gboudreau6 0:56c7173428ac 53
gboudreau6 0:56c7173428ac 54 int main() {
gboudreau6 0:56c7173428ac 55
gboudreau6 0:56c7173428ac 56 //Please have a startup and ending jingle. I provided
gboudreau6 0:56c7173428ac 57 //a wimpy one note startup jingle
gboudreau6 0:56c7173428ac 58 GameJingle Jingle;
gboudreau6 0:56c7173428ac 59 GobbleBuzz buzz1;
gboudreau6 0:56c7173428ac 60 Jingle.playIntro();
gboudreau6 0:56c7173428ac 61
gboudreau6 0:56c7173428ac 62 uLCD.cls();
gboudreau6 0:56c7173428ac 63 //You can use this following command if you want landscape setup
gboudreau6 0:56c7173428ac 64 //uLCD.display_control(LANDSCAPE);
gboudreau6 0:56c7173428ac 65
gboudreau6 0:56c7173428ac 66 //This time(0) should return the number of seconds that the mbed
gboudreau6 0:56c7173428ac 67 //has been powered on.
gboudreau6 0:56c7173428ac 68 srand(time(0));
gboudreau6 0:56c7173428ac 69
gboudreau6 0:56c7173428ac 70 //if you are not getting random numbers after each reset try debugging with
gboudreau6 0:56c7173428ac 71 //uLCD.printf("Time(0)=%i\n",time(0));
gboudreau6 0:56c7173428ac 72
gboudreau6 0:56c7173428ac 73 //setup push buttons
gboudreau6 0:56c7173428ac 74 pb1.mode(PullUp);
gboudreau6 0:56c7173428ac 75 pb2.mode(PullUp);
gboudreau6 0:56c7173428ac 76 pb3.mode(PullUp);
gboudreau6 0:56c7173428ac 77 wait(0.3); //This can make the system more stable as it sets up the pins
gboudreau6 0:56c7173428ac 78
gboudreau6 0:56c7173428ac 79 //If you don't have this your system will be really slow.
gboudreau6 0:56c7173428ac 80 //This sets the communication rate between the uLCD and the
gboudreau6 0:56c7173428ac 81 //mbed system.
gboudreau6 0:56c7173428ac 82 uLCD.baudrate(300000);
gboudreau6 0:56c7173428ac 83 wait(0.3); //Always good to wait as mbed system set itself up!
gboudreau6 0:56c7173428ac 84
gboudreau6 0:56c7173428ac 85
gboudreau6 0:56c7173428ac 86 //let's draw a border around the screen in a festive GREEN
gboudreau6 0:56c7173428ac 87 //NOTE: In your program make sure the turkey is prevented from
gboudreau6 0:56c7173428ac 88 //going outside the bounds set by this boarder!!
gboudreau6 0:56c7173428ac 89 uLCD.cls(); //This clears the screen
gboudreau6 0:56c7173428ac 90 uLCD.rectangle(0,0,120,110,GREEN);
gboudreau6 0:56c7173428ac 91
gboudreau6 0:56c7173428ac 92 //In this sample program I have one square that randomly
gboudreau6 0:56c7173428ac 93 //changes position after it is gobbled up by the gobbler!
gboudreau6 0:56c7173428ac 94 //In your program you will have various choices for
gboudreau6 0:56c7173428ac 95 //your turkey to eat!
gboudreau6 0:56c7173428ac 96 int randXPos = (rand()%(118-TURKEY_WIDTH))+2;
gboudreau6 0:56c7173428ac 97 int randYPos = (rand()%(108-TURKEY_HEIGHT))+2;
gboudreau6 0:56c7173428ac 98
gboudreau6 0:56c7173428ac 99 int points = 0; //this will keep track of the number of times the
gboudreau6 0:56c7173428ac 100 //turkey eats the square.
gboudreau6 0:56c7173428ac 101
gboudreau6 0:56c7173428ac 102 //In your game you will have to keep track of points in
gboudreau6 0:56c7173428ac 103 //a more complex scoring scenario; however, this illustrates
gboudreau6 0:56c7173428ac 104 //a way that you can put text anywhere on the screen! In this
gboudreau6 0:56c7173428ac 105 //case it is the number of times the turkey eats the square.
gboudreau6 0:56c7173428ac 106 //NOTE: The locate function coordinates are not in units of pixels
gboudreau6 0:56c7173428ac 107 //but in units of the size of the character printed.
gboudreau6 0:56c7173428ac 108
gboudreau6 0:56c7173428ac 109 uLCD.text_width(1); //You can change the size of your text if you want
gboudreau6 0:56c7173428ac 110 uLCD.text_height(1); //using these member functions for uLCD
gboudreau6 0:56c7173428ac 111 uLCD.locate(1,14); //units are not pixels but character sizes
gboudreau6 0:56c7173428ac 112 uLCD.printf("%i",points); //This will print out the points at bottom of screen
gboudreau6 0:56c7173428ac 113
gboudreau6 0:56c7173428ac 114 bool GETOUT = false;
gboudreau6 0:56c7173428ac 115 int color = WHITE;
gboudreau6 0:56c7173428ac 116
gboudreau6 0:56c7173428ac 117 while(!GETOUT)
gboudreau6 0:56c7173428ac 118 {
gboudreau6 0:56c7173428ac 119 //You can read about this in the lab. Allows quick printing of graphic sprites
gboudreau6 0:56c7173428ac 120 uLCD.BLIT(buzz1.getXPos(), buzz1.getYPos(), TURKEY_WIDTH, TURKEY_HEIGHT, turkey_sprite);
gboudreau6 0:56c7173428ac 121
gboudreau6 0:56c7173428ac 122 //This is what the turkey 'eats'. This is so boring; you must spice up the game!
gboudreau6 0:56c7173428ac 123 uLCD.filled_rectangle(randXPos, randYPos,randXPos+TURKEY_WIDTH, randYPos+TURKEY_HEIGHT,color);
gboudreau6 0:56c7173428ac 124
gboudreau6 0:56c7173428ac 125 //In order to make the sprite move I must keep track of the old
gboudreau6 0:56c7173428ac 126 //position before it is changed by maneveurs on the joystick
gboudreau6 0:56c7173428ac 127 buzz1.copyX();
gboudreau6 0:56c7173428ac 128 buzz1.copyY();
gboudreau6 0:56c7173428ac 129
gboudreau6 0:56c7173428ac 130 //Here is an example on how you can use the joystick using the joystick.h
gboudreau6 0:56c7173428ac 131 //You might want to put in a move() function for the GobbleBuzz
gboudreau6 0:56c7173428ac 132 if (myNav.up()) buzz1.increaseY();
gboudreau6 0:56c7173428ac 133 if (myNav.down()) buzz1.decreaseY();
gboudreau6 0:56c7173428ac 134 if (myNav.left()) buzz1.increaseX();
gboudreau6 0:56c7173428ac 135 if (myNav.right()) buzz1.decreaseX();
gboudreau6 0:56c7173428ac 136 if (myNav.fire()) { GETOUT = true; }
gboudreau6 0:56c7173428ac 137
gboudreau6 0:56c7173428ac 138 //You can change the color of your square with the push
gboudreau6 0:56c7173428ac 139 //buttons. Notice how they are used because they are in
gboudreau6 0:56c7173428ac 140 //a pull up configuration
gboudreau6 0:56c7173428ac 141 if (!pb1) { color = WHITE; }
gboudreau6 0:56c7173428ac 142 if (!pb2) {color = BLUE; }
gboudreau6 0:56c7173428ac 143 if (!pb3) {color = GREEN; }
gboudreau6 0:56c7173428ac 144
gboudreau6 0:56c7173428ac 145 //Here is an example of the turkey 'eating' the square
gboudreau6 0:56c7173428ac 146 //IDEA: If the turkey sprite and the retangle sprite intersect
gboudreau6 0:56c7173428ac 147 //then delete the rectanglar "food" and randomly draw in another
gboudreau6 0:56c7173428ac 148 //location.
gboudreau6 0:56c7173428ac 149 if (buzz1.overlap(randXPos, randYPos, randXPos+TURKEY_WIDTH, randYPos+TURKEY_HEIGHT))
gboudreau6 0:56c7173428ac 150 //then you need to delete retangle and redraw in another place
gboudreau6 0:56c7173428ac 151 {
gboudreau6 0:56c7173428ac 152 //Note I had the edible rectangle and the turkey sprite be the same
gboudreau6 0:56c7173428ac 153 //size, but you do not have to do this.
gboudreau6 0:56c7173428ac 154 uLCD.filled_rectangle(randXPos, randYPos,randXPos+TURKEY_WIDTH, randYPos+TURKEY_HEIGHT,BLACK);
gboudreau6 0:56c7173428ac 155
gboudreau6 0:56c7173428ac 156 //See if you can make a better gobble sound than this!
gboudreau6 0:56c7173428ac 157 mySpeaker.PlayNote(450.0,0.1,0.2);
gboudreau6 0:56c7173428ac 158
gboudreau6 0:56c7173428ac 159 //Here I pick a new location of the eatable rectangle
gboudreau6 0:56c7173428ac 160 randXPos = (rand()%(118-TURKEY_WIDTH))+2;
gboudreau6 0:56c7173428ac 161 randYPos = (rand()%(108-TURKEY_HEIGHT))+2;
gboudreau6 0:56c7173428ac 162
gboudreau6 0:56c7173428ac 163 //now update the score. Looks like a cut and paste job...
gboudreau6 0:56c7173428ac 164 //make make it a function!!
gboudreau6 0:56c7173428ac 165 uLCD.locate(1,14);
gboudreau6 0:56c7173428ac 166 uLCD.text_width(1);
gboudreau6 0:56c7173428ac 167 uLCD.text_height(1);
gboudreau6 0:56c7173428ac 168 uLCD.printf("%i",++points);
gboudreau6 0:56c7173428ac 169 }
gboudreau6 0:56c7173428ac 170
gboudreau6 0:56c7173428ac 171 //This will smooth out the movement of your zoombie turkey
gboudreau6 0:56c7173428ac 172 wait(0.1);
gboudreau6 0:56c7173428ac 173
gboudreau6 0:56c7173428ac 174 //If the turkey has been moved with the joystick then you need
gboudreau6 0:56c7173428ac 175 //to delete its image at the old location. At the beginning of the loop
gboudreau6 0:56c7173428ac 176 //the turkey sprite will be redrawn again!
gboudreau6 0:56c7173428ac 177 if (( buzz1.getXPos() != buzz1.getOLDXPos()) || (buzz1.getYPos() != buzz1.getOLDYPos()))
gboudreau6 0:56c7173428ac 178 uLCD.filled_rectangle(buzz1.getOLDXPos(), buzz1.getOLDYPos(),
gboudreau6 0:56c7173428ac 179 buzz1.getOLDXPos()+TURKEY_WIDTH, buzz1.getOLDYPos()+ TURKEY_HEIGHT, BLACK);
gboudreau6 0:56c7173428ac 180
gboudreau6 0:56c7173428ac 181 }
gboudreau6 0:56c7173428ac 182
gboudreau6 0:56c7173428ac 183 //Print a final farewell to indicate win or loss. Also have a nice closing jingle for
gboudreau6 0:56c7173428ac 184 //your game!
gboudreau6 0:56c7173428ac 185 uLCD.cls();
gboudreau6 0:56c7173428ac 186 uLCD.printf("\n\n\n\n\n Happy \n Thanksgiving \n Break!!");
gboudreau6 0:56c7173428ac 187
gboudreau6 0:56c7173428ac 188
gboudreau6 0:56c7173428ac 189 }