Zhang Xin yu
/
ZhangXinyu201090208
zhangxinyu01text
CXK/CXK.cpp@12:3952ba0683c7, 2019-05-06 (annotated)
- Committer:
- Jenny121
- Date:
- Mon May 06 06:09:02 2019 +0000
- Revision:
- 12:3952ba0683c7
- Child:
- 16:061919fd3909
zhang xinyu
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Jenny121 | 12:3952ba0683c7 | 1 | #include "CXK.h" |
Jenny121 | 12:3952ba0683c7 | 2 | |
Jenny121 | 12:3952ba0683c7 | 3 | // nothing doing in the constructor and destructor |
Jenny121 | 12:3952ba0683c7 | 4 | CXK::CXK() |
Jenny121 | 12:3952ba0683c7 | 5 | { |
Jenny121 | 12:3952ba0683c7 | 6 | |
Jenny121 | 12:3952ba0683c7 | 7 | } |
Jenny121 | 12:3952ba0683c7 | 8 | |
Jenny121 | 12:3952ba0683c7 | 9 | CXK::~CXK() |
Jenny121 | 12:3952ba0683c7 | 10 | { |
Jenny121 | 12:3952ba0683c7 | 11 | |
Jenny121 | 12:3952ba0683c7 | 12 | } |
Jenny121 | 12:3952ba0683c7 | 13 | |
Jenny121 | 12:3952ba0683c7 | 14 | void CXK::init(int x,int y,int height,int width) |
Jenny121 | 12:3952ba0683c7 | 15 | { |
Jenny121 | 12:3952ba0683c7 | 16 | _x = 67 - width/2; |
Jenny121 | 12:3952ba0683c7 | 17 | _y = 25 - height/2; // y depends on height of screen and height of CXK |
Jenny121 | 12:3952ba0683c7 | 18 | _height = height; |
Jenny121 | 12:3952ba0683c7 | 19 | _width = width; |
Jenny121 | 12:3952ba0683c7 | 20 | _speed = 1; // default speed |
Jenny121 | 12:3952ba0683c7 | 21 | _score = 0; // start score from zero |
Jenny121 | 12:3952ba0683c7 | 22 | |
Jenny121 | 12:3952ba0683c7 | 23 | } |
Jenny121 | 12:3952ba0683c7 | 24 | |
Jenny121 | 12:3952ba0683c7 | 25 | void CXK::draw(N5110 &lcd) |
Jenny121 | 12:3952ba0683c7 | 26 | { |
Jenny121 | 12:3952ba0683c7 | 27 | // draw CXK in screen buffer. |
Jenny121 | 12:3952ba0683c7 | 28 | lcd.drawCircle(_x,_y,3,FILL_TRANSPARENT); |
Jenny121 | 12:3952ba0683c7 | 29 | lcd.drawRect(_x-3,_y-5,3,1,FILL_TRANSPARENT); |
Jenny121 | 12:3952ba0683c7 | 30 | lcd.drawRect(_x+4,_y-5,3,1,FILL_TRANSPARENT); |
Jenny121 | 12:3952ba0683c7 | 31 | lcd.drawRect(_x-1,_y-1,1,1,FILL_TRANSPARENT); |
Jenny121 | 12:3952ba0683c7 | 32 | lcd.drawRect(_x+1,_y-1,1,1,FILL_TRANSPARENT); |
Jenny121 | 12:3952ba0683c7 | 33 | lcd.drawRect(_x,_y+2,4,1,FILL_TRANSPARENT); |
Jenny121 | 12:3952ba0683c7 | 34 | } |
Jenny121 | 12:3952ba0683c7 | 35 | |
Jenny121 | 12:3952ba0683c7 | 36 | |
Jenny121 | 12:3952ba0683c7 | 37 | void CXK::update(Direction d,float vara) |
Jenny121 | 12:3952ba0683c7 | 38 | { |
Jenny121 | 12:3952ba0683c7 | 39 | _speed = int(vara*10.0f); // scale is arbitrary, could be changed in future |
Jenny121 | 12:3952ba0683c7 | 40 | |
Jenny121 | 12:3952ba0683c7 | 41 | // update y value depending on direction of movement |
Jenny121 | 12:3952ba0683c7 | 42 | // North is decrement as origin is at the top-left so decreasing moves up |
Jenny121 | 12:3952ba0683c7 | 43 | if (d == N) { |
Jenny121 | 12:3952ba0683c7 | 44 | _y-=_speed; |
Jenny121 | 12:3952ba0683c7 | 45 | } else if (d == S) { |
Jenny121 | 12:3952ba0683c7 | 46 | _y+=_speed; |
Jenny121 | 12:3952ba0683c7 | 47 | } else if (d == E) { |
Jenny121 | 12:3952ba0683c7 | 48 | _x+=_speed; |
Jenny121 | 12:3952ba0683c7 | 49 | } else if (d == W){ |
Jenny121 | 12:3952ba0683c7 | 50 | _x-=_speed; |
Jenny121 | 12:3952ba0683c7 | 51 | } |
Jenny121 | 12:3952ba0683c7 | 52 | |
Jenny121 | 12:3952ba0683c7 | 53 | |
Jenny121 | 12:3952ba0683c7 | 54 | // check the y origin to ensure that the CXK doesn't go off screen |
Jenny121 | 12:3952ba0683c7 | 55 | if (_y < 1) { |
Jenny121 | 12:3952ba0683c7 | 56 | _y = 1; |
Jenny121 | 12:3952ba0683c7 | 57 | } |
Jenny121 | 12:3952ba0683c7 | 58 | if (_y > HEIGHT - _height - 1) { |
Jenny121 | 12:3952ba0683c7 | 59 | _y = HEIGHT - _height - 1; |
Jenny121 | 12:3952ba0683c7 | 60 | } |
Jenny121 | 12:3952ba0683c7 | 61 | |
Jenny121 | 12:3952ba0683c7 | 62 | if (_x < 1) { |
Jenny121 | 12:3952ba0683c7 | 63 | _x = 1; |
Jenny121 | 12:3952ba0683c7 | 64 | } |
Jenny121 | 12:3952ba0683c7 | 65 | if (_x > WIDTH - _width-3 ) { |
Jenny121 | 12:3952ba0683c7 | 66 | _x = WIDTH - 3; |
Jenny121 | 12:3952ba0683c7 | 67 | } |
Jenny121 | 12:3952ba0683c7 | 68 | |
Jenny121 | 12:3952ba0683c7 | 69 | } |
Jenny121 | 12:3952ba0683c7 | 70 | |
Jenny121 | 12:3952ba0683c7 | 71 | void CXK::add_score() |
Jenny121 | 12:3952ba0683c7 | 72 | { |
Jenny121 | 12:3952ba0683c7 | 73 | _score++; |
Jenny121 | 12:3952ba0683c7 | 74 | } |
Jenny121 | 12:3952ba0683c7 | 75 | int CXK::get_score() |
Jenny121 | 12:3952ba0683c7 | 76 | { |
Jenny121 | 12:3952ba0683c7 | 77 | return _score; |
Jenny121 | 12:3952ba0683c7 | 78 | } |
Jenny121 | 12:3952ba0683c7 | 79 | |
Jenny121 | 12:3952ba0683c7 | 80 | Vector2D CXK::get_pos() { |
Jenny121 | 12:3952ba0683c7 | 81 | Vector2D p = {_x,_y}; |
Jenny121 | 12:3952ba0683c7 | 82 | return p; |
Jenny121 | 12:3952ba0683c7 | 83 | } |