![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
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.
Revision 18:11068b98e261, committed 2020-05-01
- Comitter:
- evanso
- Date:
- Fri May 01 20:37:10 2020 +0000
- Parent:
- 17:25d79cca203a
- Child:
- 19:1bc0a2d22054
- Commit message:
- Added functions so if button A is press the spaceship shoots a bullet.
Changed in this revision
--- a/GameEngine/GameEngine.cpp Wed Apr 29 12:35:14 2020 +0000 +++ b/GameEngine/GameEngine.cpp Fri May 01 20:37:10 2020 +0000 @@ -14,6 +14,7 @@ spaceship.init(); map.init(pad); move_map_= 0; + is_bullet_firing_ = false; } void GameEngine::gameplay_loop() { @@ -24,53 +25,31 @@ // Gets movements read_joystick_direction(); spaceship.movement(d_); - weapons.calc_bullets_start_pos(spaceship.get_pos(),spaceship.get_spaceship_sprite_direction()); - // Redraws - spaceship.draw(lcd); - map.draw_map(lcd, calc_map_movement()); - - //refresh's screen - lcd.refresh(); - - //draws map again and refreshes screen so map moves faster than spaceship but doesnt require double pixel moement - map.draw_map(lcd, move_map_); - lcd.refresh(); -} - -int GameEngine::calc_map_movement(){ - // Gets postition of spaceship and sprite direction - Vector2D spaceship_pos= spaceship.get_pos(); - - // moves the map in oposite direction to spaceship when it's position is at min and max x positions and joystick has direction - if (d_ == W || d_ == NW || d_ == SW){ - move_map_ = 1; - - // moves map fater when changing direction to offset ship - if (spaceship_pos.x != 57){ - move_map_ = 1; - } - }else if (d_ == E || d_ == NE || d_ == SE){ - move_map_ = -1; - - // moves map fater when changing direction to offset ship - if (spaceship_pos.x != 14){ - move_map_ = -1; - } - }else { - move_map_ = 0; + // Fires bullet + if (pad.A_pressed()) { + is_bullet_firing_ = true; + //printf("is_bullet_firing_ = %d",is_bullet_firing_); + } + if (is_bullet_firing_){ + Weapons new_bullet; + new_bullet.calc_bullets_start_pos(spaceship.get_pos(), spaceship.get_spaceship_sprite_direction()); + bullet_vector.push_back(new_bullet); + is_bullet_firing_ = false; + } + for (int i = 0; i < bullet_vector.size(); i++){ + bullet_vector[i].draw_bullet(lcd); } - return move_map_; - - // Debug and check variables when function is called - #ifdef CALCULATE_MAP_MOVEMENT_DEBUG - printf("move map = %d\n", move_map_); - printf("direction = %d\n", d_); - printf("x = %d\n", position_x_spaceship_); - #endif + // Draws + spaceship.draw(lcd); + map.draw_map(lcd, d_); + + // refresh's screen + lcd.refresh(); } void GameEngine::read_joystick_direction(){ d_ = pad.get_direction(); -} \ No newline at end of file +} +
--- a/GameEngine/GameEngine.h Wed Apr 29 12:35:14 2020 +0000 +++ b/GameEngine/GameEngine.h Fri May 01 20:37:10 2020 +0000 @@ -8,6 +8,7 @@ #include "Spaceship.h" #include "Map.h" #include "Weapons.h" +#include <vector> /** GameEngine class @@ -30,27 +31,23 @@ /** Main gameplay loop that runs playable part of game */ void gameplay_loop(); - /** Gets joystick direction from gamepad and stores it in d_*/ - void read_joystick_direction(); - // Accessors and mutators ---------------------------------------------- private: // Function prototypes ------------------------------------------------- - - /** Calulates the map movement depeding on spaceship positions and joystick input - * @retrun move_map_ @details move map variable for map draw function - */ - int calc_map_movement(); - + + /** Gets joystick direction from gamepad and stores it in d_*/ + void read_joystick_direction(); // Variables ----------------------------------------------------------- // Changes the drawing x postion of map, 1 moves right, -1 moves left and 0 doesnt change map position int move_map_; + bool is_bullet_firing_; + // Direction of joystick Direction d_; - + // Objects ------------------------------------------------------------- // Gamepad object @@ -66,7 +63,9 @@ Map map; // Weapons object - Weapons weapons; + //Weapons weapons; + + std::vector<Weapons> bullet_vector; }; #endif \ No newline at end of file
--- a/Map/Map.cpp Wed Apr 29 12:35:14 2020 +0000 +++ b/Map/Map.cpp Fri May 01 20:37:10 2020 +0000 @@ -52,7 +52,7 @@ position_x_map_ += line_length; } -void Map::draw_map(N5110 &lcd, int move_map){ +void Map::draw_map(N5110 &lcd, Direction d_){ //usb.printf("position_x_map_ = %d\n", position_x_map_); //usb.printf("move map = %d\n", move_map); @@ -79,7 +79,7 @@ check_duplicates_map_backwards(lcd); // Resets postion of map and moves it - position_x_map_ = reset_position_x_map_to_origonal_ + move_map; + position_x_map_ = reset_position_x_map_to_origonal_ + calc_map_movement(d_); // Moves map to different persition so make it look like its looping if(position_x_map_+ map_length_ < 0){ @@ -117,6 +117,24 @@ } } +int Map::calc_map_movement(Direction d_){ + // moves the map in oposite direction to spaceship when it's position is at min and max x positions and joystick has direction + if (d_ == W || d_ == NW || d_ == SW){ + return 2; + }else if (d_ == E || d_ == NE || d_ == SE){ + return -2; + }else { + return 0; + } + + // Debug and check variables when function is called + #ifdef CALCULATE_MAP_MOVEMENT_DEBUG + printf("move map = %d\n", move_map_); + printf("direction = %d\n", d_); + printf("x = %d\n", position_x_spaceship_); + #endif +} + int Map::get_position_x_map(){ return position_x_map_; }
--- a/Map/Map.h Wed Apr 29 12:35:14 2020 +0000 +++ b/Map/Map.h Fri May 01 20:37:10 2020 +0000 @@ -21,27 +21,21 @@ ~Map(); /** Initalises Spaceship - * @param pad @details : Gampad object - */ + * @param pad @details : Gampad object + */ void init(Gamepad &pad); /** Draws map out of combination of random hight traingle and random - * length lines so map is differnt each new game and loops roudn at ends - * @param lcd, move_map @details : N5110 object and variable to move x postion of map - */ - void draw_map(N5110 &lcd, int move_map); - - /** Gets 11 random integers for random lengths and hights arrays - * and fills arrays with the random integers so same random map is drawn each frame - * @param Pad @details : Gampad adc object used to generate seed - */ - void fill_random_arrays(Gamepad &pad); + * length lines so map is differnt each new game and loops roudn at ends + * @param lcd, d_ @details : N5110 object and Direction variable for move map + */ + void draw_map(N5110 &lcd, Direction d_); // Accessors and mutators ---------------------------------------------- - /** Gets x postion of the map - * @return maps x postion - */ + /** Gets x postion of the map for testing + * @return maps x postion + */ int get_position_x_map(); private: @@ -57,15 +51,26 @@ */ void draw_line(N5110 &lcd, int line_length); - /** Duplicates the first part of the map to fill the gap when the map loops round - * @ param lcd @details : N5110 object - */ + /** Duplicates the first part of the map to fill the gap when the map loops round + * @ param lcd @details : N5110 object + */ void check_duplicates_map_forward(N5110 &lcd); - /** Duplicates the last part of the map to fill the gap when the map loops round - * @ param lcd @details : N5110 object - */ + /** Duplicates the last part of the map to fill the gap when the map loops round + * @ param lcd @details : N5110 object + */ void check_duplicates_map_backwards(N5110 &lcd); + + /** Gets 11 random integers for random lengths and hights arrays + * and fills arrays with the random integers so same random map is drawn each frame + * @param Pad @details : Gampad adc object used to generate seed + */ + void fill_random_arrays(Gamepad &pad); + + /** Calulates the map movement depeding on spaceship positions and joystick input + * @retrun move_map_ @details move map variable for map draw function + */ + int calc_map_movement(Direction d_); // Variables -----------------------------------------------------------
--- a/Map/Map_test.h Wed Apr 29 12:35:14 2020 +0000 +++ b/Map/Map_test.h Fri May 01 20:37:10 2020 +0000 @@ -1,33 +1,30 @@ #ifndef MAP_TEST_H #define MAP_TEST_H -// Objects reqired for test ---------------------------------------------------- -Gamepad pad; -N5110 lcd; -Map map; /** Map Test -@brief Checks that the map moves to the correct x position depedning on map_move input. -Also checks that map can go to required range of x positions. +@brief Checks that the map moves to the correct x position depedning on joystick input @author Benjamin Evans, University of Leeds @date April 2020 @return true if test are passed */ -bool map_move_test(int move_map, int expected_x_position){ +bool map_move_test(Direction d_, int expected_x_position){ // Objects reqired for test ------------------------------------------------ Gamepad pad; Map map; + GameEngine engine; + N5110 lcd; // Initialise map in start postion of -84,42 pad.init(); map.init(pad); - printf("move map = %d : ", move_map); + printf("Espected map x position = %d, : ",expected_x_position); // Draws map but with move variable set so it starts above 0 // As can't rea pixel of the LCD - map.draw_map(lcd, move_map); + map.draw_map(lcd, d_); //Reads start postion of map drawn int map_start_draw_postion = map.get_position_x_map();
--- a/Spaceship/Spaceship_test.h Wed Apr 29 12:35:14 2020 +0000 +++ b/Spaceship/Spaceship_test.h Fri May 01 20:37:10 2020 +0000 @@ -23,15 +23,16 @@ spaceship.movement(d_); //Reads finish spaceship positon - int finish_x_postion = spaceship.get_position_x_spaceship(); - int finish_y_postion = spaceship.get_position_y_spaceship(); + Vector2D finish_postion = spaceship.get_pos(); // Checks final position with espected - if (finish_x_postion == expected_x && finish_y_postion == expected_y) { + if (finish_postion.x == expected_x && finish_postion.y == expected_y) { printf ( "Passed!\n"); return true; } else { - printf ( "Failed! value = %d,%d (expecting %d,%d)\n", finish_x_postion, finish_y_postion, expected_x, expected_y); + int finish_x_postion = finish_postion.x; + int finish_y_postion = finish_postion.y; + printf ( "Failed! value = %d,%d (expecting %d,%d)\n", finish_x_postion, finish_y_postion, expected_x, expected_y); return false; } @@ -62,10 +63,6 @@ // Reads pixel where spaceship is expected to be drawn int actual_pixel_status = lcd.getPixel(expected_postion_x, expected_postion_y); - //Reads finish spaceship positon - int finish_x_postion = spaceship.get_position_x_spaceship(); - int finish_y_postion = spaceship.get_position_y_spaceship(); - // Checks if pixel is drawn and therefor testing it hasnt gone of screen if (actual_pixel_status) { printf ( "Passed!\n");
--- a/Weapons/Weapons.cpp Wed Apr 29 12:35:14 2020 +0000 +++ b/Weapons/Weapons.cpp Fri May 01 20:37:10 2020 +0000 @@ -26,7 +26,17 @@ #endif } -Vector2D Weapons::get_pos(){ +void Weapons::draw_bullet(N5110 &lcd){ + lcd.drawLine(position_x_bullet_, position_y_bullet_, position_x_bullet_+1, position_y_bullet_, 1); + position_x_bullet_ += 3; +} + +Vector2D Weapons::get_pos_one(){ Vector2D pos = {position_x_bullet_,position_y_bullet_}; return pos; +} + +Vector2D Weapons::get_pos_two(){ + Vector2D pos = {position_x_bullet_+1,position_y_bullet_}; + return pos; } \ No newline at end of file
--- a/Weapons/Weapons.h Wed Apr 29 12:35:14 2020 +0000 +++ b/Weapons/Weapons.h Fri May 01 20:37:10 2020 +0000 @@ -22,22 +22,33 @@ /** Initalises Weapons */ void init(); - + /** Calculates bullets start postion * @param spaceship_pos @details x and y postion of spaceship * @param spaceship_sprite_direction_ @details sprite direction bool, true = E, false = W */ void calc_bullets_start_pos(Vector2D spaceship_pos, bool spaceship_sprite_direction_); + /** Draws the bullet and moves it in x direction each frame + * @param lcd @details : N5110 object + */ + void draw_bullet(N5110 &lcd); + // Accessors and mutators ----------------------------------------------- - + /** Gets the xy position of the bullet - * @returns position_x_spaceship_, position_x_spaceship_ + * @returns position_x_bullet_, position_x_bullet_ */ - Vector2D get_pos(); + Vector2D get_pos_one(); + + /** Gets the 2nd xy position of the bullet for colition detection + * @returns position_x_bullet_ + 1, position_x_bullet_ + */ + Vector2D get_pos_two(); private: // Function prototypes ----------------------------------------------------- + // Variables --------------------------------------------------------------- // bullets x position on lcd
--- a/main.cpp Wed Apr 29 12:35:14 2020 +0000 +++ b/main.cpp Fri May 01 20:37:10 2020 +0000 @@ -25,6 +25,7 @@ // TO DO // change comment so nulti line // seperate details comments +// do params on seperate line and have seperate detials for each one // Objects --------------------------------------------------------------------- @@ -71,7 +72,6 @@ } // Time-triggered interrupt to wake MCU from sleep -void lcd_frame_time_isr() -{ +void lcd_frame_time_isr(){ g_lcd_frame_time_flag = 1; // set flag in ISR } \ No newline at end of file
--- a/test.h Wed Apr 29 12:35:14 2020 +0000 +++ b/test.h Fri May 01 20:37:10 2020 +0000 @@ -52,13 +52,18 @@ int passed_counter = 0; // Runs multiple map tests - if (map_move_test(84, 0)) passed_counter++; - if (map_move_test(0, -84)) passed_counter++; - if (map_move_test(150, 66)) passed_counter++; - if (map_move_test(-150, -234)) passed_counter++; + if (map_move_test(CENTRE, -84)) passed_counter++; + if (map_move_test(N, -84)) passed_counter++; + if (map_move_test(NE,-86)) passed_counter++; + if (map_move_test(E, -86)) passed_counter++; + if (map_move_test(SE, -86)) passed_counter++; + if (map_move_test(S, -84)) passed_counter++; + if (map_move_test(SW, -82)) passed_counter++; + if (map_move_test(W, -82)) passed_counter++; + if (map_move_test(NW, -82)) passed_counter++; // prints results - printf ("\nmap_draw_test passed %d tests out of 4\n",passed_counter); + printf ("\nmap_draw_test passed %d tests out of 9\n",passed_counter); } // GameEngine tests ------------------------------------------------------------