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.
Weapons/Weapons.cpp@82:3211b31e9421, 2020-05-25 (annotated)
- Committer:
- evanso
- Date:
- Mon May 25 18:39:51 2020 +0000
- Revision:
- 82:3211b31e9421
- Parent:
- 36:27aa597db3d2
- 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 | 16:1ee3d3804557 | 1 | #include "Weapons.h" |
evanso | 16:1ee3d3804557 | 2 | |
evanso | 16:1ee3d3804557 | 3 | Weapons::Weapons() { |
evanso | 16:1ee3d3804557 | 4 | |
evanso | 16:1ee3d3804557 | 5 | } |
evanso | 16:1ee3d3804557 | 6 | |
evanso | 16:1ee3d3804557 | 7 | Weapons::~Weapons() { |
evanso | 16:1ee3d3804557 | 8 | |
evanso | 16:1ee3d3804557 | 9 | } |
evanso | 16:1ee3d3804557 | 10 | |
evanso | 28:a5958497d5ce | 11 | void Weapons::init(Vector2D sprite_pos, bool sprite_direction, |
evanso | 28:a5958497d5ce | 12 | bool bullet_type) { |
evanso | 82:3211b31e9421 | 13 | // Bullet for alien or for spacship |
evanso | 28:a5958497d5ce | 14 | set_direction(sprite_direction); |
evanso | 82:3211b31e9421 | 15 | if (bullet_type) { |
evanso | 28:a5958497d5ce | 16 | calc_bullets_start_pos(sprite_pos, sprite_direction); |
evanso | 28:a5958497d5ce | 17 | }else{ |
evanso | 28:a5958497d5ce | 18 | calc_alien_bullets_start_pos(sprite_pos, sprite_direction); |
evanso | 28:a5958497d5ce | 19 | } |
evanso | 28:a5958497d5ce | 20 | bullet_delete_counter_ = 0; |
evanso | 16:1ee3d3804557 | 21 | } |
evanso | 16:1ee3d3804557 | 22 | |
evanso | 82:3211b31e9421 | 23 | void Weapons::smart_bomb(N5110 &lcd) { |
evanso | 82:3211b31e9421 | 24 | // Turn backlight off and on twice |
evanso | 31:6015e8ed859c | 25 | lcd.backLightOff(); |
evanso | 31:6015e8ed859c | 26 | wait(0.02); |
evanso | 31:6015e8ed859c | 27 | lcd.backLightOn(); |
evanso | 31:6015e8ed859c | 28 | wait(0.02); |
evanso | 31:6015e8ed859c | 29 | lcd.backLightOff(); |
evanso | 31:6015e8ed859c | 30 | wait(0.02); |
evanso | 31:6015e8ed859c | 31 | lcd.backLightOn(); |
evanso | 31:6015e8ed859c | 32 | } |
evanso | 31:6015e8ed859c | 33 | |
evanso | 31:6015e8ed859c | 34 | |
evanso | 27:8bb2bd97c319 | 35 | void Weapons::calc_bullets_start_pos(Vector2D spaceship_pos, |
evanso | 82:3211b31e9421 | 36 | bool spaceship_sprite_direction_) { |
evanso | 17:25d79cca203a | 37 | // Sets start position of bullet to spaceships nose |
evanso | 82:3211b31e9421 | 38 | if (spaceship_sprite_direction_) { |
evanso | 27:8bb2bd97c319 | 39 | position_x_ = spaceship_pos.x + 13; |
evanso | 27:8bb2bd97c319 | 40 | position_y_ = spaceship_pos.y + 2; |
evanso | 17:25d79cca203a | 41 | }else{ |
evanso | 27:8bb2bd97c319 | 42 | position_x_ = spaceship_pos.x; |
evanso | 27:8bb2bd97c319 | 43 | position_y_ = spaceship_pos.y + 2; |
evanso | 17:25d79cca203a | 44 | } |
evanso | 17:25d79cca203a | 45 | #ifdef POSITION_BULLET_DEBUG |
evanso | 27:8bb2bd97c319 | 46 | printf("X = %d\n", position_x_); |
evanso | 27:8bb2bd97c319 | 47 | printf("Y = %d\n", position_y_); |
evanso | 17:25d79cca203a | 48 | #endif |
evanso | 17:25d79cca203a | 49 | } |
evanso | 17:25d79cca203a | 50 | |
evanso | 82:3211b31e9421 | 51 | void Weapons::draw_bullet(N5110 &lcd) { |
evanso | 82:3211b31e9421 | 52 | // Draws then moves the bullet position |
evanso | 27:8bb2bd97c319 | 53 | lcd.drawLine(position_x_, position_y_, position_x_+1, position_y_, 1); |
evanso | 82:3211b31e9421 | 54 | if(direction_) { |
evanso | 27:8bb2bd97c319 | 55 | position_x_ += 3; |
evanso | 19:1bc0a2d22054 | 56 | }else{ |
evanso | 27:8bb2bd97c319 | 57 | position_x_ -= 3; |
evanso | 19:1bc0a2d22054 | 58 | } |
evanso | 28:a5958497d5ce | 59 | |
evanso | 82:3211b31e9421 | 60 | // Increments counter |
evanso | 28:a5958497d5ce | 61 | bullet_delete_counter_++; |
evanso | 28:a5958497d5ce | 62 | } |
evanso | 28:a5958497d5ce | 63 | |
evanso | 28:a5958497d5ce | 64 | void Weapons::calc_alien_bullets_start_pos(Vector2D alien_pos, |
evanso | 82:3211b31e9421 | 65 | bool alien_sprite_direction_) { |
evanso | 28:a5958497d5ce | 66 | // Sets start position of bullet to middle of alien |
evanso | 28:a5958497d5ce | 67 | position_x_ = alien_pos.x + 3; |
evanso | 28:a5958497d5ce | 68 | position_y_ = alien_pos.y + 3; |
evanso | 28:a5958497d5ce | 69 | |
evanso | 82:3211b31e9421 | 70 | // Gets a random direction for the bullet to travel in |
evanso | 28:a5958497d5ce | 71 | set_random_move(); |
evanso | 28:a5958497d5ce | 72 | } |
evanso | 28:a5958497d5ce | 73 | |
evanso | 82:3211b31e9421 | 74 | void Weapons::draw_alien_bullet(N5110 &lcd,Direction d_) { |
evanso | 82:3211b31e9421 | 75 | // Draws then moves the bullet position |
evanso | 28:a5958497d5ce | 76 | lcd.drawLine(position_x_, position_y_, position_x_+1, position_y_, 1); |
evanso | 28:a5958497d5ce | 77 | |
evanso | 82:3211b31e9421 | 78 | // Moves bullet in random direction and accomodates for spaceship movement |
evanso | 28:a5958497d5ce | 79 | move_direction(); |
evanso | 34:85ccc16f24d2 | 80 | position_x_ += calc_sprite_movement(d_); |
evanso | 20:febd920ec29e | 81 | |
evanso | 82:3211b31e9421 | 82 | // Increments counter |
evanso | 28:a5958497d5ce | 83 | bullet_delete_counter_++; |
evanso | 28:a5958497d5ce | 84 | } |
evanso | 28:a5958497d5ce | 85 | |
evanso | 82:3211b31e9421 | 86 | void Weapons::set_pos_one(Vector2D pos) { |
evanso | 27:8bb2bd97c319 | 87 | position_x_ = pos.x; |
evanso | 27:8bb2bd97c319 | 88 | position_y_ = pos.y; |
evanso | 22:053c11a202e1 | 89 | } |
evanso | 22:053c11a202e1 | 90 | |
evanso | 82:3211b31e9421 | 91 | void Weapons::set_direction(bool sprite_direction) { |
evanso | 28:a5958497d5ce | 92 | direction_ = sprite_direction; |
evanso | 20:febd920ec29e | 93 | } |
evanso | 20:febd920ec29e | 94 | |
evanso | 82:3211b31e9421 | 95 | int Weapons::get_bullet_delete_counter() { |
evanso | 28:a5958497d5ce | 96 | return bullet_delete_counter_; |
evanso | 20:febd920ec29e | 97 | } |
evanso | 20:febd920ec29e | 98 | |
evanso | 82:3211b31e9421 | 99 | bool Weapons::get_direction() { |
evanso | 27:8bb2bd97c319 | 100 | return direction_; |
evanso | 16:1ee3d3804557 | 101 | } |