2222222

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers CXK.cpp Source File

CXK.cpp

00001 #include "CXK.h"
00002 
00003 // nothing doing in the constructor and destructor
00004 CXK::CXK()
00005 {
00006 
00007 }
00008 
00009 CXK::~CXK()
00010 {
00011 
00012 }
00013 
00014 void CXK::init(int x,int y,int height,int width)
00015 {
00016     _x = 67 - width/2;  
00017     _y = 25 - height/2;  // y depends on height of screen and height of CXK
00018     _height = height;
00019     _width = width;
00020     _speed = 1;  // default speed
00021     _score = 0;  // start score from zero
00022 
00023 }
00024 
00025 void CXK::draw(N5110 &lcd)
00026 {
00027     // draw CXK in screen buffer. 
00028   lcd.drawCircle(_x,_y,3,FILL_TRANSPARENT);
00029               lcd.drawRect(_x-3,_y-5,4,1,FILL_TRANSPARENT);
00030             lcd.drawRect(_x+4,_y-5,4,1,FILL_TRANSPARENT);
00031             lcd.drawRect(_x-1,_y-1,1,1,FILL_TRANSPARENT);
00032             lcd.drawRect(_x+1,_y-1,1,1,FILL_TRANSPARENT);
00033             lcd.drawRect(_x,_y+2,4,1,FILL_TRANSPARENT);
00034 }
00035 
00036 
00037 void CXK::update(Direction d,float vara)
00038 {
00039     _speed = int(vara*10.0f);  // scale is arbitrary, could be changed in future
00040 
00041     // update y value depending on direction of movement
00042     // North is decrement as origin is at the top-left so decreasing moves up
00043     if (d == N) {
00044         _y-=_speed;
00045     } else if (d == S) {
00046         _y+=_speed;
00047     } else if (d == E) {
00048         _x+=_speed;
00049     } else if (d == W){
00050         _x-=_speed; 
00051         }
00052 
00053     
00054     // check the y origin to ensure that the CXK doesn't go off screen
00055     if (_y < 1) {
00056         _y = 1;
00057     }
00058     if (_y > HEIGHT - _height - 1) {
00059         _y = HEIGHT - _height - 1;
00060     }
00061     
00062     if (_x < 1) {
00063         _x = 1;
00064     }
00065     if (_x > WIDTH - _width-3 ) {
00066         _x = WIDTH - 3;
00067     }
00068       
00069 }
00070 
00071 void CXK::add_score()
00072 {
00073     _score++;
00074 }
00075 int CXK::get_score()
00076 {
00077     return _score;
00078 }
00079 
00080 Vector2D CXK::get_pos() {
00081     Vector2D p = {_x,_y};
00082     return p;    
00083 }