LAME
Dependencies: mbed 4DGL-uLCD-SE SDFileSystem PinDetect
Diff: main.cpp
- Revision:
- 5:05f7a84d0078
- Parent:
- 4:9a4d22a279b3
diff -r 9a4d22a279b3 -r 05f7a84d0078 main.cpp --- a/main.cpp Thu Jan 23 16:47:05 2014 +0000 +++ b/main.cpp Sun May 01 22:17:57 2022 +0000 @@ -1,141 +1,135 @@ -// skeleton code for ECE 2036 thermostat lab -// code must be added by students #include "mbed.h" #include "TMP36.h" #include "SDFileSystem.h" #include "uLCD_4DGL.h" #include "PinDetect.h" #include "Speaker.h" -// must add your new class code to the project file Shiftbrite.h -#include "Shiftbrite.h" - -// use class to setup temperature sensor pins -TMP36 myTMP36(p15); //Analog in - -// use class to setup microSD card filesystem -SDFileSystem sd(p5, p6, p7, p8, "sd"); +#include "Alien.h" +#include <vector> -// use class to setup the Color LCD -uLCD_4DGL uLCD(p28, p27, p29); // create a global uLCD object +//Initialize button for shooting +PinDetect pb1(p21); -// use class to setup pushbuttons pins -PinDetect pb1(p23); -PinDetect pb2(p24); -PinDetect pb3(p25); - -// use class to setup speaker pin -Speaker mySpeaker(p21); //PWM out - -// use class to setup Shiftbrite pins -Shiftbrite myShiftbrite(p9, p10, p11, p12, p13);// ei li di n/c ci +//Create speaker object +Speaker mySpeaker(p21); -// use class to setup Mbed's four on-board LEDs -DigitalOut myLED1(LED1); -DigitalOut myLED2(LED2); -DigitalOut myLED3(LED3); -DigitalOut myLED4(LED4); - - +using namespace std; -//also setting any unused analog input pins to digital outputs reduces A/D noise a bit -//see http://mbed.org/users/chris/notebook/Getting-best-ADC-performance/ -DigitalOut P16(p16); -DigitalOut P17(p17); -DigitalOut P18(p18); -DigitalOut P19(p19); -DigitalOut P20(p20); - +//Variables +bool addBullet = false; +int bul = 0; +int numAliens = 5; - - -// Global variables used in callbacks and main program -// C variables in interrupt routines should use volatile keyword -int volatile heat_setting=78; // heat to temp -int volatile cool_setting=68; // cool to temp -bool volatile mode=false; // heat or cool mode - -// Callback routine is interrupt activated by a debounced pb1 hit +//Interupt function for the shooting button void pb1_hit_callback (void) { -// ADD CODE HERE -} -// Callback routine is interrupt activated by a debounced pb2 hit -void pb2_hit_callback (void) -{ -// ADD CODE HERE + addBullet = true; } -// Callback routine is interrupt activated by a debounced pb3 hit -void pb3_hit_callback (void) -{ -// ADD CODE HERE -} - int main() { - float Current_temp=0.0; + //Sets up speaker object + Speaker mySpeaker(p25); + //Sets start time of the game + time_t startTime = time(0); - // Use internal pullups for the three pushbuttons + //Set up interrupts pb1.mode(PullUp); - pb2.mode(PullUp); - pb3.mode(PullUp); // Delay for initial pullup to take effect - wait(.01); + wait(0.3); // Setup Interrupt callback functions for a pb hit pb1.attach_deasserted(&pb1_hit_callback); - pb2.attach_deasserted(&pb2_hit_callback); - pb3.attach_deasserted(&pb3_hit_callback); // Start sampling pb inputs using interrupts + wait(0.3); pb1.setSampleFrequency(); - pb2.setSampleFrequency(); - pb3.setSampleFrequency(); // pushbuttons now setup and running - - // start I/O examples - DELETE THIS IN YOUR CODE..BUT WILL USE THESE I/O IDEAS ELSEWHERE - // since all this compiles - the needed *.h files for these are in the project - // - Current_temp = myTMP36; //Read temp sensor - printf("Hello PC World\n\r"); // need terminal application running on PC to see this output - uLCD.printf("\n\rHello LCD World\n\r"); // LCD - mySpeaker.PlayNote(500.0, 1.0, 1.0); // Speaker buzz - myShiftbrite.write( 0, 50 ,0); // Green RGB LED - // SD card write file example - prints error message on PC when running until SD card hooked up - // Delete to avoid run time error - mkdir("/sd/mydir", 0777); // set up directory and permissions - FILE *fp = fopen("/sd/mydir/sdtest.txt", "w"); //open SD - if(fp == NULL) { - error("Could not open file for write\n"); + srand(time(0)); + + vector<ScreenObject *> pieces(7); //Create an STL vector of pointers + + //Initialize aliens randomly from each type in random positions + for (int i = 0; i <= 5; i++) { + int posX = (20 + rand() % 80); + int posY = 12*i+10; + //(new AlienAlice(posX, posY))->draw(); + int type = (rand() % 4 ); + if(type == 0) + pieces[i] = (new AlienAlice(posX, posY)); + else if(type == 1) + pieces[i] = (new AlienBob(posX, posY)); + else if(type == 2) + pieces[i] = (new AlienOne(posX, posY)); + else if(type == 3) + pieces[i] = (new AlienTwo(posX, posY)); + //pieces[i]->draw(); } - fprintf(fp, "Hello SD Card World!"); // write SD - fclose(fp); // close SD card - // - // end I/O examples - - - - - // State machine code below will need changes and additions - while (1) { - { - enum Statetype { Heat_off = 0, Heat_on }; - Statetype state = Heat_off; - while(1) { - switch (state) { - case Heat_off: - myLED4 = 0; - state = Heat_on; - break; - case Heat_on: - myLED4 = 1; - state = Heat_off; - break; + + //Initialize the ship + pieces[6] = (new Ship(50)); + pieces[6]->draw(); + + //Initialize the LCD + pieces[0]->initialize(); + + bool won = false; + int inc = 0; + + //Loop while playing + while(won == false) { + + //Update the alien positions + for (int i = 0; i <= numAliens; i++) { + pieces[i]->clear(); + pieces[i]->update(4); + } + + //Update the ship's position + pieces[numAliens+1]->clear(); + pieces[numAliens+1]->update(0); + + //If a bullet has been trigger and is allowed initialize it + if(addBullet == 1) { + bul = 1; + //addBullet = true; + if(inc == 1) + pieces[numAliens + 2]->clear(); + inc = 1; + pieces[numAliens + 2] = (new Bullet(pieces[numAliens + 1]->getPosX(), 119)); + //pieces[7]->clear(); + //pieces[6]->draw(); + addBullet = 0; + } + + //Check if bullet is in same space as an alien and remove both if that is the case + if(bul == 1) { + pieces[numAliens + 2]->clear(); + pieces[numAliens + 2]->update(0); + for (int i = 0; i < (pieces.size() - 1); i++) { + if((((pieces[numAliens + 2]->getPosX()+15) > pieces[i]->getPosX() && (pieces[numAliens + 2]->getPosX()+10) < (pieces[i]->getPosX() + 12)) && ((pieces[numAliens + 2]->getPosY()-8) < (pieces[i]->getPosY()+12) && (pieces[numAliens + 2]->getPosY()) > (pieces[i]->getPosY())))) { + pieces[numAliens + 2]->clear(); + pieces[i]->clear(); + pieces.erase(pieces.begin() + i); + numAliens -= 1; + bul = 0; + mySpeaker.PlayNote(300.0,0.1,0.025); } - wait(0.33); - // heartbeat LED - common debug tool - // blinks as long as code is running and not locked up - myLED1=!myLED1; + } + + //End game if all the aliens are gone + if(numAliens == -1) { + time_t endTime = time(0)-startTime; + pieces[numAliens + 1]->end(endTime); + won = true; + for(int j = 0; j < 3; j++) { + mySpeaker.PlayNote(300.0,0.1,0.025); + mySpeaker.PlayNote(400.0,0.1,0.025); + } } } } + + //Loop eternally after winning + while(1) { + } } \ No newline at end of file