SharpShooter
Dependencies: 4DGL-uLCD-SE SDFileSystem mbed-rtos mbed wave_player
Obstacle.h@15:e09ab0d14d4b, 2016-10-31 (annotated)
- Committer:
- jboettcher
- Date:
- Mon Oct 31 18:15:21 2016 +0000
- Revision:
- 15:e09ab0d14d4b
- Parent:
- 12:2f358065ba3f
Finished
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jboettcher | 11:55b65415b6ba | 1 | extern uLCD_4DGL uLCD; |
jboettcher | 12:2f358065ba3f | 2 | extern Mutex mutex; |
jboettcher | 12:2f358065ba3f | 3 | extern DigitalOut myled1; |
jboettcher | 11:55b65415b6ba | 4 | |
jboettcher | 12:2f358065ba3f | 5 | struct ObstLocation { |
jboettcher | 12:2f358065ba3f | 6 | int x1, y1, x2, y2; |
jboettcher | 12:2f358065ba3f | 7 | }; |
jboettcher | 11:55b65415b6ba | 8 | |
jboettcher | 1:8a3fa9e90572 | 9 | class Obstacle |
jboettcher | 1:8a3fa9e90572 | 10 | { |
jboettcher | 5:f51cdddf541e | 11 | int x1,x2,y1,y2; |
jboettcher | 5:f51cdddf541e | 12 | bool direction; |
jboettcher | 5:f51cdddf541e | 13 | |
jboettcher | 5:f51cdddf541e | 14 | public: |
jboettcher | 12:2f358065ba3f | 15 | Obstacle(); |
jboettcher | 12:2f358065ba3f | 16 | Obstacle(int x1, int y1, int x2, int y2); |
jboettcher | 5:f51cdddf541e | 17 | void drawObstacle(); |
jboettcher | 12:2f358065ba3f | 18 | void eraseObstacle(); |
jboettcher | 5:f51cdddf541e | 19 | void move(int speed); |
jboettcher | 5:f51cdddf541e | 20 | void changeDirection();// need change direction when hitting wall |
jboettcher | 5:f51cdddf541e | 21 | void setDirection(bool direction); |
jboettcher | 12:2f358065ba3f | 22 | ObstLocation getLocation(); |
jboettcher | 5:f51cdddf541e | 23 | }; |
jboettcher | 5:f51cdddf541e | 24 | |
jboettcher | 12:2f358065ba3f | 25 | Obstacle::Obstacle() { |
jboettcher | 12:2f358065ba3f | 26 | x1 = 2; |
jboettcher | 12:2f358065ba3f | 27 | x2 = 50; |
jboettcher | 12:2f358065ba3f | 28 | y1 = 62; |
jboettcher | 12:2f358065ba3f | 29 | y2 = 65; |
jboettcher | 12:2f358065ba3f | 30 | direction = 1; |
jboettcher | 12:2f358065ba3f | 31 | } |
jboettcher | 12:2f358065ba3f | 32 | |
jboettcher | 11:55b65415b6ba | 33 | Obstacle::Obstacle(int a, int b, int c, int d) { |
jboettcher | 5:f51cdddf541e | 34 | x1 = a; |
jboettcher | 12:2f358065ba3f | 35 | y1 = b; |
jboettcher | 12:2f358065ba3f | 36 | x2 = c; |
jboettcher | 5:f51cdddf541e | 37 | y2 = d; |
jboettcher | 5:f51cdddf541e | 38 | } |
jboettcher | 5:f51cdddf541e | 39 | |
jboettcher | 5:f51cdddf541e | 40 | void Obstacle::drawObstacle() { |
jboettcher | 12:2f358065ba3f | 41 | mutex.lock(); |
jboettcher | 12:2f358065ba3f | 42 | uLCD.filled_rectangle(x1,y1,x2,y2,DGREY); |
jboettcher | 12:2f358065ba3f | 43 | mutex.unlock(); |
jboettcher | 12:2f358065ba3f | 44 | |
jboettcher | 12:2f358065ba3f | 45 | } |
jboettcher | 12:2f358065ba3f | 46 | |
jboettcher | 12:2f358065ba3f | 47 | void Obstacle::eraseObstacle() { |
jboettcher | 12:2f358065ba3f | 48 | mutex.lock(); |
jboettcher | 12:2f358065ba3f | 49 | uLCD.filled_rectangle(x1,y1,x2,y2,BACKGROUND); |
jboettcher | 12:2f358065ba3f | 50 | mutex.unlock(); |
jboettcher | 12:2f358065ba3f | 51 | |
jboettcher | 5:f51cdddf541e | 52 | } |
jboettcher | 5:f51cdddf541e | 53 | |
jboettcher | 5:f51cdddf541e | 54 | void Obstacle::move(int speed) { |
jboettcher | 12:2f358065ba3f | 55 | mutex.lock(); |
jboettcher | 12:2f358065ba3f | 56 | Obstacle::eraseObstacle(); |
jboettcher | 15:e09ab0d14d4b | 57 | if ((x1<=1 && direction==0) || (x2>=127 && direction==1)) { |
jboettcher | 12:2f358065ba3f | 58 | Obstacle::changeDirection(); |
jboettcher | 12:2f358065ba3f | 59 | } |
jboettcher | 5:f51cdddf541e | 60 | if (direction==1) { |
jboettcher | 5:f51cdddf541e | 61 | x1 += speed; |
jboettcher | 5:f51cdddf541e | 62 | x2 += speed; |
jboettcher | 5:f51cdddf541e | 63 | } |
jboettcher | 5:f51cdddf541e | 64 | else { |
jboettcher | 5:f51cdddf541e | 65 | x1 -= speed; |
jboettcher | 5:f51cdddf541e | 66 | x2 -= speed; |
jboettcher | 5:f51cdddf541e | 67 | } |
jboettcher | 12:2f358065ba3f | 68 | Obstacle::drawObstacle(); |
jboettcher | 12:2f358065ba3f | 69 | mutex.unlock(); |
jboettcher | 5:f51cdddf541e | 70 | } |
jboettcher | 5:f51cdddf541e | 71 | |
jboettcher | 5:f51cdddf541e | 72 | void Obstacle::changeDirection() { |
jboettcher | 11:55b65415b6ba | 73 | direction = !direction; |
jboettcher | 5:f51cdddf541e | 74 | } |
jboettcher | 5:f51cdddf541e | 75 | |
jboettcher | 5:f51cdddf541e | 76 | void Obstacle::setDirection(bool a) { |
jboettcher | 5:f51cdddf541e | 77 | direction = a; |
SeanBuckingham | 8:56a24df93680 | 78 | } |
jboettcher | 12:2f358065ba3f | 79 | |
jboettcher | 12:2f358065ba3f | 80 | ObstLocation Obstacle::getLocation() { |
jboettcher | 12:2f358065ba3f | 81 | ObstLocation coor {x1, y1, x2, y2}; |
jboettcher | 12:2f358065ba3f | 82 | return coor; |
jboettcher | 12:2f358065ba3f | 83 | } |