Ben Evans University Second Year Project. Game Called Defender.
Hello, soldier, you have been specially selected as the defender of planet earth.
Your mission, if you choose to accept it. Fly around the planet and pulverise invading alien ships for as long as you can. Stop the aliens abducting the innocent people on the ground. Be warned if an alien ship manages to abduct a person and take them to top of the screen, they will no longer move randomly and will begin to hunt you down. This sounds like a challenge you were trained for.
But don’t worry soldier you’re not going into battle empty-handed. Your ship is equipped with a state of the art laser beam that has unlimited ammo and four smart bombs that will destroy anything on the screen. The ship also has three lives so use them wisely.
As time goes on more alien ships will arrive on planet earth increasing the difficulty of your mission. And remember the landscape bellow loops around so if you continually fly in the same direction you go to your original position. Good luck soldier.
ParentClasses/CheckAlienCollision/CheckAlienCollision.cpp@82:3211b31e9421, 2020-05-25 (annotated)
- Committer:
- evanso
- Date:
- Mon May 25 18:39:51 2020 +0000
- Revision:
- 82:3211b31e9421
- Parent:
- 40:71f947254fda
- Child:
- 85:87bc28b151d8
Made commenting and formatting of code more consistent.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
evanso | 36:27aa597db3d2 | 1 | #include "CheckAlienCollision.h" |
evanso | 36:27aa597db3d2 | 2 | |
evanso | 36:27aa597db3d2 | 3 | bool CheckAlienCollision::check_alien_collision(Alien alien) { |
evanso | 36:27aa597db3d2 | 4 | bool collision = false; |
evanso | 36:27aa597db3d2 | 5 | Vector2D alien_pos = alien.get_pos(); |
evanso | 36:27aa597db3d2 | 6 | |
evanso | 82:3211b31e9421 | 7 | // Checks collision of alien and sprite |
evanso | 82:3211b31e9421 | 8 | if(check_top_right(alien, alien_pos)) { |
evanso | 36:27aa597db3d2 | 9 | collision = true; |
evanso | 82:3211b31e9421 | 10 | }else if(check_top_left(alien, alien_pos)) { |
evanso | 36:27aa597db3d2 | 11 | collision = true; |
evanso | 82:3211b31e9421 | 12 | }else if(check_bottom_left(alien, alien_pos)) { |
evanso | 36:27aa597db3d2 | 13 | collision = true; |
evanso | 82:3211b31e9421 | 14 | }else if(check_bottom_left(alien, alien_pos)) { |
evanso | 36:27aa597db3d2 | 15 | collision = true; |
evanso | 36:27aa597db3d2 | 16 | } |
evanso | 40:71f947254fda | 17 | alien_track = alien.get_track_flag(); |
evanso | 36:27aa597db3d2 | 18 | return collision; |
evanso | 36:27aa597db3d2 | 19 | } |
evanso | 36:27aa597db3d2 | 20 | |
evanso | 82:3211b31e9421 | 21 | bool CheckAlienCollision::check_top_left(Alien alien, Vector2D alien_pos) { |
evanso | 36:27aa597db3d2 | 22 | bool check_collision = false; |
evanso | 36:27aa597db3d2 | 23 | |
evanso | 82:3211b31e9421 | 24 | // Checks collision of top left corner of alien with sprite |
evanso | 36:27aa597db3d2 | 25 | if(alien_pos.x >= position_x_ && |
evanso | 36:27aa597db3d2 | 26 | alien_pos.x <= (position_x_ + sprite_x_length) && |
evanso | 36:27aa597db3d2 | 27 | position_y_ <= alien_pos.y && |
evanso | 82:3211b31e9421 | 28 | alien_pos.y <= (position_y_ + sprite_y_length)) { |
evanso | 36:27aa597db3d2 | 29 | check_collision = true; |
evanso | 36:27aa597db3d2 | 30 | alien_collision_flag = true; |
evanso | 36:27aa597db3d2 | 31 | } |
evanso | 36:27aa597db3d2 | 32 | |
evanso | 36:27aa597db3d2 | 33 | return check_collision; |
evanso | 36:27aa597db3d2 | 34 | } |
evanso | 36:27aa597db3d2 | 35 | |
evanso | 82:3211b31e9421 | 36 | bool CheckAlienCollision::check_top_right(Alien alien, Vector2D alien_pos) { |
evanso | 36:27aa597db3d2 | 37 | bool check_collision = false; |
evanso | 36:27aa597db3d2 | 38 | |
evanso | 82:3211b31e9421 | 39 | // Checks collision of top right corner of alien with sprite |
evanso | 36:27aa597db3d2 | 40 | if((alien_pos.x + 7) >= position_x_ && |
evanso | 36:27aa597db3d2 | 41 | (alien_pos.x + 7) <= (position_x_ + sprite_x_length) && |
evanso | 36:27aa597db3d2 | 42 | position_y_ <= alien_pos.y && |
evanso | 82:3211b31e9421 | 43 | alien_pos.y <= (position_y_ + sprite_y_length)) { |
evanso | 36:27aa597db3d2 | 44 | check_collision = true; |
evanso | 36:27aa597db3d2 | 45 | alien_collision_flag = true; |
evanso | 36:27aa597db3d2 | 46 | } |
evanso | 36:27aa597db3d2 | 47 | |
evanso | 36:27aa597db3d2 | 48 | return check_collision; |
evanso | 36:27aa597db3d2 | 49 | } |
evanso | 36:27aa597db3d2 | 50 | |
evanso | 82:3211b31e9421 | 51 | bool CheckAlienCollision::check_bottom_left(Alien alien, Vector2D alien_pos) { |
evanso | 36:27aa597db3d2 | 52 | bool check_collision = false; |
evanso | 36:27aa597db3d2 | 53 | |
evanso | 82:3211b31e9421 | 54 | // Checks collision of bottom left corner of alien with sprite |
evanso | 36:27aa597db3d2 | 55 | if(alien_pos.x >= position_x_ && |
evanso | 36:27aa597db3d2 | 56 | alien_pos.x <= (position_x_ + sprite_x_length) && |
evanso | 36:27aa597db3d2 | 57 | position_y_ <= (alien_pos.y + 6)&& |
evanso | 82:3211b31e9421 | 58 | (alien_pos.y + 6) <= (position_y_ + sprite_y_length)) { |
evanso | 36:27aa597db3d2 | 59 | check_collision = true; |
evanso | 36:27aa597db3d2 | 60 | alien_collision_flag = true; |
evanso | 36:27aa597db3d2 | 61 | } |
evanso | 36:27aa597db3d2 | 62 | |
evanso | 36:27aa597db3d2 | 63 | return check_collision; |
evanso | 36:27aa597db3d2 | 64 | } |
evanso | 36:27aa597db3d2 | 65 | |
evanso | 82:3211b31e9421 | 66 | bool CheckAlienCollision::check_bottom_right(Alien alien, Vector2D alien_pos) { |
evanso | 36:27aa597db3d2 | 67 | bool check_collision = false; |
evanso | 36:27aa597db3d2 | 68 | |
evanso | 82:3211b31e9421 | 69 | // Checks collision of bottom right corner of alien with sprite |
evanso | 36:27aa597db3d2 | 70 | if((alien_pos.x + 7) >= position_x_ && |
evanso | 36:27aa597db3d2 | 71 | (alien_pos.x + 7) <= (position_x_ + sprite_x_length) && |
evanso | 36:27aa597db3d2 | 72 | position_y_ <= (alien_pos.y + 6) && |
evanso | 82:3211b31e9421 | 73 | (alien_pos.y + 6) <= (position_y_ + sprite_y_length)) { |
evanso | 36:27aa597db3d2 | 74 | check_collision = true; |
evanso | 36:27aa597db3d2 | 75 | alien_collision_flag = true; |
evanso | 36:27aa597db3d2 | 76 | } |
evanso | 36:27aa597db3d2 | 77 | |
evanso | 36:27aa597db3d2 | 78 | return check_collision; |
evanso | 36:27aa597db3d2 | 79 | } |