Robin Milward Cooney 200849894

Dependencies:   N5110 SDFileSystem gameCharacters mbed

Revision:
2:158d57cdbf1e
Parent:
1:046e66f1ca76
Child:
3:4b254b4bd6d4
--- a/main.cpp	Fri Apr 29 10:45:15 2016 +0000
+++ b/main.cpp	Wed May 04 10:12:58 2016 +0000
@@ -6,6 +6,9 @@
 
 */
 
+/** 
+
+*/
 #include "mbed.h"
 #include "N5110.h"
 #include "math.h"
@@ -31,11 +34,18 @@
 InterruptIn press_b_B(PTE25);                           //declare the push button B as an interrupt
 Ticker Ticker_Menu;
 Ticker Ticker_Game;
+Ticker Ticker_ds;
+Ticker Ticker_music;
+Timeout tOut;
+FILE *Highscores;
+FILE *Progress;
 
 int i;                                          //loop counter
 int run=0;
 int loop=0;
 int difficulty=2;
+int recks_movement=2;
+int fall=37;
 int g_jump=36;
 int jumpUp;
 int accel=0;
@@ -45,10 +55,12 @@
 int score=0;
 int highscore;
 int kill_score;
+int shield_score;
+int shield_counter=0;
 int h_movement=0;
 int ammo=24;
-int rat_movement=85;
-int hound_hMovement=90;
+int rat_movement=95;
+int hound_hMovement=98;
 int hound_vMovement=40;
 int hound_jump=0;
 int bear_movement=100;
@@ -56,13 +68,16 @@
 int bird_vMovement=20;
 int cactus_movement=110;
 int t_rex_movement=120;
+int chasm_movement=85;
 int fire_ball_hMovement=t_rex_movement-6;
 int fire_ball_vMovement=25;
 int heart_movement=90;
 int ammo_movement=100;
+int speed_boost_movement=130;
 int menuState=0;          //initial state for the menu fsm
 int jump_flag=0;
 int shoot_flag=0;
+int shield_flag=0;
 int print_rat_flag=0;
 int print_hound_flag=0;
 int hound_jump_flag=0;
@@ -70,29 +85,42 @@
 int print_bird_flag=0;
 int print_heart_flag=0;
 int print_ammo_flag=0;
+int print_speed_boost_flag=0;
 int print_cactus_flag=0;
 int print_t_rex_flag=0;
+int print_chasm_flag=0;
 int print_fire_ball_flag=0;
 int lose_lives_delay_flag=0;
+int story_mode_flag=0;
 int lives_delay_loop=0;                 //number of times the lose_lives_delay_flag if statement has iterated
 int fire_on_screen=0;
 int story_progress=1;
+int g_story_mode_win=0;
 double lives=4;
 int bear_lives=0;
 int t_rex_lives=0;
 int random_num;
+int g_music_count=1;
+int g_top_scores[6];
 float brightness=0.5;
 volatile int g_Ticker_Menu_flag=0;
 volatile int g_Ticker_Game_flag=0;
+volatile int g_Ticker_Music_flag=0;
+volatile int g_Ticker_ds_flag=0;
 volatile int g_press_b_A_flag=0;
 volatile int g_press_b_B_flag=0;
 
 void initialize_values()
 {
+    b_A.mode(PullDown);
+    b_B.mode(PullDown);
+    swJoy.mode(PullDown);
     red_led = 1;
     green_led=1;
     i=0;                                          //loop counter
     loop=0;
+    recks_movement=2;
+    fall=37;
     g_jump=36;
     accel=0;
     bullet=9;
@@ -107,6 +135,7 @@
     bird_vMovement=20;
     cactus_movement=110;
     t_rex_movement=120;
+    chasm_movement=85;
     fire_ball_hMovement=t_rex_movement-6;
     fire_ball_vMovement=25;
     heart_movement=90;
@@ -114,6 +143,8 @@
     menuState=0;          //initial state for the menu fsm
     jump_flag=0;
     shoot_flag=0;
+    shield_flag=0;
+    shield_counter=0;
     print_rat_flag=0;
     print_hound_flag=0;
     hound_jump_flag=0;
@@ -121,8 +152,10 @@
     print_bird_flag=0;
     print_heart_flag=0;
     print_ammo_flag=0;
+    print_speed_boost_flag=0;
     print_cactus_flag=0;
     print_t_rex_flag=0;
+    print_chasm_flag=0;
     print_fire_ball_flag=0;
     lose_lives_delay_flag=0;
     lives_delay_loop=0;
@@ -130,9 +163,175 @@
     bear_lives=0;
     score=0;
     kill_score=0;
+    shield_score=0;
     fire_on_screen=0;
 }
 
+void ticker_wait(int t)
+{
+    for (int q=0; q<=t; q++) {
+        if (g_Ticker_ds_flag) {
+            g_Ticker_ds_flag=0;
+            q++;
+        }
+        sleep();
+    }
+}
+void readSD_progress()
+{
+    Progress=fopen("/sd/Progress.txt", "r");
+    if (Progress!=NULL) {
+        green_led=0;
+        fscanf(Progress,"%d, ",&story_progress);
+        fclose(Progress);
+        ticker_wait(5);
+        green_led=1;
+    } else {
+        red_led=0;
+        ticker_wait(5);
+        red_led=1;
+    }
+}
+void writeSD_progress()
+{
+    Progress=fopen("/sd/Progress.txt", "w");
+    if (Progress!=NULL) {
+        green_led=0;
+        fprintf(Progress,"%d, ",story_progress);
+        fclose(Progress);
+        ticker_wait(5);
+        green_led=1;
+    } else {
+        red_led=0;
+        ticker_wait(5);
+        red_led=1;
+    }
+}
+void readSD_and_print_top_score()
+{
+    Highscores=fopen("/sd/Highscores.txt", "r");
+    if (Highscores!=NULL) {
+        green_led=0;
+        ticker_wait(3);
+        green_led=1;
+        for (int j=0; j<=5; j++) {
+            fscanf(Highscores,"%d, ",&g_top_scores[j]);
+        }
+        lcd.printString("Highscores:",0,0);
+        for (int j=1; j<=5; j++) {
+            char buffer[14];
+            int temp = sprintf(buffer,"%d",g_top_scores[j]);
+            lcd.printString(buffer,12,j);
+        }
+        fclose(Highscores);
+    } else {
+        red_led=0;
+        ticker_wait(3);
+        red_led=1;
+        lcd.printString("NO HIGHSCORES",3,2);
+        lcd.printString("SET YET!!",18,3);
+    }
+}
+
+void writeSD()
+{
+    Highscores=fopen("/sd/Highscores.txt", "w");
+    if (Highscores!=NULL) {
+        green_led=0;
+        ticker_wait(3);
+        green_led=1;
+        for (int j=0; j<=5; j++) {
+            fprintf(Highscores,"%d, ",g_top_scores[j]);
+        }
+        fclose(Highscores);
+    } else {
+        red_led=0;
+        ticker_wait(3);
+        red_led=1;
+    }
+}
+
+void delete_file(char filename[])
+{
+    lcd.printString("Deleting file",0,1);
+    ticker_wait(5);
+    FILE *fp = fopen(filename, "r");  // try and open file
+    if (fp != NULL) {  // if it does open...
+        fclose(fp);    // close it
+        remove(filename);  // and then delete
+        lcd.printString("Done!",0,5);
+        ticker_wait(10);
+    }
+    // if we can't open it, it doesn't exist and so we can't delete it
+    lcd.refresh();
+}
+
+void Delete_Highscores()
+{
+    lcd.clear();
+    while (1) {
+        if (g_Ticker_Menu_flag) {
+            g_Ticker_Menu_flag=0;
+            lcd.printString("Delete all",0,2);
+            lcd.printString("high scores?",0,3);
+            if (b_A==1) {
+                lcd.clear();
+                while (1) {
+                    if (g_Ticker_Menu_flag) {
+                        g_Ticker_Menu_flag=0;
+                        lcd.printString("Are you sure?",0,1);
+                        if (b_A==1) {
+                            lcd.clear();
+                            delete_file("/sd/Highscores.txt");
+                            return;
+                        } else if (b_B==1) {
+                            return;
+                        }
+                    }
+                    lcd.refresh();
+                    sleep();
+                }
+            } else if (b_B==1) {
+                return;
+            }
+        }
+        lcd.refresh();
+        sleep();
+    }
+}
+
+void sort_top_scores()
+{
+    g_top_scores[5]=score;
+    int temp;
+    for (int j=0; j<=5; j++) {
+        for (int n=1; n<=5; n++) {
+            if (g_top_scores[j]>g_top_scores[n]) {
+                temp = g_top_scores[j];
+                g_top_scores[j] = g_top_scores[n];
+                g_top_scores[n] = temp;
+            }
+        }
+    }
+}
+void music_tOut();
+void play_music()
+{
+
+    tOut.attach(&music_tOut, (song1[0]/2));
+    PWM.period (1.0/1000);
+    PWM.write(0.0);
+    if (song1[g_music_count]!=0) {
+        PWM.period(1/(song1[g_music_count]));
+        PWM.write(0.5);
+    }
+    if (g_music_count<(sizeof(song1)/sizeof(song1[1]))) {
+        g_music_count++;
+    } else {
+        g_music_count=1;
+    }
+}
+
 void generate_random_number()
 {
     time_t seconds = time(NULL);
@@ -189,6 +388,23 @@
     }
 }
 
+void print_speed_boost()
+{
+    for (int c=0; c<=13; c++) {
+        for (int r=0; r<=6; r++) {
+            if (g_speed_boost[r][c]==0) {
+                if (lcd.getPixel(c+speed_boost_movement,r+38)!=0) {
+                    lcd.setPixel(c+speed_boost_movement,r+38);
+                } else {
+                    lcd.clearPixel(c+speed_boost_movement,r+38);
+                }
+            } else if (g_speed_boost[r][c]==1) {
+                lcd.setPixel(c+speed_boost_movement,r+38);
+            }
+        }
+    }
+}
+
 void print_recks_still_gun()
 {
 
@@ -242,6 +458,23 @@
     }
 }
 
+void print_recks_shield()
+{
+    for(int c=0; c<=14; c++) {
+        for(int r=0; r<=14; r++) {
+            if (g_recks_shield[r][c]==0) {
+                if (lcd.getPixel(c,r+33)!=0) {
+                    lcd.setPixel(c,r+33);
+                } else {
+                    lcd.clearPixel(c,r+33);
+                }
+            } else if (g_recks_shield[r][c]==1) {
+                lcd.setPixel(c,r+33);
+            }
+        }
+    }
+}
+
 
 void print_recks_jump_gun()
 {
@@ -260,6 +493,35 @@
     }
 }
 
+void print_recks_falling()
+{
+    for(int c=0; c<=9; c++) { //9 beacause the the loop stats from 0 but the array size from 1
+        for(int r=0; r<=9; r++) {
+            if (g_recks_still_gun[r][c]==0) {
+                if (lcd.getPixel(c+3,r+fall)!=0) {
+                    lcd.setPixel(c+3,r+fall);
+                } else {
+                    lcd.clearPixel(c+3,r+fall);
+                }
+            } else if (g_recks_still_gun[r][c]==1) {
+                lcd.setPixel(c+3,r+fall);
+            }
+        }
+    }
+}
+
+void falling_animation() {
+    while (fall<60) {
+        if (g_Ticker_Menu_flag) {
+            g_Ticker_Menu_flag=0;
+            print_recks_falling();
+            fall+=3;
+        }
+        lcd.refresh();
+        sleep();
+        lcd.clear();
+    }
+}
 void print_mob_rat_p1()            //funtion to print the still bear on the LCD
 {
     for(int c=0; c<=7; c++) { // 7 beacause the the loop stats from 0 but the array size from 1
@@ -477,6 +739,13 @@
     }
 }
 
