ELEC2645 (2018/19) / Mbed 2 deprecated el17ph

Dependencies:   mbed

Committer:
el17ph
Date:
Sat May 11 14:58:43 2019 +0000
Revision:
0:8fb740fa6356
Child:
1:679d7ada8de7
last

Who changed what in which revision?

UserRevisionLine numberNew contents of line
el17ph 0:8fb740fa6356 1 #include "MainTank.h"
el17ph 0:8fb740fa6356 2
el17ph 0:8fb740fa6356 3
el17ph 0:8fb740fa6356 4 /** MainTank Class
el17ph 0:8fb740fa6356 5 * @brief Set and update the value of tank in the game
el17ph 0:8fb740fa6356 6 * @author Pengxi Huang
el17ph 0:8fb740fa6356 7 * @date may,2019
el17ph 0:8fb740fa6356 8 */
el17ph 0:8fb740fa6356 9
el17ph 0:8fb740fa6356 10 /** Constructor */
el17ph 0:8fb740fa6356 11 MainTank::MainTank()
el17ph 0:8fb740fa6356 12 {
el17ph 0:8fb740fa6356 13
el17ph 0:8fb740fa6356 14 }
el17ph 0:8fb740fa6356 15 /** Destructor */
el17ph 0:8fb740fa6356 16 MainTank::~MainTank()
el17ph 0:8fb740fa6356 17 {
el17ph 0:8fb740fa6356 18
el17ph 0:8fb740fa6356 19 }
el17ph 0:8fb740fa6356 20
el17ph 0:8fb740fa6356 21 /** Inital the every value
el17ph 0:8fb740fa6356 22 * @param the all value of the class(int)
el17ph 0:8fb740fa6356 23 */
el17ph 0:8fb740fa6356 24 //init the postion, score and life of the tank
el17ph 0:8fb740fa6356 25 void MainTank::init()
el17ph 0:8fb740fa6356 26 {
el17ph 0:8fb740fa6356 27 m_x = 29; // y is the initial horizontal position of tank
el17ph 0:8fb740fa6356 28 m_y = 40; // y is the fixed height of tank
el17ph 0:8fb740fa6356 29 m_speed = 1; // speed for tank moving
el17ph 0:8fb740fa6356 30 m_score = 0; // start score from zero
el17ph 0:8fb740fa6356 31 m_life = 5;//palyer got 5 lifes
el17ph 0:8fb740fa6356 32
el17ph 0:8fb740fa6356 33 }
el17ph 0:8fb740fa6356 34
el17ph 0:8fb740fa6356 35
el17ph 0:8fb740fa6356 36 //draw the basic shape of tank from it's position
el17ph 0:8fb740fa6356 37 void MainTank::draw(N5110 &lcd)
el17ph 0:8fb740fa6356 38 {
el17ph 0:8fb740fa6356 39 lcd.drawRect(m_x - 2, m_y - 2,5,5, FILL_BLACK);
el17ph 0:8fb740fa6356 40 lcd.drawRect(m_x - 4, m_y - 5,2,11, FILL_BLACK);
el17ph 0:8fb740fa6356 41 lcd.drawRect(m_x + 3, m_y - 5,2,11, FILL_BLACK);
el17ph 0:8fb740fa6356 42 lcd.drawLine(m_x,m_y,m_x,m_y - 6,1);
el17ph 0:8fb740fa6356 43 }
el17ph 0:8fb740fa6356 44
el17ph 0:8fb740fa6356 45 /** Set the new position
el17ph 0:8fb740fa6356 46 * @param the value of the new position(Vector2D)
el17ph 0:8fb740fa6356 47 */
el17ph 0:8fb740fa6356 48 //update the new position of tank base on joystick move
el17ph 0:8fb740fa6356 49 void MainTank::update(Direction d,float mag)
el17ph 0:8fb740fa6356 50 {
el17ph 0:8fb740fa6356 51 m_speed = int(mag*8.0f); // the movemnet of joystick is too small and make it bigger to help tank move
el17ph 0:8fb740fa6356 52
el17ph 0:8fb740fa6356 53 // update x value depending on joystick's direction
el17ph 0:8fb740fa6356 54 // West is decrement as origin and it should move left.
el17ph 0:8fb740fa6356 55 // east is ncrement as origin and it should move right.
el17ph 0:8fb740fa6356 56 if (d == W) {
el17ph 0:8fb740fa6356 57 m_x-=m_speed;
el17ph 0:8fb740fa6356 58 } else if (d == E) {
el17ph 0:8fb740fa6356 59 m_x+=m_speed;
el17ph 0:8fb740fa6356 60 }
el17ph 0:8fb740fa6356 61
el17ph 0:8fb740fa6356 62 // check the x origin to ensure that the tank stay in screen
el17ph 0:8fb740fa6356 63 if (m_x < 5) {
el17ph 0:8fb740fa6356 64 m_x = 5;
el17ph 0:8fb740fa6356 65 }
el17ph 0:8fb740fa6356 66 if (m_x > 54) {
el17ph 0:8fb740fa6356 67 m_x = 54;
el17ph 0:8fb740fa6356 68 }
el17ph 0:8fb740fa6356 69 }
el17ph 0:8fb740fa6356 70
el17ph 0:8fb740fa6356 71 /** Add the score
el17ph 0:8fb740fa6356 72 * @param the value of the score (int)
el17ph 0:8fb740fa6356 73 */
el17ph 0:8fb740fa6356 74 //add 1 to score
el17ph 0:8fb740fa6356 75 void MainTank::add_score()
el17ph 0:8fb740fa6356 76 {
el17ph 0:8fb740fa6356 77 m_score++;
el17ph 0:8fb740fa6356 78 }
el17ph 0:8fb740fa6356 79
el17ph 0:8fb740fa6356 80 /** Get the score
el17ph 0:8fb740fa6356 81 * @return the current score
el17ph 0:8fb740fa6356 82 */
el17ph 0:8fb740fa6356 83 //return current score amount
el17ph 0:8fb740fa6356 84 int MainTank::get_score()
el17ph 0:8fb740fa6356 85 {
el17ph 0:8fb740fa6356 86 return m_score;
el17ph 0:8fb740fa6356 87 }
el17ph 0:8fb740fa6356 88
el17ph 0:8fb740fa6356 89 /** Minus the life
el17ph 0:8fb740fa6356 90 * @param the value of the life (int)
el17ph 0:8fb740fa6356 91 */
el17ph 0:8fb740fa6356 92 //minus 1 to score
el17ph 0:8fb740fa6356 93 void MainTank::lose_life()
el17ph 0:8fb740fa6356 94 {
el17ph 0:8fb740fa6356 95 m_life--;
el17ph 0:8fb740fa6356 96 }
el17ph 0:8fb740fa6356 97
el17ph 0:8fb740fa6356 98 /** Get the remain life
el17ph 0:8fb740fa6356 99 * @return the life amount
el17ph 0:8fb740fa6356 100 */
el17ph 0:8fb740fa6356 101 //return current life amount
el17ph 0:8fb740fa6356 102 int MainTank::remain_life()
el17ph 0:8fb740fa6356 103 {
el17ph 0:8fb740fa6356 104 return m_life;
el17ph 0:8fb740fa6356 105 }
el17ph 0:8fb740fa6356 106
el17ph 0:8fb740fa6356 107 /** Get the position
el17ph 0:8fb740fa6356 108 * @return the current poition in 2d
el17ph 0:8fb740fa6356 109 */
el17ph 0:8fb740fa6356 110 //get the 2d position of tank
el17ph 0:8fb740fa6356 111 Vector2D MainTank::get_pos()
el17ph 0:8fb740fa6356 112 {
el17ph 0:8fb740fa6356 113 Vector2D p = {m_x,m_y};
el17ph 0:8fb740fa6356 114 return p;
el17ph 0:8fb740fa6356 115 }