“Race Collision” is a one player game in which a truck has to avoid “particles” that appear on the road. By the use of the joystick, the player can guide themselves through the menu system to start the game. The truck is the main element of the game and it can be moved from side to side with the joystick. The road curves randomly from time to time and the player has to be careful to keep the truck within the road boundaries. Particles appear on the screen at random positions and 4 collisions lead to the end of the game.

Dependencies:   ELEC2645_JoystickLCD_LPC1768_2021

Files at this revision

API Documentation at this revision

Comitter:
alex_20
Date:
Fri Mar 19 20:04:39 2021 +0000
Parent:
2:18fd28044860
Child:
4:def20a1665d1
Commit message:
everything in N5110

Changed in this revision

lib/N5110.cpp Show annotated file Show diff for this revision Revisions of this file
lib/N5110.h Show annotated file Show diff for this revision Revisions of this file
lib/Road.cpp Show annotated file Show diff for this revision Revisions of this file
lib/Road.h Show annotated file Show diff for this revision Revisions of this file
lib/Utils.cpp Show annotated file Show diff for this revision Revisions of this file
lib/Utils.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/lib/N5110.cpp	Thu Mar 18 13:21:12 2021 +0000
+++ b/lib/N5110.cpp	Fri Mar 19 20:04:39 2021 +0000
@@ -1,5 +1,7 @@
 #include "mbed.h"
 #include "N5110.h"
+#include <vector>
+
 
 // overloaded constructor includes power pin - LCD Vcc connected to GPIO pin
 // this constructor works fine with LPC1768 - enough current sourced from GPIO
@@ -411,38 +413,56 @@
 
 }
 
-int N5110::pointsCurve(unsigned int const n1, 
-                       unsigned int const  n2, 
-                       float perc)
+//******************************************************************************
+float N5110::aidCurve(float const n1, 
+                      float const n2, 
+                      float perc)
 { 
-    int diff = n2 - n1;
+    float diff = n2 - n1;
     return n1 + ( diff * perc );
 }
 
-//funtion to draw curve with Benzier curve
-void N5110::drawCurve(unsigned int const x0,
-                      unsigned int const y0,
-                      unsigned int const x1,
-                      unsigned int const y1,
-                      unsigned int const x2,
-                      unsigned int const y2)
+
+
+//******************************************************************************
+//funtion to draw curve with Bezier curve
+std::vector<Vector2Df> N5110::getCurve(float const x0,
+                                 float const y0,
+                                 float const x1,
+                                 float const y1,
+                                 float const x2,
+                                 float const y2)
 {  
+    std::vector<Vector2Df> curve_points;//_x,curve_points_y;
     for( float i = 0 ; i < 1 ; i += 0.01 )
     {
-        int xa, ya, xb, yb, x, y;
-        xa = pointsCurve(x0 , x1 , i);
-        ya = pointsCurve(y0 , y1 , i);
-        xb = pointsCurve(x1 , x2 , i);
-        yb = pointsCurve(y1 , y2 , i);
+        float xa, ya, xb, yb; //x, y;
+        xa = aidCurve(x0 , x1 , i);
+        ya = aidCurve(y0 , y1 , i);
+        xb = aidCurve(x1 , x2 , i);
+        yb = aidCurve(y1 , y2 , i);
+        
+        //                                x - coord             y - coord
+        Vector2Df curvePoint = {aidCurve(xa , xb , i),aidCurve(ya , yb , i)};
     
-        x = pointsCurve(xa , xb , i);
-        y = pointsCurve(ya , yb , i);
-    
-        setPixel(x,  y, true);
-    }    
+        curve_points.push_back(curvePoint);
+    }
+    return curve_points;    
 }
 
 
+// *****************************************************************************
+// take points on a curve, decide whether to draw them or not
+void N5110::drawCurve(std::vector<Vector2Df> curve_points, int offset, int step, int type)
+{
+    if(type == TYPE_SOLID){
+        for(int i = 0; i < curve_points.size()-1; i += step){
+            drawLine(static_cast<int>(curve_points[i].x),static_cast<int>(curve_points[i].y), 
+            static_cast<int>(curve_points[i+step].x),static_cast<int>(curve_points[i+step].y), FILL_BLACK);
+        }
+    }
+}
+
 void N5110::drawLine(unsigned int const x0,
                      unsigned int const y0,
                      unsigned int const x1,
--- a/lib/N5110.h	Thu Mar 18 13:21:12 2021 +0000
+++ b/lib/N5110.h	Fri Mar 19 20:04:39 2021 +0000
@@ -1,6 +1,8 @@
 #ifndef N5110_H
 #define N5110_H
 
+#include <vector>
+#include "Utils.h"
 #include "mbed.h"
 
 // number of pixels on display
@@ -15,6 +17,9 @@
     FILL_WHITE,       ///< Filled white (no outline)
 };
 
