Haoyan Zhang / Mbed 2 deprecated el17h2z1

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Battleship.cpp Source File

Battleship.cpp

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