Final Submission. I have read and agreed with Statement of Academic Integrity.
Dependencies: mbed
Revision 10:4f204c2ac16c, committed 2020-06-05
- Comitter:
- louisberard
- Date:
- Fri Jun 05 19:41:15 2020 +0000
- Parent:
- 9:b160a3de0d00
- Commit message:
- Final Submission. I have read and agreed with Statement of Academic Integrity.
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r b160a3de0d00 -r 4f204c2ac16c main.cpp --- a/main.cpp Fri Jun 05 18:15:28 2020 +0000 +++ b/main.cpp Fri Jun 05 19:41:15 2020 +0000 @@ -1,44 +1,49 @@ +/** +ELEC2645 Embedded Systems Project +School of Electronic & Electrical Engineering +University of Leeds +2019/20 + +Name: Louis Berard +Username: LL17L2B +Student ID Number: 201209172 +Date: 05/06/20 +*/ #include "mbed.h" #include "Gamepad.h" #include "N5110.h" - ///Objects Gamepad pad; N5110 lcd; ///initialise global variables to be used throughout the game ///initial player position in all levels. char player_char = 'x'; -int player_xpos = 8; +int player_xpos = 9; int player_ypos = 45; - int aim_xpos; int aim_ypos; - int portal_1_xpos; int portal_1_ypos; - int portal_2_xpos; int portal_2_ypos; - ///initialise x direction movement int move = 0; ///initialise terminators for menu and game loops. bool choice = false; bool endgame = false; - ///initialise Game array char array[84][48]; /**the characters in the level array's represent the following: -'0' = air -'1' = right facing wall -'2' = floor -'3' = cieling -'4' = left facing wall -'B' = Button -'S' = Spikes -'P' = portal 1 -'Q' = portal 2 -'x' = player +*'0' = air +*'1' = right facing wall +*'2' = floor +*'3' = cieling +*'4' = left facing wall +*'B' = Button +*'S' = Spikes +*'P' = portal 1 +*'Q' = portal 2 +*'x' = player */ /// level 1 char _level_1[84][48] = { @@ -300,9 +305,7 @@ {'3','0','0','0','0','0','0','2','3','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','1','D','D','D','D','D','D','D','D','D','D','D','D','D','D','D','D','D','2'}, {'3','0','0','0','0','0','0','2','3','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','1','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','2'}, {'3','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','2'}, - }; - - + }; ///Prototypes void pad_init(); void update_display(); @@ -317,12 +320,11 @@ void place_portal_1(); void place_portal_2(); ///Main function -int main() -{ +int main(){ ///initialise the pad pad_init(); ///loop until player has selected an option from the menu - while(choice == false) { + while(choice == false){ ///print welcome screen lcd.clear(); lcd.printString("Welcome to",0,0); @@ -360,29 +362,38 @@ lcd.printString("Pot2 = aim ",0,4); lcd.printString("A = jump",0,3); lcd.printString("Reset=Restart",0,5); - lcd.refresh(); wait_ms(5000); choice = false; - } + //printf("instructions selected\n"); + } ///loads level 1. else if(pad.B_pressed() == 1){ for(int i = 0; i < 84; i ++){ for(int j = 0; j < 48; j ++){ array[i][j] = _level_1[i][j]; - choice = true;}}} + choice = true; + } + } + } ///loads level 2 else if(pad.X_pressed() == 1){ for(int i = 0; i < 84; i ++){ for(int j = 0; j < 48; j ++){ array[i][j] = _level_2[i][j]; - choice = true;}}} + choice = true; + } + } + } ///loads level 3 else if(pad.Y_pressed() == 1){ for(int i = 0; i < 84; i ++){ for(int j = 0; j < 48; j ++){ array[i][j] = _level_3[i][j]; - choice = true;}}} + choice = true; + } + } + } ///refresh lcd to display chosen level when menu loop breaks. lcd.refresh(); } @@ -390,42 +401,75 @@ while(endgame == false){ /// converts joystick input into left or right movement. get_move(move, pad); - /// checks to see if the move is valid and handles teleportation logic. collision_detection(move); /// checks to see if the player has jumped, if they have then the players y pos will descrease by 1 if(pad.A_pressed() == 1){ jump(); - } + } ///checks to see if the player has placed portal 1, if they have then the portal will be placed in the array else if(pad.B_pressed() == 1){ + int portal_1_counter = 0; place_portal_1(); ///turns on left red led (LED1) to show portal 1 has been placed. pad.led(1,1); + for(int i = 0; i < 84; i ++){ + for(int j = 0; j < 48; j ++){ + if (array[i][j] == 'P'){ + portal_1_counter+= 1; + } + } } + if (portal_1_counter == 1){ + //printf("exactly 1 portal 1 has been placed\n"); + } + } ///checks to see if the player has placed portal 2, if they have then the portal will be placed in the array else if(pad.Y_pressed() == 1){ + int portal_2_counter = 0; place_portal_2(); ///turns on right red led (LED4) to show portal 2 has been placed. pad.led(4,1); + for(int i = 0; i < 84; i ++){ + for(int j = 0; j < 48; j ++){ + if (array[i][j] == 'Q'){ + portal_2_counter+= 1; + } + } } + if (portal_2_counter == 1){ + //printf("exactly 1 portal 2 has been placed\n"); + } + } else ///handles setting the new position of the player in the array and erasing the previous player position from the array. apply_move(); ///prints to the lcd + int player_counter = 0; + for(int i = 0; i < 84; i ++){ + for(int j = 0; j < 48; j ++){ + if (array[i][j] == 'x'){ + player_counter+=1; + } + } + } + if (player_counter == 1){ + //printf("exactly 1 player has been placed\n"); + } print_screen(); ///refreshes the lcd to display the new screen after movement. update_display(); /// the objective of the game is to open the door and walk through it. as the door is on the right hand side of the screen, when the player /// xpos exceeds 82, the puzzle is assumed to be solved as the door has to be opened to progress. if(player_xpos > 82){ - endgame = true;} + endgame = true; + } } ///clear the level and print the win screen lcd.clear(); lcd.printString("Puzzle solved!",0,0); - lcd.printString("Press reset",0,0); - lcd.printString("To play again",0,0); + lcd.printString("Press reset",0,1); + lcd.printString("To play again",0,2); lcd.refresh(); } ///initialise the game array with 0's @@ -433,19 +477,22 @@ for(int j = 0; j < 48; j++){ for(int i = 0; i < 84; i++){ array[i][j] = '0'; - }}} - + } + } +} void apply_move(){ /// removes old player char in the array and increments the value of the player_xpos by the value in move int old_player_xpos = player_xpos; player_xpos += move; array[old_player_xpos][player_ypos] = '0'; + //printf("player has moved!"); ///checks if the player is in the air, if they are then increase y by 1 until the player is on the floor and remove the old player char from the array int old_player_ypos = player_ypos; if(array[player_xpos][player_ypos+1] == '0'){ player_ypos += 1; array[player_xpos][old_player_ypos] = '0'; - } + //printf("player has moved down!"); + } ///checks if the player has landed on spikes and displays the end game screen with instructions for how to restart the game if they have. else if(array[player_xpos][player_ypos+1] == 'S'){ lcd.clear(); @@ -456,38 +503,41 @@ wait_ms(1000000000); ///places the player char in the array add_player_array(); - - } + } } - /// converts the chars in the array to on or off pixels, dependant on the type of object in the array. i.e walls are a dark pixel and portals are a light pixel /// this helps to distinguis where the portals are. void print_screen(){ - for(int j = 0; j < 48; j++){ - for(int i = 0; i < 84; i++){ + for(int j = 0; j < 48; j++){ + for(int i = 0; i < 84; i++){ - if (array[i][j] == '0'){ - lcd.setPixel(i,j,false); - } - else if(array[i][j] == '1'){ - lcd.setPixel(i,j, true);} - else if(array[i][j] == '2'){ - lcd.setPixel(i,j, true);} - else if(array[i][j] == '3'){ - lcd.setPixel(i,j, true);} - else if(array[i][j] == '4'){ - lcd.setPixel(i,j, true);} - else if(array[i][j] == 'x'){ - lcd.setPixel(i,j, true); + if (array[i][j] == '0'){ + lcd.setPixel(i,j,false); + } + else if(array[i][j] == '1'){ + lcd.setPixel(i,j, true); + } + else if(array[i][j] == '2'){ + lcd.setPixel(i,j, true); + } + else if(array[i][j] == '3'){ + lcd.setPixel(i,j, true); + } + else if(array[i][j] == '4'){ + lcd.setPixel(i,j, true); + } + else if(array[i][j] == 'x'){ + lcd.setPixel(i,j, true); + } - } - else if(array[i][j] == 'P' || array[i][j] == 'Q'){ - lcd.setPixel(i,j, false); + else if(array[i][j] == 'P' || array[i][j] == 'Q'){ + lcd.setPixel(i,j, false); } - else if(array[i][j] == 'B' || array[i][j] == 'D'|| array[i][j] == 'S'){ - lcd.setPixel(i,j, true); + else if(array[i][j] == 'B' || array[i][j] == 'D'|| array[i][j] == 'S'){ + lcd.setPixel(i,j, true); } - }} + } + } /// finds the position pot(2) is aiming at and prints the aim assist line get_portal_angle(); /// prints the player sprite @@ -499,56 +549,43 @@ lcd.setPixel(player_xpos-1,player_ypos-4, true); lcd.setPixel(player_xpos-2,player_ypos-1, true); lcd.setPixel(player_xpos-2,player_ypos, true); - } - - - - +} ///initilise the pad and LCD -void pad_init() -{ +void pad_init(){ lcd.init(); lcd.setContrast(0.45); lcd.clear(); pad.init(); } ///places the player char in the array -void add_player_array() -{ +void add_player_array(){ array[player_xpos][player_ypos] = player_char; } - - /// takes the input from the joystick and converts it into left/rigth movments -void get_move(int, Gamepad &pad) -{ -char d = pad.get_direction(); - - if(d == E){ - move = 1; - } - else if(d == W){ - move = -1; +void get_move(int, Gamepad &pad){ + char d = pad.get_direction(); + if(d == E){ + move = 1; + } + else if(d == W){ + move = -1; } else move = 0; - } - - +} /// refreshes the display with the updated game screen -void update_display() -{ +void update_display(){ lcd.refresh(); ///Refreshes the lcd so the pixels appear wait_ms(150); ///Frame rate of game } - -void collision_detection(int) -{ +void collision_detection(int){ ///check if the player is moving into any object other than air. if they are then x direction movement = 0 char dir = pad.get_direction(); if(array[player_xpos+1][player_ypos] != '0' && dir == E){ - move = 0;} + move = 0; + } else if(array[player_xpos-1][player_ypos] != '0' && dir == W){ - move = 0;} + move = 0; + } /// checks if the player is directly next to a portal and runs the logic to check where the player should be teleported to. This is dependant on the surfaces the second portal is placed on. if(array[player_xpos+1][player_ypos] == 'P' || array[player_xpos-1][player_ypos] == 'P' || array[player_xpos][player_ypos-1] == 'P' || array[player_xpos][player_ypos+1] == 'P' ){ move = 0; @@ -558,20 +595,24 @@ /// if the position horizontally next to portal 2 is a '3' then portal 2 has been placed on a ceiling and the player must be placed below portal 2. if (array[portal_2_xpos-1][portal_2_ypos] == '3'){ player_xpos = portal_2_xpos; - player_ypos = portal_2_ypos+2;} + player_ypos = portal_2_ypos+2; + } /// if the position horizontally next to portal 2 is a '2' then portal 2 has been placed on a floor and the player must be placed above and to the side of portal 2 so they dont fall back in. else if(array[portal_2_xpos-1][portal_2_ypos] == '2'){ player_xpos = portal_2_xpos + 1; - player_ypos = portal_2_ypos-1;} + player_ypos = portal_2_ypos-1; + } /// if the position vertically next to portal 2 is a '1' then portal 2 has been placed on a right facing wall and the player must be placed on the left of portal 2. else if(array[portal_2_xpos][portal_2_ypos - 1] == '1'){ player_xpos = portal_2_xpos - 2; - player_ypos = portal_2_ypos;} + player_ypos = portal_2_ypos; + } /// if the position vertically next to portal 2 is a '4' then portal 2 has been placed on a left facing wall and the player must be placed on the right of portal 2. else if(array[portal_2_xpos][portal_2_ypos - 1] == '4'){ player_xpos = portal_2_xpos + 2; - player_ypos = portal_2_ypos;} - } + player_ypos = portal_2_ypos; + } + } /// logic for entering portal 2 else if(array[player_xpos+1][player_ypos] == 'Q' || array[player_xpos-1][player_ypos] == 'Q' || array[player_xpos][player_ypos-1] == 'Q' || array[player_xpos][player_ypos+1] == 'Q' ){ move = 0; @@ -579,25 +620,24 @@ /// if the position horizontally next to portal 1 is a '3' then portal 1 has been placed on a ceiling and the player must be placed below portal 1. if (array[portal_1_xpos-1][portal_1_ypos] == '3'){ player_xpos = portal_1_xpos; - player_ypos = portal_1_ypos+2;} + player_ypos = portal_1_ypos+2; + } /// if the position horizontally next to portal 1 is a '2' then portal 1 has been placed on a floor and the player must be placed above and to the side of portal 1 so they dont fall back in. else if(array[portal_1_xpos-1][portal_1_ypos] == '2'){ player_xpos = portal_1_xpos + 1; - player_ypos = portal_1_ypos-1;} + player_ypos = portal_1_ypos-1; + } /// if the position vertically next to portal 1 is a '1' then portal 1 has been placed on a right facing wall and the player must be placed on the left of portal 1. else if(array[portal_1_xpos][portal_1_ypos - 1] == '1'){ player_xpos = portal_1_xpos - 2; - player_ypos = portal_1_ypos;} + player_ypos = portal_1_ypos; + } /// if the position vertically next to portal 1 is a '4' then portal 1 has been placed on a left facing wall and the player must be placed on the right of portal 1. else if(array[portal_1_xpos][portal_1_ypos - 1] == '4'){ player_xpos = portal_1_xpos + 2; - player_ypos = portal_1_ypos;} - } - - - - - + player_ypos = portal_1_ypos; + } + } ///removes the old player char from the array and replaces it with zero erase_old_sprites(player_xpos, player_ypos); ///checks if the player has stepped on the button, if they have then the button and door are replaced by air and the route to the exit is open. @@ -605,40 +645,38 @@ for(int i = 0; i < 84; i++){ for(int j = 0; j < 48; j++){ if(array[i][j] == 'D' || array[i][j] == 'B'){ - array[i][j] = '0';}}}} + array[i][j] = '0'; + } + } + } + //printf("Doors and buttons removed!"); + } } /// removes the old player character from the array. -void erase_old_sprites(char,char) -{ +void erase_old_sprites(char,char){ int old_player_xpos = player_xpos; array[old_player_xpos][player_ypos] = '0'; int old_player_ypos = player_ypos; array[player_xpos][old_player_ypos] = '0'; - } - - -void jump() -{ ///if the space beneath the player = floor, then increase ypos by 1. Apply move causes the player to fall again shortly after on the next loop. +} +void jump(){ ///if the space beneath the player = floor, then increase ypos by 1. Apply move causes the player to fall again shortly after on the next loop. if(array[player_xpos][player_ypos+1] == '2'){ int old_player_ypos = player_ypos; player_ypos += -1; + //printf("player has jumped!"); ///removes old player char from the array array[player_xpos][old_player_ypos] = '0'; } } - /// next steps are to turn pot into degrees and make a function which chechks which pixel the pot is pointing at. /// this function takes the pot(2) input range and converts it into 45 degree segments, after which the aim detector is sent out until it hits another object -void get_portal_angle() -{ +void get_portal_angle(){ ///take pot(2) val as a double to allow for more precise segments. double angle_input = pad.read_pot2(); - ///position the player is aiming at aim_xpos = player_xpos; aim_ypos = player_ypos; - /// if pot(2) is between 0 and 45 degrees, aim detection is projected at the floor, if(angle_input > 0 && angle_input < 0.125){ /// The aim_ypos will increment until it is next to a surface that isnt the air. @@ -647,8 +685,7 @@ } /// increment once more to place portal in the surface aim_ypos = player_ypos + 1; - } - + } /// if pot(2) is between 45 and 90 degrees, aim detection is diagonally down left else if(angle_input > 0.125 && angle_input < 0.25){ while(array[aim_xpos -1 ][aim_ypos + 1] == '0'){ @@ -659,7 +696,7 @@ /// performs increments and decrements once more to place portal in the surface aim_xpos += - 1; aim_ypos += 1; - } + } /// if pot(2) is between 90 and 135 degrees, aim detection is projected towards the left else if(angle_input > 0.25 && angle_input < 0.375){ while(array[aim_xpos -1 ][aim_ypos] == '0'){ @@ -668,7 +705,7 @@ } /// decrement once more to place portal in the surface aim_xpos += - 1; - } + } /// if pot(2) is between 135 and 180 degrees, aim detection is diagonally up left else if(angle_input > 0.375 && angle_input < 0.5){ while(array[aim_xpos -1 ][aim_ypos-1] == '0'){ @@ -679,7 +716,7 @@ /// performs decrements once more to place portal in the surface aim_xpos += - 1; aim_ypos += -1; - } + } /// if pot(2) is between 180 and 225 degrees, aim detection is upwards else if(angle_input > 0.5 && angle_input < 0.625){ while(array[aim_xpos][aim_ypos-1] == '0'){ @@ -688,7 +725,7 @@ } /// decrement once more to place portal in the surface aim_ypos += -1; - } + } /// if pot(2) is between 225 and 270 degrees, aim detection is diagonally up right else if(angle_input > 0.625 && angle_input < 0.75){ while(array[aim_xpos+1][aim_ypos-1] == '0'){ @@ -699,7 +736,7 @@ /// performs increments and decrements once more to place portal in the surface. aim_ypos += -1; aim_xpos += 1; - } + } /// if pot(2) is between 270 and 315 degrees, aim detection is to the right else if(angle_input > 0.75 && angle_input < 0.875){ while(array[aim_xpos+1][aim_ypos] == '0'){ @@ -708,7 +745,7 @@ } /// The aim_xpos will increment once more to place the portal in the surface. aim_xpos += 1; - } + } /// if pot(2) is between 315 and 360 degrees, aim detection is diagonally down right else if(angle_input > 0.875 && angle_input < 1){ while(array[aim_xpos+1][aim_ypos + 1] == '0'){ @@ -719,32 +756,30 @@ /// performs increments once more to place portal in the surface aim_xpos += 1; aim_ypos += 1; - } + } /// draw the aim assist dashed line to give a visual representation of where the player is aiming the portal gun /// this code allows for dynamic drawing of the aim assist relevant to the players position and the aim direction given by the potentiometer lcd.drawLine((player_xpos),(player_ypos), aim_xpos,aim_ypos,2); lcd.refresh(); - } - +} ///removed the old portal 1 and places the new one where the aim assist is pointed. -void place_portal_1() -{ +void place_portal_1(){ int old_p1_xpos = portal_1_xpos; int old_p1_ypos = portal_1_ypos; array[old_p1_xpos][old_p1_ypos] = '1'; portal_1_xpos = aim_xpos; portal_1_ypos = aim_ypos; - array[portal_1_xpos][portal_1_ypos] = 'P';} - + array[portal_1_xpos][portal_1_ypos] = 'P'; +} ///removed the old portal 2 and places the new one where the aim assist is pointed. -void place_portal_2() -{ +void place_portal_2(){ int old_p2_xpos = portal_2_xpos; int old_p2_ypos = portal_2_ypos; array[old_p2_xpos][old_p2_ypos] = '1'; portal_2_xpos = aim_xpos; portal_2_ypos = aim_ypos; - array[portal_2_xpos][portal_2_ypos] = 'Q';} + array[portal_2_xpos][portal_2_ypos] = 'Q'; +}