“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@6:40ef2030334c, 2021-04-22 (annotated)
- Committer:
- alex_20
- Date:
- Thu Apr 22 10:52:37 2021 +0000
- Revision:
- 6:40ef2030334c
- Parent:
- 5:7930d289e7fc
- Child:
- 7:559edc36f261
Car done (still needs improvement) ; Start Ball
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
eencae | 0:be41a15e7a86 | 1 | /* mbed Microcontroller Library |
eencae | 0:be41a15e7a86 | 2 | * Copyright (c) 2019 ARM Limited |
eencae | 0:be41a15e7a86 | 3 | * SPDX-License-Identifier: Apache-2.0 |
eencae | 0:be41a15e7a86 | 4 | */ |
eencae | 0:be41a15e7a86 | 5 | |
eencae | 0:be41a15e7a86 | 6 | #include "mbed.h" |
eencae | 0:be41a15e7a86 | 7 | #include "Joystick.h" |
eencae | 0:be41a15e7a86 | 8 | #include "N5110.h" |
alex_20 | 2:18fd28044860 | 9 | #include "Road.h" |
alex_20 | 6:40ef2030334c | 10 | #include "Car.h" |
alex_20 | 4:def20a1665d1 | 11 | #include "Utils.h" |
alex_20 | 4:def20a1665d1 | 12 | #include "Vector.h" |
eencae | 0:be41a15e7a86 | 13 | |
alex_20 | 1:2ae7a8b01771 | 14 | // objects |
alex_20 | 1:2ae7a8b01771 | 15 | // BusOut leds(LED4,LED3,LED2,LED1); |
eencae | 0:be41a15e7a86 | 16 | |
alex_20 | 6:40ef2030334c | 17 | N5110 lcd(p14,p8,p9,p10,p11,p13,p21); |
alex_20 | 6:40ef2030334c | 18 | DigitalIn button_A(p29); |
alex_20 | 6:40ef2030334c | 19 | DigitalIn button_C(p27); |
eencae | 0:be41a15e7a86 | 20 | |
alex_20 | 6:40ef2030334c | 21 | // y x |
alex_20 | 6:40ef2030334c | 22 | Joystick joystick(p20,p19); |
alex_20 | 1:2ae7a8b01771 | 23 | |
alex_20 | 2:18fd28044860 | 24 | // B G R |
alex_20 | 1:2ae7a8b01771 | 25 | //BusOut leds(p22,p23,p24); |
alex_20 | 1:2ae7a8b01771 | 26 | // LSB MSB |
alex_20 | 1:2ae7a8b01771 | 27 | |
alex_20 | 2:18fd28044860 | 28 | Road road; |
alex_20 | 4:def20a1665d1 | 29 | Utils utils; |
alex_20 | 6:40ef2030334c | 30 | Car car; |
alex_20 | 2:18fd28044860 | 31 | |
alex_20 | 4:def20a1665d1 | 32 | #define speed 0.01 |
alex_20 | 5:7930d289e7fc | 33 | #define road_speed 0.008 |
alex_20 | 6:40ef2030334c | 34 | #define car_speed 0.2 |
alex_20 | 6:40ef2030334c | 35 | |
alex_20 | 1:2ae7a8b01771 | 36 | // functions |
alex_20 | 1:2ae7a8b01771 | 37 | void init_buttons(); |
eencae | 0:be41a15e7a86 | 38 | |
eencae | 0:be41a15e7a86 | 39 | int main() |
eencae | 0:be41a15e7a86 | 40 | { |
alex_20 | 2:18fd28044860 | 41 | // initialisation |
eencae | 0:be41a15e7a86 | 42 | lcd.init(); |
alex_20 | 2:18fd28044860 | 43 | road.init(); |
alex_20 | 6:40ef2030334c | 44 | car.init(); |
alex_20 | 6:40ef2030334c | 45 | joystick.init(); |
eencae | 0:be41a15e7a86 | 46 | lcd.setContrast(0.5); |
alex_20 | 1:2ae7a8b01771 | 47 | |
alex_20 | 6:40ef2030334c | 48 | float i = 0; |
alex_20 | 6:40ef2030334c | 49 | float offset = 0; |
alex_20 | 6:40ef2030334c | 50 | float start_pos = 38.0; |
alex_20 | 6:40ef2030334c | 51 | float y = 0.0; |
alex_20 | 6:40ef2030334c | 52 | |
alex_20 | 1:2ae7a8b01771 | 53 | while(1) { |
alex_20 | 1:2ae7a8b01771 | 54 | lcd.clear(); |
alex_20 | 5:7930d289e7fc | 55 | road.draw(lcd, utils, i, offset); |
eencae | 0:be41a15e7a86 | 56 | |
alex_20 | 2:18fd28044860 | 57 | if (button_A.read() == 1) { |
alex_20 | 4:def20a1665d1 | 58 | |
alex_20 | 5:7930d289e7fc | 59 | road.draw_warn(lcd, 1); |
alex_20 | 6:40ef2030334c | 60 | i += speed; |
alex_20 | 1:2ae7a8b01771 | 61 | } |
alex_20 | 1:2ae7a8b01771 | 62 | |
alex_20 | 3:cbe2dcca5058 | 63 | if (button_C.read() == 1) { |
alex_20 | 3:cbe2dcca5058 | 64 | road.draw_warn(lcd, 2); |
alex_20 | 6:40ef2030334c | 65 | i -= speed; |
alex_20 | 1:2ae7a8b01771 | 66 | } |
alex_20 | 6:40ef2030334c | 67 | offset += road_speed; |
alex_20 | 6:40ef2030334c | 68 | |
alex_20 | 6:40ef2030334c | 69 | Vector2D coord = joystick.get_mapped_coord(); |
alex_20 | 6:40ef2030334c | 70 | int direction = car.get_direction(coord.x); |
alex_20 | 6:40ef2030334c | 71 | int magnitude = car.get_magnitude(coord.x); |
alex_20 | 6:40ef2030334c | 72 | |
alex_20 | 6:40ef2030334c | 73 | if(direction == STRAIGHT) { |
alex_20 | 6:40ef2030334c | 74 | y = 0; |
alex_20 | 6:40ef2030334c | 75 | } |
alex_20 | 3:cbe2dcca5058 | 76 | |
alex_20 | 6:40ef2030334c | 77 | if(direction == LEFT) { |
alex_20 | 6:40ef2030334c | 78 | if(magnitude == 1) { |
alex_20 | 6:40ef2030334c | 79 | y = 4.0; |
alex_20 | 6:40ef2030334c | 80 | } |
alex_20 | 6:40ef2030334c | 81 | if(magnitude == 2) { |
alex_20 | 6:40ef2030334c | 82 | y = 4.0; |
alex_20 | 6:40ef2030334c | 83 | } |
alex_20 | 6:40ef2030334c | 84 | start_pos -= car_speed; |
alex_20 | 6:40ef2030334c | 85 | } |
alex_20 | 6:40ef2030334c | 86 | |
alex_20 | 6:40ef2030334c | 87 | if(direction == RIGHT) { |
alex_20 | 6:40ef2030334c | 88 | if(magnitude == 1) { |
alex_20 | 6:40ef2030334c | 89 | y = 4.0; |
alex_20 | 6:40ef2030334c | 90 | } |
alex_20 | 6:40ef2030334c | 91 | if(magnitude == 2) { |
alex_20 | 6:40ef2030334c | 92 | y = 8.0; |
alex_20 | 6:40ef2030334c | 93 | } |
alex_20 | 6:40ef2030334c | 94 | start_pos += car_speed; |
alex_20 | 6:40ef2030334c | 95 | } |
alex_20 | 6:40ef2030334c | 96 | |
alex_20 | 6:40ef2030334c | 97 | // define boundaries for car movement |
alex_20 | 6:40ef2030334c | 98 | if(start_pos < 20.0) { |
alex_20 | 6:40ef2030334c | 99 | start_pos = 20.0; |
alex_20 | 6:40ef2030334c | 100 | } |
alex_20 | 6:40ef2030334c | 101 | |
alex_20 | 6:40ef2030334c | 102 | if(start_pos > 55.0) { |
alex_20 | 6:40ef2030334c | 103 | start_pos = 55.0; |
alex_20 | 6:40ef2030334c | 104 | } |
alex_20 | 6:40ef2030334c | 105 | |
alex_20 | 6:40ef2030334c | 106 | car.draw(lcd,start_pos,y); |
alex_20 | 6:40ef2030334c | 107 | |
alex_20 | 6:40ef2030334c | 108 | printf("m= %d\n\n", magnitude); |
alex_20 | 6:40ef2030334c | 109 | printf("d= %f\n\n", direction); |
alex_20 | 3:cbe2dcca5058 | 110 | |
alex_20 | 1:2ae7a8b01771 | 111 | lcd.refresh(); |
eencae | 0:be41a15e7a86 | 112 | } |
eencae | 0:be41a15e7a86 | 113 | } |
alex_20 | 1:2ae7a8b01771 | 114 | |
alex_20 | 1:2ae7a8b01771 | 115 | void init_buttons() |
alex_20 | 1:2ae7a8b01771 | 116 | { |
alex_20 | 1:2ae7a8b01771 | 117 | // PCB has external pull-down resistors so turn the internal ones off |
alex_20 | 1:2ae7a8b01771 | 118 | // (default for DigitalIn) |
alex_20 | 1:2ae7a8b01771 | 119 | button_A.mode(PullNone); |
alex_20 | 1:2ae7a8b01771 | 120 | button_C.mode(PullNone); |
alex_20 | 1:2ae7a8b01771 | 121 | } |