Kamikaze Race. Bin Mohd Kamaludin. 200876993.

Dependencies:   el14hzbm_Final_Project mbed

Fork of el14hzbm_FinalProject by ELEC2645 (2015/16)

Revision:
3:c2227b3e2f0c
Parent:
1:58b4f9dda460
--- a/main.cpp	Thu May 05 12:02:03 2016 +0000
+++ b/main.cpp	Tue Feb 20 11:56:52 2018 +0000
@@ -3,15 +3,8 @@
 @brief Program implementation
 */
 
-#include "mbed.h"
-#include "N5110.h"
-#include "tone.h"
 #include "main.h"
 
-#define PI 3.14159265359
-#define PAUSE 1
-#define PLAY 0
-
 int main()
 {
     lcd.init();                     //initialize the lcd
@@ -30,7 +23,7 @@
         finishView(g_score);                //state 6 - final score interface
     }
 }
-                                                                
+
 void intro()                                                        //state 0 - introduction interface
 {
     if(state==0)    {
@@ -120,8 +113,8 @@
                     playermovement(g_level);                                     //function to control the player's car movement
                     lcd.refresh();
                     checkLastCar(g_level);                                      //function to check when will the last last car go out from the screen to finish level
-                    
-                    if((y_move-(lastCar))>40)   {                               
+
+                    if((y_move-(lastCar))>40)   {
                         finishLevel(g_level);                                   //finish level when last car has passed through
                         break;
                     }
@@ -145,7 +138,7 @@
                     g_score=g_score+1;                                  //increase the score by 1 after every loop
                 } else {                                                //when game is paused
                     y_move=y_move-1;                                    //fix y_move to make the vehicle static
-                    game_interface(g_score,g_level,g_liveleft);         
+                    game_interface(g_score,g_level,g_liveleft);
                     lcd.printString("Paused",15,2);                     //display pause on the screen
                     lcd.refresh();
                 }
@@ -161,7 +154,7 @@
                 }
             }
             g_level=g_level+1;
-        }                                                               //when the game finished, move to finish View(display high score) 
+        }                                                               //when the game finished, move to finish View(display high score)
         state=6;
         finishView(g_score);
         state=0;                                                        //goes back to main menu
