Final Submission. I have read and agreed with Statement of Academic Integrity.
Dependencies: mbed
Diff: main.cpp
- Revision:
- 2:586409836de7
- Parent:
- 1:86f96ceaf593
- Child:
- 3:2e2134c27c23
--- a/main.cpp Fri May 29 12:07:22 2020 +0000 +++ b/main.cpp Fri May 29 21:15:42 2020 +0000 @@ -10,8 +10,16 @@ char player_char = 'x'; int player_xpos = 1; int player_ypos = 45; + +int portal_1_xpos = 30; +int portal_1_ypos = 45; + +int portal_2_xpos = 60; +int portal_2_ypos = 45; + char array[84][48]; int move = 0; +bool endgame = false; //Prototypes void pad_init(); @@ -25,24 +33,39 @@ void array_init(); void add_surface_array(); void add_player_array(); -void get_move(int); +void get_move(int, Gamepad &pad); void print_screen(); +void collision_detection(int); +void spawn_object(); +void spawn_portal_1(); +void spawn_portal_2(); +void erase_old_sprites(char,char); +void jump(); //Main function int main() { + + wait_ms(50); pad_init(); array_init(); add_surface_array(); add_player_array(); + spawn_object(); + spawn_portal_1(); + spawn_portal_2(); print_screen(); update_display(); //Loop runs forever - while(1){ - get_move(move); + while(endgame == false){ + get_move(move, pad); + collision_detection(move); + if(pad.A_pressed() == 1){ + jump(); + } + else apply_move(); add_player_array(); print_screen(); - update_display(); } } @@ -54,9 +77,17 @@ }}} void apply_move(){ + int old_player_xpos = player_xpos; player_xpos += move; array[old_player_xpos][player_ypos] = '0'; + + int old_player_ypos = player_ypos; + if(array[player_xpos][player_ypos+1] == '0'){ + player_ypos += 1; + array[player_xpos][old_player_ypos] = '0'; + + } } @@ -74,8 +105,11 @@ lcd.setPixel(i,j, true); } + else if(array[i][j] == 'P' || array[i][j] == 'Q'){ + lcd.setPixel(i,j, false); } }} + } @@ -83,7 +117,7 @@ //Prototype functions void pad_init(){ lcd.init(); - lcd.setContrast(0.5); + lcd.setContrast(0.6); lcd.clear(); pad.init(); } @@ -102,20 +136,99 @@ -void get_move(int) -{ while(pad.A_pressed() == 0 && pad.B_pressed() == 0){ - if(pad.A_pressed() == 1){ +void get_move(int, Gamepad &pad) +{ +char d = pad.get_direction(); + + if(d == E){ move = 1; + } - else if(pad.B_pressed() == 1){ + else if(d == W){ move = -1; + + } + else move = 0; } - } + void update_display() { lcd.refresh(); //Refreshes the lcd so the pixels appear - wait_ms(200); //Frame rate of game -} \ No newline at end of file + wait_ms(150); //Frame rate of game +} + +/////////////////////////////// all of above is the main loop and a little bit of player stuff. + +void collision_detection(int) +{ + char dir = pad.get_direction(); + if(array[player_xpos+1][player_ypos] == '1' && dir == E){ + move = 0;} + if(array[player_xpos-1][player_ypos] == '1' && dir == W){ + move = 0;} + else if(array[player_xpos+1][player_ypos] == 'P' && dir == E){ + erase_old_sprites(player_xpos, player_ypos); + player_xpos = portal_2_xpos+1;} + else if(array[player_xpos-1][player_ypos] == 'Q' && dir == W){ + erase_old_sprites(player_xpos, player_ypos); + player_xpos = portal_1_xpos-1;} + else{ move = move;} +} + +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 spawn_object() +{ + array[10][45] = '1'; + array[11][45] = '1'; + array[11][44] = '1'; + + + array[30][44] = '1'; + array[30][43] = '1'; + + array[31][44] = '1'; + array[31][43] = '1'; + array[31][45] = '1'; + + + + array[60][44] = '1'; + array[60][43] = '1'; + + array[59][44] = '1'; + array[59][43] = '1'; + array[59][45] = '1'; + } +void spawn_portal_1() +{ + array[portal_1_xpos][portal_1_ypos] = 'P'; + } +void spawn_portal_2() +{ + array[portal_2_xpos][portal_2_ypos] = 'Q'; + } + +void jump() +{ + if(array[player_xpos][player_ypos+1] == '1'){ + int old_player_ypos = player_ypos; + player_ypos += -1; + array[player_xpos][old_player_ypos] = '0'; + } +} + + +//////////////////code so far has movement, surface and portal detection and relevant behaviours. \ No newline at end of file