“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

main.cpp

Committer:
alex_20
Date:
2021-04-22
Revision:
7:559edc36f261
Parent:
6:40ef2030334c
Child:
8:1fc5e14b0db6

File content as of revision 7:559edc36f261:

/* mbed Microcontroller Library
 * Copyright (c) 2019 ARM Limited
 * SPDX-License-Identifier: Apache-2.0
 */

#include "mbed.h"
#include "Joystick.h"
#include "N5110.h"
#include "Road.h"
#include "Car.h"
#include "Utils.h"
#include "Vector.h"

// objects
// BusOut leds(LED4,LED3,LED2,LED1);

N5110 lcd(p14,p8,p9,p10,p11,p13,p21); 
DigitalIn button_A(p29);
DigitalIn button_C(p27);

//                 y   x  
Joystick joystick(p20,p19);

//             B   G   R
//BusOut leds(p22,p23,p24);
//          LSB     MSB

Road road;
Utils utils;
Car car;

#define speed 0.01
#define road_speed 0.008
#define car_speed 0.2

// functions
void init_buttons();

int main()
{
    // initialisation
    lcd.init();
    road.init();
    car.init();
    joystick.init();
    lcd.setContrast(0.5);
   
    float i = 0;
    float offset = 0;
    float start_pos = 38.0;
    float y = 0.0;
    
    while(1) {
        lcd.clear();
        road.draw(lcd, utils, i, offset);
        
        if (button_A.read() == 1) {
            
            road.draw_warn(lcd, 1);  
            i += speed;  
        }
        
        if (button_C.read() == 1) {
            road.draw_warn(lcd, 2);
            i -= speed;   
        }
        offset += road_speed;
        
        Vector2D coord = joystick.get_mapped_coord();
        int direction = car.get_direction(coord.x);
        int magnitude = car.get_magnitude(coord.x);
        
        if(direction == STRAIGHT) {
            y = 0;
            }
        
        if(direction == LEFT) {
            if(magnitude == 1) {
                y = 4.0;
                }
            if(magnitude == 2) {
                y = 4.0;
                }
            start_pos -= car_speed;
        } 
              
        if(direction ==  RIGHT) {
            if(magnitude == 1) {
                y = 4.0;
                }
            if(magnitude == 2) {
                y = 8.0;
                }
            start_pos += car_speed;
        }
        
        // define boundaries for car movement
         if(start_pos < 20.0) {
            start_pos = 20.0;
        }
        
        if(start_pos > 55.0) {
            start_pos = 55.0;
        }
        
        car.draw(lcd,start_pos,y);
        
        lcd.drawEllipse(42,20,15,10);
        
        lcd.refresh(); 
    }
}

void init_buttons()
{
    // PCB has external pull-down resistors so turn the internal ones off
    // (default for DigitalIn)
    button_A.mode(PullNone);
    button_C.mode(PullNone);
}