Joseph Boettcher / ECE4180_Lab4_Sharpshooter

Dependencies:   4DGL-uLCD-SE SDFileSystem mbed-rtos mbed wave_player

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Obstacle.h Source File

Obstacle.h

00001 extern uLCD_4DGL uLCD;
00002 extern Mutex mutex;
00003 extern DigitalOut myled1;
00004 
00005 struct ObstLocation {
00006     int x1, y1, x2, y2;
00007 };
00008 
00009 class Obstacle
00010 {
00011     int x1,x2,y1,y2;
00012     bool direction;
00013 
00014 public:
00015     Obstacle();
00016     Obstacle(int x1, int y1, int x2, int y2);
00017     void drawObstacle();
00018     void eraseObstacle();
00019     void move(int speed);
00020     void changeDirection();// need change direction when hitting wall
00021     void setDirection(bool direction);
00022     ObstLocation getLocation();
00023 };
00024 
00025 Obstacle::Obstacle() {
00026     x1 = 2;
00027     x2 = 50;
00028     y1 = 62;
00029     y2 = 65;
00030     direction = 1;
00031 }
00032 
00033 Obstacle::Obstacle(int a, int b, int c, int d) {
00034     x1 = a;
00035     y1 = b;
00036     x2 = c;
00037     y2 = d;
00038 }
00039 
00040 void Obstacle::drawObstacle() {
00041     mutex.lock();
00042     uLCD.filled_rectangle(x1,y1,x2,y2,DGREY);
00043     mutex.unlock();
00044 
00045 }
00046 
00047 void Obstacle::eraseObstacle() {
00048     mutex.lock();
00049     uLCD.filled_rectangle(x1,y1,x2,y2,BACKGROUND);
00050     mutex.unlock();
00051 
00052 }
00053 
00054 void Obstacle::move(int speed) {
00055     mutex.lock();
00056     Obstacle::eraseObstacle();
00057     if ((x1<=1 && direction==0) || (x2>=127 && direction==1)) {
00058         Obstacle::changeDirection();
00059     }
00060     if (direction==1) {
00061         x1 += speed;
00062         x2 += speed;
00063     }
00064     else {
00065         x1 -= speed;
00066         x2 -= speed;
00067     }
00068     Obstacle::drawObstacle();
00069     mutex.unlock();
00070 }
00071 
00072 void Obstacle::changeDirection() {
00073     direction = !direction;
00074 }
00075 
00076 void Obstacle::setDirection(bool a) {
00077     direction = a;
00078 }
00079 
00080 ObstLocation Obstacle::getLocation() {
00081     ObstLocation coor {x1, y1, x2, y2};
00082     return coor;
00083 }