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.
SavedGames/SavedGames.cpp@87:832ca78426b5, 2020-05-27 (annotated)
- Committer:
- evanso
- Date:
- Wed May 27 02:06:05 2020 +0000
- Revision:
- 87:832ca78426b5
- Parent:
- 85:87bc28b151d8
Final Submission. I have read and agreed with Statement of Academic Integrity.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
evanso | 49:ed569eceeaa4 | 1 | #include "SavedGames.h" |
evanso | 49:ed569eceeaa4 | 2 | |
evanso | 49:ed569eceeaa4 | 3 | SavedGames::SavedGames() { |
evanso | 49:ed569eceeaa4 | 4 | |
evanso | 49:ed569eceeaa4 | 5 | } |
evanso | 49:ed569eceeaa4 | 6 | |
evanso | 49:ed569eceeaa4 | 7 | SavedGames::~SavedGames() { |
evanso | 49:ed569eceeaa4 | 8 | |
evanso | 49:ed569eceeaa4 | 9 | } |
evanso | 49:ed569eceeaa4 | 10 | |
evanso | 82:3211b31e9421 | 11 | bool SavedGames::save_test(Direction d_,SDFileSystem &sd, N5110 &lcd) { |
evanso | 67:a2984682d641 | 12 | |
evanso | 85:87bc28b151d8 | 13 | // Objects required for test |
evanso | 67:a2984682d641 | 14 | |
evanso | 67:a2984682d641 | 15 | printf("Data save = %d : ",d_); |
evanso | 67:a2984682d641 | 16 | |
evanso | 82:3211b31e9421 | 17 | // Define input data object and set values |
evanso | 67:a2984682d641 | 18 | SavedGamesData input_data; |
evanso | 67:a2984682d641 | 19 | input_data.score = 0; |
evanso | 67:a2984682d641 | 20 | input_data.lives = 3; |
evanso | 67:a2984682d641 | 21 | input_data.smart_bombs = 4; |
evanso | 67:a2984682d641 | 22 | input_data.alien_number = 5; |
evanso | 67:a2984682d641 | 23 | |
evanso | 82:3211b31e9421 | 24 | // Change the save file |
evanso | 67:a2984682d641 | 25 | saved_games_scroll(d_); |
evanso | 67:a2984682d641 | 26 | |
evanso | 67:a2984682d641 | 27 | add_saved_data(sd, input_data, lcd); |
evanso | 67:a2984682d641 | 28 | |
evanso | 85:87bc28b151d8 | 29 | // Define output data object |
evanso | 67:a2984682d641 | 30 | SavedGamesData output_data; |
evanso | 67:a2984682d641 | 31 | |
evanso | 67:a2984682d641 | 32 | output_data = read_saved_data(sd, lcd); |
evanso | 67:a2984682d641 | 33 | |
evanso | 67:a2984682d641 | 34 | // Checks if the input and output are equal |
evanso | 67:a2984682d641 | 35 | if (output_data.score == input_data.score && |
evanso | 67:a2984682d641 | 36 | output_data.lives == input_data.lives && |
evanso | 67:a2984682d641 | 37 | output_data.smart_bombs == input_data.smart_bombs && |
evanso | 67:a2984682d641 | 38 | output_data.alien_number == input_data.alien_number ) { |
evanso | 67:a2984682d641 | 39 | printf ( "Passed!\n"); |
evanso | 67:a2984682d641 | 40 | return true; |
evanso | 67:a2984682d641 | 41 | } else { |
evanso | 67:a2984682d641 | 42 | printf ( "Failed!\n"); |
evanso | 67:a2984682d641 | 43 | return false; |
evanso | 67:a2984682d641 | 44 | } |
evanso | 67:a2984682d641 | 45 | } |
evanso | 67:a2984682d641 | 46 | |
evanso | 82:3211b31e9421 | 47 | void SavedGames::run_save_test(SDFileSystem &sd, N5110 &lcd) { |
evanso | 67:a2984682d641 | 48 | printf ("\nTesting save_test() \n\n"); |
evanso | 67:a2984682d641 | 49 | int passed_counter = 0; |
evanso | 67:a2984682d641 | 50 | |
evanso | 85:87bc28b151d8 | 51 | // Runs test with all different possible inputs |
evanso | 67:a2984682d641 | 52 | if (save_test(CENTRE, sd, lcd)) passed_counter++; |
evanso | 67:a2984682d641 | 53 | if (save_test(N, sd, lcd)) passed_counter++; |
evanso | 67:a2984682d641 | 54 | if (save_test(NE, sd, lcd)) passed_counter++; |
evanso | 67:a2984682d641 | 55 | if (save_test(E, sd, lcd)) passed_counter++; |
evanso | 67:a2984682d641 | 56 | if (save_test(SE, sd, lcd)) passed_counter++; |
evanso | 67:a2984682d641 | 57 | if (save_test(S, sd, lcd)) passed_counter++; |
evanso | 67:a2984682d641 | 58 | if (save_test(SW, sd, lcd)) passed_counter++; |
evanso | 67:a2984682d641 | 59 | if (save_test(W, sd, lcd)) passed_counter++; |
evanso | 67:a2984682d641 | 60 | if (save_test(NW, sd, lcd)) passed_counter++; |
evanso | 67:a2984682d641 | 61 | |
evanso | 82:3211b31e9421 | 62 | // Prints results |
evanso | 67:a2984682d641 | 63 | printf ("\n save_test passed %d tests out of 9\n\n\n", |
evanso | 67:a2984682d641 | 64 | passed_counter); |
evanso | 67:a2984682d641 | 65 | } |
evanso | 67:a2984682d641 | 66 | |
evanso | 82:3211b31e9421 | 67 | void SavedGames::init() { |
evanso | 50:13c8710985f4 | 68 | error_ = false; |
evanso | 67:a2984682d641 | 69 | display_data_number_ = 1; |
evanso | 50:13c8710985f4 | 70 | } |
evanso | 49:ed569eceeaa4 | 71 | |
evanso | 82:3211b31e9421 | 72 | void SavedGames::check_sd_present(SDFileSystem &sd, N5110 &lcd) { |
evanso | 64:e9dfc35a1738 | 73 | if(!sd.card_present()) { |
evanso | 82:3211b31e9421 | 74 | // Print error screen |
evanso | 50:13c8710985f4 | 75 | lcd.clear(); |
evanso | 50:13c8710985f4 | 76 | lcd.printString("SD card not ",9,3); |
evanso | 50:13c8710985f4 | 77 | lcd.printString(" present ",9,4); |
evanso | 50:13c8710985f4 | 78 | lcd.refresh(); |
evanso | 50:13c8710985f4 | 79 | wait(3); |
evanso | 50:13c8710985f4 | 80 | } |
evanso | 50:13c8710985f4 | 81 | } |
evanso | 50:13c8710985f4 | 82 | |
evanso | 82:3211b31e9421 | 83 | void SavedGames::error_open_file(N5110 &lcd) { |
evanso | 53:01be7898c23f | 84 | error_ = true; |
evanso | 63:4631447440cf | 85 | |
evanso | 82:3211b31e9421 | 86 | // Print error screen |
evanso | 53:01be7898c23f | 87 | lcd.clear(); |
evanso | 53:01be7898c23f | 88 | lcd.printString(" Unable to ",9,3); |
evanso | 53:01be7898c23f | 89 | lcd.printString(" open file ",9,4); |
evanso | 53:01be7898c23f | 90 | lcd.refresh(); |
evanso | 53:01be7898c23f | 91 | wait(3); |
evanso | 51:35cb8e604b72 | 92 | } |
evanso | 51:35cb8e604b72 | 93 | |
evanso | 82:3211b31e9421 | 94 | void SavedGames::no_saved_files(N5110 &lcd) { |
evanso | 61:7c4ec680a428 | 95 | error_ = true; |
evanso | 63:4631447440cf | 96 | |
evanso | 82:3211b31e9421 | 97 | // Print error screen |
evanso | 61:7c4ec680a428 | 98 | lcd.clear(); |
evanso | 61:7c4ec680a428 | 99 | lcd.printString("No saved Files",0,3); |
evanso | 61:7c4ec680a428 | 100 | lcd.refresh(); |
evanso | 61:7c4ec680a428 | 101 | wait(3); |
evanso | 61:7c4ec680a428 | 102 | } |
evanso | 61:7c4ec680a428 | 103 | |
evanso | 59:0b2e43312d6b | 104 | void SavedGames::add_saved_data(SDFileSystem &sd,SavedGamesData data, |
evanso | 82:3211b31e9421 | 105 | N5110 &lcd) { |
evanso | 67:a2984682d641 | 106 | check_sd_present(sd,lcd); |
evanso | 61:7c4ec680a428 | 107 | FILE *fp; |
evanso | 61:7c4ec680a428 | 108 | |
evanso | 85:87bc28b151d8 | 109 | // Overwrites selected save file |
evanso | 82:3211b31e9421 | 110 | if (display_data_number_ == 1) { |
evanso | 61:7c4ec680a428 | 111 | fp = fopen("/sd/SavedGamesOne.csv", "w"); |
evanso | 63:4631447440cf | 112 | // printf ("save1 \n"); |
evanso | 82:3211b31e9421 | 113 | }else if (display_data_number_ == 2) { |
evanso | 61:7c4ec680a428 | 114 | fp = fopen("/sd/SavedGamesTwo.csv", "w"); |
evanso | 63:4631447440cf | 115 | // printf ("save2\n" ); |
evanso | 61:7c4ec680a428 | 116 | }else{ |
evanso | 61:7c4ec680a428 | 117 | fp = fopen("/sd/SavedGamesThree.csv", "w"); |
evanso | 63:4631447440cf | 118 | // printf ("save2 \n"); |
evanso | 61:7c4ec680a428 | 119 | } |
evanso | 63:4631447440cf | 120 | |
evanso | 53:01be7898c23f | 121 | if (fp == NULL) { |
evanso | 53:01be7898c23f | 122 | error_open_file(lcd); |
evanso | 63:4631447440cf | 123 | |
evanso | 53:01be7898c23f | 124 | }else{ |
evanso | 82:3211b31e9421 | 125 | // printf ("add data.score = %d \n", data.score); |
evanso | 82:3211b31e9421 | 126 | // printf ("add data.lives = %d \n", data.lives); |
evanso | 82:3211b31e9421 | 127 | // printf ("adddata.smart_bombs = %d \n", data.smart_bombs); |
evanso | 82:3211b31e9421 | 128 | // printf ("add data.alien_number = %d \n", data.lives); |
evanso | 64:e9dfc35a1738 | 129 | |
evanso | 85:87bc28b151d8 | 130 | // Adds data to file if no error |
evanso | 59:0b2e43312d6b | 131 | fprintf(fp, "%d,%d,%d,%d\n", data.score, data.lives, data.smart_bombs, |
evanso | 59:0b2e43312d6b | 132 | data.alien_number); |
evanso | 52:feb8cf28bcff | 133 | fclose(fp); |
evanso | 53:01be7898c23f | 134 | } |
evanso | 53:01be7898c23f | 135 | } |
evanso | 53:01be7898c23f | 136 | |
evanso | 82:3211b31e9421 | 137 | SavedGamesData SavedGames::read_saved_data(SDFileSystem &sd, N5110 &lcd) { |
evanso | 67:a2984682d641 | 138 | check_sd_present(sd,lcd); |
evanso | 67:a2984682d641 | 139 | |
evanso | 61:7c4ec680a428 | 140 | FILE *fp; |
evanso | 62:f0c86a854a9e | 141 | SavedGamesData data; |
evanso | 61:7c4ec680a428 | 142 | |
evanso | 85:87bc28b151d8 | 143 | // Opens displayed file |
evanso | 82:3211b31e9421 | 144 | if (display_data_number_ == 1) { |
evanso | 62:f0c86a854a9e | 145 | fp = fopen("/sd/SavedGamesOne.csv", "r"); |
evanso | 63:4631447440cf | 146 | //printf ("open save1\n"); |
evanso | 82:3211b31e9421 | 147 | }else if (display_data_number_ == 2) { |
evanso | 67:a2984682d641 | 148 | fp = fopen("/sd/SavedGamesTwo.csv", "r"); |
evanso | 67:a2984682d641 | 149 | //printf ("open save2\n"); |
evanso | 61:7c4ec680a428 | 150 | }else{ |
evanso | 62:f0c86a854a9e | 151 | fp = fopen("/sd/SavedGamesThree.csv", "r"); |
evanso | 63:4631447440cf | 152 | // printf ("open save2\n"); |
evanso | 61:7c4ec680a428 | 153 | } |
evanso | 61:7c4ec680a428 | 154 | |
evanso | 82:3211b31e9421 | 155 | // Check if any saved game files |
evanso | 53:01be7898c23f | 156 | if (fp == NULL) { |
evanso | 63:4631447440cf | 157 | no_saved_files(lcd); |
evanso | 64:e9dfc35a1738 | 158 | |
evanso | 82:3211b31e9421 | 159 | // Reads saved data into object |
evanso | 53:01be7898c23f | 160 | }else{ |
evanso | 53:01be7898c23f | 161 | |
evanso | 85:87bc28b151d8 | 162 | // Read save file and add values to data object |
evanso | 64:e9dfc35a1738 | 163 | fscanf(fp, "%d,%d,%d,%d",&data.score, &data.lives,&data.smart_bombs, |
evanso | 64:e9dfc35a1738 | 164 | &data.alien_number); |
evanso | 59:0b2e43312d6b | 165 | |
evanso | 82:3211b31e9421 | 166 | // printf ("saved_data_vector_ 1%d \n", saved_data_vector.size()); |
evanso | 63:4631447440cf | 167 | fclose(fp); |
evanso | 61:7c4ec680a428 | 168 | } |
evanso | 63:4631447440cf | 169 | |
evanso | 61:7c4ec680a428 | 170 | return data; |
evanso | 51:35cb8e604b72 | 171 | } |
evanso | 51:35cb8e604b72 | 172 | |
evanso | 82:3211b31e9421 | 173 | void SavedGames::display_saved_games(N5110 &lcd) { |
evanso | 54:d46459104dea | 174 | lcd.clear(); |
evanso | 64:e9dfc35a1738 | 175 | |
evanso | 82:3211b31e9421 | 176 | // Prints saved game screen |
evanso | 59:0b2e43312d6b | 177 | lcd.printString("Saved Games",9,0); |
evanso | 59:0b2e43312d6b | 178 | lcd.drawSprite(39, 18, 3, 5, (int *)arrow_up); |
evanso | 59:0b2e43312d6b | 179 | lcd.drawSprite(39, 34, 3, 5, (int *)arrow_down); |
evanso | 54:d46459104dea | 180 | |
evanso | 82:3211b31e9421 | 181 | // Prints saved game that is displayed |
evanso | 54:d46459104dea | 182 | char buffer[9]; |
evanso | 59:0b2e43312d6b | 183 | sprintf(buffer,"Save %2d",display_data_number_); |
evanso | 59:0b2e43312d6b | 184 | lcd.printString(buffer,21,3); |
evanso | 69:753ba27325ce | 185 | lcd.printString("Back(B)Play(A)",0,5); |
evanso | 54:d46459104dea | 186 | |
evanso | 54:d46459104dea | 187 | lcd.refresh(); |
evanso | 54:d46459104dea | 188 | } |
evanso | 59:0b2e43312d6b | 189 | |
evanso | 82:3211b31e9421 | 190 | void SavedGames::save_game_screen(N5110 &lcd) { |
evanso | 60:55fdc6bb29b9 | 191 | lcd.clear(); |
evanso | 64:e9dfc35a1738 | 192 | |
evanso | 82:3211b31e9421 | 193 | // Prints saved game screen |
evanso | 61:7c4ec680a428 | 194 | lcd.printString("Select a save",3,0); |
evanso | 60:55fdc6bb29b9 | 195 | lcd.drawSprite(39, 18, 3, 5, (int *)arrow_up); |
evanso | 60:55fdc6bb29b9 | 196 | lcd.drawSprite(39, 34, 3, 5, (int *)arrow_down); |
evanso | 60:55fdc6bb29b9 | 197 | |
evanso | 82:3211b31e9421 | 198 | // Prints saved game that is displayed |
evanso | 60:55fdc6bb29b9 | 199 | char buffer[9]; |
evanso | 60:55fdc6bb29b9 | 200 | sprintf(buffer,"Save %2d",display_data_number_); |
evanso | 60:55fdc6bb29b9 | 201 | lcd.printString(buffer,21,3); |
evanso | 69:753ba27325ce | 202 | lcd.printString("Save(A)",21,5); |
evanso | 61:7c4ec680a428 | 203 | |
evanso | 60:55fdc6bb29b9 | 204 | lcd.refresh(); |
evanso | 60:55fdc6bb29b9 | 205 | } |
evanso | 60:55fdc6bb29b9 | 206 | |
evanso | 59:0b2e43312d6b | 207 | void SavedGames::saved_games_scroll(Direction d_) { |
evanso | 59:0b2e43312d6b | 208 | |
evanso | 85:87bc28b151d8 | 209 | // Changes displayed menu part depending on joystick input |
evanso | 54:d46459104dea | 210 | if (d_ == N || d_ == NE || d_ == NW ) { |
evanso | 54:d46459104dea | 211 | display_data_number_++; |
evanso | 54:d46459104dea | 212 | |
evanso | 85:87bc28b151d8 | 213 | // Loops back round when at end of saved game vector |
evanso | 82:3211b31e9421 | 214 | if(display_data_number_ > 3) { |
evanso | 61:7c4ec680a428 | 215 | display_data_number_ = 1; |
evanso | 54:d46459104dea | 216 | } |
evanso | 54:d46459104dea | 217 | } else if (d_ == S || d_ == SW || d_ == SE) { |
evanso | 54:d46459104dea | 218 | display_data_number_--; |
evanso | 82:3211b31e9421 | 219 | if(display_data_number_ < 1 ) { |
evanso | 61:7c4ec680a428 | 220 | display_data_number_ = 3; |
evanso | 54:d46459104dea | 221 | } |
evanso | 54:d46459104dea | 222 | } |
evanso | 54:d46459104dea | 223 | wait(0.15); |
evanso | 54:d46459104dea | 224 | } |
evanso | 64:e9dfc35a1738 | 225 | |
evanso | 64:e9dfc35a1738 | 226 | |
evanso | 82:3211b31e9421 | 227 | int SavedGames::get_display_data_number() { |
evanso | 64:e9dfc35a1738 | 228 | return display_data_number_; |
evanso | 85:87bc28b151d8 | 229 | } |