@@ -173,10 +166,10 @@
     if(state==3)    {
         pointer_x=5;
         pointer_y=7;
-        
+
         while(loop==0)    {
             sleepTimer.start(); // start timer
-            
+
             if(sleepTimer.read()<15)    {
                 lcd.printString("Instruction",13,1);
                 lcd.printString("Brightness",13,2);
@@ -187,11 +180,11 @@
                 lcd.refresh();
                 wait(0.1);
                 lcd.clear();
-                
+
                 if(g_button_flag==1)    {
                     g_button_flag=0;
                     loop=1;
-                    
+
                     if(pointer_y==7)   {
                         state=4;//go to instruction
                     } else if(pointer_y==15)   {
@@ -217,10 +210,10 @@
 void instructionView()                                                  //state 4 - how to play interface
 {
     if(state==4)    {
-        
+
         while(loop==0)    {
             sleepTimer.start(); // start timer
-            
+
             if(sleepTimer.read()<15)    {
                 lcd.printString("Make your way",0,0);                   //explain how to play the game
                 lcd.printString("through the",0,1);
@@ -231,7 +224,7 @@
                 lcd.refresh();
                 wait(0.1);
                 lcd.clear();
-                
+
                 if(g_button_flag==1)    {
                     g_button_flag=0;
                     loop=1;
@@ -248,10 +241,10 @@
         sleepTimer.reset(); // reset to 0
     }
     if(state==9)    {
-        
+
         while(loop==0)    {
             sleepTimer.start(); // start timer
-            
+
             if(sleepTimer.read()<15)    {
                 lcd.printString("You have ",0,0);
                 lcd.printString("three lives",0,1);
@@ -262,7 +255,7 @@
                 lcd.refresh();
                 wait(0.1);
                 lcd.clear();
-                
+
                 if(g_button_flag==1)    {
                     g_button_flag=0;
                     loop=1;
@@ -284,10 +277,10 @@
 {
     if(state==5)    {
         brightness_adjuster=5;
-        
+
         while(loop==0)    {
             sleepTimer.start(); // start timer
-            
+
             if(sleepTimer.read()<15)    {
                 brightnessController();
                 lcd.printString("Adjust",25,1);
@@ -300,7 +293,7 @@
                 lcd.refresh();
                 wait(0.1);
                 lcd.clear();
-                
+
                 if(g_button_flag==1)    {
                     g_button_flag=0;
                     loop=1;
@@ -321,10 +314,10 @@
 void finishView(int g_score)                                        //state 6 - final score interface
 {
     if(state==6)    {
-        
+
         while(loop==0)    {
             sleepTimer.start(); // start timer
-            
+
             if(sleepTimer.read()<15)    {
                 char buffer[14];
                 int length = sprintf(buffer,"%2d",g_score/20);      //display final score
@@ -335,7 +328,7 @@
                 lcd.refresh();
                 wait(0.1);
                 lcd.clear();
-                
+
                 if(g_button_flag==1)    {
                     g_button_flag=0;
                     loop=1;
@@ -352,11 +345,12 @@
 }
 
 void SleepView()            //this function applies the sleep mode concept
-{                           //improve power effciency for the game system
+{
+    //improve power effciency for the game system
     while(loop==0)  {       //expand the battery lifetime.
         lcd.turnOff();      //also turn off the lcd scrren when not in used
-        sleep(); 
-                           
+        sleep();
+
         if(g_button_flag==1)  {
             g_button_flag=0;        //if button pressed, turn on the screen back.
             lcd.init();
@@ -376,7 +370,7 @@
     lcd.clear();
     lcd.drawCar(k,l,1);
     lcd.refresh();
-    
+
     for(int i=0; i<=54; i++) {
         for(int j=0; j<=48; j++) {
             carPixel[i][j]=lcd.getPixel(i,j);       //check player's car pixel
@@ -387,7 +381,7 @@
     for(int i=1; i<=(g_level*20); i++)    {
 
         if (g_level>=9) {                                               //for this part, it draw different type of vehicle for given road lane.
-            if (carValue[i]<10) {                                         
+            if (carValue[i]<10) {
                 lcd.drawTruck(carValue[i],opponent_y-delay,1);          //truck is drawn on most left lane
             } else if (carValue[i]<20) {
                 lcd.drawVan(carValue[i],opponent_y-delay,1);            //van is drawn second left lane
@@ -444,11 +438,11 @@
     lcd.refresh();
     double ki=-((g_level));
     duration=(0.02*exp(ki))+0.005;                                  //use exponential decay equation to be used for speed control at increasing level
-    
+
     if(opponent_y%5==0)  {
         gameplaysound(duration);
     } else {
-        wait(duration); 
+        wait(duration);
     }
 }
 
@@ -469,7 +463,7 @@
 
 void playermovement(int g_level)            //function to control the player's car movement
 {
-    if (g_level>=3)  {                      //at higher level, speed is faster, so give small scale movement to give better handling 
+    if (g_level>=3)  {                      //at higher level, speed is faster, so give small scale movement to give better handling
         playerSpeed=1;
     } else if(g_level<3) {                  //at lower level, speed is slower, so high scale movemement is allowed.
         playerSpeed=2;
@@ -499,8 +493,9 @@
 }
 
 void checkLastCar(int g_level)          //check for last car will be on the screen
-{                                       //needed in order to stop the for loop and finish the level
-    if(g_level>=9)   {                  
+{
+    //needed in order to stop the for loop and finish the level
+    if(g_level>=9)   {
         lastCar=g_level*33*4;
     } else if(g_level>=5) {
         lastCar=g_level*50*4;
@@ -706,13 +701,14 @@
 }
 
 void roadline(int moving)           //draw the road line +border line on the screen
-{   //road line
+{
+    //road line
     lcd.drawLine(11,46,11,2,2);
     lcd.drawLine(20,46,20,2,2);
     lcd.drawLine(29,46,29,2,2);
     lcd.drawLine(38,45,38,2,1);
     lcd.drawLine(47,46,47,2,2);
-    //border line 
+    //border line
     lcd.drawLine(0,47,0,0,1);
     lcd.drawLine(83,47,83,0,1);
     lcd.drawLine(83,0,0,0,1);
@@ -728,7 +724,7 @@
         lcd.refresh();
     }
 }
-    
+
 void score(int m,int my)            //display the score text on the screen
 {
     //show score title