ELEC2645 (2019/20) / Mbed 2 deprecated ELEC2645_Project_el18rg

Dependencies:   mbed Gamepad2 ELEC2645_Project_el18rg

Dependents:   ELEC2645_Project_el18rg

Committer:
el18rg
Date:
Fri May 29 21:31:43 2020 +0000
Revision:
22:0b207694a5f7
Parent:
19:bdfab290446a
Final check through

Who changed what in which revision?

UserRevisionLine numberNew contents of line
el18rg 10:b6e45e4acde7 1 #include "Swatter.h"
el18rg 11:93da75c1849d 2
el18rg 15:4ed54ff548cf 3 Swatter::Swatter() {} //Constuctor
el18rg 15:4ed54ff548cf 4 Swatter::~Swatter() {} //Destructor
el18rg 11:93da75c1849d 5
el18rg 15:4ed54ff548cf 6 void Swatter::init(int x,int height,int width) //initilises the swatter parameters
el18rg 10:b6e45e4acde7 7 {
el18rg 22:0b207694a5f7 8 _x = x; //x-axis of swatter
el18rg 22:0b207694a5f7 9 _y = HEIGHT - 10; //y-axis of swatter
el18rg 22:0b207694a5f7 10 _height = height; //height
el18rg 22:0b207694a5f7 11 _width = width; //width
el18rg 22:0b207694a5f7 12 _speed = 1; //speed
el18rg 10:b6e45e4acde7 13 }
el18rg 11:93da75c1849d 14
el18rg 15:4ed54ff548cf 15 void Swatter::draw(N5110 &lcd) //draws the swatter
el18rg 10:b6e45e4acde7 16 {
el18rg 15:4ed54ff548cf 17 const int swat[10][9] = { //The array for the swatter, 1=pixel turn on
el18rg 11:93da75c1849d 18 {1,1,1,1,1,1,1,1,1},
el18rg 11:93da75c1849d 19 {1,0,1,0,1,0,1,0,1},
el18rg 11:93da75c1849d 20 {1,1,0,1,0,1,0,1,1},
el18rg 11:93da75c1849d 21 {1,0,1,0,1,0,1,0,1},
el18rg 11:93da75c1849d 22 {1,1,0,1,0,1,0,1,1},
el18rg 11:93da75c1849d 23 {1,1,1,1,1,1,1,1,1},
el18rg 11:93da75c1849d 24 {0,0,0,0,1,0,0,0,0},
el18rg 11:93da75c1849d 25 {0,0,0,0,1,0,0,0,0},
el18rg 11:93da75c1849d 26 {0,0,0,0,1,0,0,0,0},
el18rg 11:93da75c1849d 27 {0,0,0,0,1,0,0,0,0},
el18rg 10:b6e45e4acde7 28 };
el18rg 15:4ed54ff548cf 29 lcd.drawSprite(_x,_y,10,9,(int *)swat); //draws the swatter
el18rg 10:b6e45e4acde7 30 }
el18rg 15:4ed54ff548cf 31 void Swatter::update(Direction d,float mag) //updates the direction of the swatter from the joystick movement
el18rg 10:b6e45e4acde7 32 {
el18rg 19:bdfab290446a 33 _speed = int(mag*10.0f); //sets the speeds
el18rg 19:bdfab290446a 34 if (d == E) { //if direction is east (right)
el18rg 19:bdfab290446a 35 _x+=_speed; //x-axis moves in positive direction
el18rg 19:bdfab290446a 36 } else if (d == W) { //but if direction is west (left)
el18rg 19:bdfab290446a 37 _x-=_speed; //x-axis moves in negative direction
el18rg 10:b6e45e4acde7 38 }
el18rg 19:bdfab290446a 39 if (_x < 1) { //stops the swatter moving off the left side of the screen
el18rg 10:b6e45e4acde7 40 _x = 1;
el18rg 19:bdfab290446a 41 } //stops the swatter moving off the right side of the screen
el18rg 22:0b207694a5f7 42 if (_x > 75) {
el18rg 22:0b207694a5f7 43 _x = 75;
el18rg 10:b6e45e4acde7 44 }
el18rg 10:b6e45e4acde7 45 }
el18rg 10:b6e45e4acde7 46
el18rg 15:4ed54ff548cf 47 Vector2D Swatter::get_pos() //gets the swatter's actual position
el18rg 11:93da75c1849d 48 {
el18rg 19:bdfab290446a 49 Vector2D p = {_x,_y};
el18rg 11:93da75c1849d 50 return p;
el18rg 10:b6e45e4acde7 51 }