Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Map/Map.cpp@40:a1cdb6ab08af, 2019-05-09 (annotated)
- Committer:
- JamesCummins
- Date:
- Thu May 09 10:52:00 2019 +0000
- Revision:
- 40:a1cdb6ab08af
- Parent:
- 38:a85bc227b907
Final Submission. I have read and agreed with Statement of Academic Integrity.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JamesCummins | 28:cc7a93ebd7e7 | 1 | #include "Map.h" |
JamesCummins | 28:cc7a93ebd7e7 | 2 | |
JamesCummins | 28:cc7a93ebd7e7 | 3 | Map::Map(){ |
JamesCummins | 28:cc7a93ebd7e7 | 4 | } |
JamesCummins | 28:cc7a93ebd7e7 | 5 | |
JamesCummins | 28:cc7a93ebd7e7 | 6 | Map::~Map(){ |
JamesCummins | 28:cc7a93ebd7e7 | 7 | } |
JamesCummins | 28:cc7a93ebd7e7 | 8 | |
JamesCummins | 28:cc7a93ebd7e7 | 9 | void Map::init(){ |
JamesCummins | 38:a85bc227b907 | 10 | _coord.x = 47; //initialise the map region to the start area |
JamesCummins | 31:c95f1b1d6423 | 11 | _coord.y = 25; |
JamesCummins | 28:cc7a93ebd7e7 | 12 | } |
JamesCummins | 28:cc7a93ebd7e7 | 13 | |
JamesCummins | 29:42651f87522b | 14 | void Map::read_input(FXOS8700CQ &accelerometer, Ball &ball){ |
JamesCummins | 28:cc7a93ebd7e7 | 15 | Data values = accelerometer.get_values(); |
JamesCummins | 38:a85bc227b907 | 16 | int ball_speed = ball.get_ball_speed(); //get the sensitivity from ball object |
JamesCummins | 40:a1cdb6ab08af | 17 | _map_change.x = -0.25*(1+0.5*ball_speed)*values.ay; //axes of accelerometer different to gamepad so use -y for x and vice versa |
JamesCummins | 40:a1cdb6ab08af | 18 | _map_change.y = -0.25*(1+0.5*ball_speed)*values.ax; |
JamesCummins | 28:cc7a93ebd7e7 | 19 | } |
JamesCummins | 28:cc7a93ebd7e7 | 20 | |
JamesCummins | 28:cc7a93ebd7e7 | 21 | void Map::update(){ |
JamesCummins | 38:a85bc227b907 | 22 | _coord.x += _map_change.x; //map change is the equivalent of velocity in ball.cpp |
JamesCummins | 28:cc7a93ebd7e7 | 23 | _coord.y += _map_change.y; |
JamesCummins | 28:cc7a93ebd7e7 | 24 | if(_coord.x < 0){ _coord.x = 0;} //boundary conditions to stop the |
JamesCummins | 28:cc7a93ebd7e7 | 25 | if(_coord.y < 0){ _coord.y = 0;} //the programme trying to display |
JamesCummins | 29:42651f87522b | 26 | if(_coord.x > 506) {_coord.x = 506;} //undefined region outside the |
JamesCummins | 29:42651f87522b | 27 | if(_coord.y > 176) {_coord.y = 176;} //gamemap array |
JamesCummins | 28:cc7a93ebd7e7 | 28 | } |
JamesCummins | 28:cc7a93ebd7e7 | 29 | |
JamesCummins | 28:cc7a93ebd7e7 | 30 | void Map::draw(N5110 &lcd){ |
JamesCummins | 28:cc7a93ebd7e7 | 31 | bool pixelstate = false; |
JamesCummins | 38:a85bc227b907 | 32 | for(int y = _coord.y; y < (48+_coord.y); y++){ //row by row |
JamesCummins | 38:a85bc227b907 | 33 | for(int x = _coord.x; x < (84+_coord.x); x++){ //get each pixel from the |
JamesCummins | 38:a85bc227b907 | 34 | if(gamemap[y][x] == 1){ pixelstate = true;} //gamemap array |
JamesCummins | 38:a85bc227b907 | 35 | else{ pixelstate = false;} //and set the pixel according to its value in gamemap array |
JamesCummins | 38:a85bc227b907 | 36 | lcd.setPixel((x-_coord.x), (y-_coord.y), pixelstate); //easier to use than draw sprite with only part of a very large sprite |
JamesCummins | 28:cc7a93ebd7e7 | 37 | } |
JamesCummins | 28:cc7a93ebd7e7 | 38 | } |
JamesCummins | 28:cc7a93ebd7e7 | 39 | } |
JamesCummins | 28:cc7a93ebd7e7 | 40 | |
JamesCummins | 28:cc7a93ebd7e7 | 41 | Vector2D Map::get_map_display(){ |
JamesCummins | 38:a85bc227b907 | 42 | Vector2D top_left_coord = _coord; //top left coord used to represent entire screen region |
JamesCummins | 28:cc7a93ebd7e7 | 43 | return top_left_coord; |
JamesCummins | 28:cc7a93ebd7e7 | 44 | } |
JamesCummins | 28:cc7a93ebd7e7 | 45 | |
JamesCummins | 28:cc7a93ebd7e7 | 46 | void Map::set_map_display(Vector2D coord){ |
JamesCummins | 38:a85bc227b907 | 47 | _coord = coord; //change private member variable |
JamesCummins | 29:42651f87522b | 48 | } |
JamesCummins | 29:42651f87522b | 49 | |
JamesCummins | 29:42651f87522b | 50 | bool Map::check_wall_collision(Gamepad &gamepad, Ball &ball){ |
JamesCummins | 29:42651f87522b | 51 | bool collision = false; |
JamesCummins | 38:a85bc227b907 | 52 | Vector2D ball_screen_pos = ball.get_position(); |
JamesCummins | 30:c36a08e8f5de | 53 | Vector2D c; |
JamesCummins | 38:a85bc227b907 | 54 | c.x = ball_screen_pos.x + _coord.x; //c represents the absolute position of the ball's centre |
JamesCummins | 38:a85bc227b907 | 55 | c.y = ball_screen_pos.y + _coord.y; //relative to the game map |
JamesCummins | 29:42651f87522b | 56 | Vector2D ball_pixels[37] = { |
JamesCummins | 38:a85bc227b907 | 57 | {c.x,c.y},{c.x+1,c.y},{c.x+2,c.y},{c.x+3,c.y},{c.x-1,c.y},{c.x-2,c.y}, //this is every pixel in ball |
JamesCummins | 29:42651f87522b | 58 | {c.x-3,c.y},{c.x,c.y+1},{c.x+1,c.y+1},{c.x+2,c.y+1},{c.x+3,c.y+1}, |
JamesCummins | 38:a85bc227b907 | 59 | {c.x-1,c.y+1},{c.x-2,c.y+1},{c.x-3,c.y+1},{c.x,c.y-1},{c.x+1,c.y-1}, //could this be done with |
JamesCummins | 38:a85bc227b907 | 60 | {c.x+2,c.y-1},{c.x+3,c.y-1},{c.x-1,c.y-1},{c.x-2,c.y-1},{c.x-3,c.y-1}, //same algorithm as for |
JamesCummins | 38:a85bc227b907 | 61 | {c.x,c.y+2},{c.x+1,c.y+2},{c.x+2,c.y+2},{c.x-1,c.y+2},{c.x-2,c.y+2}, //lcd.drawCircle? |
JamesCummins | 29:42651f87522b | 62 | {c.x,c.y-2},{c.x+1,c.y-2},{c.x+2,c.y-2},{c.x-1,c.y-2},{c.x-2,c.y-2}, |
JamesCummins | 38:a85bc227b907 | 63 | {c.x,c.y+3},{c.x+1,c.y+3},{c.x-1,c.y+3},{c.x,c.y-3},{c.x+1,c.y-3}, //this is fixed for a radius of 3 |
JamesCummins | 29:42651f87522b | 64 | {c.x-1,c.y-3} }; |
JamesCummins | 29:42651f87522b | 65 | for(int i = 0; i < 37; i++){ |
JamesCummins | 29:42651f87522b | 66 | int y = ball_pixels[i].y; |
JamesCummins | 29:42651f87522b | 67 | int x = ball_pixels[i].x; |
JamesCummins | 38:a85bc227b907 | 68 | if(gamemap[y][x] == 1){ //check each pixel in the ball to see if it is on a '1' in game map (representing wall) |
JamesCummins | 29:42651f87522b | 69 | collision = true; |
JamesCummins | 38:a85bc227b907 | 70 | break; //break with true if wall collision |
JamesCummins | 38:a85bc227b907 | 71 | } else { collision = false; } //keep iterating if not |
JamesCummins | 38:a85bc227b907 | 72 | } |
JamesCummins | 29:42651f87522b | 73 | return collision; |
JamesCummins | 29:42651f87522b | 74 | } |
JamesCummins | 37:de1f584bce71 | 75 | |
JamesCummins | 40:a1cdb6ab08af | 76 | int Map::get_coordinate(Vector2D coord){ |
JamesCummins | 40:a1cdb6ab08af | 77 | int position; |
JamesCummins | 38:a85bc227b907 | 78 | int x = coord.x; |
JamesCummins | 38:a85bc227b907 | 79 | int y = coord.y; |
JamesCummins | 40:a1cdb6ab08af | 80 | if(gamemap[y][x] == 1){ position = 1; } //find the desired coord in the game map |
JamesCummins | 40:a1cdb6ab08af | 81 | else{ position = 0; } //return true if its a wall, false if it's pathway |
JamesCummins | 37:de1f584bce71 | 82 | return position; |
JamesCummins | 37:de1f584bce71 | 83 | } |