“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
Diff: lib/N5110.cpp
- Revision:
- 4:def20a1665d1
- Parent:
- 3:cbe2dcca5058
- Child:
- 5:7930d289e7fc
--- a/lib/N5110.cpp Fri Mar 19 20:04:39 2021 +0000 +++ b/lib/N5110.cpp Sun Mar 21 17:03:59 2021 +0000 @@ -1,6 +1,5 @@ #include "mbed.h" #include "N5110.h" -#include <vector> // overloaded constructor includes power pin - LCD Vcc connected to GPIO pin @@ -396,10 +395,10 @@ int type = (fill==FILL_BLACK) ? 1:0; // black or white fill - drawLine(x+x0,y+y0,-x+x0,y+y0,type); - drawLine(y+x0,x+y0,-y+x0,x+y0,type); - drawLine(y+x0,-x+y0,-y+x0,-x+y0,type); - drawLine(x+x0,-y+y0,-x+x0,-y+y0,type); + drawLine(x+x0,y+y0,-x+x0,y+y0,0,type); + drawLine(y+x0,x+y0,-y+x0,x+y0,0,type); + drawLine(y+x0,-x+y0,-y+x0,-x+y0,0,type); + drawLine(x+x0,-y+y0,-x+x0,-y+y0,0,type); } y++; @@ -413,70 +412,141 @@ } -//****************************************************************************** -float N5110::aidCurve(float const n1, - float const n2, - float perc) -{ - float diff = n2 - n1; - return n1 + ( diff * perc ); -} - - - -//****************************************************************************** -//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 ) - { - 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; -} - - // ***************************************************************************** // 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) +void N5110::drawCurve(std::vector<Vector2Df> curve_points, int offset, int dash_len, int type) { if(type == TYPE_SOLID){ - for(int i = 0; i < curve_points.size()-1; i += step){ + 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+step].x),static_cast<int>(curve_points[i+step].y), FILL_BLACK); + static_cast<int>(curve_points[i+1].x),static_cast<int>(curve_points[i+1].y),0,1); } } -} + if(type == TYPE_DOTTED){ + int counter = 0; + for(int i = 0; i < curve_points.size()-1; i++){ + 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 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 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); +// } +// } + } +} void N5110::drawLine(unsigned int const x0, unsigned int const y0, unsigned int const x1, unsigned int const y1, - unsigned int const type) + unsigned int const offset, + unsigned int const step) { // Note that the ranges can be negative so we have to turn the input values // into signed integers first int const y_range = static_cast<int>(y1) - static_cast<int>(y0); int const x_range = static_cast<int>(x1) - static_cast<int>(x0); - // if dotted line, set step to 2, else step is 1 - unsigned int const step = (type==2) ? 2:1; - // 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 @@ -489,12 +559,12 @@ // loop between x pixels for (unsigned int x = start; x<= stop ; x+=step) { // do linear interpolation - int const dx = static_cast<int>(x)-static_cast<int>(x0); + 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, type); + setPixel(x,y, FILL_BLACK); } } else { @@ -509,7 +579,7 @@ // 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, type); + setPixel(x,y, FILL_BLACK); } } @@ -522,14 +592,14 @@ FillType const fill) { if (fill == FILL_TRANSPARENT) { // transparent, just outline - drawLine(x0,y0,x0+(width-1),y0,1); // top - drawLine(x0,y0+(height-1),x0+(width-1),y0+(height-1),1); // bottom - drawLine(x0,y0,x0,y0+(height-1),1); // left - drawLine(x0+(width-1),y0,x0+(width-1),y0+(height-1),1); // right + drawLine(x0,y0,x0+(width-1),y0,0,1); // top + drawLine(x0,y0+(height-1),x0+(width-1),y0+(height-1),0,1); // bottom + drawLine(x0,y0,x0,y0+(height-1),0,1); // left + drawLine(x0+(width-1),y0,x0+(width-1),y0+(height-1),0,1); // right } else { // filled rectangle - int type = (fill==FILL_BLACK) ? 1:0; // black or white fill + int step = (fill==FILL_BLACK) ? 1:0; // black or white fill for (int y = y0; y<y0+height; y++) { // loop through rows of rectangle - drawLine(x0,y,x0+(width-1),y,type); // draw line across screen + drawLine(x0,y,x0+(width-1),y,0,step); // draw line across screen } } }