Final Submission. I have read and agreed with Statement of Academic Integrity.
Dependencies: mbed
main.cpp@2:586409836de7, 2020-05-29 (annotated)
- Committer:
- louisberard
- Date:
- Fri May 29 21:15:42 2020 +0000
- Revision:
- 2:586409836de7
- Parent:
- 1:86f96ceaf593
- Child:
- 3:2e2134c27c23
code has movement, collision with surfaces and portal logic.; next implementation will be the portal gun.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
eencae | 0:7423345f87c5 | 1 | ///////// pre-processor directives //////// |
eencae | 0:7423345f87c5 | 2 | #include "mbed.h" |
eencae | 0:7423345f87c5 | 3 | #include "Gamepad.h" |
eencae | 0:7423345f87c5 | 4 | #include "N5110.h" |
eencae | 0:7423345f87c5 | 5 | |
louisberard | 1:86f96ceaf593 | 6 | //Objects |
louisberard | 1:86f96ceaf593 | 7 | Gamepad pad; |
louisberard | 1:86f96ceaf593 | 8 | N5110 lcd; |
eencae | 0:7423345f87c5 | 9 | |
louisberard | 1:86f96ceaf593 | 10 | char player_char = 'x'; |
louisberard | 1:86f96ceaf593 | 11 | int player_xpos = 1; |
louisberard | 1:86f96ceaf593 | 12 | int player_ypos = 45; |
louisberard | 2:586409836de7 | 13 | |
louisberard | 2:586409836de7 | 14 | int portal_1_xpos = 30; |
louisberard | 2:586409836de7 | 15 | int portal_1_ypos = 45; |
louisberard | 2:586409836de7 | 16 | |
louisberard | 2:586409836de7 | 17 | int portal_2_xpos = 60; |
louisberard | 2:586409836de7 | 18 | int portal_2_ypos = 45; |
louisberard | 2:586409836de7 | 19 | |
louisberard | 1:86f96ceaf593 | 20 | char array[84][48]; |
louisberard | 1:86f96ceaf593 | 21 | int move = 0; |
louisberard | 2:586409836de7 | 22 | bool endgame = false; |
louisberard | 1:86f96ceaf593 | 23 | |
louisberard | 1:86f96ceaf593 | 24 | //Prototypes |
louisberard | 1:86f96ceaf593 | 25 | void pad_init(); |
louisberard | 1:86f96ceaf593 | 26 | void update_display(); |
louisberard | 1:86f96ceaf593 | 27 | void draw_array(); |
louisberard | 1:86f96ceaf593 | 28 | void player_object(); |
louisberard | 1:86f96ceaf593 | 29 | void Copy_array(); |
louisberard | 1:86f96ceaf593 | 30 | void player_movements(); |
louisberard | 1:86f96ceaf593 | 31 | void update_array(); |
louisberard | 1:86f96ceaf593 | 32 | void apply_move(); |
louisberard | 1:86f96ceaf593 | 33 | void array_init(); |
louisberard | 1:86f96ceaf593 | 34 | void add_surface_array(); |
louisberard | 1:86f96ceaf593 | 35 | void add_player_array(); |
louisberard | 2:586409836de7 | 36 | void get_move(int, Gamepad &pad); |
louisberard | 1:86f96ceaf593 | 37 | void print_screen(); |
louisberard | 2:586409836de7 | 38 | void collision_detection(int); |
louisberard | 2:586409836de7 | 39 | void spawn_object(); |
louisberard | 2:586409836de7 | 40 | void spawn_portal_1(); |
louisberard | 2:586409836de7 | 41 | void spawn_portal_2(); |
louisberard | 2:586409836de7 | 42 | void erase_old_sprites(char,char); |
louisberard | 2:586409836de7 | 43 | void jump(); |
louisberard | 1:86f96ceaf593 | 44 | //Main function |
eencae | 0:7423345f87c5 | 45 | int main() |
eencae | 0:7423345f87c5 | 46 | { |
louisberard | 2:586409836de7 | 47 | |
louisberard | 2:586409836de7 | 48 | wait_ms(50); |
louisberard | 1:86f96ceaf593 | 49 | pad_init(); |
louisberard | 1:86f96ceaf593 | 50 | array_init(); |
louisberard | 1:86f96ceaf593 | 51 | add_surface_array(); |
louisberard | 1:86f96ceaf593 | 52 | add_player_array(); |
louisberard | 2:586409836de7 | 53 | spawn_object(); |
louisberard | 2:586409836de7 | 54 | spawn_portal_1(); |
louisberard | 2:586409836de7 | 55 | spawn_portal_2(); |
louisberard | 1:86f96ceaf593 | 56 | print_screen(); |
louisberard | 1:86f96ceaf593 | 57 | update_display(); |
louisberard | 1:86f96ceaf593 | 58 | //Loop runs forever |
louisberard | 2:586409836de7 | 59 | while(endgame == false){ |
louisberard | 2:586409836de7 | 60 | get_move(move, pad); |
louisberard | 2:586409836de7 | 61 | collision_detection(move); |
louisberard | 2:586409836de7 | 62 | if(pad.A_pressed() == 1){ |
louisberard | 2:586409836de7 | 63 | jump(); |
louisberard | 2:586409836de7 | 64 | } |
louisberard | 2:586409836de7 | 65 | else |
louisberard | 1:86f96ceaf593 | 66 | apply_move(); |
louisberard | 1:86f96ceaf593 | 67 | add_player_array(); |
louisberard | 1:86f96ceaf593 | 68 | print_screen(); |
louisberard | 1:86f96ceaf593 | 69 | update_display(); |
eencae | 0:7423345f87c5 | 70 | } |
eencae | 0:7423345f87c5 | 71 | } |
eencae | 0:7423345f87c5 | 72 | |
louisberard | 1:86f96ceaf593 | 73 | void array_init(){ |
louisberard | 1:86f96ceaf593 | 74 | for(int j = 0; j < 47; j++){ |
louisberard | 1:86f96ceaf593 | 75 | for(int i = 0; i < 83; i++){ |
louisberard | 1:86f96ceaf593 | 76 | array[i][j] = '0'; |
louisberard | 1:86f96ceaf593 | 77 | }}} |
louisberard | 1:86f96ceaf593 | 78 | |
louisberard | 1:86f96ceaf593 | 79 | void apply_move(){ |
louisberard | 2:586409836de7 | 80 | |
louisberard | 1:86f96ceaf593 | 81 | int old_player_xpos = player_xpos; |
louisberard | 1:86f96ceaf593 | 82 | player_xpos += move; |
louisberard | 1:86f96ceaf593 | 83 | array[old_player_xpos][player_ypos] = '0'; |
louisberard | 2:586409836de7 | 84 | |
louisberard | 2:586409836de7 | 85 | int old_player_ypos = player_ypos; |
louisberard | 2:586409836de7 | 86 | if(array[player_xpos][player_ypos+1] == '0'){ |
louisberard | 2:586409836de7 | 87 | player_ypos += 1; |
louisberard | 2:586409836de7 | 88 | array[player_xpos][old_player_ypos] = '0'; |
louisberard | 2:586409836de7 | 89 | |
louisberard | 2:586409836de7 | 90 | } |
louisberard | 1:86f96ceaf593 | 91 | } |
louisberard | 1:86f96ceaf593 | 92 | |
louisberard | 1:86f96ceaf593 | 93 | |
louisberard | 1:86f96ceaf593 | 94 | void print_screen(){ |
louisberard | 1:86f96ceaf593 | 95 | for(int j = 0; j < 47; j++){ |
louisberard | 1:86f96ceaf593 | 96 | for(int i = 0; i < 83; i++){ |
louisberard | 1:86f96ceaf593 | 97 | |
louisberard | 1:86f96ceaf593 | 98 | if (array[i][j] == '0'){ |
louisberard | 1:86f96ceaf593 | 99 | lcd.setPixel(i,j,false); |
louisberard | 1:86f96ceaf593 | 100 | } |
louisberard | 1:86f96ceaf593 | 101 | else if(array[i][j] == '1'){ |
louisberard | 1:86f96ceaf593 | 102 | lcd.setPixel(i,j, true); |
louisberard | 1:86f96ceaf593 | 103 | } |
louisberard | 1:86f96ceaf593 | 104 | else if(array[i][j] == 'x'){ |
louisberard | 1:86f96ceaf593 | 105 | lcd.setPixel(i,j, true); |
louisberard | 1:86f96ceaf593 | 106 | |
louisberard | 1:86f96ceaf593 | 107 | } |
louisberard | 2:586409836de7 | 108 | else if(array[i][j] == 'P' || array[i][j] == 'Q'){ |
louisberard | 2:586409836de7 | 109 | lcd.setPixel(i,j, false); |
louisberard | 1:86f96ceaf593 | 110 | } |
louisberard | 1:86f96ceaf593 | 111 | }} |
louisberard | 2:586409836de7 | 112 | } |
louisberard | 1:86f96ceaf593 | 113 | |
louisberard | 1:86f96ceaf593 | 114 | |
louisberard | 1:86f96ceaf593 | 115 | |
louisberard | 1:86f96ceaf593 | 116 | |
louisberard | 1:86f96ceaf593 | 117 | //Prototype functions |
louisberard | 1:86f96ceaf593 | 118 | void pad_init(){ |
eencae | 0:7423345f87c5 | 119 | lcd.init(); |
louisberard | 2:586409836de7 | 120 | lcd.setContrast(0.6); |
louisberard | 1:86f96ceaf593 | 121 | lcd.clear(); |
eencae | 0:7423345f87c5 | 122 | pad.init(); |
eencae | 0:7423345f87c5 | 123 | } |
eencae | 0:7423345f87c5 | 124 | |
louisberard | 1:86f96ceaf593 | 125 | void add_surface_array() |
eencae | 0:7423345f87c5 | 126 | { |
louisberard | 1:86f96ceaf593 | 127 | for(int i = 0; i < 84; i++){ |
louisberard | 1:86f96ceaf593 | 128 | array[i][46] = '1'; |
louisberard | 1:86f96ceaf593 | 129 | } |
louisberard | 1:86f96ceaf593 | 130 | } |
louisberard | 1:86f96ceaf593 | 131 | |
louisberard | 1:86f96ceaf593 | 132 | void add_player_array() |
louisberard | 1:86f96ceaf593 | 133 | { |
louisberard | 1:86f96ceaf593 | 134 | array[player_xpos][player_ypos] = player_char; |
eencae | 0:7423345f87c5 | 135 | } |
eencae | 0:7423345f87c5 | 136 | |
louisberard | 1:86f96ceaf593 | 137 | |
louisberard | 1:86f96ceaf593 | 138 | |
louisberard | 2:586409836de7 | 139 | void get_move(int, Gamepad &pad) |
louisberard | 2:586409836de7 | 140 | { |
louisberard | 2:586409836de7 | 141 | char d = pad.get_direction(); |
louisberard | 2:586409836de7 | 142 | |
louisberard | 2:586409836de7 | 143 | if(d == E){ |
louisberard | 1:86f96ceaf593 | 144 | move = 1; |
louisberard | 2:586409836de7 | 145 | |
louisberard | 1:86f96ceaf593 | 146 | } |
louisberard | 2:586409836de7 | 147 | else if(d == W){ |
louisberard | 1:86f96ceaf593 | 148 | move = -1; |
louisberard | 2:586409836de7 | 149 | |
louisberard | 2:586409836de7 | 150 | |
louisberard | 1:86f96ceaf593 | 151 | } |
louisberard | 2:586409836de7 | 152 | else move = 0; |
louisberard | 1:86f96ceaf593 | 153 | } |
louisberard | 2:586409836de7 | 154 | |
louisberard | 1:86f96ceaf593 | 155 | |
eencae | 0:7423345f87c5 | 156 | |
louisberard | 1:86f96ceaf593 | 157 | void update_display() |
louisberard | 1:86f96ceaf593 | 158 | { |
louisberard | 1:86f96ceaf593 | 159 | lcd.refresh(); //Refreshes the lcd so the pixels appear |
louisberard | 2:586409836de7 | 160 | wait_ms(150); //Frame rate of game |
louisberard | 2:586409836de7 | 161 | } |
louisberard | 2:586409836de7 | 162 | |
louisberard | 2:586409836de7 | 163 | /////////////////////////////// all of above is the main loop and a little bit of player stuff. |
louisberard | 2:586409836de7 | 164 | |
louisberard | 2:586409836de7 | 165 | void collision_detection(int) |
louisberard | 2:586409836de7 | 166 | { |
louisberard | 2:586409836de7 | 167 | char dir = pad.get_direction(); |
louisberard | 2:586409836de7 | 168 | if(array[player_xpos+1][player_ypos] == '1' && dir == E){ |
louisberard | 2:586409836de7 | 169 | move = 0;} |
louisberard | 2:586409836de7 | 170 | if(array[player_xpos-1][player_ypos] == '1' && dir == W){ |
louisberard | 2:586409836de7 | 171 | move = 0;} |
louisberard | 2:586409836de7 | 172 | else if(array[player_xpos+1][player_ypos] == 'P' && dir == E){ |
louisberard | 2:586409836de7 | 173 | erase_old_sprites(player_xpos, player_ypos); |
louisberard | 2:586409836de7 | 174 | player_xpos = portal_2_xpos+1;} |
louisberard | 2:586409836de7 | 175 | else if(array[player_xpos-1][player_ypos] == 'Q' && dir == W){ |
louisberard | 2:586409836de7 | 176 | erase_old_sprites(player_xpos, player_ypos); |
louisberard | 2:586409836de7 | 177 | player_xpos = portal_1_xpos-1;} |
louisberard | 2:586409836de7 | 178 | else{ move = move;} |
louisberard | 2:586409836de7 | 179 | } |
louisberard | 2:586409836de7 | 180 | |
louisberard | 2:586409836de7 | 181 | void erase_old_sprites(char,char) |
louisberard | 2:586409836de7 | 182 | { |
louisberard | 2:586409836de7 | 183 | int old_player_xpos = player_xpos; |
louisberard | 2:586409836de7 | 184 | array[old_player_xpos][player_ypos] = '0'; |
louisberard | 2:586409836de7 | 185 | |
louisberard | 2:586409836de7 | 186 | int old_player_ypos = player_ypos; |
louisberard | 2:586409836de7 | 187 | array[player_xpos][old_player_ypos] = '0'; |
louisberard | 2:586409836de7 | 188 | } |
louisberard | 2:586409836de7 | 189 | |
louisberard | 2:586409836de7 | 190 | |
louisberard | 2:586409836de7 | 191 | |
louisberard | 2:586409836de7 | 192 | void spawn_object() |
louisberard | 2:586409836de7 | 193 | { |
louisberard | 2:586409836de7 | 194 | array[10][45] = '1'; |
louisberard | 2:586409836de7 | 195 | array[11][45] = '1'; |
louisberard | 2:586409836de7 | 196 | array[11][44] = '1'; |
louisberard | 2:586409836de7 | 197 | |
louisberard | 2:586409836de7 | 198 | |
louisberard | 2:586409836de7 | 199 | array[30][44] = '1'; |
louisberard | 2:586409836de7 | 200 | array[30][43] = '1'; |
louisberard | 2:586409836de7 | 201 | |
louisberard | 2:586409836de7 | 202 | array[31][44] = '1'; |
louisberard | 2:586409836de7 | 203 | array[31][43] = '1'; |
louisberard | 2:586409836de7 | 204 | array[31][45] = '1'; |
louisberard | 2:586409836de7 | 205 | |
louisberard | 2:586409836de7 | 206 | |
louisberard | 2:586409836de7 | 207 | |
louisberard | 2:586409836de7 | 208 | array[60][44] = '1'; |
louisberard | 2:586409836de7 | 209 | array[60][43] = '1'; |
louisberard | 2:586409836de7 | 210 | |
louisberard | 2:586409836de7 | 211 | array[59][44] = '1'; |
louisberard | 2:586409836de7 | 212 | array[59][43] = '1'; |
louisberard | 2:586409836de7 | 213 | array[59][45] = '1'; |
louisberard | 2:586409836de7 | 214 | } |
louisberard | 2:586409836de7 | 215 | void spawn_portal_1() |
louisberard | 2:586409836de7 | 216 | { |
louisberard | 2:586409836de7 | 217 | array[portal_1_xpos][portal_1_ypos] = 'P'; |
louisberard | 2:586409836de7 | 218 | } |
louisberard | 2:586409836de7 | 219 | void spawn_portal_2() |
louisberard | 2:586409836de7 | 220 | { |
louisberard | 2:586409836de7 | 221 | array[portal_2_xpos][portal_2_ypos] = 'Q'; |
louisberard | 2:586409836de7 | 222 | } |
louisberard | 2:586409836de7 | 223 | |
louisberard | 2:586409836de7 | 224 | void jump() |
louisberard | 2:586409836de7 | 225 | { |
louisberard | 2:586409836de7 | 226 | if(array[player_xpos][player_ypos+1] == '1'){ |
louisberard | 2:586409836de7 | 227 | int old_player_ypos = player_ypos; |
louisberard | 2:586409836de7 | 228 | player_ypos += -1; |
louisberard | 2:586409836de7 | 229 | array[player_xpos][old_player_ypos] = '0'; |
louisberard | 2:586409836de7 | 230 | } |
louisberard | 2:586409836de7 | 231 | } |
louisberard | 2:586409836de7 | 232 | |
louisberard | 2:586409836de7 | 233 | |
louisberard | 2:586409836de7 | 234 | //////////////////code so far has movement, surface and portal detection and relevant behaviours. |