contains my game for the embedded systems project 2645

Dependencies:   mbed FXOS8700CQQQ

Committer:
OmarAlebiary
Date:
Wed Mar 27 00:04:00 2019 +0000
Revision:
9:edb39a8334ee
Parent:
8:b547037f42be
Child:
10:7323785c071c
added the game loop method

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 7:7e50cac5e0f4 48 // int score=64;
OmarAlebiary 7:7e50cac5e0f4 49
OmarAlebiary 7:7e50cac5e0f4 50 sprintf(buffer,"score");
OmarAlebiary 7:7e50cac5e0f4 51 lcd.printString(buffer,55,0);
OmarAlebiary 9:edb39a8334ee 52 sprintf(buffer1,"%d",score);
OmarAlebiary 9:edb39a8334ee 53 lcd.printString(buffer1,58,1);
OmarAlebiary 6:958376d55d70 54 lcd.refresh();
OmarAlebiary 7:7e50cac5e0f4 55
OmarAlebiary 7:7e50cac5e0f4 56 }
OmarAlebiary 7:7e50cac5e0f4 57
OmarAlebiary 9:edb39a8334ee 58 void RocketRacer::GameLoop(N5110 &lcd,Gamepad &pad){
OmarAlebiary 8:b547037f42be 59
OmarAlebiary 9:edb39a8334ee 60 lcd.clear();
OmarAlebiary 9:edb39a8334ee 61 //MainGameDisplay(lcd);
OmarAlebiary 9:edb39a8334ee 62 // POS=2;
OmarAlebiary 9:edb39a8334ee 63 // score=0;
OmarAlebiary 9:edb39a8334ee 64 // enemy_dead = true;
OmarAlebiary 9:edb39a8334ee 65 // control = true;
OmarAlebiary 8:b547037f42be 66
OmarAlebiary 8:b547037f42be 67 Direction d=pad.get_direction();
OmarAlebiary 8:b547037f42be 68
OmarAlebiary 9:edb39a8334ee 69 if(d==E && POS!=3 && control==true){
OmarAlebiary 9:edb39a8334ee 70 POS++;
OmarAlebiary 9:edb39a8334ee 71 control = false;
OmarAlebiary 9:edb39a8334ee 72 printf("its Right\n");
OmarAlebiary 8:b547037f42be 73 }
OmarAlebiary 9:edb39a8334ee 74 else if(d==W && POS!=1 && control==true){
OmarAlebiary 9:edb39a8334ee 75 POS--;
OmarAlebiary 9:edb39a8334ee 76 control = false;
OmarAlebiary 9:edb39a8334ee 77 printf("its left\n");
OmarAlebiary 8:b547037f42be 78 }
OmarAlebiary 8:b547037f42be 79 else if(d==CENTRE){
OmarAlebiary 9:edb39a8334ee 80 control = true;
OmarAlebiary 9:edb39a8334ee 81 printf("its middle\n");
OmarAlebiary 9:edb39a8334ee 82 }
OmarAlebiary 9:edb39a8334ee 83
OmarAlebiary 9:edb39a8334ee 84 player_position(lcd,POS);
OmarAlebiary 9:edb39a8334ee 85
OmarAlebiary 9:edb39a8334ee 86 srand(time(NULL));
OmarAlebiary 9:edb39a8334ee 87
OmarAlebiary 9:edb39a8334ee 88 if (enemy_dead){
OmarAlebiary 9:edb39a8334ee 89 enemy_0_pos = POS;
OmarAlebiary 9:edb39a8334ee 90 enemy_1_pos = (rand() % 3)+1;
OmarAlebiary 9:edb39a8334ee 91 enemy_phase = 0;
OmarAlebiary 9:edb39a8334ee 92 enemy_dead = false;
OmarAlebiary 9:edb39a8334ee 93 }
OmarAlebiary 9:edb39a8334ee 94 enemy_position(lcd,enemy_0_pos, enemy_phase);
OmarAlebiary 9:edb39a8334ee 95 enemy_phase++;
OmarAlebiary 9:edb39a8334ee 96 enemy_position(lcd,enemy_1_pos, enemy_phase);
OmarAlebiary 9:edb39a8334ee 97 enemy_phase++;
OmarAlebiary 9:edb39a8334ee 98
OmarAlebiary 9:edb39a8334ee 99 if (enemy_phase>22 && ((enemy_0_pos == POS) || (enemy_1_pos == POS)) ){
OmarAlebiary 9:edb39a8334ee 100 EndGame(lcd);
OmarAlebiary 9:edb39a8334ee 101 }
OmarAlebiary 9:edb39a8334ee 102 if (enemy_phase>40){
OmarAlebiary 9:edb39a8334ee 103 enemy_dead = true;
OmarAlebiary 9:edb39a8334ee 104 score++;
OmarAlebiary 9:edb39a8334ee 105 }
OmarAlebiary 9:edb39a8334ee 106 wait(0.1);
OmarAlebiary 9:edb39a8334ee 107 lcd.refresh();
OmarAlebiary 8:b547037f42be 108 }
OmarAlebiary 8:b547037f42be 109
OmarAlebiary 9:edb39a8334ee 110
OmarAlebiary 9:edb39a8334ee 111
OmarAlebiary 9:edb39a8334ee 112
OmarAlebiary 9:edb39a8334ee 113
OmarAlebiary 9:edb39a8334ee 114 void RocketRacer::enemy_position(N5110 &lcd,int place, int phase){
OmarAlebiary 9:edb39a8334ee 115 if (place==1){
OmarAlebiary 9:edb39a8334ee 116 lcd.drawSprite(2,phase,11,9,(int *)enemy);
OmarAlebiary 9:edb39a8334ee 117 }
OmarAlebiary 9:edb39a8334ee 118
OmarAlebiary 9:edb39a8334ee 119 if (place==2){
OmarAlebiary 9:edb39a8334ee 120 lcd.drawSprite(18,phase,11,9,(int *)enemy);
OmarAlebiary 9:edb39a8334ee 121 }
OmarAlebiary 9:edb39a8334ee 122
OmarAlebiary 9:edb39a8334ee 123 if (place==3){
OmarAlebiary 9:edb39a8334ee 124 lcd.drawSprite(34,phase,11,9,(int *)enemy);
OmarAlebiary 9:edb39a8334ee 125 }
OmarAlebiary 9:edb39a8334ee 126 lcd.refresh();
OmarAlebiary 9:edb39a8334ee 127
OmarAlebiary 9:edb39a8334ee 128 }
OmarAlebiary 9:edb39a8334ee 129
OmarAlebiary 9:edb39a8334ee 130 void RocketRacer::player_position(N5110 &lcd,char RocketPosition){
OmarAlebiary 9:edb39a8334ee 131
OmarAlebiary 9:edb39a8334ee 132 MainGameDisplay(lcd);
OmarAlebiary 9:edb39a8334ee 133
OmarAlebiary 9:edb39a8334ee 134
OmarAlebiary 9:edb39a8334ee 135 if (RocketPosition==1){
OmarAlebiary 9:edb39a8334ee 136 lcd.drawSprite(2,34,11,9,(int *)rocket);
OmarAlebiary 9:edb39a8334ee 137 }
OmarAlebiary 9:edb39a8334ee 138 if (RocketPosition==2){
OmarAlebiary 9:edb39a8334ee 139 lcd.drawSprite(18,34,11,9,(int *)rocket);
OmarAlebiary 9:edb39a8334ee 140 }
OmarAlebiary 9:edb39a8334ee 141 if (RocketPosition==3){
OmarAlebiary 9:edb39a8334ee 142 lcd.drawSprite(34,34,11,9,(int *)rocket);
OmarAlebiary 9:edb39a8334ee 143 }
OmarAlebiary 9:edb39a8334ee 144 lcd.refresh();
OmarAlebiary 9:edb39a8334ee 145 }
OmarAlebiary 9:edb39a8334ee 146
OmarAlebiary 9:edb39a8334ee 147
OmarAlebiary 7:7e50cac5e0f4 148 void RocketRacer::EndGame(N5110 &lcd){
OmarAlebiary 9:edb39a8334ee 149 lcd.clear(); while(1){
OmarAlebiary 8:b547037f42be 150 lcd.printString("Game over!!!",5,0);
OmarAlebiary 8:b547037f42be 151 lcd.printString("Better Luck ",2,2);
OmarAlebiary 8:b547037f42be 152 lcd.printString("next time",2,3);
OmarAlebiary 9:edb39a8334ee 153 }
OmarAlebiary 9:edb39a8334ee 154 // lcd.refresh();
OmarAlebiary 7:7e50cac5e0f4 155
OmarAlebiary 7:7e50cac5e0f4 156 }