Final Submission. I have read and agreed with Statement of Academic Integrity.
Dependencies: mbed
main.cpp
- Committer:
- louisberard
- Date:
- 2020-06-03
- Revision:
- 3:2e2134c27c23
- Parent:
- 2:586409836de7
- Child:
- 4:8b75386f68ef
File content as of revision 3:2e2134c27c23:
///////// pre-processor directives //////// #include "mbed.h" #include "Gamepad.h" #include "N5110.h" //Objects Gamepad pad; N5110 lcd; char player_char = 'x'; int player_xpos = 1; int player_ypos = 46; int portal_1_xpos = 30; int portal_1_ypos = 46; int portal_2_xpos = 60; int portal_2_ypos = 46; char array[84][48]; int move = 0; bool endgame = false; //Prototypes void pad_init(); void update_display(); void draw_array(); void player_object(); void Copy_array(); void player_movements(); void update_array(); void apply_move(); void array_init(); void add_surface_array(); void add_player_array(); 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(); void portal_angle(); //Main function int main() { 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(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(); } } void array_init(){ for(int j = 0; j < 48; j++){ for(int i = 0; i < 84; i++){ array[i][j] = '0'; }}} 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'; } } void print_screen(){ 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] == 'x'){ lcd.setPixel(i,j, true); } else if(array[i][j] == 'P' || array[i][j] == 'Q'){ lcd.setPixel(i,j, false); } }} portal_angle(); } //Prototype functions void pad_init(){ lcd.init(); lcd.setContrast(0.45); lcd.clear(); pad.init(); } void add_surface_array() { for(int i = 0; i < 84; i++){ array[i][47] = '1'; } for(int i = 0; i < 84; i++){ array[i][0] = '1'; } for(int i = 0; i < 48; i++){ array[0][i] = '1'; } for(int i = 0; i < 48; i++){ array[83][i] = '1'; } } void add_player_array() { array[player_xpos][player_ypos] = player_char; } 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; } void update_display() { lcd.refresh(); //Refreshes the lcd so the pixels appear 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][46] = '1'; array[11][46] = '1'; array[11][45] = '1'; array[30][45] = '1'; array[30][44] = '1'; array[31][45] = '1'; array[31][44] = '1'; array[31][46] = '1'; array[60][45] = '1'; array[60][44] = '1'; array[59][45] = '1'; array[59][44] = '1'; array[59][46] = '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. // next steps are to turn pot into degrees and make a function which chechks which pixel the pot is pointing at. void portal_angle() { double angle_input = pad.read_pot2(); float gradient = tan(angle_input*360); int aim_xpos = player_xpos; int aim_ypos = player_ypos; double aim_yposs = player_ypos; if(angle_input > 0.25 && angle_input < 0.5){ while(array[aim_xpos -1 ][aim_ypos - 1] != '1'){ aim_xpos += - 1; aim_yposs+= - gradient; aim_ypos = aim_yposs; }} else if(angle_input > 0 && angle_input < 0.25){ while(array[aim_xpos -1 ][aim_ypos + 1] != '1'){ aim_xpos += - 1; aim_yposs+= gradient/2; aim_ypos = aim_yposs; }} else if(angle_input > 0.75 && angle_input < 1){ while(array[aim_xpos +1 ][aim_ypos + 1] != '1'){ aim_xpos += 1; aim_yposs+= gradient/3; aim_ypos = aim_yposs; }} else if(angle_input > 0.5 && angle_input < 0.75){ while(array[aim_xpos +1 ][aim_ypos - 1] != '1'){ aim_xpos += 1; aim_yposs+= - gradient/4; aim_ypos = aim_yposs; }} lcd.drawLine(player_xpos,player_ypos, aim_xpos,aim_ypos,1); lcd.refresh(); }