ELEC2645 (2017/18) / Mbed 2 deprecated el16a2t

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
el16a2t
Date:
Mon May 07 00:00:49 2018 +0000
Parent:
10:e81aacef805b
Child:
12:c36bc59d120e
Commit message:
integrated a "difficulty" measure by allowing the user to change the speed of the game by turning the potentiometer. the potentiometer sets the wait time at the end of the game function. also created another random variable so that each car sprite us

Changed in this revision

engine/main.cpp Show annotated file Show diff for this revision Revisions of this file
engine/main.h Show annotated file Show diff for this revision Revisions of this file
--- a/engine/main.cpp	Sun May 06 21:45:41 2018 +0000
+++ b/engine/main.cpp	Mon May 07 00:00:49 2018 +0000
@@ -47,9 +47,9 @@
     //set initial location conditions for the player and car(s) and score
     x_player = 24;
     y_player = 29;
-    
+
 
-    
+
     x_car = 4;
     y_car = 0;
     x_car2 = 24;
@@ -64,11 +64,18 @@
 void startUp()
 {
     //print start screen
-    lcd.drawSprite (1,1,20,70,(int*) titlescreen);
-    lcd.printString ("PRESS START", 3,4);
-    lcd.refresh();
     while ( pad.check_event(Gamepad::START_PRESSED) == false) {
-
+        lcd.drawSprite (1,1,20,70,(int*) titlescreen);
+        lcd.printString ("PRESS START", 3,5);
+        lcd.printString ("SPEED", 3,3);
+        pot_value = pad.read_pot();
+        if(pot_value>9.9) {
+            pot_value=9.9;
+        }
+        sprintf (speed, "%.1f", (pot_value*10)); //convert an integer to pointer
+        lcd.printString(speed,55,3);
+        lcd.refresh();
+        wait(0.2);
     }
 }
 
