zhangxinyu01text

Dependencies:   mbed

Committer:
Jenny121
Date:
Mon May 06 06:57:21 2019 +0000
Revision:
18:a7d8b45648f4
Parent:
17:bb67309871c3
Child:
19:0657c7e4d4bf
7time

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 18:a7d8b45648f4 43
Jenny121 12:3952ba0683c7 44
Jenny121 12:3952ba0683c7 45
Jenny121 12:3952ba0683c7 46 // check the y origin to ensure that the CXK doesn't go off screen
Jenny121 12:3952ba0683c7 47 if (_y < 1) {
Jenny121 12:3952ba0683c7 48 _y = 1;
Jenny121 12:3952ba0683c7 49 }
Jenny121 12:3952ba0683c7 50 if (_y > HEIGHT - _height - 1) {
Jenny121 12:3952ba0683c7 51 _y = HEIGHT - _height - 1;
Jenny121 12:3952ba0683c7 52 }
Jenny121 12:3952ba0683c7 53
Jenny121 12:3952ba0683c7 54 if (_x < 1) {
Jenny121 12:3952ba0683c7 55 _x = 1;
Jenny121 12:3952ba0683c7 56 }
Jenny121 12:3952ba0683c7 57 if (_x > WIDTH - _width-3 ) {
Jenny121 12:3952ba0683c7 58 _x = WIDTH - 3;
Jenny121 12:3952ba0683c7 59 }
Jenny121 12:3952ba0683c7 60
Jenny121 12:3952ba0683c7 61 }
Jenny121 12:3952ba0683c7 62
Jenny121 12:3952ba0683c7 63 void CXK::add_score()
Jenny121 12:3952ba0683c7 64 {
Jenny121 12:3952ba0683c7 65 _score++;
Jenny121 12:3952ba0683c7 66 }
Jenny121 12:3952ba0683c7 67 int CXK::get_score()
Jenny121 12:3952ba0683c7 68 {
Jenny121 12:3952ba0683c7 69 return _score;
Jenny121 12:3952ba0683c7 70 }
Jenny121 12:3952ba0683c7 71
Jenny121 12:3952ba0683c7 72 Vector2D CXK::get_pos() {
Jenny121 12:3952ba0683c7 73 Vector2D p = {_x,_y};
Jenny121 12:3952ba0683c7 74 return p;
Jenny121 12:3952ba0683c7 75 }