
Zirui Chen 201235448
Dependencies: mbed
Board/Board.cpp@9:a8420b353bb0, 2020-05-29 (annotated)
- Committer:
- ChenZirui
- Date:
- Fri May 29 03:27:27 2020 +0000
- Revision:
- 9:a8420b353bb0
- Parent:
- 7:f61ac963eb07
not final
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ChenZirui | 5:7207c9b70108 | 1 | #include "Board.h" |
ChenZirui | 7:f61ac963eb07 | 2 | #define LENGTH 84 |
ChenZirui | 5:7207c9b70108 | 3 | |
ChenZirui | 6:b393cfe4e0a7 | 4 | //borad initial function |
ChenZirui | 7:f61ac963eb07 | 5 | void Board::init(int x,int y,int length,int width) |
ChenZirui | 5:7207c9b70108 | 6 | { |
ChenZirui | 7:f61ac963eb07 | 7 | _x=x ; // horizontal coordinate of Board |
ChenZirui | 7:f61ac963eb07 | 8 | _y=y ; // vertical coordinate of board |
ChenZirui | 7:f61ac963eb07 | 9 | _length =length; // length |
ChenZirui | 7:f61ac963eb07 | 10 | _width = width; // width |
ChenZirui | 7:f61ac963eb07 | 11 | _speed = 1; // default speed |
ChenZirui | 7:f61ac963eb07 | 12 | _score = 0; // initi8al socre |
ChenZirui | 5:7207c9b70108 | 13 | |
ChenZirui | 7:f61ac963eb07 | 14 | |
ChenZirui | 7:f61ac963eb07 | 15 | } |
ChenZirui | 7:f61ac963eb07 | 16 | void Board::draw(N5110 &lcd) // use N5110 series function to draw a board |
ChenZirui | 7:f61ac963eb07 | 17 | { |
ChenZirui | 7:f61ac963eb07 | 18 | lcd.drawRect(_x,_y,_length,_width,FILL_BLACK); |
ChenZirui | 7:f61ac963eb07 | 19 | } |
ChenZirui | 7:f61ac963eb07 | 20 | void Board::update(Direction d,float mag) // update position and speed increment information |
ChenZirui | 7:f61ac963eb07 | 21 | { |
ChenZirui | 7:f61ac963eb07 | 22 | _speed = int(mag*10.0f); // speed variable |
ChenZirui | 7:f61ac963eb07 | 23 | |
ChenZirui | 7:f61ac963eb07 | 24 | // eight directions control |
ChenZirui | 5:7207c9b70108 | 25 | if (d == N) |
ChenZirui | 5:7207c9b70108 | 26 | { |
ChenZirui | 7:f61ac963eb07 | 27 | _y-=_speed; // North-up |
ChenZirui | 5:7207c9b70108 | 28 | }else if (d == S) |
ChenZirui | 5:7207c9b70108 | 29 | { |
ChenZirui | 7:f61ac963eb07 | 30 | _y+=_speed; // South-down |
ChenZirui | 7:f61ac963eb07 | 31 | }else if(d == W) |
ChenZirui | 5:7207c9b70108 | 32 | { |
ChenZirui | 7:f61ac963eb07 | 33 | _x-=_speed; // West-left |
ChenZirui | 7:f61ac963eb07 | 34 | }else if(d == E) |
ChenZirui | 5:7207c9b70108 | 35 | { |
ChenZirui | 7:f61ac963eb07 | 36 | _x+=_speed; // East-right |
ChenZirui | 5:7207c9b70108 | 37 | }else if(d == NE) |
ChenZirui | 5:7207c9b70108 | 38 | { |
ChenZirui | 5:7207c9b70108 | 39 | _y+=_speed; |
ChenZirui | 7:f61ac963eb07 | 40 | _x+=_speed; //Northeast-upright |
ChenZirui | 5:7207c9b70108 | 41 | }else if(d == NW) |
ChenZirui | 5:7207c9b70108 | 42 | { |
ChenZirui | 5:7207c9b70108 | 43 | _y+=_speed; |
ChenZirui | 7:f61ac963eb07 | 44 | _x-=_speed; //Northwest-upleft |
ChenZirui | 5:7207c9b70108 | 45 | }else if(d == SE) |
ChenZirui | 5:7207c9b70108 | 46 | { |
ChenZirui | 5:7207c9b70108 | 47 | _y-=_speed; |
ChenZirui | 7:f61ac963eb07 | 48 | _x+=_speed; //Southeast-downright |
ChenZirui | 5:7207c9b70108 | 49 | }else if(d == SW) |
ChenZirui | 5:7207c9b70108 | 50 | { |
ChenZirui | 5:7207c9b70108 | 51 | _y-=_speed; |
ChenZirui | 7:f61ac963eb07 | 52 | _x-=_speed; //Southwest-downleft |
ChenZirui | 5:7207c9b70108 | 53 | } |
ChenZirui | 7:f61ac963eb07 | 54 | // boundary judging and dealing |
ChenZirui | 7:f61ac963eb07 | 55 | |
ChenZirui | 7:f61ac963eb07 | 56 | if (_y <= 24) //up |
ChenZirui | 5:7207c9b70108 | 57 | { |
ChenZirui | 7:f61ac963eb07 | 58 | _y = 24; |
ChenZirui | 5:7207c9b70108 | 59 | } |
ChenZirui | 7:f61ac963eb07 | 60 | if (_y >= HEIGHT - _width - 1) //bottom |
ChenZirui | 7:f61ac963eb07 | 61 | { |
ChenZirui | 7:f61ac963eb07 | 62 | _y = HEIGHT - _width - 1; |
ChenZirui | 7:f61ac963eb07 | 63 | } |
ChenZirui | 7:f61ac963eb07 | 64 | if (_x < 1) //left |
ChenZirui | 5:7207c9b70108 | 65 | { |
ChenZirui | 5:7207c9b70108 | 66 | _x = 1; |
ChenZirui | 5:7207c9b70108 | 67 | } |
ChenZirui | 7:f61ac963eb07 | 68 | if (_x > WIDTH - _length - 1) //right |
ChenZirui | 5:7207c9b70108 | 69 | { |
ChenZirui | 7:f61ac963eb07 | 70 | _x = WIDTH - _length - 1; |
ChenZirui | 5:7207c9b70108 | 71 | } |
ChenZirui | 5:7207c9b70108 | 72 | } |
ChenZirui | 9:a8420b353bb0 | 73 | void Board::add_score() |
ChenZirui | 7:f61ac963eb07 | 74 | { |
ChenZirui | 5:7207c9b70108 | 75 | _score++; |
ChenZirui | 7:f61ac963eb07 | 76 | } |
ChenZirui | 7:f61ac963eb07 | 77 | int Board::get_score() |
ChenZirui | 7:f61ac963eb07 | 78 | { |
ChenZirui | 7:f61ac963eb07 | 79 | return _score; |
ChenZirui | 9:a8420b353bb0 | 80 | } |
ChenZirui | 5:7207c9b70108 | 81 | |
ChenZirui | 7:f61ac963eb07 | 82 | Vector2D Board::get_pos() //position data |
ChenZirui | 7:f61ac963eb07 | 83 | { |
ChenZirui | 7:f61ac963eb07 | 84 | Vector2D p = {_x,_y}; |
ChenZirui | 7:f61ac963eb07 | 85 | return p; |
ChenZirui | 7:f61ac963eb07 | 86 | } |