zhangxinyu01text

Dependencies:   mbed

Committer:
Jenny121
Date:
Mon May 06 06:53:33 2019 +0000
Revision:
16:061919fd3909
Parent:
12:3952ba0683c7
Child:
17:bb67309871c3
the 4 time

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 16:061919fd3909 29
Jenny121 12:3952ba0683c7 30 lcd.drawRect(_x+1,_y-1,1,1,FILL_TRANSPARENT);
Jenny121 12:3952ba0683c7 31 lcd.drawRect(_x,_y+2,4,1,FILL_TRANSPARENT);
Jenny121 12:3952ba0683c7 32 }
Jenny121 12:3952ba0683c7 33
Jenny121 12:3952ba0683c7 34
Jenny121 12:3952ba0683c7 35 void CXK::update(Direction d,float vara)
Jenny121 12:3952ba0683c7 36 {
Jenny121 12:3952ba0683c7 37 _speed = int(vara*10.0f); // scale is arbitrary, could be changed in future
Jenny121 12:3952ba0683c7 38
Jenny121 12:3952ba0683c7 39 // update y value depending on direction of movement
Jenny121 12:3952ba0683c7 40 // North is decrement as origin is at the top-left so decreasing moves up
Jenny121 12:3952ba0683c7 41 if (d == N) {
Jenny121 12:3952ba0683c7 42 _y-=_speed;
Jenny121 12:3952ba0683c7 43 } else if (d == S) {
Jenny121 12:3952ba0683c7 44 _y+=_speed;
Jenny121 12:3952ba0683c7 45 } else if (d == E) {
Jenny121 12:3952ba0683c7 46 _x+=_speed;
Jenny121 12:3952ba0683c7 47 } else if (d == W){
Jenny121 12:3952ba0683c7 48 _x-=_speed;
Jenny121 12:3952ba0683c7 49 }
Jenny121 12:3952ba0683c7 50
Jenny121 12:3952ba0683c7 51
Jenny121 12:3952ba0683c7 52 // check the y origin to ensure that the CXK doesn't go off screen
Jenny121 12:3952ba0683c7 53 if (_y < 1) {
Jenny121 12:3952ba0683c7 54 _y = 1;
Jenny121 12:3952ba0683c7 55 }
Jenny121 12:3952ba0683c7 56 if (_y > HEIGHT - _height - 1) {
Jenny121 12:3952ba0683c7 57 _y = HEIGHT - _height - 1;
Jenny121 12:3952ba0683c7 58 }
Jenny121 12:3952ba0683c7 59
Jenny121 12:3952ba0683c7 60 if (_x < 1) {
Jenny121 12:3952ba0683c7 61 _x = 1;
Jenny121 12:3952ba0683c7 62 }
Jenny121 12:3952ba0683c7 63 if (_x > WIDTH - _width-3 ) {
Jenny121 12:3952ba0683c7 64 _x = WIDTH - 3;
Jenny121 12:3952ba0683c7 65 }
Jenny121 12:3952ba0683c7 66
Jenny121 12:3952ba0683c7 67 }
Jenny121 12:3952ba0683c7 68
Jenny121 12:3952ba0683c7 69 void CXK::add_score()
Jenny121 12:3952ba0683c7 70 {
Jenny121 12:3952ba0683c7 71 _score++;
Jenny121 12:3952ba0683c7 72 }
Jenny121 12:3952ba0683c7 73 int CXK::get_score()
Jenny121 12:3952ba0683c7 74 {
Jenny121 12:3952ba0683c7 75 return _score;
Jenny121 12:3952ba0683c7 76 }
Jenny121 12:3952ba0683c7 77
Jenny121 12:3952ba0683c7 78 Vector2D CXK::get_pos() {
Jenny121 12:3952ba0683c7 79 Vector2D p = {_x,_y};
Jenny121 12:3952ba0683c7 80 return p;
Jenny121 12:3952ba0683c7 81 }