Harry Rance 200925395 Embedded Systems Project

Dependencies:   mbed

Committer:
harryrance
Date:
Wed May 03 16:33:20 2017 +0000
Revision:
7:569f3fc70ac5
Parent:
5:2eb139b24219
Committed with full documentation generated.;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
harryrance 0:c9bf674fe0c7 1 #include "AliensArray.h"
harryrance 0:c9bf674fe0c7 2
harryrance 5:2eb139b24219 3 //constructor
harryrance 0:c9bf674fe0c7 4 AliensArray::AliensArray()
harryrance 0:c9bf674fe0c7 5 {
harryrance 0:c9bf674fe0c7 6
harryrance 0:c9bf674fe0c7 7 }
harryrance 5:2eb139b24219 8 //destructor
harryrance 0:c9bf674fe0c7 9 AliensArray::~AliensArray()
harryrance 0:c9bf674fe0c7 10 {
harryrance 0:c9bf674fe0c7 11
harryrance 0:c9bf674fe0c7 12 }
harryrance 5:2eb139b24219 13 //initialise function - initialises the x and y origin of the array, and the speed of movement for the array.
harryrance 0:c9bf674fe0c7 14 void AliensArray::initialise(int x_origin, int y_origin, int speed)
harryrance 0:c9bf674fe0c7 15 {
harryrance 5:2eb139b24219 16 _x = x_origin; //sets the _x variable equal to the predefined and initialised x_origin variable.
harryrance 5:2eb139b24219 17 _y = y_origin; //sets the _y variable equal to the predefined and initialised y_origin variable.
harryrance 0:c9bf674fe0c7 18
harryrance 5:2eb139b24219 19 int direction = 0; // sets the direction variable equal to 0. Used to change direction of movement of the array.
harryrance 0:c9bf674fe0c7 20
harryrance 0:c9bf674fe0c7 21 if (direction == 0) {
harryrance 5:2eb139b24219 22 _velocity.x = speed * 1.5; // sets the velocity in the x-direction of the array equal to the predefined and initialised speed
harryrance 5:2eb139b24219 23 // multiplied by 1.5.
harryrance 0:c9bf674fe0c7 24 } else if (direction == 1) {
harryrance 5:2eb139b24219 25 _velocity.x = -speed * 1.5; // reverses the direction of movement in the x-direction of the alien array.
harryrance 0:c9bf674fe0c7 26 }
harryrance 0:c9bf674fe0c7 27
harryrance 0:c9bf674fe0c7 28 }
harryrance 5:2eb139b24219 29 //update function - updates the velocity in the x and y direction for the movement o the alien array.
harryrance 0:c9bf674fe0c7 30 void AliensArray::update()
harryrance 0:c9bf674fe0c7 31 {
harryrance 5:2eb139b24219 32 _x += _velocity.x; // sets the _x variable equal to itself, plus the current value of the x velocity.
harryrance 5:2eb139b24219 33 _y += _velocity.y; // sets the _y variable equal to itself, plus the current value of the y velocity.
harryrance 0:c9bf674fe0c7 34 }
harryrance 5:2eb139b24219 35 //draw function - sends the command to the LCD screen to draw sprites on the screen for every render.
harryrance 0:c9bf674fe0c7 36 void AliensArray::draw(N5110 &lcd)
harryrance 0:c9bf674fe0c7 37 {
harryrance 5:2eb139b24219 38 type_1_draw(lcd); //draws the first line of alien sprites on the LCD screen with regards to it's updated variables.
harryrance 5:2eb139b24219 39 type_1_1_draw(lcd); //draws the second line of alien sprites on the LCD screen with regards to it's updated variables.
harryrance 5:2eb139b24219 40 lcd.drawRect (0,0,84,48,0); //prints a rectangle 1 pixel thick around the outside perimeter of the LCD screen to give a border.
harryrance 2:50feb42b982c 41
harryrance 0:c9bf674fe0c7 42 }
harryrance 5:2eb139b24219 43 //line 1 draw function - sets the position of each alien sprite and tells if the alien is active.
harryrance 0:c9bf674fe0c7 44 void AliensArray::type_1_draw(N5110 &lcd)
harryrance 0:c9bf674fe0c7 45 {
harryrance 2:50feb42b982c 46 int add_x = 0;
harryrance 5:2eb139b24219 47 _x_origin = _x/8; //sets a new variable, _x_origin, equal to the predefined _x velocity divided by 8 (sets an appropriate speed).
harryrance 5:2eb139b24219 48 _y_origin = _y; //sets a new variable, _y_origin, equal to the predefined _y velocity.
harryrance 5:2eb139b24219 49
harryrance 5:2eb139b24219 50 /* For loop loops 11 times (equal to the number of alien sprites in a line).
harryrance 5:2eb139b24219 51 * increments the perviously initialised add_x variable by 5, to be used to set the x-position of each sprite.
harryrance 5:2eb139b24219 52 * sets the x-position of the sprite at the current iteration of the for loop equal to the add_x variable.
harryrance 5:2eb139b24219 53 */
harryrance 2:50feb42b982c 54 for (int j = 0; j < 11; j++){
harryrance 2:50feb42b982c 55 add_x +=5;
harryrance 2:50feb42b982c 56 _array_x[0][j] = add_x;
harryrance 0:c9bf674fe0c7 57
harryrance 0:c9bf674fe0c7 58 }
harryrance 2:50feb42b982c 59
harryrance 5:2eb139b24219 60 /* For loop scans through each sprite in the line, as done in the pervious for loop.
harryrance 5:2eb139b24219 61 * creates a new variable named 'ind' that is equal to the value stored at the current x-index at the iretation of the for loop.
harryrance 5:2eb139b24219 62 * checks if the current alien should be alive. If so, set the defined pixels in the shape of the required sprite,
harryrance 5:2eb139b24219 63 * at the correct position for where the alien array has been moved to.
harryrance 5:2eb139b24219 64 */
harryrance 2:50feb42b982c 65 for (int scan_j = 0; scan_j < 11; scan_j++){
harryrance 2:50feb42b982c 66 int ind = _array_x[0][scan_j];
harryrance 2:50feb42b982c 67
harryrance 2:50feb42b982c 68 if (array_active[0][scan_j] == 1){
harryrance 2:50feb42b982c 69 lcd.setPixel((_x_origin+10+ind)-1,(_y_origin+10)-1);
harryrance 2:50feb42b982c 70 lcd.setPixel((_x_origin+10+ind),(_y_origin+10)-1);
harryrance 2:50feb42b982c 71 lcd.setPixel((_x_origin+10+ind),(_y_origin+10)+1);
harryrance 2:50feb42b982c 72 lcd.setPixel((_x_origin+10+ind)-1,(_y_origin+10)+1);
harryrance 2:50feb42b982c 73 lcd.setPixel((_x_origin+10+ind)+1,(_y_origin+10));
harryrance 2:50feb42b982c 74 }
harryrance 2:50feb42b982c 75 }
harryrance 2:50feb42b982c 76
harryrance 0:c9bf674fe0c7 77 }
harryrance 5:2eb139b24219 78 // Alien Array line 2 function - uses same techniques as the previous function, but for the second line of aliens
harryrance 5:2eb139b24219 79 // in the second row of the array.
harryrance 2:50feb42b982c 80 void AliensArray::type_1_1_draw(N5110 &lcd)
harryrance 0:c9bf674fe0c7 81 {
harryrance 2:50feb42b982c 82 int add_x = 0;
harryrance 0:c9bf674fe0c7 83 _x_origin = _x/8;
harryrance 0:c9bf674fe0c7 84 _y_origin = _y;
harryrance 2:50feb42b982c 85 for (int j = 0; j < 11; j++){
harryrance 2:50feb42b982c 86 add_x +=5;
harryrance 2:50feb42b982c 87 _array_x[1][j] = add_x;
harryrance 0:c9bf674fe0c7 88
harryrance 0:c9bf674fe0c7 89 }
harryrance 2:50feb42b982c 90
harryrance 2:50feb42b982c 91 for (int scan_j = 0; scan_j < 11; scan_j++){
harryrance 2:50feb42b982c 92 int ind = _array_x[1][scan_j];
harryrance 2:50feb42b982c 93
harryrance 2:50feb42b982c 94 if (array_active[1][scan_j] == 1){
harryrance 2:50feb42b982c 95 lcd.setPixel((_x_origin+10+ind)-1,(_y_origin+16)-1);
harryrance 2:50feb42b982c 96 lcd.setPixel((_x_origin+10+ind),(_y_origin+16)-1);
harryrance 2:50feb42b982c 97 lcd.setPixel((_x_origin+10+ind),(_y_origin+16)+1);
harryrance 2:50feb42b982c 98 lcd.setPixel((_x_origin+10+ind)-1,(_y_origin+16)+1);
harryrance 2:50feb42b982c 99 lcd.setPixel((_x_origin+10+ind)+1,(_y_origin+16));
harryrance 2:50feb42b982c 100 }
harryrance 2:50feb42b982c 101 }
harryrance 2:50feb42b982c 102
harryrance 0:c9bf674fe0c7 103 }
harryrance 5:2eb139b24219 104 // gets the current velocity of the array.
harryrance 0:c9bf674fe0c7 105 Vector2D AliensArray::get_velocity()
harryrance 0:c9bf674fe0c7 106 {
harryrance 5:2eb139b24219 107 Vector2D v = {_velocity.x,_velocity.y}; //sets the variable v equal to a 2D vector containing the x and y members of the velocity.
harryrance 0:c9bf674fe0c7 108 return v;
harryrance 0:c9bf674fe0c7 109 }
harryrance 5:2eb139b24219 110 // gets the current position of the array.
harryrance 0:c9bf674fe0c7 111 Vector2D AliensArray::get_pos()
harryrance 0:c9bf674fe0c7 112 {
harryrance 5:2eb139b24219 113 Vector2D p = {_x,_y}; //sets the variable p equal to a 2D vector containing the x and y members of the positino.
harryrance 0:c9bf674fe0c7 114 return p;
harryrance 0:c9bf674fe0c7 115 }
harryrance 5:2eb139b24219 116 // sets the velocity of the array with regards to the x and y members of the variable v.
harryrance 0:c9bf674fe0c7 117 void AliensArray::set_velocity(Vector2D v)
harryrance 0:c9bf674fe0c7 118 {
harryrance 0:c9bf674fe0c7 119 _velocity.x = v.x;
harryrance 0:c9bf674fe0c7 120 _velocity.y = v.y;
harryrance 0:c9bf674fe0c7 121 }
harryrance 5:2eb139b24219 122 // sets the position of the array with regards to the x and y members of the variable p.
harryrance 0:c9bf674fe0c7 123 void AliensArray::set_pos(Vector2D p)
harryrance 0:c9bf674fe0c7 124 {
harryrance 0:c9bf674fe0c7 125 _x = p.x;
harryrance 0:c9bf674fe0c7 126 _y = p.y;
harryrance 0:c9bf674fe0c7 127 }
harryrance 0:c9bf674fe0c7 128