Final Submission. I have read and agreed with Statement of Academic Integrity.

Dependencies:   mbed

main.cpp

Committer:
louisberard
Date:
2020-06-05
Revision:
5:a33d791bd7fb
Parent:
4:8b75386f68ef
Child:
6:65c01b46fc88

File content as of revision 5:a33d791bd7fb:

///////// pre-processor directives ////////
#include "mbed.h"
#include "Gamepad.h"
#include "N5110.h"
#include "PortalEngine.h"

//Objects
Gamepad pad;
N5110 lcd;

char player_char = 'x';
int player_xpos = 8;
int player_ypos = 45;

int aim_xpos = 0;
int aim_ypos = 0;

int portal_1_xpos;
int portal_1_ypos;

int portal_2_xpos;
int portal_2_ypos;

char array[84][48] = {
    {'3','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','2'},
    {'3','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','2'},
    {'3','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','2'},
    {'3','0','0','0','0','0','0','0','0','B','2','3','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','2'},
    {'3','0','0','0','0','0','0','0','0','B','2','3','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','2'},
    {'3','0','0','0','0','0','0','0','0','B','2','3','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','2'},
    {'3','0','0','0','0','0','0','0','0','B','2','3','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','2'},
    {'3','0','0','0','0','0','0','0','0','B','2','3','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','2'},
    {'3','0','0','0','0','0','0','0','0','B','2','3','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','2'},
    {'3','0','0','0','0','0','0','0','0','B','2','3','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','2'},
    {'3','0','0','0','0','0','0','0','0','B','2','3','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','2'},
    {'3','0','0','0','0','0','0','0','0','B','2','3','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','2'},
    {'3','0','0','0','0','0','0','0','0','B','2','3','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','2'},
    {'3','0','0','0','0','0','0','0','0','B','2','3','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','2'},
    {'3','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','2'},
    {'3','0','0','0','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','0','0','0','0','0','0','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','1','1','1','1','1','1','1','1','1','1','1','2'},
    {'3','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','4','4','4','4','4','4','4','4','4','4','4','2'},
    {'3','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','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','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','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','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','0','0','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'},
 };   





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 add_surface_array();
void add_player_array();
void get_move(int, Gamepad &pad);
void print_screen();
void collision_detection(int);
void erase_old_sprites(char,char);
void jump();
void get_portal_angle();
void place_portal_1();
void place_portal_2();
//Main function
int main()
{
    
    pad_init();
    add_player_array();
    print_screen();
    update_display();
    //Loop runs forever
    while(endgame == false){
        get_move(move, pad);
        collision_detection(move);
        if(pad.A_pressed() == 1){
            jump();
            }
        else if(pad.B_pressed() == 1){
            place_portal_1();
            }

        else if(pad.Y_pressed() == 1){
            place_portal_2();
            }
        else
        apply_move();
        add_player_array();
        print_screen();
        update_display();
        if(player_xpos > 82){
            endgame = true;}
    }
    lcd.clear();
    lcd.printString("puzzle solved",0,0);
    lcd.refresh();
}

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] == '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] == 'B' || array[i][j] == 'D'){
             lcd.setPixel(i,j, true);
            }
            }}
            
         get_portal_angle();
         lcd.setPixel(player_xpos,player_ypos-1, true);
         lcd.setPixel(player_xpos-1,player_ypos-2, true);
         lcd.setPixel(player_xpos-1,player_ypos-3, true);
         lcd.setPixel(player_xpos-2,player_ypos-3, true);
         lcd.setPixel(player_xpos,player_ypos-3, true);
         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);
            }
         
 
 

