contains my game for the embedded systems project 2645

Dependencies:   mbed FXOS8700CQQQ

Committer:
OmarAlebiary
Date:
Thu Mar 28 11:21:39 2019 +0000
Revision:
10:7323785c071c
Parent:
9:edb39a8334ee
Child:
11:d4aaa959bb20
fixed LED lights on main menu and instructions menu

Who changed what in which revision?

UserRevisionLine numberNew contents of line
OmarAlebiary 6:958376d55d70 1 #include "RocketRacer.h"
OmarAlebiary 6:958376d55d70 2
OmarAlebiary 8:b547037f42be 3 Gamepad Mypad;
OmarAlebiary 9:edb39a8334ee 4 const int enemy[11][9] = {
OmarAlebiary 9:edb39a8334ee 5 { 0,0,0,0,1,0,0,0,0 },
OmarAlebiary 9:edb39a8334ee 6 { 0,0,0,1,1,1,0,0,0 },
OmarAlebiary 9:edb39a8334ee 7 { 0,0,1,1,1,1,1,0,0 },
OmarAlebiary 9:edb39a8334ee 8 { 0,1,1,1,1,1,1,1,0 },
OmarAlebiary 9:edb39a8334ee 9 { 0,1,1,1,1,1,1,1,0 },
OmarAlebiary 9:edb39a8334ee 10 { 0,1,1,1,1,1,1,1,0 },
OmarAlebiary 9:edb39a8334ee 11 { 1,1,1,1,1,1,1,1,1 },
OmarAlebiary 9:edb39a8334ee 12 { 1,1,1,1,1,1,1,1,1 },
OmarAlebiary 9:edb39a8334ee 13 { 1,1,1,1,1,1,1,1,1 },
OmarAlebiary 9:edb39a8334ee 14 { 1,1,1,1,1,1,1,1,1 },
OmarAlebiary 9:edb39a8334ee 15 { 1,1,1,1,1,1,1,1,1 },
OmarAlebiary 9:edb39a8334ee 16 };
OmarAlebiary 9:edb39a8334ee 17
OmarAlebiary 9:edb39a8334ee 18 const int rocket[11][9] = {
OmarAlebiary 9:edb39a8334ee 19 { 0,0,0,0,1,0,0,0,0 },
OmarAlebiary 9:edb39a8334ee 20 { 0,0,0,1,1,1,0,0,0 },
OmarAlebiary 9:edb39a8334ee 21 { 0,0,1,1,1,1,1,0,0 },
OmarAlebiary 9:edb39a8334ee 22 { 0,1,1,1,1,1,1,1,0 },
OmarAlebiary 9:edb39a8334ee 23 { 0,1,1,1,1,1,1,1,0 },
OmarAlebiary 9:edb39a8334ee 24 { 0,1,1,1,1,1,1,1,0 },
OmarAlebiary 9:edb39a8334ee 25 { 0,0,1,1,1,1,1,0,0 },
OmarAlebiary 9:edb39a8334ee 26 { 0,0,0,1,1,1,0,0,0 },
OmarAlebiary 9:edb39a8334ee 27 { 0,0,0,0,1,0,0,0,0 },
OmarAlebiary 9:edb39a8334ee 28 { 0,0,1,1,1,1,1,0,0 },
OmarAlebiary 9:edb39a8334ee 29 { 0,0,1,1,1,1,1,0,0 },
OmarAlebiary 9:edb39a8334ee 30 };
OmarAlebiary 9:edb39a8334ee 31
OmarAlebiary 9:edb39a8334ee 32 int enemy_0_pos, enemy_1_pos, enemy_phase;
OmarAlebiary 9:edb39a8334ee 33 int Joy_X;
OmarAlebiary 9:edb39a8334ee 34 int game_speed = 0;
OmarAlebiary 9:edb39a8334ee 35 int score = 0;
OmarAlebiary 9:edb39a8334ee 36 char POS=2;
OmarAlebiary 9:edb39a8334ee 37 bool enemy_dead = true;
OmarAlebiary 9:edb39a8334ee 38 bool control = true;
OmarAlebiary 9:edb39a8334ee 39
OmarAlebiary 9:edb39a8334ee 40
OmarAlebiary 6:958376d55d70 41
OmarAlebiary 7:7e50cac5e0f4 42 void RocketRacer::MainGameDisplay(N5110 &lcd){
OmarAlebiary 7:7e50cac5e0f4 43
OmarAlebiary 6:958376d55d70 44 lcd.clear();
OmarAlebiary 9:edb39a8334ee 45 lcd.drawRect(0,0,50,47,FILL_TRANSPARENT);
OmarAlebiary 7:7e50cac5e0f4 46 char buffer[14];
OmarAlebiary 9:edb39a8334ee 47 char buffer1[14];
OmarAlebiary 10:7323785c071c 48 char buffer2[14];
OmarAlebiary 10:7323785c071c 49 char buffer3[14];
OmarAlebiary 10:7323785c071c 50
OmarAlebiary 10:7323785c071c 51 //dispaly score
OmarAlebiary 7:7e50cac5e0f4 52 sprintf(buffer,"score");
OmarAlebiary 7:7e50cac5e0f4 53 lcd.printString(buffer,55,0);
OmarAlebiary 9:edb39a8334ee 54 sprintf(buffer1,"%d",score);
OmarAlebiary 9:edb39a8334ee 55 lcd.printString(buffer1,58,1);
OmarAlebiary 10:7323785c071c 56 //dispaly level
OmarAlebiary 10:7323785c071c 57 sprintf(buffer2,"Level");
OmarAlebiary 10:7323785c071c 58 lcd.printString(buffer2,55,3);
OmarAlebiary 10:7323785c071c 59 sprintf(buffer3,"%d",game_speed);
OmarAlebiary 10:7323785c071c 60 lcd.printString(buffer3,58,4);
OmarAlebiary 6:958376d55d70 61 lcd.refresh();
OmarAlebiary 7:7e50cac5e0f4 62
OmarAlebiary 7:7e50cac5e0f4 63 }
OmarAlebiary 7:7e50cac5e0f4 64
OmarAlebiary 9:edb39a8334ee 65 void RocketRacer::GameLoop(N5110 &lcd,Gamepad &pad){
OmarAlebiary 8:b547037f42be 66
OmarAlebiary 10:7323785c071c 67 lcd.clear();
OmarAlebiary 8:b547037f42be 68
OmarAlebiary 8:b547037f42be 69 Direction d=pad.get_direction();
OmarAlebiary 8:b547037f42be 70
OmarAlebiary 9:edb39a8334ee 71 if(d==E && POS!=3 && control==true){
OmarAlebiary 9:edb39a8334ee 72 POS++;
OmarAlebiary 9:edb39a8334ee 73 control = false;
OmarAlebiary 9:edb39a8334ee 74 printf("its Right\n");
OmarAlebiary 8:b547037f42be 75 }
OmarAlebiary 9:edb39a8334ee 76 else if(d==W && POS!=1 && control==true){
OmarAlebiary 9:edb39a8334ee 77 POS--;
OmarAlebiary 9:edb39a8334ee 78 control = false;
OmarAlebiary 9:edb39a8334ee 79 printf("its left\n");
OmarAlebiary 8:b547037f42be 80 }
OmarAlebiary 8:b547037f42be 81 else if(d==CENTRE){
OmarAlebiary 9:edb39a8334ee 82 control = true;
OmarAlebiary 9:edb39a8334ee 83 printf("its middle\n");
OmarAlebiary 9:edb39a8334ee 84 }
OmarAlebiary 9:edb39a8334ee 85
OmarAlebiary 9:edb39a8334ee 86 player_position(lcd,POS);
OmarAlebiary 9:edb39a8334ee 87
OmarAlebiary 9:edb39a8334ee 88 srand(time(NULL));
OmarAlebiary 9:edb39a8334ee 89
OmarAlebiary 9:edb39a8334ee 90 if (enemy_dead){
OmarAlebiary 9:edb39a8334ee 91 enemy_0_pos = POS;
OmarAlebiary 9:edb39a8334ee 92 enemy_1_pos = (rand() % 3)+1;
OmarAlebiary 9:edb39a8334ee 93 enemy_phase = 0;
OmarAlebiary 9:edb39a8334ee 94 enemy_dead = false;
OmarAlebiary 9:edb39a8334ee 95 }
OmarAlebiary 9:edb39a8334ee 96 enemy_position(lcd,enemy_0_pos, enemy_phase);
OmarAlebiary 9:edb39a8334ee 97 enemy_phase++;
OmarAlebiary 9:edb39a8334ee 98 enemy_position(lcd,enemy_1_pos, enemy_phase);
OmarAlebiary 9:edb39a8334ee 99 enemy_phase++;
OmarAlebiary 9:edb39a8334ee 100
OmarAlebiary 9:edb39a8334ee 101 if (enemy_phase>22 && ((enemy_0_pos == POS) || (enemy_1_pos == POS)) ){
OmarAlebiary 10:7323785c071c 102 EndGame(pad,lcd);
OmarAlebiary 9:edb39a8334ee 103 }
OmarAlebiary 9:edb39a8334ee 104 if (enemy_phase>40){
OmarAlebiary 9:edb39a8334ee 105 enemy_dead = true;
OmarAlebiary 9:edb39a8334ee 106 score++;
OmarAlebiary 9:edb39a8334ee 107 }
OmarAlebiary 10:7323785c071c 108
OmarAlebiary 10:7323785c071c 109 Game_difficulty();
OmarAlebiary 10:7323785c071c 110
OmarAlebiary 9:edb39a8334ee 111 lcd.refresh();
OmarAlebiary 8:b547037f42be 112 }
OmarAlebiary 8:b547037f42be 113
OmarAlebiary 9:edb39a8334ee 114
OmarAlebiary 10:7323785c071c 115 //adds difficulty to the game after proceeding each level
OmarAlebiary 10:7323785c071c 116 void RocketRacer::Game_difficulty(){
OmarAlebiary 10:7323785c071c 117
OmarAlebiary 10:7323785c071c 118 if (score>=0 && score<=5){
OmarAlebiary 10:7323785c071c 119 game_speed = 1;
OmarAlebiary 10:7323785c071c 120 wait(0.09);
OmarAlebiary 10:7323785c071c 121 }
OmarAlebiary 10:7323785c071c 122 if (score>5 && score<=10){
OmarAlebiary 10:7323785c071c 123 game_speed = 2;
OmarAlebiary 10:7323785c071c 124 wait(0.07);
OmarAlebiary 10:7323785c071c 125 }
OmarAlebiary 10:7323785c071c 126 if (score>10 && score<=20){
OmarAlebiary 10:7323785c071c 127 game_speed = 3;
OmarAlebiary 10:7323785c071c 128 wait(0.06);
OmarAlebiary 10:7323785c071c 129 }
OmarAlebiary 10:7323785c071c 130 if (score>20 && score<=30){
OmarAlebiary 10:7323785c071c 131 game_speed = 4;
OmarAlebiary 10:7323785c071c 132 wait(0.05);
OmarAlebiary 10:7323785c071c 133 }
OmarAlebiary 10:7323785c071c 134 if (score>30){
OmarAlebiary 10:7323785c071c 135 game_speed = 5;
OmarAlebiary 10:7323785c071c 136 wait(0.03);
OmarAlebiary 10:7323785c071c 137 }
OmarAlebiary 10:7323785c071c 138 }
OmarAlebiary 9:edb39a8334ee 139
OmarAlebiary 9:edb39a8334ee 140
OmarAlebiary 9:edb39a8334ee 141 void RocketRacer::enemy_position(N5110 &lcd,int place, int phase){
OmarAlebiary 9:edb39a8334ee 142 if (place==1){
OmarAlebiary 9:edb39a8334ee 143 lcd.drawSprite(2,phase,11,9,(int *)enemy);
OmarAlebiary 9:edb39a8334ee 144 }
OmarAlebiary 9:edb39a8334ee 145
OmarAlebiary 9:edb39a8334ee 146 if (place==2){
OmarAlebiary 9:edb39a8334ee 147 lcd.drawSprite(18,phase,11,9,(int *)enemy);
OmarAlebiary 9:edb39a8334ee 148 }
OmarAlebiary 9:edb39a8334ee 149
OmarAlebiary 9:edb39a8334ee 150 if (place==3){
OmarAlebiary 9:edb39a8334ee 151 lcd.drawSprite(34,phase,11,9,(int *)enemy);
OmarAlebiary 9:edb39a8334ee 152 }
OmarAlebiary 9:edb39a8334ee 153 lcd.refresh();
OmarAlebiary 9:edb39a8334ee 154
OmarAlebiary 9:edb39a8334ee 155 }
OmarAlebiary 9:edb39a8334ee 156
OmarAlebiary 9:edb39a8334ee 157 void RocketRacer::player_position(N5110 &lcd,char RocketPosition){
OmarAlebiary 9:edb39a8334ee 158
OmarAlebiary 9:edb39a8334ee 159 MainGameDisplay(lcd);
OmarAlebiary 9:edb39a8334ee 160
OmarAlebiary 9:edb39a8334ee 161
OmarAlebiary 9:edb39a8334ee 162 if (RocketPosition==1){
OmarAlebiary 9:edb39a8334ee 163 lcd.drawSprite(2,34,11,9,(int *)rocket);
OmarAlebiary 9:edb39a8334ee 164 }
OmarAlebiary 9:edb39a8334ee 165 if (RocketPosition==2){
OmarAlebiary 9:edb39a8334ee 166 lcd.drawSprite(18,34,11,9,(int *)rocket);
OmarAlebiary 9:edb39a8334ee 167 }
OmarAlebiary 9:edb39a8334ee 168 if (RocketPosition==3){
OmarAlebiary 9:edb39a8334ee 169 lcd.drawSprite(34,34,11,9,(int *)rocket);
OmarAlebiary 9:edb39a8334ee 170 }
OmarAlebiary 9:edb39a8334ee 171 lcd.refresh();
OmarAlebiary 9:edb39a8334ee 172 }
OmarAlebiary 9:edb39a8334ee 173
OmarAlebiary 9:edb39a8334ee 174
OmarAlebiary 10:7323785c071c 175 void RocketRacer::EndGame(Gamepad &pad,N5110 &lcd){
OmarAlebiary 10:7323785c071c 176 lcd.clear();
OmarAlebiary 10:7323785c071c 177 char buffer1[14];
OmarAlebiary 8:b547037f42be 178 lcd.printString("Game over!!!",5,0);
OmarAlebiary 10:7323785c071c 179 lcd.printString("Better Luck ",2,1);
OmarAlebiary 10:7323785c071c 180 lcd.printString("next time",2,2);
OmarAlebiary 10:7323785c071c 181 lcd.printString("High score:",2,3);
OmarAlebiary 10:7323785c071c 182
OmarAlebiary 10:7323785c071c 183 sprintf(buffer1,"%d",score);
OmarAlebiary 10:7323785c071c 184 lcd.printString(buffer1,20,4);
OmarAlebiary 10:7323785c071c 185 pad.leds_on();
OmarAlebiary 10:7323785c071c 186
OmarAlebiary 10:7323785c071c 187 lcd.refresh();
OmarAlebiary 10:7323785c071c 188 wait(500);
OmarAlebiary 7:7e50cac5e0f4 189
OmarAlebiary 7:7e50cac5e0f4 190 }