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@31:c95f1b1d6423, 2019-04-27 (annotated)
- Committer:
- JamesCummins
- Date:
- Sat Apr 27 18:57:06 2019 +0000
- Revision:
- 31:c95f1b1d6423
- Parent:
- 30:c36a08e8f5de
- Child:
- 34:7e03391cb8a6
Classic: failed menu working.; Girlfriend tried it. Likes it
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 | 31:c95f1b1d6423 | 10 | _coord.x = 47; |
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 | 29:42651f87522b | 16 | int ball_speed = ball.get_ball_speed(); |
JamesCummins | 29:42651f87522b | 17 | _map_change.x = -(1+0.5*ball_speed)*values.ay; |
JamesCummins | 29:42651f87522b | 18 | _map_change.y = -(1+0.5*ball_speed)*values.ax; |
JamesCummins | 28:cc7a93ebd7e7 | 19 | } |
JamesCummins | 28:cc7a93ebd7e7 | 20 | |
JamesCummins | 28:cc7a93ebd7e7 | 21 | void Map::update(){ |
JamesCummins | 28:cc7a93ebd7e7 | 22 | _coord.x += _map_change.x; |
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 | 28:cc7a93ebd7e7 | 32 | for(int y = _coord.y; y < (48+_coord.y); y++){ |
JamesCummins | 28:cc7a93ebd7e7 | 33 | for(int x = _coord.x; x < (84+_coord.x); x++){ |
JamesCummins | 28:cc7a93ebd7e7 | 34 | if(gamemap[y][x] == 1){ pixelstate = true;} |
JamesCummins | 28:cc7a93ebd7e7 | 35 | else{ pixelstate = false;} |
JamesCummins | 28:cc7a93ebd7e7 | 36 | lcd.setPixel((x-_coord.x), (y-_coord.y), pixelstate); |
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 | 28:cc7a93ebd7e7 | 42 | Vector2D top_left_coord = _coord; |
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 | 28:cc7a93ebd7e7 | 47 | _coord = coord; |
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 | 30:c36a08e8f5de | 52 | Vector2D ball_screen_pos = ball.get_position(); |
JamesCummins | 30:c36a08e8f5de | 53 | Vector2D c; |
JamesCummins | 30:c36a08e8f5de | 54 | c.x = ball_screen_pos.x + _coord.x; |
JamesCummins | 30:c36a08e8f5de | 55 | c.y = ball_screen_pos.y + _coord.y; |
JamesCummins | 29:42651f87522b | 56 | Vector2D ball_pixels[37] = { |
JamesCummins | 29:42651f87522b | 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}, |
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 | 29:42651f87522b | 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}, |
JamesCummins | 29:42651f87522b | 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}, |
JamesCummins | 29:42651f87522b | 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}, |
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 | 29:42651f87522b | 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}, |
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 | 29:42651f87522b | 68 | if(gamemap[y][x] == 1){ |
JamesCummins | 29:42651f87522b | 69 | collision = true; |
JamesCummins | 29:42651f87522b | 70 | printf("colliding pixel = %d,%d\n", x, y); |
JamesCummins | 29:42651f87522b | 71 | break; |
JamesCummins | 29:42651f87522b | 72 | } else { collision = false; } |
JamesCummins | 29:42651f87522b | 73 | } |
JamesCummins | 29:42651f87522b | 74 | return collision; |
JamesCummins | 29:42651f87522b | 75 | } |