deemo1

Dependencies:   mbed

Committer:
haoyan
Date:
Thu May 14 05:21:50 2020 +0000
Revision:
5:32dbfaf578dd
Parent:
1:8c48fb8ca5e0
Child:
6:b59bc5e15cf3
final

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