“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:
5:7930d289e7fc
Parent:
4:def20a1665d1
Child:
6:40ef2030334c
--- a/lib/N5110.cpp	Sun Mar 21 17:03:59 2021 +0000
+++ b/lib/N5110.cpp	Fri Mar 26 15:21:07 2021 +0000
@@ -414,7 +414,7 @@
 
 // *****************************************************************************
 // take points on a curve, decide whether to draw them or not
-void N5110::drawCurve(std::vector<Vector2Df> curve_points, int offset, int dash_len, int type)
+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 ++){
@@ -425,113 +425,30 @@
     }
     if(type == TYPE_DOTTED){
         int counter = 0;
-        for(int i = 0; i < curve_points.size()-1; i++){
+        for(int i = curve_points.size()-1; i > 0 ; i--){
             int x0 = static_cast<int>(curve_points[i].x);
-            int x1 = static_cast<int>(curve_points[i+1].x);
+            int x1 = static_cast<int>(curve_points[i-1].x);
             int y0 = static_cast<int>(curve_points[i].y);
-            int y1 = static_cast<int>(curve_points[i+1].y);  
+            int y1 = static_cast<int>(curve_points[i-1].y);  
                 
-            int const y_range = static_cast<int>(y1) - static_cast<int>(y0);
-            int const x_range = static_cast<int>(x1) - static_cast<int>(x0);
-        
-            // make sure we loop over the largest range to get the most pixels on the display
-            // for instance, if drawing a vertical line (x_range = 0), we need to loop down the y pixels
-            // or else we'll only end up with 1 pixel in the x column
-            if ( abs(x_range) > abs(y_range) ) {
-        
-                // ensure we loop from smallest to largest or else for-loop won't run as expected
-                //unsigned int const start = x_range > 0 ? x0:x1;
-                //unsigned int const stop =  x_range > 0 ? x1:x0;
-        
-                // loop between x pixels
-                for (unsigned int x = x0; x<= x1 ; x++) {
-                    // do linear interpolation
-                    int const dx = static_cast<int>(x) - static_cast<int>(x0);
-                    unsigned int const y = y0 + y_range * dx / x_range;
-        
-                    // If the line type is '0', this will clear the pixel
-                    // If it is '1' or '2', the pixel will be set
-                    setPixel(x,y, ((int)pow(counter, 0.9) - (offset % (2 * dash_len))) % (2 * dash_len) < dash_len);
-                    counter++;
-                }
-            } else {
-        
-                // ensure we loop from smallest to largest or else for-loop won't run as expected
-                //unsigned int const start = y_range > 0 ? y0:y1;
-                //unsigned int const stop =  y_range > 0 ? y1:y0;
-        
-                for (unsigned int y = y0; y<= y1 ; y++) {
-                    // do linear interpolation
-                    int const dy = static_cast<int>(y)-static_cast<int>(y0);
-                    unsigned int const x = x0 + x_range * dy / y_range;
-        
-                    // If the line type is '0', this will clear the pixel
-                    // If it is '1' or '2', the pixel will be set
-                    setPixel(x,y, ((int)pow(counter, 0.9) - (offset % (2 * dash_len))) % (2 * dash_len) < dash_len);
-                    counter++;
-                }
-            }    
+            int const y_range = y0 - y1;
+            int const x_range = x1 - x0;
+    
+            // ensure we loop from smallest to largest or else for-loop won't run as expected
+            //unsigned int const start = y_range > 0 ? y0:y1;
+            //unsigned int const stop =  y_range > 0 ? y1:y0;
+            
+            for (unsigned int y = y0; y <= y1 ; y++) {
+                // do linear interpolation
+                int const dy = y0 - y;
+                unsigned int const x = x0 + (x_range * (dy / (float)y_range));
+    
+                // If the line type is '0', this will clear the pixel
+                // If it is '1' or '2', the pixel will be set
+                setPixel(x,y, fmod(pow(counter, 0.55) - fmod(offset, (2 * dash_len)), (2 * dash_len)) < dash_len);
+                counter++;
+            }
         }
-        //----------------------------------------
-        //int x, y;   
-//        int last_elem = curve_points.size();
-//        
-//        int x_range = static_cast<int>(curve_points[last_elem].x) - static_cast<int>(curve_points[0].x);
-//        int y_range = static_cast<int>(curve_points[last_elem].y) - static_cast<int>(curve_points[0].y);
-//        
-//        if (abs(x_range) > abs(y_range)) {      
-//            for(int i = 0; i < curve_points.size()-1; i += 1){
-//                
-//                
-//            // ensure we loop from smallest to largest or else for-loop won't run as expected
-//            unsigned int const start = x_range > 0 ? x0:x1;
-//            unsigned int const stop =  x_range > 0 ? x1:x0;
-//    
-//            // loop between x pixels
-//            for (unsigned int x = start; x<= stop ; x++) {
-//                // do linear interpolation
-//                int const dx = static_cast<int>(x) - static_cast<int>(x0);
-//                unsigned int const y = y0 + y_range * dx / x_range;
-//    
-//                // If the line type is '0', this will clear the pixel
-//                // If it is '1' or '2', the pixel will be set
-//                setPixel(x,y, ((x - (offset % (2 * dash_len))) % (2 * dash_len) < dash_len));
-//            }
-//            
-//            
-//            
-//                
-//                
-//                int x0 = static_cast<int>(curve_points[i].x);
-//                int x1 = static_cast<int>(curve_points[i+1].x);
-//                int y0 = static_cast<int>(curve_points[i].y);
-//                int y1 = static_cast<int>(curve_points[i+1].y);      
-//                
-//                int const dy = y1 - y0;
-//                int const dx = x1 - x0;
-//                              
-//                bool col = (x1 - (offset % (2 * dash_len))) % (2 * dash_len) < dash_len;
-//                y = (y0 + y_range * dx ) / x_range;
-//                setPixel(x0, y0, FILL_BLACK);    
-//            }
-//        }
-//            
-//        else {
-//             for(int i = 0; i < curve_points.size()-1; i += step){
-//                
-//                int x0 = static_cast<int>(curve_points[i].x);
-//                int x1 = static_cast<int>(curve_points[i+1].x);
-//                int y0 = static_cast<int>(curve_points[i].y);
-//                int y1 = static_cast<int>(curve_points[i+1].y);      
-//                
-//                int const dy = y1 - y0;
-//                int const dx = x1 - x0;
-//                
-//                // y = (y1 - (offset % (2 * step))) % (2 * step);
-//                x = (x0 + x_range * dy) / y_range;
-//                setPixel(x0, y0, FILL_BLACK);   
-//            }  
-//        }
     }
 }