@@ -76,135 +83,143 @@
 
 void newGame()
 {
-        printf("newgamereached\n"); //checking if new game function reached
-
-        int Index = rand() % 3; //picks a random number between 1 and 3
-        printf ("Index = %d\n", Index);
-        int arr[3] = {4, 24, 44}; //array corresponding to the x values of the 3 lanes of traffic
-        int random_number = arr[Index]; //pick number at Index value from array, assign to random_number
-        printf ("random_number = %d\n", random_number);
-        
-        
-
-        
-        lcd.clear();
+    printf("newgamereached\n"); //checking if new game function reached
 
-        lcd.drawSprite(x_player,y_player,20,15,(int*) player); //print the player sprite
-        
-        //if X pressed, move player left, if B pressed, move player right
-        if ( pad.check_event(Gamepad::X_PRESSED) == true) {
+    int Index = rand() % 3; //picks a random number between 1 and 3
+    printf ("Index = %d\n", Index);
+    int arr[3] = {4, 24, 44}; //array corresponding to the x values of the 3 lanes of traffic
+    int random_number = arr[Index]; //pick number at Index value from array, assign to random_number
+    printf ("random_number = %d\n", random_number);
+    
+    int Index2 = rand() % 3; 
+    int arr2[3] = {4, 24, 44}; 
+    int random_number2 = arr2[Index2]; 
+    
 
-            x_player = x_player-20;
-        }
-        //car cannot go further left than the left lane etc
-        if (x_player <4) {
-            x_player = 4;
-        }
-        if ( pad.check_event(Gamepad::B_PRESSED) == true) {
 
-            x_player = x_player+20;
-        }
-        if (x_player >44) {
-            x_player = 44;
-        }
 
 
-        //print road lines
-        lcd.drawLine(1,1,1,1500,2);
+    lcd.clear();
+
+    lcd.drawSprite(x_player,y_player,20,15,(int*) player); //print the player sprite
+
+    //if X pressed, move player left, if B pressed, move player right
+    if ( pad.check_event(Gamepad::X_PRESSED) == true) {
+
+        x_player = x_player-20;
+    }
+    //car cannot go further left than the left lane etc
+    if (x_player <4) {
+        x_player = 4;
+    }
+    if ( pad.check_event(Gamepad::B_PRESSED) == true) {
 
-        lcd.drawLine(21,1,21,1500,2);
+        x_player = x_player+20;
+    }
+    if (x_player >44) {
+        x_player = 44;
+    }
+
 
-        lcd.drawLine(41,1,41,1500,2);
+    //print road lines
+    lcd.drawLine(1,1,1,1500,2);
 
-        lcd.drawLine(61,1,61,1500,2);
+    lcd.drawLine(21,1,21,1500,2);
+
+    lcd.drawLine(41,1,41,1500,2);
+
+    lcd.drawLine(61,1,61,1500,2);
 
 
 
 
-        //cars move down the road
-
-        lcd.drawSprite(x_car,y_car,20,15,(int*) car);
-        y_car++;
-        lcd.drawSprite(x_car2,y_car2,20,15,(int*) car2);
-        y_car2++;
-
-        // re-generate cars continously
-
+    //cars move down the road
 
-        int YIndex = rand() % 4;
-        int array[5] = {50, 55, 60, 65, 70};
-        int nextCar = array[YIndex];
-        
-        if (y_car2 == nextCar) {
-            x_car2 = random_number;
-            y_car2 = -24;
-
+    lcd.drawSprite(x_car,y_car,20,15,(int*) car);
+    y_car++;
+    lcd.drawSprite(x_car2,y_car2,20,15,(int*) car2);
+    y_car2++;
 
-        }  
-        
-            if (y_car == nextCar) {
-            x_car = random_number;
-            y_car = -24;
-
-
-        }
-
+    // re-generate cars continously
 
 
-        
-        score++;//increase score
+    int YIndex = rand() % 4;
+    int array[5] = {50, 55, 60, 65, 70};
+    int nextCar = array[YIndex];
 
-//        difficulty = read_pot();
-        sprintf (str, "%d", score); //convert an integer to pointer
-        lcd.printString(str,64,1);
+    if (y_car == nextCar) {
+        x_car = random_number;
+        y_car = -24;
 
 
-        //if there is a collision, display "game over" screen
-        if (x_car == x_player && y_car > 12 && y_car < 50) {
-            printf("collision1\n");// so i can tell which car sprite the collision was with
-            y_car = y_car;
-            y_car2 = y_car2;
-            score=score;
-            lives--;
-            printf("lives=%d\n", lives);
+    }
 
-        }
-        if (x_car2 == x_player && y_car2 > 12 && y_car2 < 50) {
-            printf("collision2\n");
-            y_car2 = y_car2;
-            y_car = y_car;
-            score=score;
-            lives--;
-            printf("lives=%d\n", lives);
+    int YIndex2 = rand() % 4;
+    int array2[5] = {50, 55, 60, 65, 70};
+    int nextCar2 = array2[YIndex2];
 
-        }
-
-        if ( pad.check_event(Gamepad::X_PRESSED) == true) {
+    if (y_car2 == nextCar2) {
+        x_car2 = random_number2;
+        y_car2 = -24;
 
-            x_player = x_player-20;
-        }
-        //car cannot go further left than the left lane etc
-        if (x_player <4) {
-            x_player = 4;
-        }
-        if ( pad.check_event(Gamepad::B_PRESSED) == true) {
 
-            x_player = x_player+20;
-        }
-        if (x_player >44) {
-            x_player = 44;
-        }
+    }
 
 
 
 
+    score++;//increase score
+
+//        difficulty = read_pot();
+    sprintf (str, "%d", score); //convert an integer to pointer
+    lcd.printString(str,64,1);
 
 
-        lcd.refresh();
-//control the wait time using the potentiometer value to control difficulty
-//lower potentiometer = easier difficulty = longer wait time = cars and score move slower
-//e.g - use wait(1-potentiometer value) where potentiometer value 0<x<1 is difficulty
-        wait(0.1);
+    //if there is a collision, display "game over" screen
+    if (x_car == x_player && y_car > 12 && y_car < 50) {
+        printf("collision1\n");// so i can tell which car sprite the collision was with
+        y_car = y_car;
+        y_car2 = y_car2;
+        score=score;
+        lives--;
+        printf("lives=%d\n", lives);
+
+    }
+    if (x_car2 == x_player && y_car2 > 12 && y_car2 < 50) {
+        printf("collision2\n");
+        y_car2 = y_car2;
+        y_car = y_car;
+        score=score;
+        lives--;
+        printf("lives=%d\n", lives);
+
+    }
+
+    if ( pad.check_event(Gamepad::X_PRESSED) == true) {
+
+        x_player = x_player-20;
+    }
+    //car cannot go further left than the left lane etc
+    if (x_player <4) {
+        x_player = 4;
+    }
+    if ( pad.check_event(Gamepad::B_PRESSED) == true) {
+
+        x_player = x_player+20;
+    }
+    if (x_player >44) {
+        x_player = 44;
+    }
+
+    pot_value = pad.read_pot();
+    printf("pot_value = %f\n", pot_value);
+    float wait_time = pot_value/10;
+
+
+
+    lcd.refresh();
+
+    wait(0.1-wait_time);
 
 
 
@@ -212,7 +227,7 @@
 
 
 
-    
+
 
 
 }
@@ -220,19 +235,20 @@
 void gameOver()
 {
     printf("reached gameover\n");
-    if (lives ==0){
-    
+    if (lives ==0) {
+
 
-    while ( pad.check_event(Gamepad::START_PRESSED) == false) {
-    
-    printf("gameoverlives=%d\n", lives);
-    lcd.clear();
+        while ( pad.check_event(Gamepad::START_PRESSED) == false) {
+
+            printf("gameoverlives=%d\n", lives);
+            lcd.clear();
 
-    lcd.printString(str,64,1);
-    lcd.printString ("GAME OVER", 3,1);
-    lcd.printString ("PRESS START", 3,3);
-    lcd.refresh();
-    wait(0.2);   
+            lcd.printString(str,64,1);
+            lcd.printString ("GAME OVER", 3,1);
+            lcd.printString ("PRESS START", 3,3);
+            lcd.refresh();
+            wait(0.2);
+        }
+        init ();
     }
-    init ();
-}}
+}
--- a/engine/main.h	Sun May 06 21:45:41 2018 +0000
+++ b/engine/main.h	Mon May 07 00:00:49 2018 +0000
@@ -7,8 +7,11 @@
 int x_car2;
 int y_car2;
 int score;
-int difficulty;
+char speed[14];
 char str[14];
 int lives;
 int Index;
-int YIndex;
\ No newline at end of file
+int YIndex;
+int YIndex2;
+float pot_value;
+float wait_time;
\ No newline at end of file