This is an old revision of the document!


Racing game

This racing game is a demonstration of using ARM-CAN UserInterface module to display 2D graphics on the LCD and use the joystick. The game itself is very simple – user has to drive the car on the track as fast as possible and there are no opponents nor obstacles on the track. Steering and driving is accomplished by moving the joystick. The program is made of several C source files which each take care of different tasks.

Data types

There are structure data types to hold information about 2D points, track sections and the car. All of them are declared in “types.h” file.

typedef struct
{
    short sX;
    short sY;
}
tPoint;
 
typedef struct
{
    tPoint pPosition;
    unsigned short usRadius;
}
tTrackSection;
 
typedef struct
{
    short sRadius;
    unsigned long ulAngle;
}
tPolarPoint;
 
typedef struct
{	
    long lVelocity;
    long lSteering;
    tPoint pPosition;
    unsigned long ulAngle;
    unsigned long ulColor;
    unsigned long ulLapTime;
    tBoolean bOnFinishLine;
}
tCar;

Trigonometric functions

StellarisWare has a function to get the sine of different angles, but there is a need for cosine and arctangent functions. These functions are written in the source file named “trigo.c”. The cosine function is a simple π/2 - sin(α) calculation, but for arctangent, look-up table is used. arctangent2 is an extension of arctangent which gives full 360 degrees output for a line specified by the x and y coordinates.

long cosine(unsigned long ulAngle);
unsigned long arctangent(unsigned long ulRise);
unsigned long arctangent2(signed long y, signed long x);

Polygon drawing

Rendering track

Rendering car

Main program

The main function resides in “racing_game.c”.

Screenshot from game
int main(void);
void DoCarDriving(tCar *pCar);
void PrepareCar(void);
void GrPolyDraw(const tContext *pContext, const tPoint *pPoints, 
           unsigned long ulNumPoints, unsigned char ucClosedLoop);
void GrPolyFill(const tContext *pContext, const tPoint *pPoints, 
           unsigned long ulNumPoints);

Download

en/projects/racing_game.1276459713.txt.gz · Last modified: 2020/07/20 09:00 (external edit)
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0