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

Dependencies:   mbed

Committer:
davidwst421
Date:
Thu May 09 01:27:58 2019 +0000
Revision:
7:193c0fd7afdd
Parent:
6:a0f3dbbc8d33
Child:
8:97576c8761a8
improve collision factor and add tutorial

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 7:193c0fd7afdd 14 const int player0[6][11] = {
davidwst421 7:193c0fd7afdd 15 { 0,0,0,0,1,1,1,0,0,0,0 },
davidwst421 7:193c0fd7afdd 16 { 0,1,0,0,0,0,1,1,0,1,0 },
davidwst421 7:193c0fd7afdd 17 { 1,1,1,1,1,1,1,1,1,1,1 },
davidwst421 7:193c0fd7afdd 18 { 1,1,1,1,1,1,1,1,1,1,1 },
davidwst421 7:193c0fd7afdd 19 { 0,1,0,0,0,0,1,1,0,1,0 },
davidwst421 7:193c0fd7afdd 20 { 0,0,0,0,1,1,1,0,0,0,0 },
davidwst421 7:193c0fd7afdd 21 };
davidwst421 7:193c0fd7afdd 22
davidwst421 7:193c0fd7afdd 23 void Avenger::init(int x) {
davidwst421 0:fd8eda608206 24
davidwst421 0:fd8eda608206 25 _x = x; // x value on screen is fixed
davidwst421 7:193c0fd7afdd 26 _y = HEIGHT/2 - 3; // y depends on height of screen and height of paddle
davidwst421 0:fd8eda608206 27 _scale = 0;
davidwst421 0:fd8eda608206 28 _score = 1;
davidwst421 0:fd8eda608206 29 }
davidwst421 0:fd8eda608206 30
davidwst421 0:fd8eda608206 31 void Avenger::draw(N5110 &lcd) {
davidwst421 0:fd8eda608206 32 // draw paddle in screen buffer.
davidwst421 7:193c0fd7afdd 33 lcd.drawSprite(_x,_y,6,11,(int *)player0);
davidwst421 0:fd8eda608206 34 }
davidwst421 0:fd8eda608206 35
davidwst421 6:a0f3dbbc8d33 36 void Avenger::update(Direction d,float mag,Vector2D mapped) {
davidwst421 0:fd8eda608206 37
davidwst421 6:a0f3dbbc8d33 38 _y = 24 + int(-mapped.y*23.0f); // scale is arbitrary, could be changed in future
davidwst421 0:fd8eda608206 39
davidwst421 0:fd8eda608206 40 // update y value depending on direction of movement
davidwst421 0:fd8eda608206 41 // North is decrement as origin is at the top-left so decreasing moves up
davidwst421 6:a0f3dbbc8d33 42
davidwst421 0:fd8eda608206 43 // check the y origin to ensure that the paddle doesn't go off screen
davidwst421 0:fd8eda608206 44 if (_y < 1) {
davidwst421 0:fd8eda608206 45 _y = 1;
davidwst421 0:fd8eda608206 46 }
davidwst421 0:fd8eda608206 47 if (_y > HEIGHT - _size - 1) {
davidwst421 0:fd8eda608206 48 _y = HEIGHT - _size - 1;
davidwst421 0:fd8eda608206 49 }
davidwst421 0:fd8eda608206 50 }
davidwst421 0:fd8eda608206 51
davidwst421 0:fd8eda608206 52 void Avenger::add_score() {
davidwst421 0:fd8eda608206 53 _score++;
davidwst421 0:fd8eda608206 54 }
davidwst421 0:fd8eda608206 55
davidwst421 0:fd8eda608206 56 void Avenger::lose_score() {
davidwst421 0:fd8eda608206 57 _score--;
davidwst421 0:fd8eda608206 58 }
davidwst421 0:fd8eda608206 59
davidwst421 0:fd8eda608206 60 int Avenger::get_score() {
davidwst421 0:fd8eda608206 61 return _score;
davidwst421 0:fd8eda608206 62 }
davidwst421 0:fd8eda608206 63
davidwst421 0:fd8eda608206 64 Vector2D Avenger::get_pos() {
davidwst421 0:fd8eda608206 65 Vector2D p = {_x,_y};
davidwst421 0:fd8eda608206 66 return p;
davidwst421 0:fd8eda608206 67 }