deemo1

Dependencies:   mbed

Committer:
haoyan
Date:
Thu May 14 06:30:39 2020 +0000
Revision:
6:b59bc5e15cf3
Parent:
5:32dbfaf578dd
test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
haoyan 1:8c48fb8ca5e0 1 #include "Battleship.h"
haoyan 1:8c48fb8ca5e0 2
haoyan 1:8c48fb8ca5e0 3 // do nothing in the constructor and destructor
haoyan 1:8c48fb8ca5e0 4
haoyan 1:8c48fb8ca5e0 5 Battleship::Battleship()
haoyan 1:8c48fb8ca5e0 6 {
haoyan 1:8c48fb8ca5e0 7
haoyan 1:8c48fb8ca5e0 8 }
haoyan 1:8c48fb8ca5e0 9
haoyan 1:8c48fb8ca5e0 10 Battleship::~Battleship()
haoyan 1:8c48fb8ca5e0 11 {
haoyan 1:8c48fb8ca5e0 12
haoyan 1:8c48fb8ca5e0 13 }
haoyan 1:8c48fb8ca5e0 14
haoyan 6:b59bc5e15cf3 15 int Battleshipp[4][6] = {
haoyan 6:b59bc5e15cf3 16 {0,0,1,1,0,0},
haoyan 6:b59bc5e15cf3 17 {0,0,1,1,0,0},
haoyan 6:b59bc5e15cf3 18 {1,1,1,1,1,1},
haoyan 6:b59bc5e15cf3 19 {1,1,1,1,1,1},
haoyan 1:8c48fb8ca5e0 20 };
haoyan 1:8c48fb8ca5e0 21
haoyan 1:8c48fb8ca5e0 22 void Battleship::init(int x,int height, int width)
haoyan 1:8c48fb8ca5e0 23 {
haoyan 1:8c48fb8ca5e0 24 _x = WIDTH/2; // x value on screen is fixed
haoyan 1:8c48fb8ca5e0 25 _y = HEIGHT/2 - height/2; // y depends on height of screen and height of Battleship
haoyan 1:8c48fb8ca5e0 26 _height = height;
haoyan 1:8c48fb8ca5e0 27 _width = width;
haoyan 1:8c48fb8ca5e0 28 _speed = 1; // default speed
haoyan 1:8c48fb8ca5e0 29 _score = 0; // start score from zero
haoyan 6:b59bc5e15cf3 30 _life = 3; // set the life
haoyan 1:8c48fb8ca5e0 31 }
haoyan 1:8c48fb8ca5e0 32
haoyan 1:8c48fb8ca5e0 33 void Battleship::draw(N5110 &lcd)
haoyan 1:8c48fb8ca5e0 34 {
haoyan 1:8c48fb8ca5e0 35 // draw a battleship in screen buffer.
haoyan 6:b59bc5e15cf3 36 lcd.drawSprite(_x,_y,_width,_height,(int*)Battleshipp);
haoyan 1:8c48fb8ca5e0 37 }
haoyan 1:8c48fb8ca5e0 38
haoyan 1:8c48fb8ca5e0 39 void Battleship::update(Direction d,float mag)
haoyan 1:8c48fb8ca5e0 40 {
haoyan 1:8c48fb8ca5e0 41 _speed = int(mag*10.0f); // scale is arbitrary, could be changed in future
haoyan 1:8c48fb8ca5e0 42
haoyan 1:8c48fb8ca5e0 43 // update x and y values depending on direction of movement
haoyan 1:8c48fb8ca5e0 44 // West is decrement as origin is at the top-left so decreasing moves up
haoyan 1:8c48fb8ca5e0 45
haoyan 1:8c48fb8ca5e0 46 if (d == W) {
haoyan 1:8c48fb8ca5e0 47 _x-=_speed;
haoyan 1:8c48fb8ca5e0 48 }
haoyan 1:8c48fb8ca5e0 49 if (d == E) {
haoyan 1:8c48fb8ca5e0 50 _x+=_speed;
haoyan 1:8c48fb8ca5e0 51 }
haoyan 1:8c48fb8ca5e0 52 // North is decrement as origin is at the top-left so decreasing moves up
haoyan 1:8c48fb8ca5e0 53
haoyan 1:8c48fb8ca5e0 54 if (d == N) {
haoyan 1:8c48fb8ca5e0 55 _y-=_speed;
haoyan 1:8c48fb8ca5e0 56 }
haoyan 1:8c48fb8ca5e0 57 else if (d == S) {
haoyan 1:8c48fb8ca5e0 58 _y+=_speed;
haoyan 1:8c48fb8ca5e0 59 }
haoyan 1:8c48fb8ca5e0 60
haoyan 1:8c48fb8ca5e0 61 // check the x and y origin to ensure that the paddle doesn't go off screen
haoyan 1:8c48fb8ca5e0 62 if (_x < 1) {
haoyan 1:8c48fb8ca5e0 63 _x = 1;
haoyan 1:8c48fb8ca5e0 64 }
haoyan 1:8c48fb8ca5e0 65 if (_x > WIDTH - _width - 1) {
haoyan 1:8c48fb8ca5e0 66 _x = WIDTH - _width - 1;
haoyan 1:8c48fb8ca5e0 67 }
haoyan 1:8c48fb8ca5e0 68
haoyan 1:8c48fb8ca5e0 69 if (_y < 1) {
haoyan 1:8c48fb8ca5e0 70 _y = 1;
haoyan 1:8c48fb8ca5e0 71 }
haoyan 1:8c48fb8ca5e0 72 if (_y > HEIGHT - _height - 1) {
haoyan 1:8c48fb8ca5e0 73 _y = HEIGHT - _height - 1;
haoyan 1:8c48fb8ca5e0 74 }
haoyan 1:8c48fb8ca5e0 75 }
haoyan 1:8c48fb8ca5e0 76
haoyan 1:8c48fb8ca5e0 77 void Battleship::add_score()
haoyan 1:8c48fb8ca5e0 78 {
haoyan 1:8c48fb8ca5e0 79 _score = _score + 1;
haoyan 1:8c48fb8ca5e0 80 }
haoyan 1:8c48fb8ca5e0 81
haoyan 1:8c48fb8ca5e0 82 int Battleship::get_score()
haoyan 1:8c48fb8ca5e0 83 {
haoyan 1:8c48fb8ca5e0 84 return _score;
haoyan 1:8c48fb8ca5e0 85 }
haoyan 1:8c48fb8ca5e0 86
haoyan 1:8c48fb8ca5e0 87 Vector2D Battleship::get_pos() {
haoyan 1:8c48fb8ca5e0 88 Vector2D p = {_x,_y};
haoyan 1:8c48fb8ca5e0 89 return p;
haoyan 1:8c48fb8ca5e0 90 }
haoyan 1:8c48fb8ca5e0 91
haoyan 1:8c48fb8ca5e0 92 void Battleship::minus_life()
haoyan 1:8c48fb8ca5e0 93 {
haoyan 1:8c48fb8ca5e0 94 _life = _life - 1;
haoyan 1:8c48fb8ca5e0 95 }
haoyan 1:8c48fb8ca5e0 96
haoyan 1:8c48fb8ca5e0 97 int Battleship::get_life()
haoyan 1:8c48fb8ca5e0 98 {
haoyan 1:8c48fb8ca5e0 99 return _life;
haoyan 1:8c48fb8ca5e0 100 }