zhangxinyu01text

Dependencies:   mbed

Committer:
Jenny121
Date:
Mon May 06 09:33:50 2019 +0000
Revision:
24:8b1494a6bdbe
Parent:
20:5efed4030c7b
the final WORK

Who changed what in which revision?

UserRevisionLine numberNew 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 17:bb67309871c3 29 lcd.drawRect(_x-3,_y-5,3,1,FILL_TRANSPARENT);
Jenny121 17:bb67309871c3 30 lcd.drawRect(_x+4,_y-5,3,1,FILL_TRANSPARENT);
Jenny121 17:bb67309871c3 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 19:0657c7e4d4bf 43 if (d == N) {
Jenny121 19:0657c7e4d4bf 44 _y-=_speed;
Jenny121 19:0657c7e4d4bf 45 } else if (d == S) {
Jenny121 19:0657c7e4d4bf 46 _y+=_speed;
Jenny121 19:0657c7e4d4bf 47 } else if (d == E) {
Jenny121 19:0657c7e4d4bf 48 _x+=_speed;
Jenny121 19:0657c7e4d4bf 49 } else if (d == W){
Jenny121 19:0657c7e4d4bf 50 _x-=_speed;
Jenny121 19:0657c7e4d4bf 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 20:5efed4030c7b 80 Vector2D CXK::get_pos() {
Jenny121 20:5efed4030c7b 81 Vector2D p = {_x,_y};
Jenny121 20:5efed4030c7b 82 return p;
Jenny121 20:5efed4030c7b 83 }