//Prototype functions
void pad_init(){   
    lcd.init();
    lcd.setContrast(0.45);
    lcd.clear();
    pad.init();
}

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] == '4' && dir == W){
        move = 0;}
    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;
        array[player_xpos][player_ypos] = '0';
        if (array[portal_2_xpos-1][portal_2_ypos] == '3'){
            player_xpos = portal_2_xpos;
            player_ypos = portal_2_ypos+2;}
        else if(array[portal_2_xpos-1][portal_2_ypos] == '2'){
                player_xpos = portal_2_xpos + 1;
                player_ypos = portal_2_ypos-1;}
        else if(array[portal_2_xpos][portal_2_ypos + 1] == '1'){
                player_xpos = portal_2_xpos - 2;
                player_ypos = portal_2_ypos;}
        else if(array[portal_2_xpos][portal_2_ypos+ 1] == '4'){
                player_xpos = portal_2_xpos + 2;
                player_ypos = portal_2_ypos;}
                }
    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;
        array[player_xpos][player_ypos] = '0';
        if (array[portal_1_xpos-1][portal_1_ypos] == '3'){
            player_xpos = portal_1_xpos;
            player_ypos = portal_1_ypos+2;}
        else if(array[portal_1_xpos-1][portal_1_ypos] == '2'){
                player_xpos = portal_1_xpos + 1;
                player_ypos = portal_1_ypos-1;}
        else if(array[portal_1_xpos][portal_1_ypos + 1] == '1'){
                player_xpos = portal_1_xpos - 2;
                player_ypos = portal_1_ypos;}
        else if(array[portal_1_xpos][portal_1_ypos+ 1] == '4'){
                player_xpos = portal_1_xpos + 2;
                player_ypos = portal_1_ypos;}
                }
        
        
        
        
        
        
    erase_old_sprites(player_xpos, player_ypos);
    if(array[player_xpos][player_ypos+1] == 'B'){
        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';}}}}
    
    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 jump()
{
        if(array[player_xpos][player_ypos+1] == '2'){
        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);
    aim_xpos = player_xpos;
    aim_ypos = player_ypos;
    if(angle_input > 0 && angle_input < 0.125){
        while(array[aim_xpos][aim_ypos + 1] == '0'){
        aim_ypos = player_ypos + 1;
        }
        aim_ypos = player_ypos + 1;
        
        }
    else if(angle_input > 0.125 && angle_input < 0.25){
        while(array[aim_xpos -1 ][aim_ypos + 1] == '0'){
        aim_xpos += - 1;
        aim_ypos += 1;
        }
        aim_xpos += - 1;
        aim_ypos += 1;
        }
    else if(angle_input > 0.25 && angle_input < 0.375){
        while(array[aim_xpos -1 ][aim_ypos] == '0'){
        aim_xpos += - 1;
        }
        aim_xpos += - 1;
        }
    else if(angle_input > 0.375 && angle_input < 0.5){
        while(array[aim_xpos -1 ][aim_ypos-1] == '0'){
        aim_xpos += - 1;
        aim_ypos += - 1;
        }
        aim_xpos += - 1;
        aim_ypos += -1;
        }
    else if(angle_input > 0.5 && angle_input < 0.625){
        while(array[aim_xpos][aim_ypos-1] == '0'){
        aim_ypos += - 1;
        }
        aim_ypos += -1;
        }
    else if(angle_input > 0.625 && angle_input < 0.75){
        while(array[aim_xpos+1][aim_ypos-1] == '0'){
        aim_ypos += - 1;
        aim_xpos += 1;
        }
        aim_ypos += -1;
        aim_xpos +=  1;
        }
    else if(angle_input > 0.75 && angle_input < 0.875){
        while(array[aim_xpos+1][aim_ypos] == '0'){
        aim_xpos += 1;
        }
        aim_xpos +=  1;
        }
    else if(angle_input > 0.875 && angle_input < 1){
        while(array[aim_xpos+1][aim_ypos + 1] == '0'){
        aim_xpos += 1;
        aim_ypos += 1;
        }
        aim_xpos +=  1;
        aim_ypos += 1;
        }
    lcd.drawLine((player_xpos),(player_ypos), aim_xpos,aim_ypos,2);
    lcd.refresh();
    }
    
    
void place_portal_1()
{
        portal_angle();
        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';}        
        

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';}