“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

Revision:
8:1fc5e14b0db6
Parent:
7:559edc36f261
--- a/lib/N5110.cpp	Thu Apr 22 16:33:42 2021 +0000
+++ b/lib/N5110.cpp	Sat Apr 24 21:31:19 2021 +0000
@@ -417,87 +417,75 @@
                          unsigned int const yc,
                          unsigned int const rx,
                          unsigned int const ry)
-{
-    float dx, dy, d1, d2, x, y;
-    x = 0;
-    y = ry;
-    int radiusError = 1-x;
-  
-    // Initial decision parameter of region 1
-    d1 = (ry * ry) - (rx * rx * ry) + (0.25 * rx * rx);
-    dx = 2 * ry * ry * x;
-    dy = 2 * rx * rx * y;
-  
-    // For region 1
-    while (dx < dy) 
-    {
-        setPixel (xc + x, yc + y,true);
-        setPixel (xc - x, yc + y,true);
-        setPixel (xc + x, yc - y,true);
-        setPixel (xc - x, yc - y,true);
+{   
+    int rx2 = rx*rx, ry2 = ry*ry;
+    int x = 0, y = ry; //Starting point
+
+    int incSW = ry2*2 + rx2*2;
+
+    // Slopes deduced from incremental algorithm with the second-order logic
+    int W = ry2*(-2*x + 3); 
+    int S = rx2*(-2*y + 3);
+    int SW = W + S;
+
+    // Decision parameter of Region 1 and Region 2
+    int p1 = ry2 - rx2*ry + rx2/4; 
+    int p2 = ry2*(x - 0.5)*(x - 0.5) + rx2*(y - 1)*(y - 1) - rx2*ry2; 
+
+    // Region 1
+    while(rx2*(y-0.5) >= ry2*(-x-1)) {
         
-        if (radiusError < 0) {
-            radiusError += 2 * x + 1;
-        } else {
-            x++;
-            radiusError += 2 * (x - y) + 1;
-        }
+        // Set points based on 4-way symmetry
+        setPixel(-x+xc,-y+yc, true);
+        setPixel(-x+xc, y+yc, true);
+        setPixel( x+xc, y+yc, true); 
+        setPixel( x+xc,-y+yc, true); 
         
-        // Checking and updating value of
-        // decision parameter based on algorithm
-        if (d1 < 0)
-        {
-            dx = dx + (2 * ry * ry);
-            d1 = d1 + dx + (ry * ry);
-        }
-        else 
-        {
+        if(p1 > 0) {
+            p1 += SW;
+            W += ry2*2;
+            SW += incSW;
             y--;
-            dx = dx + (2 * ry * ry);
-            dy = dy - (2 * rx * rx);
-            d1 = d1 + dx - dy + (ry * ry);
+        }
+        else {
+            p1 += W;
+            W += 2*ry2;
+            SW += 2*ry2;
         }
+        x--;
     }
-    
+
+    SW = ry2*(2 - 2*x) + rx2*(-2*y + 3);
+
     // Region 2
-    // Decision parameter of region 2
-    d2 = ((ry * ry) * ((x + 0.5) * (x + 0.5))) + 
-         ((rx * rx) * ((y - 1) * (y - 1))) -
-          (rx * rx * ry * ry);
-  
-    // Plotting points of region 2
-    while (y >= 0)
-    {
-        setPixel (xc + x, yc + y,true);
-        setPixel (xc - x, yc + y,true);
-        setPixel (xc + x, yc - y,true);
-        setPixel (xc - x, yc - y,true);
+    while(y >= 0) {
         
-        y--;
+        // Set points based on 4-way symmetry
+        setPixel(-x+xc,-y+yc, true); 
+        setPixel(-x+xc, y+yc, true);
+        setPixel( x+xc, y+yc, true); 
+        setPixel( x+xc,-y+yc, true); 
         
-        // Checking and updating parameter
-        // value based on algorithm
-        if (d2 > 0) 
-        {
-            dy = dy - (2 * rx * rx);
-            d2 = d2 + (rx * rx) - dy;
+        if(p2 > 0) {
+            p2 += S;
+            S += rx2*2;
+            SW += rx2*2;
         }
-        else 
-        {
-            x++;
-            dx = dx + (2 * ry * ry);
-            dy = dy - (2 * rx * rx);
-            d2 = d2 + dx - dy + (rx * rx);
+        else {
+            p2 += SW;
+            SW += incSW;
+            S += rx2*2;
+            x--;
         }
+        y--;
     }
 }
 
-
 // take points on a curve, decide whether to draw them or not
 void N5110::drawCurve(std::vector<Vector2Df> curve_points, float offset, int dash_len, int type)
 {
     if(type == TYPE_SOLID){
-        for(int i = 0; i < curve_points.size()-1; i ++){
+        for(int i = 0; i < curve_points.size()-1; i++){
             // take the current and the following coordinates and draw a line between them
             drawLine(static_cast<int>(curve_points[i].x),static_cast<int>(curve_points[i].y), 
             static_cast<int>(curve_points[i+1].x),static_cast<int>(curve_points[i+1].y),0,1);