SharpShooter
Dependencies: 4DGL-uLCD-SE SDFileSystem mbed-rtos mbed wave_player
Diff: Obstacle.h
- Revision:
- 12:2f358065ba3f
- Parent:
- 11:55b65415b6ba
- Child:
- 15:e09ab0d14d4b
--- a/Obstacle.h Fri Oct 28 20:07:38 2016 +0000 +++ b/Obstacle.h Sat Oct 29 00:18:10 2016 +0000 @@ -1,5 +1,10 @@ extern uLCD_4DGL uLCD; +extern Mutex mutex; +extern DigitalOut myled1; +struct ObstLocation { + int x1, y1, x2, y2; +}; class Obstacle { @@ -7,25 +12,52 @@ bool direction; public: - Obstacle(int x1, int x2, int y1, int y2); + Obstacle(); + Obstacle(int x1, int y1, int x2, int y2); void drawObstacle(); + void eraseObstacle(); void move(int speed); void changeDirection();// need change direction when hitting wall void setDirection(bool direction); + ObstLocation getLocation(); }; +Obstacle::Obstacle() { + x1 = 2; + x2 = 50; + y1 = 62; + y2 = 65; + direction = 1; +} + Obstacle::Obstacle(int a, int b, int c, int d) { x1 = a; - x2 = b; - y1 = c; + y1 = b; + x2 = c; y2 = d; } void Obstacle::drawObstacle() { - uLCD.filled_rectangle(x1,y1,x2,y2,0xFFFFFF); + mutex.lock(); + uLCD.filled_rectangle(x1,y1,x2,y2,DGREY); + mutex.unlock(); + +} + +void Obstacle::eraseObstacle() { + mutex.lock(); + uLCD.filled_rectangle(x1,y1,x2,y2,BACKGROUND); + mutex.unlock(); + } void Obstacle::move(int speed) { + mutex.lock(); + Obstacle::eraseObstacle(); + if (x1<=1 || x2>=127) { + Obstacle::changeDirection(); + myled1=!myled1; + } if (direction==1) { x1 += speed; x2 += speed; @@ -34,6 +66,8 @@ x1 -= speed; x2 -= speed; } + Obstacle::drawObstacle(); + mutex.unlock(); } void Obstacle::changeDirection() { @@ -43,3 +77,8 @@ void Obstacle::setDirection(bool a) { direction = a; } + +ObstLocation Obstacle::getLocation() { + ObstLocation coor {x1, y1, x2, y2}; + return coor; +} \ No newline at end of file