“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 26 15:21:07 2021 +0000
Parent:
4:def20a1665d1
Child:
6:40ef2030334c
Commit message:
Road complete.; Start Car

Changed in this revision

lib/Car.cpp Show annotated file Show diff for this revision Revisions of this file
lib/Car.h Show annotated file Show diff for this revision Revisions of this file
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
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/lib/Car.cpp	Sun Mar 21 17:03:59 2021 +0000
+++ b/lib/Car.cpp	Fri Mar 26 15:21:07 2021 +0000
@@ -0,0 +1,32 @@
+#include "Road.h"
+
+// constructure
+Car::Car() {}
+
+void Road::init() {    
+    //initialize parameters
+    int direction = 0;
+    float magnitude = 0;
+    
+    _direction = direction;
+    _magnitude = magnitude;
+}
+
+
+int Car::get_direction(Joystick &joystick) {
+    Vector2Df coord = joystick.get_mapped_coord(); 
+    if ( coord.x == 0) {
+        _direction = 0;       
+    }
+    _direction = coord.x > 0 ? 1:2; 
+}
+
+    
+float Car::get_magnitude(Joystick &joystick) {
+    Vector2Df coord = joystick.get_mapped_coord();
+    _magnitude = 0;
+}
+
+void Car::draw(N5110 &lcd, int direction, float magnitude) { 
+    lcd.drawLine (0,12,84,12,0,1);
+}
\ No newline at end of file
--- a/lib/Car.h	Sun Mar 21 17:03:59 2021 +0000
+++ b/lib/Car.h	Fri Mar 26 15:21:07 2021 +0000
@@ -0,0 +1,27 @@
+#ifndef CAR_H
+#define CAR_H
+
+#include "mbed.h"
+#include "Utils.h"
+#include "N5110.h"
+#include "Vector.h"
+#include "Joystick.h"
+#include <cmath> 
+
+class Car
+{
+public:
+
+    Car();
+    void init();
+    int get_direction(Joystick &joystick);
+    float get_magnitude(Joystick &joystick);
+
+private:
+
+    void draw(N5110 &lcd, int direction, float magnitude);
+    int _direction;
+    float _magnitude;
+
+};
+#endif 
\ No newline at end of file
--- 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);   
-//            }  
-//        }
     }
 }   
 
--- a/lib/N5110.h	Sun Mar 21 17:03:59 2021 +0000
+++ b/lib/N5110.h	Fri Mar 26 15:21:07 2021 +0000
@@ -2,7 +2,8 @@
 #define N5110_H
 
 #include <vector>
-#include <cmath> 
+#include <cmath>
+#include "math.h" 
 #include "Utils.h"
 #include "Vector.h"
 #include "mbed.h"
@@ -386,7 +387,7 @@
                     
     // Draw Curve
     void drawCurve(std::vector<Vector2Df> curve_points, 
-                   int offset, 
+                   float offset, 
                    int dash_len, 
                    int type);
 
--- a/lib/Road.cpp	Sun Mar 21 17:03:59 2021 +0000
+++ b/lib/Road.cpp	Fri Mar 26 15:21:07 2021 +0000
@@ -1,5 +1,4 @@
 #include "Road.h"
-#include <vector>
 
 // constructure
 Road::Road() {}
@@ -17,25 +16,15 @@
     _inclination = inclination; 
     }
 
-void Road::draw(N5110 &lcd, Utils &utils, float direction) { 
-    /*
-    if (direction == 0) {
-        lcd.drawLine( 0,12,84,12,0,1);
-        lcd.drawLine(42,12,42,48,0,4);
-        lcd.drawLine(28,12,12,48,0,1);
-        lcd.drawLine(56,12,72,48,0,1);
-    }*/
-    
-    //else {
-        lcd.drawLine (0,12,84,12,0,1);
-        std::vector<Vector2Df> curve_points_1 = utils.getCurve(12,48,20 + 5 * direction, 30 - abs(direction),28,12);
-        std::vector<Vector2Df> curve_points_2 = utils.getCurve(72,48,64 + 5 * direction, 30 - abs(direction),56,12);
-        std::vector<Vector2Df> curve_points_3 = utils.getCurve(42,48,42 + 5 * direction, 30 - abs(direction),42,12);
+void Road::draw(N5110 &lcd, Utils &utils, float direction, float offset) { 
+    lcd.drawLine (0,12,84,12,0,1);
+    std::vector<Vector2Df> curve_points_1 = utils.getCurve(12,48,20 + 5 * direction, 30 - abs(direction),28,12);
+    std::vector<Vector2Df> curve_points_2 = utils.getCurve(72,48,64 + 5 * direction, 30 - abs(direction),56,12);
+    std::vector<Vector2Df> curve_points_3 = utils.getCurve(42,48,42 + 5 * direction, 30 - abs(direction),42,12);
         
-        lcd.drawCurve(curve_points_1, 0, 1, TYPE_SOLID);
-        lcd.drawCurve(curve_points_2, 0, 1, TYPE_SOLID);
-        lcd.drawCurve(curve_points_3, 0, 1, TYPE_DOTTED);
-    //}
+    lcd.drawCurve(curve_points_1, 0, 1, TYPE_SOLID);
+    lcd.drawCurve(curve_points_2, 0, 1, TYPE_SOLID);
+    lcd.drawCurve(curve_points_3, offset, 1, TYPE_DOTTED);
 }
 
     
--- a/lib/Road.h	Sun Mar 21 17:03:59 2021 +0000
+++ b/lib/Road.h	Fri Mar 26 15:21:07 2021 +0000
@@ -5,6 +5,7 @@
 #include "Utils.h"
 #include "N5110.h"
 #include <cmath> 
+#include <vector>
 #include "Vector.h"
 
 class Road
@@ -14,7 +15,7 @@
     Road();
     void init();
     void set_inclination(int inclination);
-    void draw(N5110 &lcd, Utils &utils, float direction);
+    void draw(N5110 &lcd, Utils &utils, float direction, float offset);
     void draw_warn(N5110 &lcd, int warn_position);
 
 private:
--- a/main.cpp	Sun Mar 21 17:03:59 2021 +0000
+++ b/main.cpp	Fri Mar 26 15:21:07 2021 +0000
@@ -28,6 +28,7 @@
 Utils utils;
 
 #define speed 0.01
+#define road_speed 0.008
 // functions
 void init_buttons();
 
@@ -39,15 +40,15 @@
     lcd.setContrast(0.5);
    
    float i = 0;
+   float offset = 0;
     while(1) {
         lcd.clear();
         //road.draw(lcd, utils, 0);
-        road.draw(lcd, utils, i);
+        road.draw(lcd, utils, i, offset);
         
         if (button_A.read() == 1) {
             
-            road.draw_warn(lcd, 1);
-            
+            road.draw_warn(lcd, 1);  
             i+=speed;  
         }
         
@@ -55,7 +56,7 @@
             road.draw_warn(lcd, 2);
             i-=speed;   
         }
-        
+        offset+=road_speed;
         
         road.draw_warn(lcd, 0);