ELEC2645 (2018/19) / Mbed 2 deprecated 2645_Project_SiutingWong201186503

Dependencies:   mbed

Committer:
davidwst421
Date:
Wed May 08 18:50:21 2019 +0000
Revision:
0:fd8eda608206
Child:
4:fcd80b40f257
12

Who changed what in which revision?

UserRevisionLine numberNew contents of line
davidwst421 0:fd8eda608206 1 #include "Avenger.h"
davidwst421 0:fd8eda608206 2
davidwst421 0:fd8eda608206 3 // nothing doing in the constructor and destructor
davidwst421 0:fd8eda608206 4 Avenger::Avenger()
davidwst421 0:fd8eda608206 5 {
davidwst421 0:fd8eda608206 6
davidwst421 0:fd8eda608206 7 }
davidwst421 0:fd8eda608206 8
davidwst421 0:fd8eda608206 9 Avenger::~Avenger()
davidwst421 0:fd8eda608206 10 {
davidwst421 0:fd8eda608206 11
davidwst421 0:fd8eda608206 12 }
davidwst421 0:fd8eda608206 13
davidwst421 0:fd8eda608206 14 void Avenger::init(int x,int size) {
davidwst421 0:fd8eda608206 15
davidwst421 0:fd8eda608206 16 _x = x; // x value on screen is fixed
davidwst421 0:fd8eda608206 17 _y = HEIGHT/2 - size/2; // y depends on height of screen and height of paddle
davidwst421 0:fd8eda608206 18 _size = size;
davidwst421 0:fd8eda608206 19 _scale = 0;
davidwst421 0:fd8eda608206 20 _score = 1;
davidwst421 0:fd8eda608206 21 }
davidwst421 0:fd8eda608206 22
davidwst421 0:fd8eda608206 23 void Avenger::draw(N5110 &lcd) {
davidwst421 0:fd8eda608206 24
davidwst421 0:fd8eda608206 25 // draw paddle in screen buffer.
davidwst421 0:fd8eda608206 26 lcd.drawRect(_x,_y,_size,_size,FILL_BLACK);
davidwst421 0:fd8eda608206 27 }
davidwst421 0:fd8eda608206 28
davidwst421 0:fd8eda608206 29 void Avenger::update(Direction d,float mag) {
davidwst421 0:fd8eda608206 30
davidwst421 0:fd8eda608206 31 _scale = int(mag*23.0f); // scale is arbitrary, could be changed in future
davidwst421 0:fd8eda608206 32
davidwst421 0:fd8eda608206 33 // update y value depending on direction of movement
davidwst421 0:fd8eda608206 34 // North is decrement as origin is at the top-left so decreasing moves up
davidwst421 0:fd8eda608206 35 if (d == N || d == NE || d == NW) {
davidwst421 0:fd8eda608206 36 _y = 23 - _scale;
davidwst421 0:fd8eda608206 37 } else if (d == S || d == SE || d == SW) {
davidwst421 0:fd8eda608206 38 _y = 23 + _scale;
davidwst421 0:fd8eda608206 39 }
davidwst421 0:fd8eda608206 40
davidwst421 0:fd8eda608206 41 // check the y origin to ensure that the paddle doesn't go off screen
davidwst421 0:fd8eda608206 42 if (_y < 1) {
davidwst421 0:fd8eda608206 43 _y = 1;
davidwst421 0:fd8eda608206 44 }
davidwst421 0:fd8eda608206 45 if (_y > HEIGHT - _size - 1) {
davidwst421 0:fd8eda608206 46 _y = HEIGHT - _size - 1;
davidwst421 0:fd8eda608206 47 }
davidwst421 0:fd8eda608206 48 }
davidwst421 0:fd8eda608206 49
davidwst421 0:fd8eda608206 50 void Avenger::add_score() {
davidwst421 0:fd8eda608206 51 _score++;
davidwst421 0:fd8eda608206 52 }
davidwst421 0:fd8eda608206 53
davidwst421 0:fd8eda608206 54 void Avenger::lose_score() {
davidwst421 0:fd8eda608206 55 _score--;
davidwst421 0:fd8eda608206 56 }
davidwst421 0:fd8eda608206 57
davidwst421 0:fd8eda608206 58 int Avenger::get_score() {
davidwst421 0:fd8eda608206 59 return _score;
davidwst421 0:fd8eda608206 60 }
davidwst421 0:fd8eda608206 61
davidwst421 0:fd8eda608206 62 Vector2D Avenger::get_pos() {
davidwst421 0:fd8eda608206 63 Vector2D p = {_x,_y};
davidwst421 0:fd8eda608206 64 return p;
davidwst421 0:fd8eda608206 65 }