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.
Dependencies: mbed
SpaceInvaderEngine/SpaceInvaderEngine.cpp@14:e88bcf5c0887, 2020-04-03 (annotated)
- Committer:
- josh_ohara
- Date:
- Fri Apr 03 11:19:53 2020 +0000
- Revision:
- 14:e88bcf5c0887
- Parent:
- 12:be491ab6e742
- Child:
- 15:dde4ce4bf7fe
Bullet and aliens now functioning correctly. Collision function completed using get_position method instead of checking if pixels were black (finally). Space invaders must use this method as we want alien bullets to go through aliens without collsion
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
josh_ohara | 7:06a2558155f0 | 1 | |
josh_ohara | 7:06a2558155f0 | 2 | #include "SpaceInvaderEngine.h" |
josh_ohara | 7:06a2558155f0 | 3 | |
josh_ohara | 8:86cb9a9f8a73 | 4 | // N5110 lcd1; |
josh_ohara | 8:86cb9a9f8a73 | 5 | |
josh_ohara | 7:06a2558155f0 | 6 | SpaceInvaderEngine::SpaceInvaderEngine() |
josh_ohara | 7:06a2558155f0 | 7 | { |
josh_ohara | 7:06a2558155f0 | 8 | |
josh_ohara | 7:06a2558155f0 | 9 | } |
josh_ohara | 7:06a2558155f0 | 10 | |
josh_ohara | 8:86cb9a9f8a73 | 11 | SpaceInvaderEngine::~SpaceInvaderEngine() |
josh_ohara | 8:86cb9a9f8a73 | 12 | { |
josh_ohara | 8:86cb9a9f8a73 | 13 | |
josh_ohara | 8:86cb9a9f8a73 | 14 | } |
josh_ohara | 8:86cb9a9f8a73 | 15 | |
josh_ohara | 12:be491ab6e742 | 16 | void SpaceInvaderEngine::init(int ship_height, int ship_width, int alien_size, int no_aliens, int column_size, int row_size) { |
josh_ohara | 7:06a2558155f0 | 17 | |
josh_ohara | 7:06a2558155f0 | 18 | S1_height = ship_height; |
josh_ohara | 7:06a2558155f0 | 19 | S1_width = ship_width; |
josh_ohara | 9:8e695df3cc36 | 20 | A1_size = alien_size; |
josh_ohara | 8:86cb9a9f8a73 | 21 | |
josh_ohara | 11:c174d84e4866 | 22 | Vector2D ship_pos = S1.get_position(); |
josh_ohara | 14:e88bcf5c0887 | 23 | S1x = ship_pos.x; |
josh_ohara | 14:e88bcf5c0887 | 24 | S1y = ship_pos.y; |
josh_ohara | 7:06a2558155f0 | 25 | |
josh_ohara | 12:be491ab6e742 | 26 | N = no_aliens; |
josh_ohara | 12:be491ab6e742 | 27 | CS = column_size; |
josh_ohara | 12:be491ab6e742 | 28 | RS = row_size; |
josh_ohara | 12:be491ab6e742 | 29 | |
josh_ohara | 12:be491ab6e742 | 30 | A1.init(N,A1_size,CS,RS); |
josh_ohara | 7:06a2558155f0 | 31 | S1.init(S1_height,S1_width); |
josh_ohara | 11:c174d84e4866 | 32 | BS1.init(); |
josh_ohara | 8:86cb9a9f8a73 | 33 | |
josh_ohara | 7:06a2558155f0 | 34 | } |
josh_ohara | 7:06a2558155f0 | 35 | |
josh_ohara | 7:06a2558155f0 | 36 | void SpaceInvaderEngine::read_input(Gamepad &pad) |
josh_ohara | 7:06a2558155f0 | 37 | { |
josh_ohara | 7:06a2558155f0 | 38 | D = pad.get_direction(); |
josh_ohara | 7:06a2558155f0 | 39 | Mag = pad.get_mag(); |
josh_ohara | 7:06a2558155f0 | 40 | } |
josh_ohara | 8:86cb9a9f8a73 | 41 | |
josh_ohara | 8:86cb9a9f8a73 | 42 | void SpaceInvaderEngine::render(N5110 &lcd) |
josh_ohara | 8:86cb9a9f8a73 | 43 | { |
josh_ohara | 8:86cb9a9f8a73 | 44 | S1.render(lcd); |
josh_ohara | 9:8e695df3cc36 | 45 | A1.render(lcd); |
josh_ohara | 11:c174d84e4866 | 46 | BS1.render(lcd); |
josh_ohara | 8:86cb9a9f8a73 | 47 | } |
josh_ohara | 8:86cb9a9f8a73 | 48 | |
josh_ohara | 10:9189419fda68 | 49 | void SpaceInvaderEngine::update(Gamepad &pad, N5110 &lcd) |
josh_ohara | 14:e88bcf5c0887 | 50 | { |
josh_ohara | 14:e88bcf5c0887 | 51 | get_ship_pos(); |
josh_ohara | 8:86cb9a9f8a73 | 52 | S1.update(D,Mag); |
josh_ohara | 14:e88bcf5c0887 | 53 | BS1.update(pad, lcd, S1x+4, S1y); |
josh_ohara | 14:e88bcf5c0887 | 54 | A1.update(pad); |
josh_ohara | 14:e88bcf5c0887 | 55 | ship_bullet_alien_collision(pad, lcd); |
josh_ohara | 8:86cb9a9f8a73 | 56 | } |
josh_ohara | 8:86cb9a9f8a73 | 57 | |
josh_ohara | 11:c174d84e4866 | 58 | void SpaceInvaderEngine::get_ship_pos() |
josh_ohara | 11:c174d84e4866 | 59 | { |
josh_ohara | 11:c174d84e4866 | 60 | Vector2D ship_pos = S1.get_position(); |
josh_ohara | 14:e88bcf5c0887 | 61 | S1x = ship_pos.x; |
josh_ohara | 14:e88bcf5c0887 | 62 | S1y = ship_pos.y; |
josh_ohara | 11:c174d84e4866 | 63 | } |
josh_ohara | 14:e88bcf5c0887 | 64 | |
josh_ohara | 14:e88bcf5c0887 | 65 | void SpaceInvaderEngine::ship_bullet_alien_collision(Gamepad &pad, N5110 &lcd){ |
josh_ohara | 14:e88bcf5c0887 | 66 | vector<Alien> alien_vector = A1.get_vector(); |
josh_ohara | 14:e88bcf5c0887 | 67 | vector<Bullet> bullet_vector = BS1.get_vector(); |
josh_ohara | 14:e88bcf5c0887 | 68 | for(int i = 0; i < alien_vector.size(); i++){ |
josh_ohara | 14:e88bcf5c0887 | 69 | Vector2D alien_position = alien_vector[i].get_position(); |
josh_ohara | 14:e88bcf5c0887 | 70 | int alien_x = alien_position.x; |
josh_ohara | 14:e88bcf5c0887 | 71 | int alien_y = alien_position.y; |
josh_ohara | 14:e88bcf5c0887 | 72 | for (int n = 0; n < bullet_vector.size(); n++){ |
josh_ohara | 14:e88bcf5c0887 | 73 | Vector2D bullet_position = bullet_vector[n].get_position(); |
josh_ohara | 14:e88bcf5c0887 | 74 | int bullet_x = bullet_position.x; |
josh_ohara | 14:e88bcf5c0887 | 75 | int bullet_y = bullet_position.y; |
josh_ohara | 14:e88bcf5c0887 | 76 | if((alien_x < bullet_x) && |
josh_ohara | 14:e88bcf5c0887 | 77 | (bullet_x < alien_x + 4)&& |
josh_ohara | 14:e88bcf5c0887 | 78 | (alien_y < bullet_y) && |
josh_ohara | 14:e88bcf5c0887 | 79 | (bullet_y < alien_y + 4)) { |
josh_ohara | 14:e88bcf5c0887 | 80 | bool T = true; |
josh_ohara | 14:e88bcf5c0887 | 81 | bool F = false; |
josh_ohara | 14:e88bcf5c0887 | 82 | A1.set_life(i,F); |
josh_ohara | 14:e88bcf5c0887 | 83 | BS1.set_hit(n,T); |
josh_ohara | 14:e88bcf5c0887 | 84 | } |
josh_ohara | 14:e88bcf5c0887 | 85 | } |
josh_ohara | 14:e88bcf5c0887 | 86 | } |
josh_ohara | 14:e88bcf5c0887 | 87 | } |
josh_ohara | 14:e88bcf5c0887 | 88 |