+const static int TYPE_SOLID = 0;
+const static int TYPE_DOTTED = 1;
+
 /** N5110 Class
 @brief Library for interfacing with Nokia 5110 LCD display (https://www.sparkfun.com/products/10168) using the hardware SPI on the mbed.
 @brief The display is powered from a GPIO pin meaning it can be controlled via software.  The LED backlight is also software-controllable (via PWM pin).
@@ -377,26 +382,15 @@
                     FillType const     fill);
         
     // get a Bezier curves with the DeCasteljau algorithm  
-    int pointsCurve(unsigned int const  n1, 
-                    unsigned int const n2, 
+    float aidCurve(float const  n1, 
+                    float const n2, 
                     float perc );
                     
-     /** Draw Curve
-    *
-    *   This function draws a part of a circle at the specified origin with specified radius in the screen buffer
-    *   @param  x0     - x-coordinate of first point
-    *   @param  y0     - y-coordinate of first point
-    *   @param  x1     - x-coordinate of second point
-    *   @param  y1     - y-coordinate of second point
-    *   @param  x2     - x-coordinate of third point
-    *   @param  y2     - y-coordinate of third point
-    */
-   void drawCurve(unsigned int const x0,
-                  unsigned int const y0,
-                  unsigned int const x1,
-                  unsigned int const y1,
-                  unsigned int const x2,
-                  unsigned int const y2);
+     // Draw Curve
+    void drawCurve(std::vector<Vector2Df> curve_points, 
+                   int offset, 
+                   int step, 
+                   int type);
 
     /** Draw Line
     *
--- a/lib/Road.cpp	Thu Mar 18 13:21:12 2021 +0000
+++ b/lib/Road.cpp	Fri Mar 19 20:04:39 2021 +0000
@@ -1,6 +1,5 @@
 #include "Road.h"
-#include "GameEngine.h"
-#include <cmath> 
+#include <vector>
 
 // constructure
 Road::Road() {}
@@ -14,66 +13,58 @@
     _inclination = inclination;
 }
 
-void Road::draw(N5110 &lcd, int i) { 
-
-    if (_direction == 0) {
-        lcd.drawLine(0,12,84,12, FILL_BLACK);
-        lcd.drawLine(28,12,12,48, FILL_BLACK);
-        lcd.drawLine(56,12,72,48, FILL_BLACK);    
-    }
-    
-    else if (_direction == 1) {
-            lcd.drawLine(0,12,84,12, FILL_BLACK);
-            lcd.drawCurve(12,48,20 + 7 * i, 30 - abs(i),28,12);
-            lcd.drawCurve(72,48,64 + 5 * i, 30 - abs(i),56,12);
-    }
-            
-    else {
-            lcd.drawLine(0,12,84,12, FILL_BLACK);
-            lcd.drawCurve(12,48,20 - 2 * i,30 - i,28,12);
-            lcd.drawCurve(72,48,68 - 6 * i,30 - i,56,12); 
-    }
-}
-
 void Road::set_inclination(int inclination) {
     _inclination = inclination; 
     }
 
-void Road::set_direction(int direction) {
-    _direction = direction;
+void Road::draw(N5110 &lcd, int direction) { 
+
+    if (direction == 0) {
+        lcd.drawLine( 0,12,84,12, FILL_BLACK);
+        lcd.drawLine(28,12,12,48, FILL_BLACK);
+        lcd.drawLine(56,12,72,48, FILL_BLACK);
     }
-  
     
-void _draw_warn(N5110 &lcd, int warn_position) {
-    const int warn_sign[13][16] = {
-        { 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0 },
-        { 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0 },
-        { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
-        { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
-        { 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0 },
-        { 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0 },
-        { 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0 },
-        { 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0 },
-        { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },
-        { 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0 },
-        { 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0 },
-        { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
-        { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
+    else {
+        lcd.drawLine (0,12,84,12, FILL_BLACK);
+        std::vector<Vector2Df> curve_points_1 = lcd.getCurve(12,48,20 + 5 * direction, 30 - abs(direction),28,12);
+        std::vector<Vector2Df> curve_points_2 = lcd.getCurve(72,48,64 + 5 * direction, 30 - abs(direction),56,12);
+        
+        lcd.drawCurve(curve_points_1, 0, 1, TYPE_SOLID);
+        lcd.drawCurve(curve_points_2, 0, 1, TYPE_SOLID);
+    }
+}
+
+    
+void Road::draw_warn(N5110 &lcd, int warn_position) {
+    const int warn_sign[12][12] = {
+        { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
+        { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
+        { 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1 },
+        { 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1 },
+        { 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1 },
+        { 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1 },
+        { 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1 },
+        { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
+        { 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1 },
+        { 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1 },
+        { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
+        { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
     };
     
     // if ball comes from the left side
     if (warn_position == 0) {
-        lcd.drawSprite(10,18,13,16,(int *)warn_sign); 
+        lcd.drawSprite(0,18,12,12,(int *)warn_sign); 
     }
     
     // if ball comes from the top
     if (warn_position == 1) {
-        lcd.drawSprite(40,0,13,16,(int *)warn_sign); 
+        lcd.drawSprite(36,0,12,12,(int *)warn_sign); 
     }
     
     // if ball comes from the right side
     if (warn_position == 2) {
-        lcd.drawSprite(68,18,13,16,(int *)warn_sign); 
+        lcd.drawSprite(72,18,12,12,(int *)warn_sign); 
     }
 }
     
--- a/lib/Road.h	Thu Mar 18 13:21:12 2021 +0000
+++ b/lib/Road.h	Fri Mar 19 20:04:39 2021 +0000
@@ -3,7 +3,7 @@
 
 #include "mbed.h"
 #include "N5110.h"
-#include "GameEngine.h"
+#include <cmath> 
 
 class Road
 {
@@ -11,15 +11,14 @@
 
     Road();
     void init();
+    void set_inclination(int inclination);
     void draw(N5110 &lcd, int i);
-    void set_inclination(int inclination);
-    void set_direction(int direction);
+    void draw_warn(N5110 &lcd, int warn_position);
 
 private:
 
     int _inclination;
     int _direction;
-    void _draw_warn(N5110 &lcd, int warn_position);
 
 };
 #endif 
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/Utils.cpp	Fri Mar 19 20:04:39 2021 +0000
@@ -0,0 +1,41 @@
+#include "Utils.h"
+#include <vector>
+
+// constructure
+Utils::Utils() {}
+
+
+float Utils::curveEquation(float const n1, 
+                           float const n2, 
+                           float perc)
+{ 
+    float diff = n2 - n1;
+    return n1 + ( diff * perc );
+}
+
+
+//funtion to get the points using Bezier curve
+std::vector<Vector2Df> Utils::getCurve(float const x0,
+                                 float const y0,
+                                 float const x1,
+                                 float const y1,
+                                 float const x2,
+                                 float const y2)
+{  
+    std::vector<Vector2Df> curve_points;//_x,curve_points_y;
+    for( float i = 0 ; i < 1 ; i += 0.01 )
+    {
+        float xa, ya, xb, yb; //x, y;
+        xa = aidCurve(x0 , x1 , i);
+        ya = aidCurve(y0 , y1 , i);
+        xb = aidCurve(x1 , x2 , i);
+        yb = aidCurve(y1 , y2 , i);
+        
+        //                                x - coord             y - coord
+        Vector2Df curvePoint = {aidCurve(xa , xb , i),aidCurve(ya , yb , i)};
+    
+        curve_points.push_back(curvePoint);
+    }
+    
+    return curve_points;    
+}
\ No newline at end of file
--- a/lib/Utils.h	Thu Mar 18 13:21:12 2021 +0000
+++ b/lib/Utils.h	Fri Mar 19 20:04:39 2021 +0000
@@ -1,14 +1,43 @@
 #ifndef UTILS_H
 #define UTILS_H
 
+#include "mbed.h"
+#include "N5110.h"
+
 struct Position2D {
   int x;
   int y;  
 };
 
-struct Vector2D {
-  float x;
-  float y;  
+typedef struct Vector2Df { float x; float y; }  Vector2Df;
+
+Class Utils
+{
+  public:
+  
+  /** Take points from curve
+    *
+    *   This function takes all the points from a curve and stores them in a vector
+    *   @param  x0     - x-coordinate of first point
+    *   @param  y0     - y-coordinate of first point
+    *   @param  x1     - x-coordinate of second point
+    *   @param  y1     - y-coordinate of second point
+    *   @param  x2     - x-coordinate of third point
+    *   @param  y2     - y-coordinate of third point
+    */
+   std::vector<Vector2Df> getCurve(float const x0,
+                                   float const y0,
+                                   float const x1,
+                                   float const y1,
+                                   float const x2,
+                                   float const y2);
+        
+                                   
+    float curveEquation(float const  n1, 
+                        float const n2, 
+                        float perc );
+  
+    
 };
 
 #endif
\ No newline at end of file
--- a/main.cpp	Thu Mar 18 13:21:12 2021 +0000
+++ b/main.cpp	Fri Mar 19 20:04:39 2021 +0000
@@ -7,7 +7,6 @@
 #include "Joystick.h"
 #include "N5110.h"
 #include "Road.h"
-#include <cmath> 
 
 // objects
 // BusOut leds(LED4,LED3,LED2,LED1);
@@ -37,19 +36,31 @@
    
     while(1) {
         lcd.clear();
-        
         road.draw(lcd, 0);
         
-        road.set_direction(1);
-        
         if (button_A.read() == 1) {
-            road.draw(lcd, 1);    
+            int i;
+            road.draw_warn(lcd, 1);
+            road.draw(lcd, i);
+            i++;
+            if (i == 7) {
+                i = 6;
+            }    
         }
         
-        if (button_A.read() == 1) {
-            road.draw(lcd, 2);    
+        if (button_C.read() == 1) {
+            int j;
+            road.draw_warn(lcd, 2);
+            road.draw(lcd, j);
+            j--;
+            if (j == -5) {
+                j = -4;
+            }     
         }
         
+        
+        road.draw_warn(lcd, 0);
+        
         lcd.refresh(); 
         thread_sleep_for(50);
     }