+void print_chasm()
+{
+    for(int x=0; x<=16; x++) {
+        lcd.setPixel(x+chasm_movement,46);
+    }
+}
+
 void print_t_rex_moving()
 {
     for(int c=0; c<=24; c++) { //24 beacause the the loop stats from 0 but the array size from 1
@@ -530,7 +799,7 @@
 
 void print_fire_ball_p2()
 {
-    for(int c=0; c<=8; c++) { //24 beacause the the loop stats from 0 but the array size from 1
+    for(int c=0; c<=8; c++) { //24 beacause the loop stats from 0 but the array size from 1
         for(int r=0; r<=8; r++) {
             if (g_fire_ball_p2[r][c]==0) {
                 if (lcd.getPixel(c+fire_ball_hMovement,r+fire_ball_vMovement)!=0) {
@@ -612,11 +881,27 @@
     g_Ticker_Game_flag=1;
 }
 
+void Ticker_ds_isr()
+{
+    g_Ticker_ds_flag=1;
+}
+
+void music_tOut()
+{
+    PWM.write(0.0);
+}
+
+
 void press_b_A_isr()
 {
     g_press_b_A_flag=1;
 }
 
+void press_b_B_isr()
+{
+    g_press_b_B_flag=1;
+}
+
 enum joystickDirection {    //emunm type for every neccessary direction of the joystick
     CENTRE,                 //when the joystick isn't moved
     DOWN,
@@ -668,19 +953,11 @@
     }
 }
 
-struct motion {         //defines the motion of the characters
-    int x;
-    int v_x;
-    int acc_x;
-    int y;
-    int v_y;
-    int acc_y;
-};
-
 struct menuState {
     int menu_select;  // output value
     int nextState[5];  // array of next states 6 to account for all of the joystick states
 };
+
 typedef const struct menuState STyp;
 
 STyp fsm_main_menu[5] = {
@@ -692,30 +969,60 @@
 };
 
 STyp fsm_settings_menu[5] = {
-    {0,{0,1,1,0,0}},
-    {8,{1,0,0,1,1}},
-    {16,{2,2,2,2,2}},
-    {24,{3,3,3,3,3}},
+    {0,{0,1,2,0,0}},
+    {8,{1,2,0,1,1}},
+    {16,{2,3,1,2,2}},
+    {24,{3,0,2,3,3}},
     {32,{4,4,4,4,4}}
 };
 
-void game();
+
+
+
+void main_menu();
+void Game();
+void Story_Mode();
+void Tutorial();
+void Chapter1();
+void Chapter2();
+void Chapter3();
+void Chapter4();
+void Minigame();
+void Settings();
+void Brightness();
+void Difficulty();
+void set_difficulty();
+void Leaderboard();
+void guns();
+void Credits();
+int Game_over();
+void Pause();
+
+
 int main()                      //main function, currently used to test different functions
 {
-    b_A.mode(PullDown);
-    b_B.mode(PullDown);
-    swJoy.mode(PullDown);
     initialize_values();
     lcd.init();
     Ticker_Menu.attach(&Ticker_Menu_isr,0.2);
     Ticker_Game.attach(&Ticker_Game_isr,0.1);
-    //press_b_A.fall(&press_b_A_isr);
-    //press_b_A.mode(PullDown);
+    Ticker_ds.attach(&Ticker_ds_isr,0.1);
+    press_b_A.fall(&press_b_A_isr);
+    press_b_A.mode(PullDown);
+    press_b_B.fall(&press_b_B_isr);
+    press_b_B.mode(PullDown);
     calibrateJoystick();
+    main_menu();
+}
 
-Menu:       //declares a "goto" location for the goto functions
+
+// -----------------------------------------------------------------------------------------------------------------------------------------
+
+void main_menu()
+{
     menuState=0;
     while(1) {
+        g_press_b_A_flag=0;
+        g_press_b_A_flag=0;
         if (g_Ticker_Menu_flag) {
             g_Ticker_Menu_flag=0;
             updateJoystick();
@@ -739,22 +1046,54 @@
             }
             lcd.refresh();
             if (b_A & menu_select==0) {
-                goto Story_Mode;
+                Story_Mode();
             } else if (b_A & menu_select==8) {
-                goto Minigame;
+                Game();
             } else if (b_A & menu_select==16) {
-                goto Settings;
+                Settings();
             } else if (b_A & menu_select==24) {
-                goto Leaderboard;
+                Leaderboard();
+                red_led=1;
+                green_led=1;
             } else if (b_A & menu_select==32) {
-                goto Credits;
+                Credits();
+            }
+            if (joystick.direction==LEFT&swJoy==1&b_B==1) {
+                story_progress=5;
+                writeSD_progress();
+                green_led=0;
+                ticker_wait(5);
+                green_led=1;
             }
         }
         sleep();
     }
+}
 
-Story_Mode:
+
+void Minigame()
+{
+    ticker_wait(5);
+    while (1) {
+        if (g_Ticker_Game_flag) {
+            g_Ticker_Game_flag=0;
+            lcd.clear();
+            updateJoystick();
+            Game();
+
+        }
+        i++;
+        lcd.refresh();
+        sleep();
+    }
+}
+
+void Story_Mode()
+{
     menuState=0;
+    story_mode_flag=1;
+    led=0;
+    readSD_progress();
     while(1) {
         if (g_Ticker_Menu_flag) {
             g_Ticker_Menu_flag=0;
@@ -764,11 +1103,11 @@
             menuState = fsm_main_menu[menuState].nextState[joystick.direction]; // when joystick.direction has a vaule of CENTRE/RIGHT/LEFT the state machine input is 0 when it has a value of DOWN it is 0b01 when it has a value of UP is is 0b10
             lcd.clear();
             print_locks();
-            lcd.printString("Chapter 1",20,0);
-            lcd.printString("Chapter 2",20,1);
-            lcd.printString("Chapter 3",20,2);
-            lcd.printString("Chapter 4",20,3);
-            lcd.printString("Chapter 5",20,4);
+            lcd.printString("Tutorial",20,0);
+            lcd.printString("Chapter 1",20,1);
+            lcd.printString("Chapter 2",20,2);
+            lcd.printString("Chapter 3",20,3);
+            lcd.printString("Chapter 4",20,4);
             for (int i=0; i<85; i++) {      //go through every pixel on the x axis
                 for (int j=menu_select; j<(menu_select+8); j++) {  // go through relevant pixels on the y axis
                     if (lcd.getPixel(i,j)== 0) {                   //if the pixel is on trun it off
@@ -779,24 +1118,870 @@
                 }
             }
             lcd.refresh();
-            if (b_A & menu_select==0&story_progress<=1) {
-                goto Story_Mode;
-            } else if (b_A & menu_select==8&story_progress<=2) {
-                goto Minigame;
-            } else if (b_A & menu_select==16&story_progress<=3) {
-                goto Settings;
-            } else if (b_A & menu_select==24&story_progress<=4) {
-                goto Leaderboard;
-            } else if (b_A & menu_select==32&story_progress<=5) {
-                goto Credits;
+            if (b_A & menu_select==0&story_progress>=1) {
+                Tutorial();
+            } else if (b_A & menu_select==8&story_progress>=2) {
+                Chapter1();
+            } else if (b_A & menu_select==16&story_progress>=3) {
+                Chapter2();
+            } else if (b_A & menu_select==24&story_progress>=4) {
+                Chapter3();
+            } else if (b_A & menu_select==32&story_progress>=5) {
+                Chapter4();
             }
             if (b_B) {
-                goto Menu;
+                menuState=0;
+                story_mode_flag=0;
+                return;
             }
         }
         sleep();
     }
-Settings:
+}
+
+void Tutorial()
+{
+    lcd.clear();
+    lcd.printString("Welcome to",0,0);
+    lcd.printString("boot camp",0,1);
+    lcd.printString("soldier",0,2);
+    lcd.printString("Press A >>",24,5);
+    lcd.refresh();
+    ticker_wait(10);
+    g_press_b_A_flag=0;
+    while (1) {
+        if (g_press_b_A_flag) {
+            g_press_b_A_flag=0;
+            break;
+        }
+        sleep();
+    }
+    lcd.clear();
+    lcd.printString("Are you ready",0,0);
+    lcd.printString("to begin your",0,1);
+    lcd.printString("training?",0,2);
+    lcd.printString("Press A >>",24,5);
+    lcd.refresh();
+    ticker_wait(10);
+    g_press_b_A_flag=0;
+    while (1) {
+        if (g_press_b_A_flag) {
+            g_press_b_A_flag=0;
+            break;
+        }
+        sleep();
+    }
+    lcd.clear();
+    lcd.printString("Okay use",0,0);
+    lcd.printString("joysick to",0,1);
+    lcd.printString("move right and",0,2);
+    lcd.printString("left to...",0,3);
+    lcd.printString("Press A >>",24,5);
+    lcd.refresh();
+    ticker_wait(10);
+    g_press_b_A_flag=0;
+    while (1) {
+        if (g_press_b_A_flag) {
+            g_press_b_A_flag=0;
+            break;
+        }
+        sleep();
+    }
+    lcd.clear();
+    lcd.printString("... move",0,0);
+    lcd.printString("forwards and",0,1);
+    lcd.printString("backwards.",0,2);
+    lcd.printString("Understand?",0,3);
+    lcd.printString("Yes Sir!>>",22,5);
+    lcd.refresh();
+    ticker_wait(10);
+    g_press_b_A_flag=0;
+    while (1) {
+        if (g_press_b_A_flag) {
+            g_press_b_A_flag=0;
+            break;
+        }
+        sleep();
+    }
+    lcd.clear();
+    lcd.printString("Now you try...",0,0);
+    lcd.printString("Okay!>>",30,5);
+    lcd.refresh();
+    ticker_wait(10);
+    g_press_b_A_flag=0;
+    while (1) {
+        if (g_press_b_A_flag) {
+            g_press_b_A_flag=0;
+            break;
+        }
+        sleep();
+    }
+    difficulty =-1;
+    Game();
+    lcd.clear();
+    lcd.printString("Good!!!",0,0);
+    lcd.printString("Now lets try",0,1);
+    lcd.printString("jumping. To",0,2);
+    lcd.printString("jump press B",0,3);
+    lcd.printString("Press A>>",24,5);
+    lcd.refresh();
+    ticker_wait(10);
+    g_press_b_A_flag=0;
+    while (1) {
+        if (g_press_b_A_flag) {
+            g_press_b_A_flag=0;
+            break;
+        }
+        sleep();
+    }
+    difficulty =-1;
+    Game();
+    lcd.clear();
+    lcd.printString("You can also",0,0);
+    lcd.printString("crouch if you",0,1);
+    lcd.printString("move your",0,2);
+    lcd.printString("joystick down",0,3);
+    lcd.printString("Press A>>",24,5);
+    lcd.refresh();
+    ticker_wait(10);
+    g_press_b_A_flag=0;
+    while (1) {
+        if (g_press_b_A_flag) {
+            g_press_b_A_flag=0;
+            break;
+        }
+        sleep();
+    }
+    lcd.clear();
+    lcd.printString("And if you",0,0);
+    lcd.printString("move the joy-",0,1);
+    lcd.printString("stick up you",0,2);
+    lcd.printString("will activate",0,3);
+    lcd.printString("Press A>>",24,5);
+    lcd.refresh();
+    ticker_wait(10);
+    g_press_b_A_flag=0;
+    while (1) {
+        if (g_press_b_A_flag) {
+            g_press_b_A_flag=0;
+            break;
+        }
+        sleep();
+    }
+    lcd.clear();
+    lcd.printString("your shield,",0,0);
+    lcd.printString("this will",0,1);
+    lcd.printString("protect you",0,2);
+    lcd.printString("for 1 sec but",0,3);
+    lcd.printString("Press A>>",24,5);
+    lcd.refresh();
+    ticker_wait(10);
+    g_press_b_A_flag=0;
+    while (1) {
+        if (g_press_b_A_flag) {
+            g_press_b_A_flag=0;
+            break;
+        }
+        sleep();
+    }
+    lcd.clear();
+    lcd.printString("it'll cost",0,0);
+    lcd.printString("you 10 score",0,1);
+    lcd.printString("points.",0,2);
+    lcd.printString("Press A>>",24,5);
+    lcd.refresh();
+    ticker_wait(10);
+    g_press_b_A_flag=0;
+    while (1) {
+        if (g_press_b_A_flag) {
+            g_press_b_A_flag=0;
+            break;
+        }
+        sleep();
+    }
+    lcd.clear();
+    lcd.printString("Try it...",0,0);
+    lcd.printString("Okay>>",30,5);
+    lcd.refresh();
+    ticker_wait(10);
+    g_press_b_A_flag=0;
+    while (1) {
+        if (g_press_b_A_flag) {
+            g_press_b_A_flag=0;
+            break;
+        }
+        sleep();
+    }
+    difficulty =-1;
+    score=20;
+    Game();
+    lcd.clear();
+    lcd.printString("Great!!!",0,0);
+    lcd.printString("To shoot press",0,1);
+    lcd.printString("A, but watch",0,2);
+    lcd.printString("out you...",0,3);
+    lcd.printString("Press A>>",24,5);
+    lcd.refresh();
+    ticker_wait(10);
+    g_press_b_A_flag=0;
+    while (1) {
+        if (g_press_b_A_flag) {
+            g_press_b_A_flag=0;
+            break;
+        }
+        sleep();
+    }
+    lcd.clear();
+    lcd.printString("... might use",0,0);
+    lcd.printString("all your ammo",0,1);
+    lcd.printString("Give it a go!",0,3);
+    lcd.printString("Okay>>",30,5);
+    lcd.refresh();
+    ticker_wait(10);
+    g_press_b_A_flag=0;
+    while (1) {
+        if (g_press_b_A_flag) {
+            g_press_b_A_flag=0;
+            break;
+        }
+        sleep();
+    }
+    difficulty =-1;
+    Game();
+    lcd.clear();
+    lcd.printString("Remember you",0,0);
+    lcd.printString("can also shoot",0,1);
+    lcd.printString("while jumping",0,2);
+    lcd.printString("and crouching",0,3);
+    lcd.printString("Press A>>",24,5);
+    lcd.refresh();
+    ticker_wait(10);
+    g_press_b_A_flag=0;
+    while (1) {
+        if (g_press_b_A_flag) {
+            g_press_b_A_flag=0;
+            break;
+        }
+        sleep();
+    }
+    lcd.clear();
+    lcd.printString("Information",0,0);
+    lcd.printString("about your",0,1);
+    lcd.printString("status (ammo",0,2);
+    lcd.printString("and score) ...",0,3);
+    lcd.printString("Press A>>",24,5);
+    lcd.refresh();
+    ticker_wait(10);;
+    g_press_b_A_flag=0;
+    while (1) {
+        if (g_press_b_A_flag) {
+            g_press_b_A_flag=0;
+            break;
+        }
+        sleep();
+    }
+    lcd.clear();
+    lcd.printString("... is shown",0,0);
+    lcd.printString("above, and the",0,1);
+    lcd.printString("number",0,2);
+    lcd.printString("lives you ...",0,3);
+    lcd.printString("Press A>>",24,5);
+    lcd.refresh();
+    ticker_wait(10);
+    g_press_b_A_flag=0;
+    while (1) {
+        if (g_press_b_A_flag) {
+            g_press_b_A_flag=0;
+            break;
+        }
+        sleep();
+    }
+    lcd.clear();
+    lcd.printString("... have is",0,0);
+    lcd.printString("displayed on",0,1);
+    lcd.printString("the LED bar,",0,2);
+    lcd.printString("when they...",0,3);
+    lcd.printString("Press A>>",24,5);
+    lcd.refresh();
+    ticker_wait(10);
+    g_press_b_A_flag=0;
+    while (1) {
+        if (g_press_b_A_flag) {
+            g_press_b_A_flag=0;
+            break;
+        }
+        sleep();
+    }
+    lcd.clear();
+    lcd.printString("... all turn",0,0);
+    lcd.printString("off you will",0,1);
+    lcd.printString("die, so make",0,2);
+    lcd.printString("sure that...",0,3);
+    lcd.printString("Press A>>",24,5);
+    lcd.refresh();
+    ticker_wait(10);
+    g_press_b_A_flag=0;
+    while (1) {
+        if (g_press_b_A_flag) {
+            g_press_b_A_flag=0;
+            break;
+        }
+        sleep();
+    }
+    lcd.clear();
+    lcd.printString("... it doesn't",0,0);
+    lcd.printString("happen!!!",0,1);
+    lcd.printString("Press A>>",24,5);
+    lcd.refresh();
+    ticker_wait(10);
+    g_press_b_A_flag=0;
+    while (1) {
+        if (g_press_b_A_flag) {
+            g_press_b_A_flag=0;
+            break;
+        }
+        sleep();
+    }
+    lcd.clear();
+    lcd.printString("Excellent!!!",0,0);
+    lcd.printString("Your training",0,1);
+    lcd.printString("is over",0,2);
+    lcd.printString("Press A>>",24,5);
+    lcd.refresh();
+    ticker_wait(10);
+    g_press_b_A_flag=0;
+    while (1) {
+        if (g_press_b_A_flag) {
+            g_press_b_A_flag=0;
+            break;
+        }
+        sleep();
+    }
+    lcd.clear();
+    lcd.printString("Are you ready",0,0);
+    lcd.printString("for your first",0,1);
+    lcd.printString("mission?",0,2);
+    lcd.printString("Yes!!!>>",24,5);
+    lcd.refresh();
+    ticker_wait(10);
+    g_press_b_A_flag=0;
+    while (1) {
+        if (g_press_b_A_flag) {
+            g_press_b_A_flag=0;
+            story_progress=2;
+            writeSD_progress();
+            break;
+        }
+        sleep();
+    }
+}
+
+void Chapter1()
+{
+    lcd.clear();
+    lcd.printString("150+ MISSIONS",0,1);
+    lcd.printString("LATER",18,2);
+    lcd.refresh();
+    ticker_wait(20);
+    lcd.clear();
+    lcd.printString("MISSION #158:",0,0);
+    lcd.printString("We have intel",0,1);
+    lcd.printString("of a new range",0,2);
+    lcd.printString("of high tech",0,3);
+    lcd.printString("military grade",0,4);
+    lcd.printString("Press A>>",24,5);
+    lcd.refresh();
+    ticker_wait(10);
+    g_press_b_A_flag=0;
+    while (1) {
+        if (g_press_b_A_flag) {
+            g_press_b_A_flag=0;
+            break;
+        }
+        sleep();
+    }
+    lcd.clear();
+    lcd.printString("weapons being",0,0);
+    lcd.printString("developed in",0,1);
+    lcd.printString("a remote",0,2);
+    lcd.printString("island in the",0,3);
+    lcd.printString("South...",0,4);
+    lcd.printString("Press A>>",24,5);
+    lcd.refresh();
+    ticker_wait(10);
+    g_press_b_A_flag=0;
+    while (1) {
+        if (g_press_b_A_flag) {
+            g_press_b_A_flag=0;
+            break;
+        }
+        sleep();
+    }
+    ticker_wait(10);
+    lcd.clear();
+    lcd.printString("... Pacific.",0,0);
+    lcd.printString("Your mission",0,1);
+    lcd.printString("is to",0,2);
+    lcd.printString("infiltrate the",0,3);
+    lcd.printString("island and...",0,4);
+    lcd.printString("Press A>>",24,5);
+    lcd.refresh();
+    g_press_b_A_flag=0;
+    while (1) {
+        if (g_press_b_A_flag) {
+            g_press_b_A_flag=0;
+            break;
+        }
+        sleep();
+    }
+    ticker_wait(10);
+    lcd.clear();
+    lcd.printString("... destroy",0,0);
+    lcd.printString("as many of",0,1);
+    lcd.printString("the weapons as",0,2);
+    lcd.printString("you can. Are",0,3);
+    lcd.printString("you ready?",0,4);
+    lcd.printString("Yes!!>>",24,5);
+    lcd.refresh();
+    ticker_wait(10);
+    g_press_b_A_flag=0;
+    while (1) {
+        if (g_press_b_A_flag) {
+            g_press_b_A_flag=0;
+            break;
+        }
+        sleep();
+    }
+    ticker_wait(10);
+    lcd.clear();
+    lcd.printString("Excellent, you",0,0);
+    lcd.printString("will land on",0,1);
+    lcd.printString("the farside of",0,2);
+    lcd.printString("the island and",0,3);
+    lcd.printString("work your...",0,4);
+    lcd.printString("Press A>>",24,5);
+    lcd.refresh();
+    ticker_wait(10);
+    g_press_b_A_flag=0;
+    while (1) {
+        if (g_press_b_A_flag) {
+            g_press_b_A_flag=0;
+            break;
+        }
+        sleep();
+    }
+    ticker_wait(10);
+    lcd.clear();
+    lcd.printString("...way towards",0,0);
+    lcd.printString("the centre the",0,1);
+    lcd.printString("closer you get",0,2);
+    lcd.printString("the more",0,3);
+    lcd.printString("defences...",0,4);
+    lcd.printString("Press A>>",24,5);
+    lcd.refresh();
+    ticker_wait(10);
+    g_press_b_A_flag=0;
+    while (1) {
+        if (g_press_b_A_flag) {
+            g_press_b_A_flag=0;
+            break;
+        }
+        sleep();
+    }
+    ticker_wait(20);
+    lcd.clear();
+    lcd.printString("they'll have.",0,0);
+    lcd.printString("For now you",0,1);
+    lcd.printString("need to look",0,2);
+    lcd.printString("out for the",0,3);
+    lcd.printString("cacti they...",0,4);
+    lcd.printString("Press A>>",24,5);
+    lcd.refresh();
+    ticker_wait(10);
+    g_press_b_A_flag=0;
+    while (1) {
+        if (g_press_b_A_flag) {
+            g_press_b_A_flag=0;
+            break;
+        }
+        sleep();
+    }
+
+    ticker_wait(20);
+    lcd.clear();
+    lcd.printString("if you touch",0,0);
+    lcd.printString("one you'll",0,1);
+    lcd.printString("lose a life so",0,2);
+    lcd.printString("jump over...",0,3);
+    lcd.printString("Press A>>",24,5);
+    lcd.refresh();
+    ticker_wait(10);
+    g_press_b_A_flag=0;
+    while (1) {
+        if (g_press_b_A_flag) {
+            g_press_b_A_flag=0;
+            break;
+        }
+        sleep();
+    }
+    ticker_wait(20);
+    lcd.clear();
+    lcd.printString("...them, also,",0,0);
+    lcd.printString("watch out for",0,1);
+    lcd.printString("low tech cyber",0,2);
+    lcd.printString("rats running",0,3);
+    lcd.printString("around, you'll...",0,4);
+    lcd.printString("Press A>>",24,5);
+    lcd.refresh();
+    ticker_wait(10);
+    g_press_b_A_flag=0;
+    while (1) {
+        if (g_press_b_A_flag) {
+            g_press_b_A_flag=0;
+            break;
+        }
+        sleep();
+    }
+    ticker_wait(20);
+    lcd.clear();
+    lcd.printString("have to jump",0,0);
+    lcd.printString("them aswell",0,1);
+    lcd.printString("as you cannot",0,2);
+    lcd.printString("destroy them.",0,3);
+    lcd.printString("Press A>>",24,5);
+    lcd.refresh();
+    ticker_wait(10);
+    g_press_b_A_flag=0;
+    while (1) {
+        if (g_press_b_A_flag) {
+            g_press_b_A_flag=0;
+            break;
+        }
+        sleep();
+    }
+    ticker_wait(20);
+    lcd.clear();
+    lcd.printString("That's all you",0,0);
+    lcd.printString("need to know ",0,1);
+    lcd.printString("for now. Get",0,2);
+    lcd.printString("to work and",0,3);
+    lcd.printString("Good Luck!!!",0,4);
+    lcd.printString("Press A>>",24,5);
+    lcd.refresh();
+    ticker_wait(10);
+    g_press_b_A_flag=0;
+    while (1) {
+        if (g_press_b_A_flag) {
+            g_press_b_A_flag=0;
+            break;
+        }
+        sleep();
+    }
+    difficulty=0;
+    Game();
+    story_progress=3;
+    writeSD_progress();
+    lcd.clear();
+    lcd.printString("Well done!!!",0,0);
+    lcd.printString("Oh no...",0,1);
+    lcd.printString("It looks like",0,2);
+    lcd.printString("things are",0,3);
+    lcd.printString("steping up",0,4);
+    lcd.printString("Press A>>",24,5);
+    lcd.refresh();
+    ticker_wait(10);
+    g_press_b_A_flag=0;
+    while (1) {
+        if (g_press_b_A_flag) {
+            g_press_b_A_flag=0;
+            break;
+        }
+        sleep();
+    }
+}
+
+void Chapter2()
+{
+    lcd.clear();
+    lcd.printString("Now you'll",0,0);
+    lcd.printString("encounter",0,1);
+    lcd.printString("robotic birds",0,2);
+    lcd.printString("and robotic",0,3);
+    lcd.printString("hounds aswell.",0,4);
+    lcd.printString("Press A>>",24,5);
+    lcd.refresh();
+    ticker_wait(10);
+    g_press_b_A_flag=0;
+    while (1) {
+        if (g_press_b_A_flag) {
+            g_press_b_A_flag=0;
+            break;
+        }
+        sleep();
+    }
+    lcd.clear();
+    lcd.printString("Watch out for",0,0);
+    lcd.printString("the birds as",0,1);
+    lcd.printString("they may swoop",0,2);
+    lcd.printString("and the hounds",0,3);
+    lcd.printString("will pounce",0,4);
+    lcd.printString("Press A>>",24,5);
+    lcd.refresh();
+    ticker_wait(10);
+    g_press_b_A_flag=0;
+    while (1) {
+        if (g_press_b_A_flag) {
+            g_press_b_A_flag=0;
+            break;
+        }
+        sleep();
+    }
+    difficulty=1;
+    Game();
+    story_progress=4;
+    writeSD_progress();
+    lcd.clear();
+    lcd.printString("Oh dear...",0,0);
+    lcd.printString("There is more",0,1);
+    lcd.printString("trouble ahead",0,2);
+    lcd.printString("Press A>>",24,5);
+    lcd.refresh();
+    ticker_wait(10);
+    g_press_b_A_flag=0;
+    while (1) {
+        if (g_press_b_A_flag) {
+            g_press_b_A_flag=0;
+            break;
+        }
+        sleep();
+    }
+}
+
+void Chapter3()
+{
+    lcd.clear();
+    lcd.printString("It looks like",0,0);
+    lcd.printString("there is some",0,1);
+    lcd.printString("sort of",0,2);
+    lcd.printString("bionic bear!!",0,3);
+    lcd.printString("You better...",0,4);
+    lcd.printString("Press A>>",24,5);
+    lcd.refresh();
+    ticker_wait(10);
+    g_press_b_A_flag=0;
+    while (1) {
+        if (g_press_b_A_flag) {
+            g_press_b_A_flag=0;
+            break;
+        }
+        sleep();
+    }
+    lcd.clear();
+    lcd.printString("... watch out",0,0);
+    lcd.printString("it looks like",0,1);
+    lcd.printString("it will be",0,2);
+    lcd.printString("able to",0,3);
+    lcd.printString("withstand...",0,4);
+    lcd.printString("Press A>>",24,5);
+    lcd.refresh();
+    ticker_wait(10);
+    g_press_b_A_flag=0;
+    while (1) {
+        if (g_press_b_A_flag) {
+            g_press_b_A_flag=0;
+            break;
+        }
+        sleep();
+    }
+    lcd.clear();
+    lcd.printString("a few shots",0,0);
+    lcd.printString("before",0,1);
+    lcd.printString("destroying it",0,2);
+    lcd.printString("4 hits should",0,3);
+    lcd.printString("do it. GO!",0,4);
+    lcd.printString("Press A>>",24,5);
+    lcd.refresh();
+    ticker_wait(10);
+    g_press_b_A_flag=0;
+    while (1) {
+        if (g_press_b_A_flag) {
+            g_press_b_A_flag=0;
+            break;
+        }
+        sleep();
+    }
+    difficulty=2;
+    Game();
+    story_progress=5;
+    writeSD_progress();
+    lcd.clear();
+    lcd.printString("OH DEAR",0,0);
+    lcd.printString("LORD...",0,1);
+    lcd.printString("Press A>>",24,5);
+    lcd.refresh();
+    ticker_wait(10);
+    g_press_b_A_flag=0;
+    while (1) {
+        if (g_press_b_A_flag) {
+            g_press_b_A_flag=0;
+            break;
+        }
+        sleep();
+    }
+}
+
+void Chapter4()
+{
+    g_press_b_A_flag=0;
+    lcd.clear();
+    lcd.printString("You won't",0,0);
+    lcd.printString("beleve this",0,1);
+    lcd.printString("it's it's...",0,2);
+    lcd.printString("it can't be",0,3);
+    lcd.printString("it's not...",0,4);
+    lcd.printString("Press A>>",24,5);
+    lcd.refresh();
+    ticker_wait(10);
+    g_press_b_A_flag=0;
+    while (1) {
+        if (g_press_b_A_flag) {
+            g_press_b_A_flag=0;
+            break;
+        }
+        sleep();
+    }
+    lcd.clear();
+    lcd.printString("it's not",0,0);
+    lcd.printString("possible...",0,1);
+    lcd.printString("It's a T Rex,",0,2);
+    lcd.printString("a robotic",0,3);
+    lcd.printString("T Rex!!!!",0,4);
+    lcd.printString("Press A>>",24,5);
+    lcd.refresh();
+    ticker_wait(10);
+    g_press_b_A_flag=0;
+    while (1) {
+        if (g_press_b_A_flag) {
+            g_press_b_A_flag=0;
+            break;
+        }
+        sleep();
+    }
+    lcd.clear();
+    lcd.printString("It's it's...",0,0);
+    lcd.printString("it's",0,1);
+    lcd.printString("breathing",0,2);
+    lcd.printString("fire!!!",0,3);
+    lcd.printString("It looks...",0,4);
+    lcd.printString("Press A>>",24,5);
+    lcd.refresh();
+    ticker_wait(10);
+    g_press_b_A_flag=0;
+    while (1) {
+        if (g_press_b_A_flag) {
+            g_press_b_A_flag=0;
+            break;
+        }
+        sleep();
+    }
+    lcd.clear();
+    lcd.printString("... like it",0,0);
+    lcd.printString("will only",0,1);
+    lcd.printString("take damage",0,2);
+    lcd.printString("when it's",0,3);
+    lcd.printString("attacking...",0,4);
+    lcd.printString("Press A>>",24,5);
+    lcd.refresh();
+    ticker_wait(10);
+    g_press_b_A_flag=0;
+    while (1) {
+        if (g_press_b_A_flag) {
+            g_press_b_A_flag=0;
+            break;
+        }
+        sleep();
+    }
+    lcd.clear();
+    lcd.printString("and I think",0,0);
+    lcd.printString("you'll have",0,1);
+    lcd.printString("to shoot it",0,2);
+    lcd.printString("at least 8",0,3);
+    lcd.printString("times to...",0,4);
+    lcd.printString("Press A>>",24,5);
+    lcd.refresh();
+    ticker_wait(10);
+    g_press_b_A_flag=0;
+    while (1) {
+        if (g_press_b_A_flag) {
+            g_press_b_A_flag=0;
+            break;
+        }
+        sleep();
+    }
+    lcd.clear();
+    lcd.printString("...destroy it",0,0);
+    lcd.printString("I'll drop",0,1);
+    lcd.printString("some ammo",0,2);
+    lcd.printString("for you.",0,3);
+    lcd.printString("Press A>>",24,5);
+    lcd.refresh();
+    ticker_wait(10);
+    g_press_b_A_flag=0;
+    while (1) {
+        if (g_press_b_A_flag) {
+            g_press_b_A_flag=0;
+            break;
+        }
+        sleep();
+    }
+    lcd.clear();
+    lcd.printString("Good Luck",0,0);
+    lcd.printString("You'll need",0,1);
+    lcd.printString("it",0,2);
+    lcd.printString("Press A>>",24,5);
+    lcd.refresh();
+    ticker_wait(10);
+    g_press_b_A_flag=0;
+    while (1) {
+        if (g_press_b_A_flag) {
+            g_press_b_A_flag=0;
+            break;
+        }
+        sleep();
+    }
+    difficulty=3;
+    Game();
+    initialize_values();
+    if (g_story_mode_win) {
+        lcd.clear();
+        lcd.printString("Well done",0,0);
+        lcd.printString("Recks",0,1);
+        lcd.printString("Lets clear",0,2);
+        lcd.printString("get you home",0,3);
+        lcd.printString("Press A>>",24,5);
+        lcd.refresh();
+        ticker_wait(30);
+    }
+    g_press_b_A_flag=0;
+    while (1) {
+        if (g_press_b_A_flag) {
+            g_press_b_A_flag=0;
+            break;
+        }
+        sleep();
+    }
+}
+
+void freeze()
+{
+    lcd.inverseMode();
+    lcd.refresh();
+    ticker_wait(5);
+    lcd.normalMode();
+}
+
+void Settings()
+{
     menuState=0;
     while (1) {
         if (g_Ticker_Menu_flag) {
@@ -807,6 +1992,8 @@
             lcd.clear();
             lcd.printString("Brightness",13,0);
             lcd.printString("Difficulty",13,1);
+            lcd.printString("SD card",13,2);
+            lcd.printString("Guns",13,3);
             for (int i=0; i<85; i++) {      //go through every pixel on the x axis
                 for (int j=menu_select; j<(menu_select+8); j++) {  // go through relevant pixels on the y axis
                     if (lcd.getPixel(i,j)== 0) {                   //if the pixel is on trun it off
@@ -818,62 +2005,28 @@
             }
             lcd.refresh();
             if (b_A & menu_select==0) {
-                goto Brightness;
+                Brightness();
+                return;
             } else if (b_A & menu_select==8) {
-                goto Difficulty;
+                Difficulty();
+                return;
+            } else if (b_A & menu_select==16) {
+                Delete_Highscores();
+                return;
+            } else if (b_A & menu_select==24) {
+                guns();
+                return;
             }
             if (b_B) {
-                goto Menu;
+                menuState=0;
+                return;
             }
         }
     }
-Brightness:
-    menuState=0;
-    while(1) {
-        if (g_Ticker_Menu_flag) {
-            g_Ticker_Menu_flag=0;
-            updateJoystick();
-            menu_select = fsm_main_menu[menuState].menu_select;  // set ouput depending on current state
-            menuState = fsm_main_menu[menuState].nextState[joystick.direction]; // when joystick.direction has a vaule of CENTRE/RIGHT/LEFT the state machine input is 0 when it has a value of DOWN it is 0b01 when it has a value of UP is is 0b10
-            lcd.clear();
-            lcd.printString("Dark",30,0);
-            lcd.printString("Eco",33,1);
-            lcd.printString("Regular",23,2);
-            lcd.printString("Bright",26,3);
-            lcd.printString("Blinding",20,4);
-            for (int i=0; i<85; i++) {      //go through every pixel on the x axis
-                for (int j=menu_select; j<(menu_select+8); j++) {  // go through relevant pixels on the y axis
-                    if (lcd.getPixel(i,j)== 0) {                   //if the pixel is on trun it off
-                        lcd.setPixel(i,j);
-                    } else {
-                        lcd.clearPixel(i,j);                       //if the pixel is off turn it on
-                    }
-                }
-            }
-            lcd.refresh();
-            if (b_A & menu_select==0) {
-                brightness=0;
-                goto Menu;
-            } else if (b_A & menu_select==8) {
-                brightness=0.3;
-                goto Menu;
-            } else if (b_A & menu_select==16) {
-                brightness=0.5;
-                goto Menu;
-            } else if (b_A & menu_select==24) {
-                brightness=0.7;
-                goto Menu;
-            } else if (b_A & menu_select==32) {
-                brightness=1;
-                goto Menu;
-            }
-            if (b_B) {
-                goto Settings;
-            }
-        }
-        sleep();
-    }
-Difficulty:
+}
+
+void Difficulty()
+{
     menuState=0;
     while(1) {
         if (g_Ticker_Menu_flag) {
@@ -899,86 +2052,705 @@
             lcd.refresh();
             if (b_A & menu_select==0) {
                 difficulty=0;
-                goto Menu;
+                return;
             } else if (b_A & menu_select==8) {
                 difficulty=1;
-                goto Menu;
+                return;
             } else if (b_A & menu_select==16) {
                 difficulty=2;
-                goto Menu;
+                return;
             } else if (b_A & menu_select==24) {
                 difficulty=3;
-                goto Menu;
+                return;
             } else if (b_A & menu_select==32) {
                 difficulty=4;
-                goto Menu;
+                return;
+            }
+            if (b_B) {
+                menuState=0;
+                return;
+            }
+        }
+        sleep();
+    }
+}
+
+void Brightness()
+{
+    menuState=0;
+    while(1) {
+        if (g_Ticker_Menu_flag) {
+            g_Ticker_Menu_flag=0;
+            updateJoystick();
+            menu_select = fsm_main_menu[menuState].menu_select;  // set ouput depending on current state
+            menuState = fsm_main_menu[menuState].nextState[joystick.direction]; // when joystick.direction has a vaule of CENTRE/RIGHT/LEFT the state machine input is 0 when it has a value of DOWN it is 0b01 when it has a value of UP is is 0b10
+            lcd.clear();
+            lcd.printString("Dark",30,0);
+            lcd.printString("Eco",33,1);
+            lcd.printString("Regular",23,2);
+            lcd.printString("Bright",26,3);
+            lcd.printString("Blinding",20,4);
+            for (int i=0; i<85; i++) {      //go through every pixel on the x axis
+                for (int j=menu_select; j<(menu_select+8); j++) {  // go through relevant pixels on the y axis
+                    if (lcd.getPixel(i,j)== 0) {                   //if the pixel is on trun it off
+                        lcd.setPixel(i,j);
+                    } else {
+                        lcd.clearPixel(i,j);                       //if the pixel is off turn it on
+                    }
+                }
+            }
+            lcd.refresh();
+            if (b_A & menu_select==0) {
+                brightness=0;
+                return;
+            } else if (b_A & menu_select==8) {
+                brightness=0.3;
+                return;
+            } else if (b_A & menu_select==16) {
+                brightness=0.5;
+                return;
+            } else if (b_A & menu_select==24) {
+                brightness=0.7;
+                return;
+            } else if (b_A & menu_select==32) {
+                brightness=1;
+                return;
             }
             if (b_B) {
-                goto Settings;
+                menuState=0;
+                return;
+            }
+        }
+        sleep();
+    }
+}
+
+void Leaderboard()
+{
+    lcd.clear();
+    readSD_and_print_top_score();
+    g_press_b_B_flag=0;
+    while(1) {
+        if (g_press_b_B_flag) {
+            g_press_b_B_flag=0;
+            return;
+        }
+        lcd.refresh();
+        sleep();
+    }
+}
+
+void guns()
+{
+    menuState=0;
+    while(1) {
+        if (g_Ticker_Menu_flag) {
+            g_Ticker_Menu_flag=0;
+            updateJoystick();
+            menu_select = fsm_settings_menu[menuState].menu_select;  // set ouput depending on current state
+            menuState = fsm_settings_menu[menuState].nextState[joystick.direction]; // when joystick.direction has a vaule of CENTRE/RIGHT/LEFT the state machine input is 0 when it has a value of DOWN it is 0b01 when it has a value of UP is is 0b10
+            lcd.clear();
+            lcd.printString("Pistol",10,0);
+            lcd.printString("Revolver",10,1);
+            lcd.printString("Rifle",10,2);
+            lcd.printString("Random",10,3);
+            for (int i=0; i<85; i++) {      //go through every pixel on the x axis
+                for (int j=menu_select; j<(menu_select+8); j++) {  // go through relevant pixels on the y axis
+                    if (lcd.getPixel(i,j)== 0) {                   //if the pixel is on trun it off
+                        lcd.setPixel(i,j);
+                    } else {
+                        lcd.clearPixel(i,j);                       //if the pixel is off turn it on
+                    }
+                }
+            }
+            lcd.refresh();
+
+            if (b_A & menu_select==0) {
+                g_g1=0;
+                g_g2=0;
+                return;
+            } else if (b_A & menu_select==8) {
+                g_g1=1;
+                g_g2=0;
+                return;
+            } else if (b_A & menu_select==16) {
+                g_g1=1;
+                g_g2=1;
+                return;
+            } else if (b_A & menu_select==24) {
+                if (rand()%3==0) {
+                    g_g1=0;
+                    g_g2=0;
+                } else if (rand()%3==1) {
+                    g_g1=1;
+                    g_g2=0;
+                } else if (rand()%3==2) {
+                    g_g1=1;
+                    g_g2=1;
+                }
+                return;
+            }
+            if (b_B) {
+                menuState=0;
+                return;
             }
         }
         sleep();
     }
-Leaderboard:
-    while(1) {
-        lcd.clear();
-        char buffer[14];
-        int p = sprintf(buffer,"%d",score);
-        if (g_Ticker_Menu_flag) {
-            g_Ticker_Menu_flag=0;
-            FILE *Highscores = fopen("/sd/Highscores.txt", "w");
-            if (Highscores!=NULL) {
-                green_led=0;
-                fscanf(Highscores, "%d", &score);
-                fclose(Highscores);
-                lcd.printString("Highscore:",0,0);
-                lcd.printString(buffer,10,3);
-            } else if (Highscores!=NULL) {
-                red_led=0;
-                lcd.printString("Error reading",0,0);
-                lcd.printString("file try",0,1);
-                lcd.printString("re-inserting",0,2);
-                lcd.printString("SD card",0,3);
+}
+
+void Credits()
+{
+
+}
+
+void Game()
+{
+
+    int run_game=1;
+    g_music_count=1;
+    while (run_game==1) {
+        ground();
+        set_difficulty();
+        if (g_Ticker_Game_flag) {
+            play_music();
+            g_Ticker_Game_flag=0;
+            lcd.clear();
+            updateJoystick();
+            led_bar();
+            print_score();
+            generate_random_number();
+            if (lose_lives_delay_flag==1) {
+                lives_delay_loop++;
+                if (lives_delay_loop>=10) {     //means that the delay between one life beeing lost and another one beeing lost is at least 1s
+                    lives_delay_loop=0;
+                    lose_lives_delay_flag=0;
+                }
+
+            }
+            if (ammo<=5) {
+                if (i%8>=4) {
+                    print_ammo();
+                }
+            } else {
+                print_ammo();
+            }
+            if (b_B) {
+                jump_flag=1;
+            }
+            if (shoot_flag==0) {
+                bullet=9;
+                if (joystick.direction==DOWN&jump_flag!=1) {
+                    bullet_height=42;
+                } else {
+                    bullet_height=g_jump+4;
+                }
+            } else if (shoot_flag==1&loop==0) {
+                loop++;
+                ammo--;
+                if (joystick.direction==DOWN&jump_flag!=1) {
+                    bullet_height=42;
+                } else {
+                    bullet_height=g_jump+4;
+                }
+            }
+            if (b_A) {
+                if (ammo<=0) {
+                    shoot_flag=0;
+                } else {
+                    shoot_flag=1;
+                }
+            }
+            if (joystick.direction==UP&score>=10&jump_flag!=1) {
+                shield_flag=1;
+            } else if (joystick.direction==UP&score<10&jump_flag!=1) {
+                print_recks_still_gun();
+            }
+            if (print_t_rex_flag==1&random_num%10==0&fire_on_screen==0) {
+                fire_on_screen=1;
+                print_fire_ball_flag=1;
+                fire_ball_hMovement=t_rex_movement-6;
+                fire_ball_vMovement=25;
+            }
+            if (joystick.direction==CENTRE&jump_flag!=1&shield_flag!=1) {
+                lcd.setPixel(bullet,bullet_height);
+                lcd.setPixel(bullet+1,bullet_height);
+                print_recks_still_gun();
+                if (shoot_flag==1) {
+                    bullet+=20;
+                    if (bullet>=84) {
+                        shoot_flag=0;
+                        loop=0;
+                    }
+                }
+            } else if (joystick.direction==LEFT&jump_flag!=1&shield_flag!=1) {
+                if (run%2==0) {
+                    print_recks_still_gun();
+                } else if (run%2==1) {
+                    print_recks_moving_gun();
+                }
+                lcd.setPixel(bullet,bullet_height);
+                lcd.setPixel(bullet+1,bullet_height);
+                if (shoot_flag==1) {
+                    bullet+=21;
+                    if (bullet>=84) {
+                        shoot_flag=0;
+                        loop=0;
+                    }
+                }
+                h_movement--;
+                run++;
+            } else if (joystick.direction==RIGHT&jump_flag!=1&shield_flag!=1) {
+                if (run%2==0) {
+                    print_recks_still_gun();
+                } else if (run%2==1) {
+                    print_recks_moving_gun();
+                }
+                lcd.setPixel(bullet,bullet_height);
+                lcd.setPixel(bullet+1,bullet_height);
+                if (shoot_flag==1) {
+                    bullet+=19;
+                    if (bullet>=84) {
+                        shoot_flag=0;
+                        loop=0;
+                    }
+                }
+                h_movement++;
+                run++;
+            } else if (joystick.direction==DOWN&jump_flag!=1&shield_flag!=1) {
+                lcd.setPixel(bullet,bullet_height);
+                lcd.setPixel(bullet+1,bullet_height);
+                print_recks_crouch_gun();
+                bullet_height=42;
+                if (shoot_flag==1) {
+                    bullet+=20;
+                    if (bullet>=84) {
+                        shoot_flag=0;
+                        loop=0;
+                    }
+                }
+            } else if (jump_flag==1&shield_flag!=1) {
+                if (joystick.direction==LEFT) {
+                    if (shoot_flag==1) {
+                        bullet+=21;
+                        if (bullet>=84) {
+                            shoot_flag=0;
+                            loop=0;
+                        }
+                    }
+                    h_movement--;
+                } else if (joystick.direction==RIGHT) {
+                    if (shoot_flag==1) {
+                        bullet+=19;
+                        if (bullet>=84) {
+                            shoot_flag=0;
+                            loop=0;
+                        }
+                    }
+                    h_movement++;
+                } else if (joystick.direction==CENTRE) {
+                    if (shoot_flag==1) {
+                        bullet+=20;
+                        if (bullet>=84) {
+                            shoot_flag=0;
+                            loop=0;
+                        }
+                    }
+                }
+                if (g_jump<=36&jumpUp==0&g_jump!=15) {
+                    lcd.setPixel(bullet,bullet_height);
+                    lcd.setPixel(bullet+1,bullet_height);
+                    if (bullet>=84) {
+                        shoot_flag=0;
+                        loop=0;
+                    }
+                    print_recks_jump_gun();
+                    g_jump-=6-accel;
+                    accel++;
+                } else if (g_jump>=15&g_jump!=36) {
+                    lcd.setPixel(bullet,bullet_height);
+                    lcd.setPixel(bullet+1,bullet_height);
+                    if (bullet>=84) {
+                        shoot_flag=0;
+                        loop=0;
+                    }
+                    print_recks_jump_gun();
+                    g_jump+=6-accel;
+                    accel--;
+                    jumpUp=1;
+                } else if (g_jump==36&jumpUp==1) {
+                    lcd.setPixel(bullet,bullet_height);
+                    lcd.setPixel(bullet+1,bullet_height);
+                    if (bullet>=84) {
+                        shoot_flag=0;
+                        loop=0;
+                    }
+                    print_recks_still_gun();
+                    jump_flag=0;
+                    jumpUp=0;
+                    g_jump=36;
+                    accel=0;
+                }
+            }
+            if (print_rat_flag==1) {
+                if (i%4>=2) {
+                    print_mob_rat_p1();
+                } else if (i%4<2) {
+                    print_mob_rat_p2();
+                }
+                if (joystick.direction==LEFT) {
+                    rat_movement+=recks_movement;
+                } else if (joystick.direction==RIGHT) {
+                    rat_movement-=recks_movement;
+                }
+                rat_movement-=3;
+                if (rat_movement<=-15) {
+                    rat_movement=100;
+                    print_rat_flag=0;
+                }
+                if (rat_movement<=5&rat_movement>=-3&g_jump>32&lose_lives_delay_flag==0&shield_flag!=1) {     //a life is lost if recks has an vertical and horizontal position equal to one of the pixels corresponding to the bears position
+                    lives--;
+                    freeze();
+                    lose_lives_delay_flag=1;
+                }
             }
+            if (print_hound_flag==1) {
+                if (i%4>=2&hound_jump_flag!=1) {
+                    print_mob_hound_p1();
+                } else if (i%4<2&hound_jump_flag!=1) {
+                    print_mob_hound_p2();
+                }
+                if (joystick.direction==LEFT) {
+                    hound_hMovement+=recks_movement;
+                } else if (joystick.direction==RIGHT) {
+                    hound_hMovement-=recks_movement;
+                }
+                hound_hMovement-=2;
+                if (hound_hMovement<=-15) {
+                    hound_hMovement=100;
+                    print_hound_flag=0;
+                }
+                if (random_num%10==0) {
+                    hound_jump_flag=1;
+                }
+                if (hound_jump_flag==1) {
+                    hound_jump++;
+                    hound_hMovement--;
+                    if (hound_jump==1) {
+                        hound_vMovement-=3;
+                    } else if (hound_jump==2) {
+                        hound_vMovement-=2;
+                    } else if (hound_jump==3) {
+                        hound_vMovement-=1;
+                    } else if (hound_jump==4) {
+                        hound_vMovement+=1;
+                    } else if (hound_jump==5) {
+                        hound_vMovement+=2;
+                    } else if (hound_jump==6) {
+                        hound_vMovement+=3;
+                    } else if (hound_jump==7) {
+                        hound_jump_flag=0;
+                        hound_jump=0;
+                    }
+                    print_mob_hound_p2();
+
+                }
+                if ((hound_hMovement<=8&hound_hMovement>=-5&g_jump>30&lose_lives_delay_flag==0&hound_jump_flag!=1&shield_flag!=1)|(hound_hMovement<=8&hound_hMovement>=-5&g_jump>26&lose_lives_delay_flag==0&hound_jump_flag==1&shield_flag!=1)) {     //a life is lost if recks has an vertical and horizontal position equal to one of the pixels corresponding to the hounds position
+                    lives--;
+                    freeze();
+                    lose_lives_delay_flag=1;
+                }
+                if (shoot_flag==1&bullet_height>hound_vMovement&bullet_height<hound_vMovement+8) {
+                    print_mob_hound_dead();
+                    print_hound_flag=0;
+                    hound_hMovement=90;
+                    kill_score+=5;
+                }
+            }
+            if (print_bear_flag==1) {
+                if (i%4>=2) {
+                    print_mob_bear_p1();
+                } else if (i%4<2) {
+                    print_mob_bear_p2();
+                }
+                if (joystick.direction==LEFT) {
+                    bear_movement+=recks_movement;
+                } else if (joystick.direction==RIGHT) {
+                    bear_movement-=recks_movement;
+                }
+                bear_movement-=2;
+                if (bear_movement<=-15) {
+                    bear_movement=100;
+                    print_bear_flag=0;
+                }
+                if (bear_movement<=8&bear_movement>=-5&g_jump>26&lose_lives_delay_flag==0&shield_flag!=1) {     //a life is lost if recks has an vertical and horizontal position equal to one of the pixels corresponding to the bears position
+                    lives--;
+                    freeze();
+                    lose_lives_delay_flag=1;
+                }
+                if (shoot_flag==1&bullet_height>38) {
+                    bear_lives++;
+                    if (bear_lives==12-3*g_g1-3*g_g2) {
+                        print_mob_bear_dead();
+                        print_bear_flag=0;
+                        bear_movement=100;
+                        kill_score+=7;
+                        bear_lives=0;
+                    }
+                }
+            }
+            if (print_bird_flag==1) {
+                if (i%8>=4) {
+                    print_mob_bird_p1();
+                } else if (i%8<4) {
+                    print_mob_bird_p2();
+                }
+                bird_hMovement-=2;
+                if (joystick.direction==LEFT) {
+                    bird_hMovement+=recks_movement;
+                } else if (joystick.direction==RIGHT) {
+                    bird_hMovement-=recks_movement;
+                }
+                if (random_num%6==0) {
+                    bird_vMovement--;
+                } else if (random_num%6>=4) {
+                    bird_vMovement++;
+                }
+                if (bird_vMovement>=37) {
+                    bird_vMovement=37;
+                } else if (bird_vMovement<=10) {
+                    bird_vMovement=10;
+                }
+                if (bird_hMovement<=-10) {
+                    print_bird_flag=0;
+                    bird_hMovement=100;
+                    bird_vMovement=20;
+                    bear_lives=0;
+                }
+                if ((bird_hMovement>=0&bird_hMovement<=10)&(bird_vMovement+5>=g_jump&bird_vMovement+5<=g_jump+10&lose_lives_delay_flag==0)&shield_flag!=1) {
+                    lives--;
+                    freeze();
+                    lose_lives_delay_flag=1;
+                }
+                if (shoot_flag==1&(bullet_height==bird_vMovement+5|bullet_height==bird_vMovement+4)) {
+                    print_mob_bird_dead();
+                    print_bird_flag=0;
+                    bird_hMovement=100;
+                    bird_vMovement=20;
+                    kill_score+=5;
+                }
+            }
+            if (print_cactus_flag==1) {
+                print_cactus();
+                if (joystick.direction==LEFT) {
+                    cactus_movement+=recks_movement;
+                } else if (joystick.direction==RIGHT) {
+                    cactus_movement-=recks_movement;
+                }
+                if (cactus_movement<=-10) {
+                    cactus_movement=110;
+                    print_cactus_flag=0;
+                }
+                if (cactus_movement<=10&cactus_movement>=2&g_jump>32&lose_lives_delay_flag==0&shield_flag!=1) {
+                    lives--;
+                    freeze();
+                    lose_lives_delay_flag=1;
+                }
+            }
+
+            if (print_t_rex_flag==1) {
+                if (joystick.direction==LEFT) {
+                    t_rex_movement+=recks_movement;
+                } else if (joystick.direction==RIGHT) {
+                    t_rex_movement-=recks_movement;
+                }
+                if (random_num%4==0&print_fire_ball_flag!=1) {
+                    t_rex_movement+=3;
+                    print_t_rex_moving();
+                } else if (random_num%4==1&print_fire_ball_flag!=1) {
+                    t_rex_movement-=3;
+                    print_t_rex_moving();
+                } else if (random_num%4>1&print_fire_ball_flag!=1) {
+                    print_t_rex();
+                }
+                if (t_rex_movement<=-100) {
+                    t_rex_movement=120;
+                    print_t_rex_flag=0;
+                }
+                if (t_rex_movement<=6&t_rex_movement>=2&lose_lives_delay_flag==0&shield_flag!=1) {
+                    lives--;
+                    freeze();
+                    lose_lives_delay_flag=1;
+                }
+            }
+            if (print_fire_ball_flag==1) {
+                print_t_rex_attack();
+                fire_ball_hMovement-=4;
+                if (joystick.direction==LEFT) {
+                    fire_ball_hMovement+=recks_movement;
+                } else if (joystick.direction==RIGHT) {
+                    fire_ball_hMovement-=recks_movement;
+                }
+                if (i%4>=2) {
+                    print_fire_ball_p1();
+                } else {
+                    print_fire_ball_p2();
+                }
+                if (fire_ball_vMovement>=37) {
+                    fire_ball_vMovement=37;
+                }
+                if (random_num%3==0) {
+                    fire_ball_vMovement++;
+                } else if (random_num%3==1) {
+                    fire_ball_vMovement+=2;
+                }
+                if (fire_ball_hMovement<=-10) {
+                    fire_ball_vMovement=27;
+                    fire_ball_hMovement=t_rex_movement-6;
+                    print_fire_ball_flag=0;
+                    fire_on_screen=0;
+                }
+                if (fire_ball_hMovement>=3&fire_ball_hMovement<=8&fire_ball_vMovement>g_jump-5&fire_ball_vMovement<g_jump+10&lose_lives_delay_flag==0&shield_flag!=1) {
+                    lives--;
+                    freeze();
+                    lose_lives_delay_flag=1;
+                }
+                if (shoot_flag==1) {
+                    t_rex_lives++;
+                    if (t_rex_lives>=27-3*g_g1-3*g_g2) {
+                        print_t_rex_flag=0;
+                        t_rex_movement=120;
+                        kill_score+=20;
+                        t_rex_lives=0;
+                    }
+                }
+            }
+            if (print_chasm_flag==1) {
+                print_chasm();
+                if (joystick.direction==LEFT) {
+                    chasm_movement+=recks_movement;
+                } else if (joystick.direction==RIGHT) {
+                    chasm_movement-=recks_movement;
+                }
+                if (chasm_movement<3&chasm_movement>-5&jump_flag!=1) {
+                    lives-=5;
+                    falling_animation();
+                    chasm_movement=85;
+                    if (chasm_movement<-8) {
+                        print_chasm_flag=0;
+                    }
+
+                }
+            }
+            if (print_heart_flag==1) {
+                print_heart();
+                if (joystick.direction==LEFT) {
+                    heart_movement+=recks_movement;
+                } else if (joystick.direction==RIGHT) {
+                    heart_movement-=recks_movement;
+                }
+                if (heart_movement<=7&g_jump>26) {
+                    heart_movement=90;
+                    print_heart_flag=0;
+                    lives++;
+                }
+            }
+            if (print_ammo_flag==1) {
+                print_ammo_pickUp();
+                if (joystick.direction==LEFT) {
+                    ammo_movement+=recks_movement;
+                } else if (joystick.direction==RIGHT) {
+                    ammo_movement-=recks_movement;
+                }
+                if (ammo_movement<=7&g_jump>26) {
+                    ammo_movement=90;
+                    print_ammo_flag=0;
+                    ammo+=5;
+                    ammo+=rand()%7;
+                }
+            }
+            if (print_speed_boost_flag==1) {
+                print_speed_boost();
+                if (joystick.direction==LEFT) {
+                    speed_boost_movement+=recks_movement;
+                } else if (joystick.direction==RIGHT) {
+                    speed_boost_movement-=recks_movement;
+                }
+                if (speed_boost_movement<=5&g_jump>26) {
+                    speed_boost_movement=120;
+                    print_speed_boost_flag=0;
+                    recks_movement++;
+                }
+            }
+            if (recks_movement>=3&i%150==0) {
+                recks_movement=2;
+            }
+            if (lives<=0) {
+                run_game=Game_over();                       //calls game over returns 1 if continue was selected returns 0 if back to menu
+            }
+            if (swJoy==1) {
+                Pause();
+            }
+            if (shield_flag==1) {
+                print_recks_shield();
+                recks_movement=0;
+                jump_flag=0;
+                shoot_flag=0;
+                shield_counter++;
+                if (shield_counter>=10) {
+                    shield_counter=0;
+                    recks_movement=2;
+                    shield_score+=10;
+                    shield_flag=0;
+                }
+            }
+
+            if (difficulty==-1&story_mode_flag==1) {
+                if (i>70) {
+                    initialize_values();
+                    return;
+                }
+            } else if (difficulty==0&story_mode_flag==1) {
+                if (score>60) {
+                    return;
+                }
+            } else if (difficulty==1&story_mode_flag==1) {
+                if (score>200) {
+                    return;
+                }
+            } else if (difficulty==2&story_mode_flag==1) {
+                if (score>500) {
+                    return;
+                }
+            } else if (difficulty==3&story_mode_flag==1) {
+                print_t_rex_flag=1;
+                g_story_mode_win=0;
+                if (t_rex_lives>=24) {
+                    print_t_rex_flag=0;
+                    g_story_mode_win=1;
+                    return;
+                }
+            }
+            i++;
+            print_clouds();
         }
         lcd.refresh();
         sleep();
     }
-Credits:
-Minigame:
-Continue:
-    wait(0.5);
-    while (1) {
-        if (g_Ticker_Game_flag) {
-            g_Ticker_Game_flag=0;
-            if (lives==0) {
-                goto GameOver;
-            }
-            if (swJoy==1) {
-                goto Pause;
-            }
-            lcd.clear();
-            updateJoystick();
-            game();
+}
 
-        }
-        i++;
-        lcd.refresh();
-        sleep();
-    }
-
-Pause:
+void Pause()
+{
     int q;
-    while (1) {
-        q++;
-        lcd.printString("PAUSED",25,3);
+    lcd.printString("PAUSED",25,3);
+    ticker_wait(10);
+    while (!swJoy) {
         if (g_Ticker_Menu_flag) {
             g_Ticker_Menu_flag=0;
             if (swJoy&q>5) {
                 q=0;
                 lcd.setBrightness(brightness);
                 led_bar();
-                goto Continue;
             } else if (q>=20) {
                 lcd.setBrightness(0);
                 led=0;
@@ -986,43 +2758,130 @@
         }
         sleep();
     }
-GameOver:
-    char buffer[14];
-    int p = sprintf(buffer,"%d",score);
-    FILE *Highscores = fopen("/sd/Highscores.txt", "w");
-    if (Highscores!=NULL) {
-        fprintf(Highscores, "%d\n", highscore);
-        fclose(Highscores);
-        green_led=0;
-    } else {
-        red_led=0;
+}
+
+void set_difficulty()
+{
+    switch(difficulty) {
+        case -1:
+            break;
+        case 0:
+            if (random_num<3000) {
+                print_cactus_flag=1;
+            } else if (random_num>=3000&random_num<6000) {
+                print_rat_flag=1;
+            } else if (random_num>=6000&random_num<6100) {
+                print_heart_flag=1;
+            }
+            break;
+        case 1:
+            if (random_num<3000) {
+                print_cactus_flag=1;
+            } else if (random_num>=3000&random_num<5000) {
+                print_rat_flag=1;
+            } else if (random_num>=5000&random_num<6000) {
+                print_bird_flag=1;
+            } else if (random_num>=6000&random_num<6050) {
+                print_heart_flag=1;
+            } else if (random_num>=6050&random_num<7000) {
+                print_hound_flag=1;
+            } else if (random_num>=7000&random_num<7200) {
+                print_ammo_flag=1;
+            }
+            break;
+        case 2:
+            if (random_num<3000) {
+                print_cactus_flag=1;
+            } else if (random_num>=3000&random_num<5000) {
+                print_rat_flag=1;
+            } else if (random_num>=5000&random_num<6000) {
+                print_bird_flag=1;
+            } else if (random_num>=6000&random_num<6040) {
+                print_heart_flag=1;
+            } else if (random_num>=6040&random_num<7000) {
+                print_hound_flag=1;
+            } else if (random_num>=7000&random_num<7200) {
+                print_ammo_flag=1;
+            } else if (random_num>=7200&random_num<8000) {
+                print_bear_flag=1;
+            } else if (random_num>=8000&random_num<8200) {
+                print_speed_boost_flag=1;
+            }
+            break;
+        case 3:
+            if (random_num<1000) {
+                print_bear_flag=1;
+            } else if (random_num>=1000&random_num<3000) {
+                print_bird_flag=1;
+            } else if (random_num>=3000&random_num<3010) {
+                print_heart_flag=1;
+            } else if (random_num>=3010&random_num<3050) {
+                print_ammo_flag=1;
+            } else if (random_num>=3050&random_num<6000) {
+                print_cactus_flag=1;
+            } else if (random_num>=6000&random_num<6100) {
+                print_ammo_flag=1;
+                print_t_rex_flag=1;
+            } else if (random_num>=6100&random_num<7000) {
+                print_rat_flag=1;
+            }  else if (random_num>=7000&random_num<8000) {
+                print_hound_flag=1;
+            }  else if (random_num>=8000&random_num<8200) {
+                print_speed_boost_flag=1;
+            }
+            break;
+        case 4:
+            if (random_num<1000) {
+                print_bear_flag=1;
+            } else if (random_num>=1000&random_num<3000) {
+                print_bird_flag=1;
+            } else if (random_num>=3000&random_num<3010) {
+                print_heart_flag=1;
+            } else if (random_num>=3010&random_num<3050) {
+                print_ammo_flag=1;
+            } else if (random_num>=3050&random_num<6000) {
+                print_cactus_flag=1;
+            } else if (random_num>=6000&random_num<6300) {
+                print_ammo_flag=1;
+                print_t_rex_flag=1;
+            } else if (random_num>=6300&random_num<7000) {
+                print_rat_flag=1;
+            }  else if (random_num>=7000&random_num<8000) {
+                print_hound_flag=1;
+            }  else if (random_num>=8000&random_num<8200) {
+                print_speed_boost_flag=1;
+            } else if (random_num>=8200&random_num<9000) {
+                print_chasm_flag=1;
+            }
+            break;
     }
+}
+
+int Game_over()
+{
     led_bar();
     lcd.clear();
-    for (int freeze=0; freeze<=30; freeze++) {
-        if (g_Ticker_Menu_flag) {
-            g_Ticker_Menu_flag=0;
-            lcd.clear();
-            lcd.printString("GAME",25,1);
-            lcd.printString("OVER",25,2);
-            lcd.refresh();
-        }
-        sleep();
-    }
-    green_led=1;
-    red_led=1;
+    sort_top_scores();
+    writeSD();
+    lcd.printString("GAME",25,1);
+    lcd.printString("OVER",25,2);
+    lcd.refresh();
+    ticker_wait(10);
     lcd.printString("A Retry",40,4);
     lcd.printString("B Back to Menu",0,5);
     lcd.refresh();
+    g_press_b_A_flag=0;
+    g_press_b_B_flag=0;
     while (1) {
-        if (g_Ticker_Menu_flag) {
-            g_Ticker_Menu_flag=0;
-            if (b_A==1) {
+        if (g_press_b_A_flag|g_press_b_B_flag) {
+            if (g_press_b_A_flag) {
                 initialize_values();
-                goto Continue;
-            } else if (b_B==1) {
+                g_press_b_A_flag=0;
+                return 1;
+            } else {
                 initialize_values();
-                goto Menu;
+                g_press_b_B_flag=0;
+                return 0;
             }
         }
         sleep();
@@ -1030,456 +2889,6 @@
 }
 
 
-// -----------------------------------------------------------------------------------------------------------------------------------------
-
-void game()
-{
-    led_bar();
-    print_score();
-    generate_random_number();
-    if (lose_lives_delay_flag==1) {
-        lives_delay_loop++;
-        if (lives_delay_loop>=10) {     //means that the delay between one life beeing lost and another one beeing lost is at least 1s
-            lives_delay_loop=0;
-            lose_lives_delay_flag=0;
-        }
-
-    }
-    if (ammo<=5) {
-        if (i%8>=4) {
-            print_ammo();
-        }
-    } else {
-        print_ammo();
-    }
-    if (b_B) {
-        jump_flag=1;
-    }
-    if (shoot_flag==0) {
-        bullet=9;
-        if (joystick.direction==DOWN) {
-            bullet_height=42;
-        } else {
-            bullet_height=g_jump+4;
-        }
-    } else if (shoot_flag==1&loop==0) {
-        loop++;
-        ammo--;
-        if (joystick.direction==DOWN&jump_flag!=1) {
-            bullet_height=42;
-        } else {
-            bullet_height=g_jump+4;
-        }
-    }
-    if (b_A) {
-        if (ammo<=0) {
-            shoot_flag=0;
-        } else {
-            shoot_flag=1;
-        }
-    }
-    if (random_num<1000) {
-        print_bear_flag=1;
-    } else if (random_num>=1000&random_num<3000) {
-        print_bird_flag=1;
-    } else if (random_num>=3000&random_num<3010) {
-        print_heart_flag=1;
-    } else if (random_num>=3010&random_num<3050) {
-        print_ammo_flag=1;
-    } else if (random_num>=3050&random_num<6000) {
-        print_cactus_flag=1;
-    } else if (random_num>=6000&random_num<6100) {
-        print_ammo_flag=1;
-        print_t_rex_flag=1;
-    } else if (random_num>=6100&random_num<7000) {
-        print_rat_flag=1;
-    } else if (random_num>=7000&random_num<800) {
-        print_hound_flag=1;
-    }
-
-    if (print_t_rex_flag==1&random_num%10==0&fire_on_screen==0) {
-        fire_on_screen=1;
-        print_fire_ball_flag=1;
-        fire_ball_hMovement=t_rex_movement-6;
-        fire_ball_vMovement=25;
-    }
-    if (joystick.direction==CENTRE&jump_flag!=1) {
-        lcd.setPixel(bullet,bullet_height);
-        lcd.setPixel(bullet+1,bullet_height);
-        print_recks_still_gun();
-        if (shoot_flag==1) {
-            bullet+=20;
-            if (bullet>=84) {
-                shoot_flag=0;
-                loop=0;
-            }
-        }
-    } else if (joystick.direction==LEFT&jump_flag!=1) {
-        if (run%2==0) {
-            print_recks_still_gun();
-        } else if (run%2==1) {
-            print_recks_moving_gun();
-        }
-        lcd.setPixel(bullet,bullet_height);
-        lcd.setPixel(bullet+1,bullet_height);
-        if (shoot_flag==1) {
-            bullet+=21;
-            if (bullet>=84) {
-                shoot_flag=0;
-                loop=0;
-            }
-        }
-        h_movement--;
-        run++;
-    } else if (joystick.direction==RIGHT&jump_flag!=1) {
-        if (run%2==0) {
-            print_recks_still_gun();
-        } else if (run%2==1) {
-            print_recks_moving_gun();
-        }
-        lcd.setPixel(bullet,bullet_height);
-        lcd.setPixel(bullet+1,bullet_height);
-        if (shoot_flag==1) {
-            bullet+=19;
-            if (bullet>=84) {
-                shoot_flag=0;
-                loop=0;
-            }
-        }
-        h_movement++;
-        run++;
-    } else if (joystick.direction==DOWN&jump_flag!=1) {
-        lcd.setPixel(bullet,bullet_height);
-        lcd.setPixel(bullet+1,bullet_height);
-        print_recks_crouch_gun();
-        bullet_height=42;
-        if (shoot_flag==1) {
-            bullet+=20;
-            if (bullet>=84) {
-                shoot_flag=0;
-                loop=0;
-            }
-        }
-    } else if (jump_flag==1) {
-        if (joystick.direction==LEFT) {
-            if (shoot_flag==1) {
-                bullet+=21;
-                if (bullet>=84) {
-                    shoot_flag=0;
-                    loop=0;
-                }
-            }
-            h_movement--;
-        } else if (joystick.direction==RIGHT) {
-            if (shoot_flag==1) {
-                bullet+=19;
-                if (bullet>=84) {
-                    shoot_flag=0;
-                    loop=0;
-                }
-            }
-            h_movement++;
-        } else if (joystick.direction==CENTRE) {
-            if (shoot_flag==1) {
-                bullet+=20;
-                if (bullet>=84) {
-                    shoot_flag=0;
-                    loop=0;
-                }
-            }
-        }
-        if (g_jump<=36&jumpUp==0&g_jump!=15) {
-            lcd.setPixel(bullet,bullet_height);
-            lcd.setPixel(bullet+1,bullet_height);
-            if (bullet>=84) {
-                shoot_flag=0;
-                loop=0;
-            }
-            print_recks_jump_gun();
-            g_jump-=6-accel;
-            accel++;
-        } else if (g_jump>=15&g_jump!=36) {
-            lcd.setPixel(bullet,bullet_height);
-            lcd.setPixel(bullet+1,bullet_height);
-            if (bullet>=84) {
-                shoot_flag=0;
-                loop=0;
-            }
-            print_recks_jump_gun();
-            g_jump+=6-accel;
-            accel--;
-            jumpUp=1;
-        } else if (g_jump==36&jumpUp==1) {
-            lcd.setPixel(bullet,bullet_height);
-            lcd.setPixel(bullet+1,bullet_height);
-            if (bullet>=84) {
-                shoot_flag=0;
-                loop=0;
-            }
-            print_recks_still_gun();
-            jump_flag=0;
-            jumpUp=0;
-            g_jump=36;
-            accel=0;
-        }
-    }
-    if (print_rat_flag==1) {
-        if (i%4>=2) {
-            print_mob_rat_p1();
-        } else if (i%4<2) {
-            print_mob_rat_p2();
-        }
-        if (joystick.direction==LEFT) {
-            rat_movement++;
-        } else if (joystick.direction==RIGHT) {
-            rat_movement--;
-        }
-        rat_movement-=3;
-        if (rat_movement<=-15) {
-            rat_movement=100;
-            print_rat_flag=0;
-        }
-        if (rat_movement<=5&rat_movement>=-3&g_jump>32&lose_lives_delay_flag==0) {     //a life is lost if recks has an vertical and horizontal position equal to one of the pixels corresponding to the bears position
-            lives--;
-            lose_lives_delay_flag=1;
-        }
-    }
-    if (print_hound_flag==1) {
-        if (i%4>=2&hound_jump_flag!=1) {
-            print_mob_hound_p1();
-        } else if (i%4<2&hound_jump_flag!=1) {
-            print_mob_hound_p2();
-        }
-        if (joystick.direction==LEFT) {
-            hound_hMovement++;
-        } else if (joystick.direction==RIGHT) {
-            hound_hMovement--;
-        }
-        hound_hMovement-=2;
-        if (hound_hMovement<=-15) {
-            hound_hMovement=100;
-            print_hound_flag=0;
-        }
-        if (random_num%10==0) {
-            hound_jump_flag=1;
-        }
-        if (hound_jump_flag==1) {
-            hound_jump++;
-            hound_hMovement--;
-            if (hound_jump==1) {
-                hound_vMovement-=3;
-            } else if (hound_jump==2) {
-                hound_vMovement-=2;
-            } else if (hound_jump==3) {
-                hound_vMovement-=1;
-            } else if (hound_jump==4) {
-                hound_vMovement+=1;
-            } else if (hound_jump==5) {
-                hound_vMovement+=2;
-            } else if (hound_jump==6) {
-                hound_vMovement+=3;
-            } else if (hound_jump==7) {
-                hound_jump_flag=0;
-                hound_jump=0;
-            }
-            print_mob_hound_p2();
-
-        }
-        if ((hound_hMovement<=8&hound_hMovement>=-5&g_jump>28&lose_lives_delay_flag==0&hound_jump_flag!=1)|(hound_hMovement<=8&hound_hMovement>=-5&g_jump>22&lose_lives_delay_flag==0&hound_jump_flag==1)) {     //a life is lost if recks has an vertical and horizontal position equal to one of the pixels corresponding to the hounds position
-            lives--;
-            lose_lives_delay_flag=1;
-        }
-        if (shoot_flag==1&bullet_height>hound_vMovement&bullet_height<hound_vMovement+8) {
-            print_mob_hound_dead();
-            print_hound_flag=0;
-            hound_hMovement=90;
-            kill_score+=5;
-        }
-    }
-    if (print_bear_flag==1) {
-        if (i%4>=2) {
-            print_mob_bear_p1();
-        } else if (i%4<2) {
-            print_mob_bear_p2();
-        }
-        if (joystick.direction==LEFT) {
-            bear_movement++;
-        } else if (joystick.direction==RIGHT) {
-            bear_movement--;
-        }
-        bear_movement-=2;
-        if (bear_movement<=-15) {
-            bear_movement=100;
-            print_bear_flag=0;
-        }
-        if (bear_movement<=8&bear_movement>=-5&g_jump>26&lose_lives_delay_flag==0) {     //a life is lost if recks has an vertical and horizontal position equal to one of the pixels corresponding to the bears position
-            lives--;
-            lose_lives_delay_flag=1;
-        }
-        if (shoot_flag==1&bullet_height>8) {
-            bear_lives++;
-            if (bear_lives==12-3*g_g1-3*g_g2) {
-                print_mob_bear_dead();
-                print_bear_flag=0;
-                bear_movement=100;
-                kill_score+=7;
-                bear_lives=0;
-            }
-        }
-    }
-    if (print_bird_flag==1) {
-        if (i%8>=4) {
-            print_mob_bird_p1();
-        } else if (i%8<4) {
-            print_mob_bird_p2();
-        }
-        bird_hMovement-=2;
-        if (joystick.direction==LEFT) {
-            bird_hMovement++;
-        } else if (joystick.direction==RIGHT) {
-            bird_hMovement--;
-        }
-        if (random_num%6==0) {
-            bird_vMovement--;
-        } else if (random_num%6>=4) {
-            bird_vMovement++;
-        }
-        if (bird_vMovement>=37) {
-            bird_vMovement=37;
-        } else if (bird_vMovement<=10) {
-            bird_vMovement=10;
-        }
-        if (bird_hMovement<=-10) {
-            print_bird_flag=0;
-            bird_hMovement=100;
-            bird_vMovement=20;
-            bear_lives=0;
-        }
-        if ((bird_hMovement>=0&bird_hMovement<=10)&(bird_vMovement+5>=g_jump&bird_vMovement+5<=g_jump+10&lose_lives_delay_flag==0)) {
-            lives--;
-            lose_lives_delay_flag=1;
-        }
-        if (shoot_flag==1&(bullet_height==bird_vMovement+5|bullet_height==bird_vMovement+4)) {
-            print_mob_bird_dead();
-            print_bird_flag=0;
-            bird_hMovement=100;
-            bird_vMovement=20;
-            kill_score+=5;
-        }
-    }
-    if (print_cactus_flag==1) {
-        print_cactus();
-        if (joystick.direction==LEFT) {
-            cactus_movement++;
-        } else if (joystick.direction==RIGHT) {
-            cactus_movement--;
-        }
-        if (cactus_movement<=-10) {
-            cactus_movement=110;
-            print_cactus_flag=0;
-        }
-        if (cactus_movement<=10&cactus_movement>=2&g_jump>32&lose_lives_delay_flag==0) {
-            lives--;
-            lose_lives_delay_flag=1;
-        }
-    }
-
-    if (print_t_rex_flag==1) {
-        if (joystick.direction==LEFT) {
-            t_rex_movement++;
-        } else if (joystick.direction==RIGHT) {
-            t_rex_movement--;
-        }
-        if (random_num%4==0&print_fire_ball_flag!=1) {
-            t_rex_movement+=2;
-            print_t_rex_moving();
-        } else if (random_num%4==1&print_fire_ball_flag!=1) {
-            t_rex_movement-=2;
-            print_t_rex_moving();
-        } else if (random_num%4>1&print_fire_ball_flag!=1) {
-            print_t_rex();
-        }
-        if (t_rex_movement<=-100) {
-            t_rex_movement=120;
-            print_t_rex_flag=0;
-        }
-        if (t_rex_movement<=6&t_rex_movement>=2&lose_lives_delay_flag==0) {
-            lives--;
-            lose_lives_delay_flag=1;
-        }
-    }
-    if (print_fire_ball_flag==1) {
-        print_t_rex_attack();
-        fire_ball_hMovement-=2;
-        if (joystick.direction==LEFT) {
-            fire_ball_hMovement++;
-        } else if (joystick.direction==RIGHT) {
-            fire_ball_hMovement--;
-        }
-        if (i%4>=2) {
-            print_fire_ball_p1();
-        } else {
-            print_fire_ball_p2();
-        }
-        if (fire_ball_vMovement>=37) {
-            fire_ball_vMovement=37;
-        }
-        if (random_num%3==0) {
-            fire_ball_vMovement++;
-        } else if (random_num%3==1) {
-            fire_ball_vMovement+=2;
-        }
-        if (fire_ball_hMovement<=-10) {
-            fire_ball_vMovement=27;
-            fire_ball_hMovement=t_rex_movement-6;
-            print_fire_ball_flag=0;
-            fire_on_screen=0;
-        }
-        if (fire_ball_hMovement>=3&fire_ball_hMovement<=8&fire_ball_vMovement>g_jump-5&fire_ball_vMovement<g_jump+10&lose_lives_delay_flag==0) {
-            lives--;
-            lose_lives_delay_flag=1;
-        }
-        if (shoot_flag==1) {
-            t_rex_lives++;
-            if (t_rex_lives>=24-3*g_g1-3*g_g2) {
-                print_t_rex_flag=0;
-                t_rex_movement=120;
-                kill_score+=20;
-                t_rex_lives=0;
-            }
-        }
-    }
-    if (print_heart_flag==1) {
-        print_heart();
-        if (joystick.direction==LEFT) {
-            heart_movement++;
-        } else if (joystick.direction==RIGHT) {
-            heart_movement--;
-        }
-        if (heart_movement<=7&g_jump>26) {
-            heart_movement=90;
-            print_heart_flag=0;
-            lives++;
-        }
-    }
-    if (print_ammo_flag==1) {
-        print_ammo_pickUp();
-        if (joystick.direction==LEFT) {
-            ammo_movement++;
-        } else if (joystick.direction==RIGHT) {
-            ammo_movement--;
-        }
-        if (ammo_movement<=7&g_jump>26) {
-            ammo_movement=90;
-            print_ammo_flag=0;
-            ammo+=5;
-            ammo+=rand()%7;
-        }
-    }
-    print_clouds();
-    ground();
-}
-
 void print_ammo()
 {
     for(int c=0; c<=2; c++) { // 2 beacause the the loop stats from 0 but the array size from 1
@@ -1628,7 +3037,7 @@
 
 void print_score()
 {
-    score=h_movement/10+kill_score;
+    score=h_movement/10+kill_score-shield_score;
     if (score<=0) {
         score=0;
     }
@@ -1893,4 +3302,4 @@
         }
     }
     lcd.refresh();
-}
\ No newline at end of file
+}